1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 23:06:38 +02:00

Add new hooks (info and infolist), IRC plugin now return infos and infolists

This commit is contained in:
Sebastien Helleu
2008-08-30 00:25:56 +02:00
parent eb57354984
commit 0839b359f9
45 changed files with 1990 additions and 402 deletions
+1
View File
@@ -24,6 +24,7 @@ irc-completion.c irc-completion.h
irc-config.c irc-config.h
irc-debug.c irc-debug.h
irc-display.c irc-display.h
irc-info.c irc-info.h
irc-input.c irc-input.h
irc-mode.c irc-mode.h
irc-nick.c irc-nick.h
+2
View File
@@ -38,6 +38,8 @@ irc_la_SOURCES = irc.c \
irc-debug.h \
irc-display.c \
irc-display.h \
irc-info.c \
irc-info.h \
irc-input.c \
irc-input.h \
irc-mode.c \
+28
View File
@@ -33,6 +33,31 @@
#include "irc-input.h"
/*
* irc_channel_valid: check if a channel pointer exists for a server
* return 1 if channel exists
* 0 if channel is not found
*/
int
irc_channel_valid (struct t_irc_server *server, struct t_irc_channel *channel)
{
struct t_irc_channel *ptr_channel;
if (!server)
return 0;
for (ptr_channel = server->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
if (ptr_channel == channel)
return 1;
}
/* channel not found */
return 0;
}
/*
* irc_channel_new: allocate a new channel for a server and add it to servers
* list
@@ -114,6 +139,7 @@ irc_channel_new (struct t_irc_server *server, int channel_type,
new_channel->last_nick = NULL;
new_channel->buffer = new_buffer;
new_channel->nicks_speaking = NULL;
new_channel->buffer_as_string = NULL;
/* add new channel to channels list */
new_channel->prev_channel = server->last_channel;
@@ -185,6 +211,8 @@ irc_channel_free (struct t_irc_server *server, struct t_irc_channel *channel)
free (channel->away_message);
if (channel->nicks_speaking)
weechat_list_free (channel->nicks_speaking);
if (channel->buffer_as_string)
free (channel->buffer_as_string);
free (channel);
+3
View File
@@ -50,10 +50,13 @@ struct t_irc_channel
struct t_irc_nick *last_nick; /* last nick on the channel */
struct t_weelist *nicks_speaking; /* for smart completion */
struct t_gui_buffer *buffer; /* buffer allocated for channel */
char *buffer_as_string; /* used to return buffer info */
struct t_irc_channel *prev_channel; /* link to previous channel */
struct t_irc_channel *next_channel; /* link to next channel */
};
extern int irc_channel_valid (struct t_irc_server *server,
struct t_irc_channel *channel);
extern struct t_irc_channel *irc_channel_new (struct t_irc_server *server,
int channel_type,
const char *channel_name,
+3 -3
View File
@@ -863,7 +863,7 @@ irc_command_cycle (void *data, struct t_gui_buffer *buffer, int argc,
if (ptr_arg)
{
version = weechat_info_get ("version");
version = weechat_info_get ("version", "");
buf = weechat_string_replace (ptr_arg, "%v", (version) ? version : "");
irc_server_sendf (ptr_server, "PART %s :%s", channel_name,
(buf) ? buf : ptr_arg);
@@ -1141,7 +1141,7 @@ irc_command_quit_server (struct t_irc_server *server, const char *arguments)
if (ptr_arg)
{
version = weechat_info_get ("version");
version = weechat_info_get ("version", "");
buf = weechat_string_replace (ptr_arg, "%v",
(version) ? version : "");
irc_server_sendf (server, "QUIT :%s",
@@ -2169,7 +2169,7 @@ irc_command_part_channel (struct t_irc_server *server, const char *channel_name,
if (ptr_arg)
{
version = weechat_info_get ("version");
version = weechat_info_get ("version", "");
buf = weechat_string_replace (ptr_arg, "%v", (version) ? version : "");
irc_server_sendf (server, "PART %s :%s",
channel_name,
+339
View File
@@ -0,0 +1,339 @@
/*
* Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* irc-info.c: info and infolist hooks for IRC plugin */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-info.h"
#include "irc-channel.h"
#include "irc-nick.h"
#include "irc-protocol.h"
#include "irc-server.h"
/*
* irc_info_create_string_with_pointer: create a string with a pointer inside
* an IRC structure
*/
void
irc_info_create_string_with_pointer (char **string, void *pointer)
{
if (*string)
{
free (*string);
*string = NULL;
}
if (pointer)
{
*string = malloc (64);
if (*string)
{
snprintf (*string, 64 - 1, "0x%x", (unsigned int)pointer);
}
}
}
/*
* irc_info_get_info_cb: callback called when IRC info is asked
*/
char *
irc_info_get_info_cb (void *data, const char *info_name,
const char *arguments)
{
char *pos_comma, *pos_comma2, *server, *channel, *host, *nick;
static char str_true[2] = "1";
struct t_irc_server *ptr_server;
struct t_irc_channel *ptr_channel;
/* make C compiler happy */
(void) data;
if (weechat_strcasecmp (info_name, "irc_is_channel") == 0)
{
if (irc_channel_is_channel (arguments))
return str_true;
return NULL;
}
else if (weechat_strcasecmp (info_name, "irc_nick_from_host") == 0)
{
return irc_protocol_get_nick_from_host (arguments);
}
else if (weechat_strcasecmp (info_name, "irc_buffer") == 0)
{
if (arguments && arguments[0])
{
server = NULL;
channel = NULL;
host = NULL;
ptr_server = NULL;
ptr_channel = NULL;
pos_comma = strchr (arguments, ',');
if (pos_comma)
{
server = weechat_strndup (arguments, pos_comma - arguments);
pos_comma2 = strchr (pos_comma + 1, ',');
if (pos_comma2)
{
channel = weechat_strndup (pos_comma + 1,
pos_comma2 - pos_comma - 1);
host = strdup (pos_comma2 + 1);
}
else
channel = strdup (pos_comma + 1);
}
else
{
if (irc_channel_is_channel (arguments))
channel = strdup (arguments);
else
server = strdup (arguments);
}
/* replace channel by nick in host if channel is not a channel
(private ?) */
if (channel && host)
{
if (!irc_channel_is_channel (channel))
{
free (channel);
channel = NULL;
nick = irc_protocol_get_nick_from_host (host);
if (nick)
channel = strdup (nick);
}
}
/* search for server or channel buffer */
if (server)
{
ptr_server = irc_server_search (server);
if (ptr_server && channel)
ptr_channel = irc_channel_search (ptr_server, channel);
}
if (server)
free (server);
if (channel)
free (channel);
if (host)
free (host);
if (ptr_channel)
{
irc_info_create_string_with_pointer (&ptr_channel->buffer_as_string,
ptr_channel->buffer);
return ptr_channel->buffer_as_string;
}
if (ptr_server)
{
irc_info_create_string_with_pointer (&ptr_server->buffer_as_string,
ptr_server->buffer);
return ptr_server->buffer_as_string;
}
}
}
return NULL;
}
/*
* irc_info_get_infolist_cb: callback called when IRC infolist is asked
*/
struct t_infolist *
irc_info_get_infolist_cb (void *data, const char *infolist_name,
void *pointer, const char *arguments)
{
struct t_infolist *ptr_infolist;
struct t_irc_server *ptr_server;
struct t_irc_channel *ptr_channel;
struct t_irc_nick *ptr_nick;
char *pos_comma, *server_name;
/* make C compiler happy */
(void) data;
(void) arguments;
if (!infolist_name || !infolist_name[0])
return NULL;
if (weechat_strcasecmp (infolist_name, "irc_server") == 0)
{
if (pointer && !irc_server_valid (pointer))
return NULL;
ptr_infolist = weechat_infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one server */
if (!irc_server_add_to_infolist (ptr_infolist, pointer))
{
weechat_infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all servers */
for (ptr_server = irc_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (!irc_server_add_to_infolist (ptr_infolist, ptr_server))
{
weechat_infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
}
else if (weechat_strcasecmp (infolist_name, "irc_channel") == 0)
{
if (arguments && arguments[0])
{
ptr_server = irc_server_search (arguments);
if (ptr_server)
{
if (pointer && !irc_channel_valid (ptr_server, pointer))
return NULL;
ptr_infolist = weechat_infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one channel */
if (!irc_channel_add_to_infolist (ptr_infolist, pointer))
{
weechat_infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all channels of server */
for (ptr_channel = ptr_server->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
if (!irc_channel_add_to_infolist (ptr_infolist,
ptr_channel))
{
weechat_infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
}
}
}
else if (weechat_strcasecmp (infolist_name, "irc_nick") == 0)
{
if (arguments && arguments[0])
{
ptr_server = NULL;
ptr_channel = NULL;
pos_comma = strchr (arguments, ',');
if (pos_comma)
{
server_name = weechat_strndup (arguments, pos_comma - arguments);
if (server_name)
{
ptr_server = irc_server_search (server_name);
if (ptr_server)
{
ptr_channel = irc_channel_search (ptr_server,
pos_comma + 1);
}
free (server_name);
}
}
if (ptr_server && ptr_channel)
{
if (pointer && !irc_nick_valid (ptr_channel, pointer))
return NULL;
ptr_infolist = weechat_infolist_new ();
if (ptr_infolist)
{
if (pointer)
{
/* build list with only one nick */
if (!irc_nick_add_to_infolist (ptr_infolist, pointer))
{
weechat_infolist_free (ptr_infolist);
return NULL;
}
return ptr_infolist;
}
else
{
/* build list with all nicks of channel */
for (ptr_nick = ptr_channel->nicks; ptr_nick;
ptr_nick = ptr_nick->next_nick)
{
if (!irc_nick_add_to_infolist (ptr_infolist,
ptr_nick))
{
weechat_infolist_free (ptr_infolist);
return NULL;
}
}
return ptr_infolist;
}
}
}
}
}
return NULL;
}
/*
* irc_info_init: initialize info and infolist hooks for IRC plugin
*/
void
irc_info_init ()
{
/* irc info hooks */
weechat_hook_info ("irc_is_channel", &irc_info_get_info_cb, NULL);
weechat_hook_info ("irc_nick_from_host", &irc_info_get_info_cb, NULL);
weechat_hook_info ("irc_buffer", &irc_info_get_info_cb, NULL);
/* irc infolist hooks */
weechat_hook_infolist ("irc_server", &irc_info_get_infolist_cb, NULL);
weechat_hook_infolist ("irc_channel", &irc_info_get_infolist_cb, NULL);
weechat_hook_infolist ("irc_nick", &irc_info_get_infolist_cb, NULL);
}
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2003-2008 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_IRC_INFO_H
#define __WEECHAT_IRC_INFO_H 1
extern void irc_info_init ();
#endif /* irc-info.h */
+24
View File
@@ -32,6 +32,30 @@
#include "irc-channel.h"
/*
* irc_nick_valid: check if a nick pointer exists for a channel
* return 1 if nick exists
* 0 if nick is not found
*/
int
irc_nick_valid (struct t_irc_channel *channel, struct t_irc_nick *nick)
{
struct t_irc_nick *ptr_nick;
if (!channel)
return 0;
for (ptr_nick = channel->nicks; ptr_nick; ptr_nick = ptr_nick->next_nick)
{
if (ptr_nick == nick)
return 1;
}
/* nick not found */
return 0;
}
/*
* irc_nick_find_color: find a color for a nick (according to nick letters)
*/
+2
View File
@@ -56,6 +56,8 @@ struct t_irc_nick
struct t_irc_nick *next_nick; /* link to next nick on channel */
};
extern int irc_nick_valid (struct t_irc_channel *channel,
struct t_irc_nick *nick);
extern struct t_irc_nick *irc_nick_new (struct t_irc_server *server,
struct t_irc_channel *channel,
const char *nick_name, int is_chanowner,
+9 -3
View File
@@ -52,7 +52,10 @@ irc_protocol_get_nick_from_host (const char *host)
{
static char nick[128];
char *pos;
if (!host)
return NULL;
nick[0] = '\0';
if (host)
{
@@ -1004,8 +1007,8 @@ irc_protocol_reply_version (struct t_irc_server *server,
pos = NULL;
}
version = weechat_info_get ("version");
date = weechat_info_get ("date");
version = weechat_info_get ("version", "");
date = weechat_info_get ("date", "");
if (version && date)
{
irc_server_sendf (server,
@@ -4168,6 +4171,9 @@ irc_protocol_recv_command (struct t_irc_server *server, const char *entire_line,
weechat_prefix ("error"), entire_line);
}
/* send signal with received command */
irc_server_send_signal (server, "irc_in2", command, entire_line);
if (irc_message)
free (irc_message);
if (nick)
+1
View File
@@ -55,6 +55,7 @@ struct t_irc_protocol_msg
t_irc_recv_func *recv_function; /* function called when msg is received */
};
extern char *irc_protocol_get_nick_from_host (const char *host);
extern void irc_protocol_recv_command (struct t_irc_server *server,
const char *entire_line,
const char *host, const char *command,
+28
View File
@@ -48,6 +48,31 @@ struct t_irc_message *irc_recv_msgq = NULL;
struct t_irc_message *irc_msgq_last_msg = NULL;
/*
* irc_server_valid: check if a server pointer exists
* return 1 if server exists
* 0 if server is not found
*/
int
irc_server_valid (struct t_irc_server *server)
{
struct t_irc_server *ptr_server;
if (!server)
return 0;
for (ptr_server = irc_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (ptr_server == server)
return 1;
}
/* server not found */
return 0;
}
/*
* irc_server_get_name_without_port: get name of server without port
* (ends before first '/' if found)
@@ -351,6 +376,7 @@ irc_server_init (struct t_irc_server *server)
server->outqueue = NULL;
server->last_outqueue = NULL;
server->buffer = NULL;
server->buffer_as_string = NULL;
server->channels = NULL;
server->last_channel = NULL;
@@ -727,6 +753,8 @@ irc_server_free_data (struct t_irc_server *server)
irc_server_outqueue_free_all (server);
if (server->channels)
irc_channel_free_all (server);
if (server->buffer_as_string)
free (server->buffer_as_string);
}
/*
+2
View File
@@ -112,6 +112,7 @@ struct t_irc_server
struct t_irc_outqueue *outqueue; /* queue for outgoing user msgs */
struct t_irc_outqueue *last_outqueue; /* last outgoing user message */
struct t_gui_buffer *buffer; /* GUI buffer allocated for server */
char *buffer_as_string; /* used to return buffer info */
struct t_irc_channel *channels; /* opened channels on server */
struct t_irc_channel *last_channel; /* last opened channal on server */
struct t_irc_server *prev_server; /* link to previous server */
@@ -135,6 +136,7 @@ extern const int gnutls_prot_prio[];
extern struct t_irc_message *irc_recv_msgq, *irc_msgq_last_msg;
extern int irc_server_valid (struct t_irc_server *server);
extern char *irc_server_get_name_without_port (const char *name);
extern void irc_server_new_option (struct t_irc_server *server,
int index_option,
+3
View File
@@ -28,6 +28,7 @@
#include "irc-completion.h"
#include "irc-config.h"
#include "irc-debug.h"
#include "irc-info.h"
#include "irc-server.h"
#include "irc-channel.h"
#include "irc-nick.h"
@@ -114,6 +115,8 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
irc_command_init ();
irc_info_init ();
/* hook some signals */
irc_debug_init ();
weechat_hook_signal ("quit", &irc_signal_quit_cb, NULL);