diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/relay/relay-buffer.c | 80 | ||||
-rw-r--r-- | src/plugins/relay/relay-buffer.h | 2 | ||||
-rw-r--r-- | src/plugins/relay/relay-client-irc.c | 161 | ||||
-rw-r--r-- | src/plugins/relay/relay-client-irc.h | 4 | ||||
-rw-r--r-- | src/plugins/relay/relay-client-weechat.c | 37 | ||||
-rw-r--r-- | src/plugins/relay/relay-client-weechat.h | 4 | ||||
-rw-r--r-- | src/plugins/relay/relay-client.c | 51 | ||||
-rw-r--r-- | src/plugins/relay/relay-client.h | 4 | ||||
-rw-r--r-- | src/plugins/relay/relay-command.c | 2 | ||||
-rw-r--r-- | src/plugins/relay/relay-completion.c | 2 | ||||
-rw-r--r-- | src/plugins/relay/relay-config.c | 14 | ||||
-rw-r--r-- | src/plugins/relay/relay-raw.c | 40 | ||||
-rw-r--r-- | src/plugins/relay/relay-server.c | 62 | ||||
-rw-r--r-- | src/plugins/relay/relay-server.h | 12 | ||||
-rw-r--r-- | src/plugins/relay/relay-upgrade.c | 136 | ||||
-rw-r--r-- | src/plugins/relay/relay-upgrade.h | 3 | ||||
-rw-r--r-- | src/plugins/relay/relay.c | 15 |
17 files changed, 439 insertions, 190 deletions
diff --git a/src/plugins/relay/relay-buffer.c b/src/plugins/relay/relay-buffer.c index da68513f2..1a95fc324 100644 --- a/src/plugins/relay/relay-buffer.c +++ b/src/plugins/relay/relay-buffer.c @@ -28,8 +28,10 @@ #include "../weechat-plugin.h" #include "relay.h" +#include "relay-buffer.h" #include "relay-client.h" #include "relay-config.h" +#include "relay-raw.h" struct t_gui_buffer *relay_buffer = NULL; @@ -158,43 +160,51 @@ relay_buffer_input_cb (void *data, struct t_gui_buffer *buffer, /* make C compiler happy */ (void) data; - client = relay_client_search_by_number (relay_buffer_selected_line); - - /* disconnect client */ - if (weechat_strcasecmp (input_data, "d") == 0) + if (buffer == relay_raw_buffer) { - if (client && !RELAY_CLIENT_HAS_ENDED(client->status)) - { - relay_client_disconnect (client); - relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE); - } + if (weechat_strcasecmp (input_data, "q") == 0) + weechat_buffer_close (buffer); } - /* purge old clients */ - else if (weechat_strcasecmp (input_data, "p") == 0) + else if (buffer == relay_buffer) { - ptr_client = relay_clients; - while (ptr_client) + client = relay_client_search_by_number (relay_buffer_selected_line); + + /* disconnect client */ + if (weechat_strcasecmp (input_data, "d") == 0) { - next_client = ptr_client->next_client; - if (RELAY_CLIENT_HAS_ENDED(ptr_client->status)) - relay_client_free (ptr_client); - ptr_client = next_client; + if (client && !RELAY_CLIENT_HAS_ENDED(client->status)) + { + relay_client_disconnect (client); + relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE); + } } - relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE); - } - /* quit relay buffer (close it) */ - else if (weechat_strcasecmp (input_data, "q") == 0) - { - weechat_buffer_close (buffer); - } - /* remove client */ - else if (weechat_strcasecmp (input_data, "r") == 0) - { - if (client && RELAY_CLIENT_HAS_ENDED(client->status)) + /* purge old clients */ + else if (weechat_strcasecmp (input_data, "p") == 0) { - relay_client_free (client); + ptr_client = relay_clients; + while (ptr_client) + { + next_client = ptr_client->next_client; + if (RELAY_CLIENT_HAS_ENDED(ptr_client->status)) + relay_client_free (ptr_client); + ptr_client = next_client; + } relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE); } + /* quit relay buffer (close it) */ + else if (weechat_strcasecmp (input_data, "q") == 0) + { + weechat_buffer_close (buffer); + } + /* remove client */ + else if (weechat_strcasecmp (input_data, "r") == 0) + { + if (client && RELAY_CLIENT_HAS_ENDED(client->status)) + { + relay_client_free (client); + relay_buffer_refresh (WEECHAT_HOTLIST_MESSAGE); + } + } } return WEECHAT_RC_OK; @@ -209,9 +219,15 @@ relay_buffer_close_cb (void *data, struct t_gui_buffer *buffer) { /* make C compiler happy */ (void) data; - (void) buffer; - relay_buffer = NULL; + if (buffer == relay_raw_buffer) + { + relay_raw_buffer = NULL; + } + else if (buffer == relay_buffer) + { + relay_buffer = NULL; + } return WEECHAT_RC_OK; } @@ -225,7 +241,7 @@ relay_buffer_open () { if (!relay_buffer) { - relay_buffer = weechat_buffer_new ("relay.list", + relay_buffer = weechat_buffer_new (RELAY_BUFFER_NAME, &relay_buffer_input_cb, NULL, &relay_buffer_close_cb, NULL); diff --git a/src/plugins/relay/relay-buffer.h b/src/plugins/relay/relay-buffer.h index 555e44640..e798f1b3e 100644 --- a/src/plugins/relay/relay-buffer.h +++ b/src/plugins/relay/relay-buffer.h @@ -20,6 +20,8 @@ #ifndef __WEECHAT_RELAY_BUFFER_H #define __WEECHAT_RELAY_BUFFER_H 1 +#define RELAY_BUFFER_NAME "relay.list" + extern struct t_gui_buffer *relay_buffer; extern int relay_buffer_selected_line; diff --git a/src/plugins/relay/relay-client-irc.c b/src/plugins/relay/relay-client-irc.c index 61fb119f4..30d002b96 100644 --- a/src/plugins/relay/relay-client-irc.c +++ b/src/plugins/relay/relay-client-irc.c @@ -216,7 +216,7 @@ relay_client_irc_signal_irc_in2_cb (void *data, const char *signal, { weechat_printf (NULL, "%s: irc_in2: client: %s, data: %s", RELAY_PLUGIN_NAME, - client->protocol_string, + client->protocol_args, ptr_msg); } @@ -335,7 +335,7 @@ relay_client_irc_signal_irc_outtags_cb (void *data, const char *signal, { weechat_printf (NULL, "%s: irc_out: client: %s, message: %s", RELAY_PLUGIN_NAME, - client->protocol_string, + client->protocol_args, message); } @@ -374,7 +374,7 @@ relay_client_irc_signal_irc_outtags_cb (void *data, const char *signal, /* get host for nick (it is self nick) */ snprintf (str_infolist_args, sizeof (str_infolist_args) - 1, "%s,%s,%s", - client->protocol_string, + client->protocol_args, irc_channel, RELAY_IRC_DATA(client, nick)); @@ -427,7 +427,7 @@ relay_client_irc_signal_irc_disc_cb (void *data, const char *signal, client = (struct t_relay_client *)data; - if (strcmp ((char *)signal_data, client->protocol_string) == 0) + if (strcmp ((char *)signal_data, client->protocol_args) == 0) { relay_client_set_status (client, RELAY_STATUS_DISCONNECTED); } @@ -449,7 +449,7 @@ relay_client_irc_send_join (struct t_relay_client *client, int length, length_nicks; struct t_infolist *infolist_nick, *infolist_nicks; - length = strlen (client->protocol_string) + 1 + strlen (channel) + 1 + length = strlen (client->protocol_args) + 1 + strlen (channel) + 1 + strlen (RELAY_IRC_DATA(client, nick)) + 1; infolist_name = malloc (length); if (infolist_name) @@ -457,7 +457,7 @@ relay_client_irc_send_join (struct t_relay_client *client, /* get nick host */ host = NULL; snprintf (infolist_name, length, "%s,%s,%s", - client->protocol_string, + client->protocol_args, channel, RELAY_IRC_DATA(client, nick)); infolist_nick = weechat_infolist_get ("irc_nick", NULL, infolist_name); @@ -479,7 +479,7 @@ relay_client_irc_send_join (struct t_relay_client *client, if (host) free (host); snprintf (infolist_name, length, "%s,%s", - client->protocol_string, + client->protocol_args, channel); infolist_nicks = weechat_infolist_get ("irc_nick", NULL, infolist_name); if (infolist_nicks) @@ -540,7 +540,7 @@ relay_client_irc_send_join_channels (struct t_relay_client *client) const char *channel; infolist_channels = weechat_infolist_get ("irc_channel", NULL, - client->protocol_string); + client->protocol_args); if (infolist_channels) { while (weechat_infolist_next (infolist_channels)) @@ -568,7 +568,7 @@ relay_client_irc_input_send (struct t_relay_client *client, snprintf (buffer, sizeof (buffer), "%s;%s;%d;relay_client_%d;", - client->protocol_string, + client->protocol_args, (irc_channel) ? irc_channel : "", flags, client->id); @@ -592,13 +592,56 @@ relay_client_irc_input_send (struct t_relay_client *client, } /* + * relay_client_irc_hook_signals: hook signals for a client + */ + +void +relay_client_irc_hook_signals (struct t_relay_client *client) +{ + char str_signal_name[128]; + + /* + * hook signal "xxx,irc_in2_*" to catch IRC data received from + * this server + */ + snprintf (str_signal_name, sizeof (str_signal_name), + "%s,irc_in2_*", + client->protocol_args); + RELAY_IRC_DATA(client, hook_signal_irc_in2) = + weechat_hook_signal (str_signal_name, + &relay_client_irc_signal_irc_in2_cb, + client); + + /* + * hook signal "xxx,irc_outtags_*" to catch IRC data sent to + * this server + */ + snprintf (str_signal_name, sizeof (str_signal_name), + "%s,irc_outtags_*", + client->protocol_args); + RELAY_IRC_DATA(client, hook_signal_irc_outtags) = + weechat_hook_signal (str_signal_name, + &relay_client_irc_signal_irc_outtags_cb, + client); + + /* + * hook signal "irc_server_disconnected" to disconnect client if + * connection to server is lost + */ + RELAY_IRC_DATA(client, hook_signal_irc_disc) = + weechat_hook_signal ("irc_server_disconnected", + &relay_client_irc_signal_irc_disc_cb, + client); +} + +/* * relay_client_irc_recv_one_msg: read one message from client */ void relay_client_irc_recv_one_msg (struct t_relay_client *client, char *data) { - char *pos, str_time[128], str_signal_name[128], *target; + char *pos, str_time[128], *target; const char *irc_command, *irc_channel, *irc_args, *irc_args2; const char *nick, *irc_is_channel, *isupport; struct t_hashtable *hash_parsed; @@ -656,7 +699,7 @@ relay_client_irc_recv_one_msg (struct t_relay_client *client, char *data) { /* check if connection to server is ok */ infolist_server = weechat_infolist_get ("irc_server", NULL, - client->protocol_string); + client->protocol_args); if (infolist_server) { if (weechat_infolist_next (infolist_server)) @@ -668,7 +711,7 @@ relay_client_irc_recv_one_msg (struct t_relay_client *client, char *data) ":%s ERROR :WeeChat: no " "connection to server \"%s\"", RELAY_IRC_DATA(client, address), - client->protocol_string); + client->protocol_args); relay_client_irc_sendf (client, ":%s ERROR :Closing Link", RELAY_IRC_DATA(client, address)); @@ -704,7 +747,7 @@ relay_client_irc_recv_one_msg (struct t_relay_client *client, char *data) * send nick to client if server nick is different of nick asked * by client with command NICK */ - nick = weechat_info_get ("irc_nick", client->protocol_string); + nick = weechat_info_get ("irc_nick", client->protocol_args); if (nick && (strcmp (nick, RELAY_IRC_DATA(client, nick)) != 0)) { relay_client_irc_sendf (client, @@ -744,7 +787,7 @@ relay_client_irc_recv_one_msg (struct t_relay_client *client, char *data) RELAY_IRC_DATA(client, address), weechat_info_get("version", NULL)); infolist_server = weechat_infolist_get ("irc_server", NULL, - client->protocol_string); + client->protocol_args); if (infolist_server) { if (weechat_infolist_next (infolist_server)) @@ -785,38 +828,8 @@ relay_client_irc_recv_one_msg (struct t_relay_client *client, char *data) RELAY_IRC_DATA(client, address), RELAY_IRC_DATA(client, nick)); - /* - * hook signal "xxx,irc_in2_*" to catch IRC data received from - * this server - */ - snprintf (str_signal_name, sizeof (str_signal_name), - "%s,irc_in2_*", - client->protocol_string); - RELAY_IRC_DATA(client, hook_signal_irc_in2) = - weechat_hook_signal (str_signal_name, - &relay_client_irc_signal_irc_in2_cb, - client); - - /* - * hook signal "xxx,irc_outtags_*" to catch IRC data sent to - * this server - */ - snprintf (str_signal_name, sizeof (str_signal_name), - "%s,irc_outtags_*", - client->protocol_string); - RELAY_IRC_DATA(client, hook_signal_irc_outtags) = - weechat_hook_signal (str_signal_name, - &relay_client_irc_signal_irc_outtags_cb, - client); - - /* - * hook signal "irc_server_disconnected" to disconnect client if - * connection to server is lost - */ - RELAY_IRC_DATA(client, hook_signal_irc_disc) = - weechat_hook_signal ("irc_server_disconnected", - &relay_client_irc_signal_irc_disc_cb, - client); + /* hook signals */ + relay_client_irc_hook_signals (client); /* send JOIN for all channels on server to client */ relay_client_irc_send_join_channels (client); @@ -967,6 +980,30 @@ relay_client_irc_alloc (struct t_relay_client *client) } /* + * relay_client_irc_alloc_with_infolist: init relay data specific to IRC + * protocol using an infolist + */ + +void +relay_client_irc_alloc_with_infolist (struct t_relay_client *client, + struct t_infolist *infolist) +{ + struct t_relay_client_irc_data *irc_data; + const char *password; + + client->protocol_data = malloc (sizeof (*irc_data)); + if (client->protocol_data) + { + RELAY_IRC_DATA(client, address) = strdup (weechat_infolist_string (infolist, "address")); + RELAY_IRC_DATA(client, password_ok) = weechat_infolist_integer (infolist, "password_ok"); + RELAY_IRC_DATA(client, nick) = strdup (weechat_infolist_string (infolist, "nick")); + RELAY_IRC_DATA(client, user_received) = weechat_infolist_integer (infolist, "user_received"); + RELAY_IRC_DATA(client, connected) = weechat_infolist_integer (infolist, "connected"); + relay_client_irc_hook_signals (client); + } +} + +/* * relay_client_irc_free: free relay data specific to IRC protocol */ @@ -993,6 +1030,38 @@ relay_client_irc_free (struct t_relay_client *client) } /* + * relay_client_irc_add_to_infolist: add client irc data in an infolist item + * return 1 if ok, 0 if error + */ + +int +relay_client_irc_add_to_infolist (struct t_infolist_item *item, + struct t_relay_client *client) +{ + if (!item || !client) + return 0; + + if (!weechat_infolist_new_var_string (item, "address", RELAY_IRC_DATA(client, address))) + return 0; + if (!weechat_infolist_new_var_integer (item, "password_ok", RELAY_IRC_DATA(client, password_ok))) + return 0; + if (!weechat_infolist_new_var_string (item, "nick", RELAY_IRC_DATA(client, nick))) + return 0; + if (!weechat_infolist_new_var_integer (item, "user_received", RELAY_IRC_DATA(client, user_received))) + return 0; + if (!weechat_infolist_new_var_integer (item, "connected", RELAY_IRC_DATA(client, connected))) + return 0; + if (!weechat_infolist_new_var_pointer (item, "hook_signal_irc_in2", RELAY_IRC_DATA(client, hook_signal_irc_in2))) + return 0; + if (!weechat_infolist_new_var_pointer (item, "hook_signal_irc_outtags", RELAY_IRC_DATA(client, hook_signal_irc_outtags))) + return 0; + if (!weechat_infolist_new_var_pointer (item, "hook_signal_irc_disc", RELAY_IRC_DATA(client, hook_signal_irc_disc))) + return 0; + + return 1; +} + +/* * relay_client_irc_print_log: print IRC client infos in log (usually for * crash dump) */ diff --git a/src/plugins/relay/relay-client-irc.h b/src/plugins/relay/relay-client-irc.h index 951a89570..4d1e8aa40 100644 --- a/src/plugins/relay/relay-client-irc.h +++ b/src/plugins/relay/relay-client-irc.h @@ -43,7 +43,11 @@ extern void relay_client_irc_recv (struct t_relay_client *client, const char *data); extern void relay_client_irc_close_connection (struct t_relay_client *client); extern void relay_client_irc_alloc (struct t_relay_client *client); +extern void relay_client_irc_alloc_with_infolist (struct t_relay_client *client, + struct t_infolist *infolist); extern void relay_client_irc_free (struct t_relay_client *client); +extern int relay_client_irc_add_to_infolist (struct t_infolist_item *item, + struct t_relay_client *client); extern void relay_client_irc_print_log (struct t_relay_client *client); #endif /* __WEECHAT_RELAY_CLIENT_IRC_H */ diff --git a/src/plugins/relay/relay-client-weechat.c b/src/plugins/relay/relay-client-weechat.c index 7b9e31351..feffe9b58 100644 --- a/src/plugins/relay/relay-client-weechat.c +++ b/src/plugins/relay/relay-client-weechat.c @@ -208,6 +208,27 @@ relay_client_weechat_alloc (struct t_relay_client *client) } /* + * relay_client_weechat_alloc_with_infolist: init relay data specific to + * weechat protocol with an infolist + */ + +void +relay_client_weechat_alloc_with_infolist (struct t_relay_client *client, + struct t_infolist *infolist) +{ + struct t_relay_client_weechat_data *weechat_data; + + /* make C compiler happy */ + (void) infolist; + + client->protocol_data = malloc (sizeof (*weechat_data)); + if (client->protocol_data) + { + /* ... */ + } +} + +/* * relay_client_weechat_free: free relay data specific to weechat protocol */ @@ -219,6 +240,22 @@ relay_client_weechat_free (struct t_relay_client *client) } /* + * relay_client_weechat_add_to_infolist: add client weechat data in an + * infolist item + * return 1 if ok, 0 if error + */ + +int +relay_client_weechat_add_to_infolist (struct t_infolist_item *item, + struct t_relay_client *client) +{ + if (!item || !client) + return 0; + + return 1; +} + +/* * relay_client_weechat_print_log: print weechat client infos in log (usually * for crash dump) */ diff --git a/src/plugins/relay/relay-client-weechat.h b/src/plugins/relay/relay-client-weechat.h index 32172235f..5707ad170 100644 --- a/src/plugins/relay/relay-client-weechat.h +++ b/src/plugins/relay/relay-client-weechat.h @@ -38,7 +38,11 @@ struct t_relay_client_weechat_data extern void relay_client_weechat_recv (struct t_relay_client *client, const char *data); extern void relay_client_weechat_alloc (struct t_relay_client *client); +extern void relay_client_weechat_alloc_with_infolist (struct t_relay_client *client, + struct t_infolist *infolist); extern void relay_client_weechat_free (struct t_relay_client *client); +extern int relay_client_weechat_add_to_infolist (struct t_infolist_item *item, + struct t_relay_client *client); extern void relay_client_weechat_print_log (struct t_relay_client *client); #endif /* __WEECHAT_RELAY_CLIENT_WEECHAT_H */ diff --git a/src/plugins/relay/relay-client.c b/src/plugins/relay/relay-client.c index 1dec067af..ba08ae802 100644 --- a/src/plugins/relay/relay-client.c +++ b/src/plugins/relay/relay-client.c @@ -158,12 +158,11 @@ relay_client_new (int sock, char *address, struct t_relay_server *server) new_client->address = strdup ((address) ? address : "?"); new_client->status = RELAY_STATUS_CONNECTED; new_client->protocol = server->protocol; - new_client->protocol_string = strdup (server->protocol_string); + new_client->protocol_args = strdup (server->protocol_args); new_client->listen_start_time = server->start_time; new_client->start_time = time (NULL); new_client->end_time = 0; new_client->hook_fd = NULL; - new_client->hook_timer = NULL; new_client->last_activity = new_client->start_time; new_client->bytes_recv = 0; new_client->bytes_sent = 0; @@ -198,7 +197,7 @@ relay_client_new (int sock, char *address, struct t_relay_server *server) server->port, new_client->id, relay_protocol_string[new_client->protocol], - new_client->protocol_string); + new_client->protocol_args); new_client->hook_fd = weechat_hook_fd (new_client->sock, 1, 0, 0, @@ -244,11 +243,6 @@ relay_client_set_status (struct t_relay_client *client, weechat_unhook (client->hook_fd); client->hook_fd = NULL; } - if (client->hook_timer) - { - weechat_unhook (client->hook_timer); - client->hook_timer = NULL; - } switch (client->protocol) { case RELAY_PROTOCOL_WEECHAT: @@ -270,7 +264,7 @@ relay_client_set_status (struct t_relay_client *client, client->address, RELAY_COLOR_CHAT, relay_protocol_string[client->protocol], - client->protocol_string); + client->protocol_args); break; case RELAY_STATUS_DISCONNECTED: weechat_printf (NULL, @@ -280,7 +274,7 @@ relay_client_set_status (struct t_relay_client *client, client->address, RELAY_COLOR_CHAT, relay_protocol_string[client->protocol], - client->protocol_string); + client->protocol_args); break; default: break; @@ -324,12 +318,10 @@ relay_client_free (struct t_relay_client *client) /* free data */ if (client->address) free (client->address); - if (client->protocol_string) - free (client->protocol_string); + if (client->protocol_args) + free (client->protocol_args); if (client->hook_fd) weechat_unhook (client->hook_fd); - if (client->hook_timer) - weechat_unhook (client->hook_timer); if (client->protocol_data) { switch (client->protocol) @@ -417,7 +409,9 @@ relay_client_add_to_infolist (struct t_infolist *infolist, ptr_item = weechat_infolist_new_item (infolist); if (!ptr_item) return 0; - + + if (!weechat_infolist_new_var_integer (ptr_item, "id", client->id)) + return 0; if (!weechat_infolist_new_var_integer (ptr_item, "sock", client->sock)) return 0; if (!weechat_infolist_new_var_string (ptr_item, "address", client->address)) @@ -426,11 +420,19 @@ relay_client_add_to_infolist (struct t_infolist *infolist, return 0; if (!weechat_infolist_new_var_string (ptr_item, "status_string", relay_client_status_string[client->status])) return 0; + if (!weechat_infolist_new_var_integer (ptr_item, "protocol", client->protocol)) + return 0; + if (!weechat_infolist_new_var_string (ptr_item, "protocol_string", relay_protocol_string[client->protocol])) + return 0; + if (!weechat_infolist_new_var_string (ptr_item, "protocol_args", client->protocol_args)) + return 0; + if (!weechat_infolist_new_var_time (ptr_item, "listen_start_time", client->listen_start_time)) + return 0; if (!weechat_infolist_new_var_time (ptr_item, "start_time", client->start_time)) return 0; - if (!weechat_infolist_new_var_pointer (ptr_item, "hook_fd", client->hook_fd)) + if (!weechat_infolist_new_var_time (ptr_item, "end_time", client->end_time)) return 0; - if (!weechat_infolist_new_var_pointer (ptr_item, "hook_timer", client->hook_timer)) + if (!weechat_infolist_new_var_pointer (ptr_item, "hook_fd", client->hook_fd)) return 0; if (!weechat_infolist_new_var_time (ptr_item, "last_activity", client->last_activity)) return 0; @@ -441,6 +443,18 @@ relay_client_add_to_infolist (struct t_infolist *infolist, if (!weechat_infolist_new_var_string (ptr_item, "bytes_sent", value)) return 0; + switch (client->protocol) + { + case RELAY_PROTOCOL_WEECHAT: + relay_client_weechat_add_to_infolist (ptr_item, client); + break; + case RELAY_PROTOCOL_IRC: + relay_client_irc_add_to_infolist (ptr_item, client); + break; + case RELAY_NUM_PROTOCOLS: + break; + } + return 1; } @@ -467,12 +481,11 @@ relay_client_print_log () weechat_log_printf (" protocol. . . . . . . : %d (%s)", ptr_client->protocol, relay_protocol_string[ptr_client->protocol]); - weechat_log_printf (" protocol_string . . . : '%s'", ptr_client->protocol_string); + weechat_log_printf (" protocol_args . . . . : '%s'", ptr_client->protocol_args); weechat_log_printf (" listen_start_time . . : %ld", ptr_client->listen_start_time); weechat_log_printf (" start_time. . . . . . : %ld", ptr_client->start_time); weechat_log_printf (" end_time. . . . . . . : %ld", ptr_client->end_time); weechat_log_printf (" hook_fd . . . . . . . : 0x%lx", ptr_client->hook_fd); - weechat_log_printf (" hook_timer. . . . . . : 0x%lx", ptr_client->hook_timer); weechat_log_printf (" last_activity . . . . : %ld", ptr_client->last_activity); weechat_log_printf (" bytes_recv. . . . . . : %lu", ptr_client->bytes_recv); weechat_log_printf (" bytes_sent. . . . . . : %lu", ptr_client->bytes_sent); diff --git a/src/plugins/relay/relay-client.h b/src/plugins/relay/relay-client.h index 9e1eec957..47e784c04 100644 --- a/src/plugins/relay/relay-client.h +++ b/src/plugins/relay/relay-client.h @@ -49,13 +49,12 @@ struct t_relay_client char *address; /* string with IP address */ enum t_relay_status status; /* status (connecting, active,..) */ enum t_relay_protocol protocol; /* protocol (irc,..) */ - char *protocol_string; /* string used for protocol */ + char *protocol_args; /* arguments used for protocol */ /* example: server for irc protocol */ time_t listen_start_time; /* when listening started */ time_t start_time; /* time of client connection */ time_t end_time; /* time of client disconnection */ struct t_hook *hook_fd; /* hook for socket or child pipe */ - struct t_hook *hook_timer; /* timeout for recever accept */ time_t last_activity; /* time of last byte received/sent */ unsigned long bytes_recv; /* bytes received from client */ unsigned long bytes_sent; /* bytes sent to client */ @@ -71,6 +70,7 @@ extern int relay_client_count; extern int relay_client_valid (struct t_relay_client *client); extern struct t_relay_client *relay_client_search_by_number (int number); +extern int relay_client_recv_cb (void *arg_client, int fd); extern struct t_relay_client *relay_client_new (int sock, char *address, struct t_relay_server *server); extern void relay_client_set_status (struct t_relay_client *client, diff --git a/src/plugins/relay/relay-command.c b/src/plugins/relay/relay-command.c index 94a006f7b..3bf7713b4 100644 --- a/src/plugins/relay/relay-command.c +++ b/src/plugins/relay/relay-command.c @@ -132,7 +132,7 @@ relay_command_server_list () RELAY_COLOR_CHAT, RELAY_COLOR_CHAT_BUFFER, relay_protocol_string[ptr_server->protocol], - ptr_server->protocol_string, + ptr_server->protocol_args, RELAY_COLOR_CHAT, date_start); i++; diff --git a/src/plugins/relay/relay-completion.c b/src/plugins/relay/relay-completion.c index 852d19555..4f80c2db8 100644 --- a/src/plugins/relay/relay-completion.c +++ b/src/plugins/relay/relay-completion.c @@ -90,7 +90,7 @@ relay_completion_relays_cb (void *data, const char *completion_item, { snprintf (protocol_name, sizeof (protocol_name), "%s.%s", relay_protocol_string[ptr_server->protocol], - ptr_server->protocol_string); + ptr_server->protocol_args); weechat_hook_completion_list_add (completion, protocol_name, 0, WEECHAT_LIST_POS_SORT); } diff --git a/src/plugins/relay/relay-config.c b/src/plugins/relay/relay-config.c index 53b0d7e85..e1ae5f635 100644 --- a/src/plugins/relay/relay-config.c +++ b/src/plugins/relay/relay-config.c @@ -175,7 +175,7 @@ relay_config_create_option_port (void *data, const char *value) { int rc, protocol_number; - char *error, *protocol, *protocol_string; + char *error, *protocol, *protocol_args; long port; struct t_relay_server *ptr_server; @@ -184,13 +184,13 @@ relay_config_create_option_port (void *data, rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; - relay_server_get_protocol_string (option_name, - &protocol, &protocol_string); + relay_server_get_protocol_args (option_name, + &protocol, &protocol_args); protocol_number = -1; port = -1; - if (protocol && protocol_string) + if (protocol && protocol_args) protocol_number = relay_protocol_search (protocol); if (protocol_number < 0) @@ -234,7 +234,7 @@ relay_config_create_option_port (void *data, &relay_config_change_port_cb, NULL, &relay_config_delete_port_cb, NULL); - if (relay_server_new (protocol_number, protocol_string, port)) + if (relay_server_new (protocol_number, protocol_args, port)) rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; else rc = WEECHAT_CONFIG_OPTION_SET_ERROR; @@ -242,8 +242,8 @@ relay_config_create_option_port (void *data, if (protocol) free (protocol); - if (protocol_string) - free (protocol_string); + if (protocol_args) + free (protocol_args); return rc; } diff --git a/src/plugins/relay/relay-raw.c b/src/plugins/relay/relay-raw.c index 1be5b0754..10cc9e5d0 100644 --- a/src/plugins/relay/relay-raw.c +++ b/src/plugins/relay/relay-raw.c @@ -29,6 +29,7 @@ #include "../weechat-plugin.h" #include "relay.h" #include "relay-raw.h" +#include "relay-buffer.h" #include "relay-client.h" #include "relay-config.h" @@ -58,39 +59,6 @@ relay_raw_message_print (struct t_relay_raw_message *raw_message) } /* - * relay_raw_input_data_cb: callback for input data in Relay raw buffer - */ - -int -relay_raw_input_data_cb (void *data, struct t_gui_buffer *buffer, - const char *input_data) -{ - /* make C compiler happy */ - (void) data; - - if (weechat_strcasecmp (input_data, "q") == 0) - weechat_buffer_close (buffer); - - return WEECHAT_RC_OK; -} - -/* - * relay_raw_buffer_close_cb: callback called when Relay raw buffer is closed - */ - -int -relay_raw_buffer_close_cb (void *data, struct t_gui_buffer *buffer) -{ - /* make C compiler happy */ - (void) data; - (void) buffer; - - relay_raw_buffer = NULL; - - return WEECHAT_RC_OK; -} - -/* * relay_raw_open: open Relay raw buffer */ @@ -106,8 +74,8 @@ relay_raw_open (int switch_to_buffer) if (!relay_raw_buffer) { relay_raw_buffer = weechat_buffer_new (RELAY_RAW_BUFFER_NAME, - &relay_raw_input_data_cb, NULL, - &relay_raw_buffer_close_cb, NULL); + &relay_buffer_input_cb, NULL, + &relay_buffer_close_cb, NULL); /* failed to create buffer ? then return */ if (!relay_raw_buffer) @@ -288,7 +256,7 @@ relay_raw_message_add (struct t_relay_client *client, int send, client->id, weechat_color ("chat_delimiters"), weechat_color ("chat_server"), - client->protocol_string, + client->protocol_args, (send) ? weechat_color ("chat_prefix_quit") : weechat_color ("chat_prefix_join"), diff --git a/src/plugins/relay/relay-server.c b/src/plugins/relay/relay-server.c index 7e04aa818..0eb64daee 100644 --- a/src/plugins/relay/relay-server.c +++ b/src/plugins/relay/relay-server.c @@ -44,54 +44,54 @@ struct t_relay_server *last_relay_server = NULL; /* - * relay_server_get_protocol_string: get protocol and string from a string with - * format "protocol.string" - * Note: *protocol and *string must be freed - * after use + * relay_server_get_protocol_args: get protocol and arguments from a string + * with format "protocol.args" + * Note: *protocol and *protocol_args must be + * freed after use */ void -relay_server_get_protocol_string (const char *protocol_and_string, - char **protocol, char **protocol_string) +relay_server_get_protocol_args (const char *protocol_and_args, + char **protocol, char **protocol_args) { char *pos; - pos = strchr (protocol_and_string, '.'); + pos = strchr (protocol_and_args, '.'); if (pos) { - *protocol = weechat_strndup (protocol_and_string, - pos - protocol_and_string); - *protocol_string = strdup (pos + 1); + *protocol = weechat_strndup (protocol_and_args, + pos - protocol_and_args); + *protocol_args = strdup (pos + 1); } else { - *protocol = strdup (protocol_and_string); - *protocol_string = strdup ("*"); + *protocol = strdup (protocol_and_args); + *protocol_args = strdup ("*"); } } /* - * relay_server_search: search server by protocol.string + * relay_server_search: search server by protocol.args */ struct t_relay_server * -relay_server_search (const char *protocol_and_string) +relay_server_search (const char *protocol_and_args) { - char *protocol, *protocol_string; + char *protocol, *protocol_args; struct t_relay_server *ptr_server; - relay_server_get_protocol_string (protocol_and_string, - &protocol, &protocol_string); + relay_server_get_protocol_args (protocol_and_args, + &protocol, &protocol_args); ptr_server = NULL; - if (protocol && protocol_string) + if (protocol && protocol_args) { for (ptr_server = relay_servers; ptr_server; ptr_server = ptr_server->next_server) { if ((strcmp (protocol, relay_protocol_string[ptr_server->protocol]) == 0) - && (strcmp (protocol_string, ptr_server->protocol_string) == 0)) + && (strcmp (protocol_args, ptr_server->protocol_args) == 0)) { break; } @@ -100,8 +100,8 @@ relay_server_search (const char *protocol_and_string) if (protocol) free (protocol); - if (protocol_string) - free (protocol_string); + if (protocol_args) + free (protocol_args); return ptr_server; } @@ -149,7 +149,7 @@ relay_server_close_socket (struct t_relay_server *server) _("%s: socket closed for %s.%s (port %d)"), RELAY_PLUGIN_NAME, relay_protocol_string[server->protocol], - server->protocol_string, + server->protocol_args, server->port); } } @@ -186,7 +186,7 @@ relay_server_sock_cb (void *data, int fd) RELAY_PLUGIN_NAME, server->port, relay_protocol_string[server->protocol], - server->protocol_string); + server->protocol_args); return WEECHAT_RC_OK; } @@ -270,7 +270,7 @@ relay_server_create_socket (struct t_relay_server *server) weechat_prefix ("error"), RELAY_PLUGIN_NAME, server->port, relay_protocol_string[server->protocol], - server->protocol_string); + server->protocol_args); close (server->sock); server->sock = -1; return 0; @@ -285,7 +285,7 @@ relay_server_create_socket (struct t_relay_server *server) RELAY_PLUGIN_NAME, server->port, relay_protocol_string[server->protocol], - server->protocol_string, + server->protocol_args, max_clients); server->hook_fd = weechat_hook_fd (server->sock, @@ -304,7 +304,7 @@ relay_server_create_socket (struct t_relay_server *server) struct t_relay_server * relay_server_new (enum t_relay_protocol protocol, - const char *protocol_string, + const char *protocol_args, int port) { struct t_relay_server *new_server; @@ -321,8 +321,8 @@ relay_server_new (enum t_relay_protocol protocol, if (new_server) { new_server->protocol = protocol; - new_server->protocol_string = - (protocol_string) ? strdup (protocol_string) : strdup ("*"); + new_server->protocol_args = + (protocol_args) ? strdup (protocol_args) : strdup ("*"); new_server->port = port; new_server->sock = -1; new_server->hook_fd = NULL; @@ -390,8 +390,8 @@ relay_server_free (struct t_relay_server *server) /* free data */ relay_server_close_socket (server); - if (server->protocol_string) - free (server->protocol_string); + if (server->protocol_args) + free (server->protocol_args); free (server); @@ -428,7 +428,7 @@ relay_server_print_log () weechat_log_printf (" protocol. . . . . . : %d (%s)", ptr_server->protocol, relay_protocol_string[ptr_server->protocol]); - weechat_log_printf (" protocol_string . . : '%s'", ptr_server->protocol_string); + weechat_log_printf (" protocol_args . . . : '%s'", ptr_server->protocol_args); weechat_log_printf (" port. . . . . . . . : %d", ptr_server->port); weechat_log_printf (" sock. . . . . . . . : %d", ptr_server->sock); weechat_log_printf (" hook_fd . . . . . . : 0x%lx", ptr_server->hook_fd); diff --git a/src/plugins/relay/relay-server.h b/src/plugins/relay/relay-server.h index a35f1ccf2..86c56afc4 100644 --- a/src/plugins/relay/relay-server.h +++ b/src/plugins/relay/relay-server.h @@ -23,7 +23,7 @@ struct t_relay_server { enum t_relay_protocol protocol; /* protocol (irc,..) */ - char *protocol_string; /* string used for protocol */ + char *protocol_args; /* arguments used for protocol */ /* example: server for irc protocol */ int port; /* listening on this port */ int sock; /* socket for connection */ @@ -36,15 +36,15 @@ struct t_relay_server extern struct t_relay_server *relay_servers; extern struct t_relay_server *last_relay_server; -extern void relay_server_get_protocol_string (const char *protocol_and_string, - char **protocol, - char **protocol_string); -extern struct t_relay_server *relay_server_search (const char *protocol_and_string); +extern void relay_server_get_protocol_args (const char *protocol_and_string, + char **protocol, + char **protocol_args); +extern struct t_relay_server *relay_server_search (const char *protocol_and_args); extern struct t_relay_server *relay_server_search_port (int port); extern void relay_server_close_socket (struct t_relay_server *server); extern int relay_server_create_socket (struct t_relay_server *server); extern struct t_relay_server *relay_server_new (enum t_relay_protocol protocol, - const char *protocol_string, + const char *protocol_args, int port); extern void relay_server_update_port (struct t_relay_server *server, int port); extern void relay_server_free (struct t_relay_server *server); diff --git a/src/plugins/relay/relay-upgrade.c b/src/plugins/relay/relay-upgrade.c index 11495aca4..32e06ee77 100644 --- a/src/plugins/relay/relay-upgrade.c +++ b/src/plugins/relay/relay-upgrade.c @@ -22,22 +22,71 @@ */ #include <stdlib.h> +#include <stdio.h> +#include <string.h> #include "../weechat-plugin.h" #include "relay.h" #include "relay-upgrade.h" #include "relay-buffer.h" +#include "relay-client.h" +#include "relay-client-irc.h" +#include "relay-client-weechat.h" +#include "relay-raw.h" /* - * relay_upgrade_save_clients: save clients info to upgrade file + * relay_upgrade_save_all_data: save relay data to upgrade file */ int -relay_upgrade_save_clients (struct t_upgrade_file *upgrade_file) +relay_upgrade_save_all_data (struct t_upgrade_file *upgrade_file) { - /* TODO: save relay data */ - (void) upgrade_file; + struct t_infolist *infolist; + struct t_relay_client *ptr_client; + struct t_relay_raw_message *ptr_raw_message; + int rc; + + /* save clients */ + for (ptr_client = relay_clients; ptr_client; + ptr_client = ptr_client->next_client) + { + infolist = weechat_infolist_new (); + if (!infolist) + return 0; + if (!relay_client_add_to_infolist (infolist, ptr_client)) + { + weechat_infolist_free (infolist); + return 0; + } + rc = weechat_upgrade_write_object (upgrade_file, + RELAY_UPGRADE_TYPE_CLIENT, + infolist); + weechat_infolist_free (infolist); + if (!rc) + return 0; + } + + /* save raw messages */ + for (ptr_raw_message = relay_raw_messages; ptr_raw_message; + ptr_raw_message = ptr_raw_message->next_message) + { + infolist = weechat_infolist_new (); + if (!infolist) + return 0; + if (!relay_raw_add_to_infolist (infolist, ptr_raw_message)) + { + weechat_infolist_free (infolist); + return 0; + } + rc = weechat_upgrade_write_object (upgrade_file, + RELAY_UPGRADE_TYPE_RAW_MESSAGE, + infolist); + weechat_infolist_free (infolist); + if (!rc) + return 0; + } + return 1; } @@ -56,7 +105,7 @@ relay_upgrade_save () if (!upgrade_file) return 0; - rc = relay_upgrade_save_clients (upgrade_file); + rc = relay_upgrade_save_all_data (upgrade_file); weechat_upgrade_close (upgrade_file); @@ -84,6 +133,16 @@ relay_upgrade_set_buffer_callbacks () ptr_buffer = weechat_infolist_pointer (infolist, "pointer"); weechat_buffer_set_pointer (ptr_buffer, "close_callback", &relay_buffer_close_cb); weechat_buffer_set_pointer (ptr_buffer, "input_callback", &relay_buffer_input_cb); + if (strcmp (weechat_infolist_string (infolist, "name"), + RELAY_BUFFER_NAME) == 0) + { + relay_buffer = ptr_buffer; + } + if (strcmp (weechat_infolist_string (infolist, "name"), + RELAY_RAW_BUFFER_NAME) == 0) + { + relay_raw_buffer = ptr_buffer; + } } } } @@ -99,11 +158,72 @@ relay_upgrade_read_cb (void *data, int object_id, struct t_infolist *infolist) { - /* TODO: write relay read cb */ + struct t_relay_client *new_client; + + /* make C compiler happy */ (void) data; (void) upgrade_file; - (void) object_id; - (void) infolist; + + weechat_infolist_reset_item_cursor (infolist); + while (weechat_infolist_next (infolist)) + { + switch (object_id) + { + case RELAY_UPGRADE_TYPE_CLIENT: + new_client = malloc (sizeof (*new_client)); + if (new_client) + { + new_client->id = weechat_infolist_integer (infolist, "id"); + new_client->sock = weechat_infolist_integer (infolist, "sock"); + new_client->address = strdup (weechat_infolist_string (infolist, "address")); + new_client->status = weechat_infolist_integer (infolist, "status"); + new_client->protocol = weechat_infolist_integer (infolist, "protocol"); + new_client->protocol_args = strdup (weechat_infolist_string (infolist, "protocol_args")); + new_client->listen_start_time = weechat_infolist_time (infolist, "listen_start_time"); + new_client->start_time = weechat_infolist_time (infolist, "start_time"); + new_client->end_time = weechat_infolist_time (infolist, "end_time"); + new_client->hook_fd = weechat_hook_fd (new_client->sock, + 1, 0, 0, + &relay_client_recv_cb, + new_client); + new_client->last_activity = weechat_infolist_time (infolist, "last_activity"); + sscanf (weechat_infolist_string (infolist, "bytes_recv"), + "%lu", &(new_client->bytes_recv)); + sscanf (weechat_infolist_string (infolist, "bytes_sent"), + "%lu", &(new_client->bytes_sent)); + + switch (new_client->protocol) + { + case RELAY_PROTOCOL_WEECHAT: + relay_client_weechat_alloc_with_infolist (new_client, + infolist); + break; + case RELAY_PROTOCOL_IRC: + relay_client_irc_alloc_with_infolist (new_client, + infolist); + break; + case RELAY_NUM_PROTOCOLS: + break; + } + + new_client->prev_client = NULL; + new_client->next_client = relay_clients; + if (relay_clients) + relay_clients->prev_client = new_client; + else + last_relay_client = new_client; + relay_clients = new_client; + + relay_client_count++; + } + break; + case RELAY_UPGRADE_TYPE_RAW_MESSAGE: + relay_raw_message_add_to_list (weechat_infolist_time (infolist, "date"), + weechat_infolist_string (infolist, "prefix"), + weechat_infolist_string (infolist, "message")); + break; + } + } return WEECHAT_RC_OK; } diff --git a/src/plugins/relay/relay-upgrade.h b/src/plugins/relay/relay-upgrade.h index 6d308fec3..19ceeed4c 100644 --- a/src/plugins/relay/relay-upgrade.h +++ b/src/plugins/relay/relay-upgrade.h @@ -26,7 +26,8 @@ enum t_relay_upgrade_type { - RELAY_UPGRADE_TYPE_RELAY = 0, + RELAY_UPGRADE_TYPE_CLIENT = 0, + RELAY_UPGRADE_TYPE_RAW_MESSAGE, }; extern int relay_upgrade_save (); diff --git a/src/plugins/relay/relay.c b/src/plugins/relay/relay.c index 062866588..387a88170 100644 --- a/src/plugins/relay/relay.c +++ b/src/plugins/relay/relay.c @@ -136,6 +136,8 @@ relay_debug_dump_cb (void *data, const char *signal, const char *type_data, int weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) { + int i, upgrading; + /* make C compiler happy */ (void) argc; (void) argv; @@ -158,6 +160,19 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) relay_info_init (); + /* look at arguments */ + upgrading = 0; + for (i = 0; i < argc; i++) + { + if (weechat_strcasecmp (argv[i], "--upgrade") == 0) + { + upgrading = 1; + } + } + + if (upgrading) + relay_upgrade_load (); + return WEECHAT_RC_OK; } |