updates.
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
1 /*
2   silc-server.c : irssi
3
4   Copyright (C) 2000 - 2001 Timo Sirainen
5                             Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11   
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22 #include "module.h"
23
24 #include "net-nonblock.h"
25 #include "net-sendbuffer.h"
26 #include "signals.h"
27 #include "servers.h"
28 #include "commands.h"
29 #include "levels.h"
30 #include "modules.h"
31 #include "rawlog.h"
32 #include "misc.h"
33 #include "settings.h"
34
35 #include "servers-setup.h"
36
37 #include "silc-servers.h"
38 #include "silc-channels.h"
39 #include "silc-queries.h"
40 #include "window-item-def.h"
41
42 #include "fe-common/core/printtext.h"
43 #include "fe-common/silc/module-formats.h"
44
45 void silc_servers_reconnect_init(void);
46 void silc_servers_reconnect_deinit(void);
47
48 static void silc_send_channel(SILC_SERVER_REC *server,
49                               char *channel, char *msg)
50 {
51   SILC_CHANNEL_REC *rec;
52   
53   rec = silc_channel_find(server, channel);
54   if (rec == NULL || rec->entry == NULL)
55     return;
56   
57   silc_client_send_channel_message(silc_client, server->conn, rec->entry, 
58                                    NULL, 0, msg, strlen(msg), TRUE);
59 }
60
61 typedef struct {
62   char *nick;
63   char *msg;
64   SILC_SERVER_REC *server;
65 } PRIVMSG_REC;
66
67 /* Callback function that sends the private message if the client was
68    resolved from the server. */
69
70 static void silc_send_msg_clients(SilcClient client,
71                                   SilcClientConnection conn,
72                                   SilcClientEntry *clients,
73                                   uint32 clients_count,
74                                   void *context)
75 {
76   PRIVMSG_REC *rec = context;
77   SILC_SERVER_REC *server = rec->server;
78   SilcClientEntry target;
79   char *nickname = NULL;
80
81   if (!clients_count) {
82     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", rec->nick);
83   } else {
84     if (clients_count > 1) {
85       silc_parse_userfqdn(rec->nick, &nickname, NULL);
86
87       /* Find the correct one. The rec->nick might be a formatted nick
88          so this will find the correct one. */
89       clients = silc_client_get_clients_local(silc_client, server->conn, 
90                                               nickname, rec->nick, 
91                                               &clients_count);
92       if (!clients) {
93         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", 
94                   rec->nick);
95         silc_free(nickname);
96         goto out;
97       }
98       silc_free(nickname);
99     }
100
101     target = clients[0];
102
103     /* Send the private message */
104     silc_client_send_private_message(client, conn, target, 0,
105                                      rec->msg, strlen(rec->msg),
106                                      TRUE);
107   }
108   
109  out:
110   g_free(rec->nick);
111   g_free(rec->msg);
112   g_free(rec);
113 }
114
115 static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
116 {
117   PRIVMSG_REC *rec;
118   SilcClientEntry *clients;
119   uint32 clients_count;
120   char *nickname = NULL;
121
122   if (!silc_parse_userfqdn(nick, &nickname, NULL)) {
123     printformat_module("fe-common/silc", server, NULL,
124                        MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick);
125     return;
126   }
127
128   /* Find client entry */
129   clients = silc_client_get_clients_local(silc_client, server->conn, 
130                                           nickname, nick, &clients_count);
131   silc_free(nickname);
132
133   if (!clients) {
134     rec = g_new0(PRIVMSG_REC, 1);
135     rec->nick = g_strdup(nick);
136     rec->msg = g_strdup(msg);
137     rec->server = server;
138
139     /* Could not find client with that nick, resolve it from server. */
140     silc_client_get_clients(silc_client, server->conn,
141                             nickname, NULL, silc_send_msg_clients, rec);
142     return;
143   }
144
145   /* Send the private message directly */
146   silc_client_send_private_message(silc_client, server->conn, 
147                                    clients[0], 0, msg, strlen(msg), TRUE);
148 }
149
150 static int isnickflag_func(char flag)
151 {
152   return flag == '@' || flag == '+';
153 }
154
155 static int ischannel_func(const char *data)
156 {
157   return *data == '#';
158 }
159
160 const char *get_nick_flags(void)
161 {
162   return "@\0\0";
163 }
164
165 static void send_message(SILC_SERVER_REC *server, char *target, char *msg)
166 {
167   g_return_if_fail(server != NULL);
168   g_return_if_fail(target != NULL);
169   g_return_if_fail(msg != NULL);
170
171   if (*target == '#')
172     silc_send_channel(server, target, msg);
173   else
174     silc_send_msg(server, target, msg);
175 }
176
177 static void sig_connected(SILC_SERVER_REC *server)
178 {
179   SilcClientConnection conn;
180   int fd;
181
182   if (!IS_SILC_SERVER(server))
183     return;
184
185   conn = silc_client_add_connection(silc_client,
186                                     server->connrec->address,
187                                     server->connrec->port,
188                                     server);
189   server->conn = conn;
190         
191   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
192   if (!silc_client_start_key_exchange(silc_client, conn, fd)) {
193     /* some internal error occured */
194     server_disconnect(SERVER(server));
195     signal_stop();
196     return;
197   }
198
199   server->isnickflag = isnickflag_func;
200   server->ischannel = ischannel_func;
201   server->get_nick_flags = get_nick_flags;
202   server->send_message = (void *) send_message;
203 }
204
205 static void sig_disconnected(SILC_SERVER_REC *server)
206 {
207   if (!IS_SILC_SERVER(server) || server->conn == NULL)
208     return;
209   
210   if (server->conn->sock != NULL) {
211     silc_client_close_connection(silc_client, NULL, server->conn);
212     
213     /* SILC closes the handle */
214     g_io_channel_unref(net_sendbuffer_handle(server->handle));
215     net_sendbuffer_destroy(server->handle, FALSE);
216     server->handle = NULL;
217   }
218 }
219
220 SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn)
221 {
222   SILC_SERVER_REC *server;
223
224   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
225   if (conn->address == NULL || *conn->address == '\0') 
226     return NULL;
227   if (conn->nick == NULL || *conn->nick == '\0') {
228     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
229               "Cannot connect: nickname is not set");
230     return NULL;
231   }
232
233   server = g_new0(SILC_SERVER_REC, 1);
234   server->chat_type = SILC_PROTOCOL;
235   server->connrec = conn;
236   if (server->connrec->port <= 0) 
237     server->connrec->port = 706;
238
239   if (!server_start_connect((SERVER_REC *) server)) {
240     server_connect_free(SERVER_CONNECT(conn));
241     g_free(server);
242     return NULL;
243   }
244
245   return server;
246 }
247
248 /* Return a string of all channels in server in server->channels_join() 
249    format */
250
251 char *silc_server_get_channels(SILC_SERVER_REC *server)
252 {
253   GSList *tmp;
254   GString *chans;
255   char *ret;
256
257   g_return_val_if_fail(server != NULL, FALSE);
258
259   chans = g_string_new(NULL);
260   for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
261     CHANNEL_REC *channel = tmp->data;
262     
263     g_string_sprintfa(chans, "%s,", channel->name);
264   }
265
266   if (chans->len > 0)
267     g_string_truncate(chans, chans->len-1);
268
269   ret = chans->str;
270   g_string_free(chans, FALSE);
271   
272   return ret;
273 }
274
275 /* Syntaxes of all SILC commands for HELP files (the help file generation
276    will snoop these from here). */
277
278 /* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
279 /* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
280 /* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] [-pubkey|<passwd>] */
281 /* SYNTAX: GETKEY <nickname or server name> */
282 /* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
283 /* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
284 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
285 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|agreement|negotiate [<arguments>] */
286 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
287 /* SYNTAX: KILL <channel> <nickname>[@<hostname>] [<comment>] */
288 /* SYNTAX: OPER <username> [-pubkey] */
289 /* SYNTAX: SILCOPER <username> [-pubkey] */
290 /* SYNTAX: TOPIC <channel> [<topic>] */
291 /* SYNTAX: UMODE +|-<modes> */
292 /* SYNTAX: WHOIS <nickname>[@<hostname>] [<count>] */
293 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
294 /* SYNTAX: CLOSE <server> [<port>] */
295 /* SYNTAX: SHUTDOWN */
296 /* SYNTAX: MOTD [<server>] */
297 /* SYNTAX: LIST [<channel>] */
298 /* SYNTAX: ME <message> */
299 /* SYNTAX: ACTION <channel> <message> */
300 /* SYNTAX: AWAY [<message>] */
301 /* SYNTAX: INFO [<server>] */
302 /* SYNTAX: NICK <nickname> */
303 /* SYNTAX: NOTICE <message> */
304 /* SYNTAX: PART [<channel>] */
305 /* SYNTAX: PING */
306 /* SYNTAX: SCONNECT <server> [<port>] */
307 /* SYNTAX: USERS <channel> */
308
309 void silc_command_exec(SILC_SERVER_REC *server,
310                        const char *command, const char *args)
311 {
312   uint32 argc = 0;
313   unsigned char **argv;
314   uint32 *argv_lens, *argv_types;
315   char *data, *tmpcmd;
316   SilcClientCommand *cmd;
317   SilcClientCommandContext ctx;
318
319   g_return_if_fail(server != NULL);
320
321   tmpcmd = g_strdup(command); 
322   g_strup(tmpcmd);
323   cmd = silc_client_command_find(tmpcmd);
324   g_free(tmpcmd);
325   if (cmd == NULL)
326     return;
327
328   /* Now parse all arguments */
329   data = g_strconcat(command, " ", args, NULL);
330   silc_parse_command_line(data, &argv, &argv_lens,
331                           &argv_types, &argc, cmd->max_args);
332   g_free(data);
333
334   /* Allocate command context. This and its internals must be free'd
335      by the command routine receiving it. */
336   ctx = silc_client_command_alloc();
337   ctx->client = silc_client;
338   ctx->conn = server->conn;
339   ctx->command = cmd;
340   ctx->argc = argc;
341   ctx->argv = argv;
342   ctx->argv_lens = argv_lens;
343   ctx->argv_types = argv_types;
344   
345   /* Execute command */
346   (*cmd->cb)(ctx, NULL);
347 }
348
349 /* Generic command function to call any SILC command directly. */
350
351 static void command_self(const char *data, SILC_SERVER_REC *server,
352                          WI_ITEM_REC *item)
353 {
354   if (!IS_SILC_SERVER(server) || !server->connected) {
355     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
356     return;
357   }
358
359   if (IS_SILC_CHANNEL(item)) {
360     SILC_CHANNEL_REC *chanrec;
361     chanrec = silc_channel_find(server, item->name);
362     if (chanrec)
363       server->conn->current_channel = chanrec->entry;
364   }
365
366   silc_command_exec(server, current_command, data);
367   signal_stop();
368 }
369
370 /* SCONNECT command.  Calls actually SILC's CONNECT command since Irssi
371    has CONNECT command for other purposes. */
372
373 static void command_sconnect(const char *data, SILC_SERVER_REC *server)
374 {
375   if (!IS_SILC_SERVER(server) || !server->connected) {
376     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
377     return;
378   }
379
380   silc_command_exec(server, "CONNECT", data);
381   signal_stop();
382 }
383
384 static void event_text(const char *line, SILC_SERVER_REC *server,
385                        WI_ITEM_REC *item)
386 {
387   char *str;
388
389   g_return_if_fail(line != NULL);
390
391   if (!IS_SILC_ITEM(item))
392     return;
393
394   str = g_strdup_printf("%s %s", item->name, line);
395   signal_emit("command msg", 3, str, server, item);
396   g_free(str);
397
398   signal_stop();
399 }
400
401 void silc_server_init(void)
402 {
403   silc_servers_reconnect_init();
404
405   signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
406   signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
407   signal_add("send text", (SIGNAL_FUNC) event_text);
408   command_bind("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
409   command_bind("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
410   command_bind("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
411   command_bind("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
412   command_bind("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
413   command_bind("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
414   command_bind("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
415   command_bind("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
416   command_bind("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
417   command_bind("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
418   command_bind("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
419   command_bind("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
420   command_bind("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
421   command_bind("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
422   command_bind("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
423   command_bind("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
424   command_bind("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
425   command_bind("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
426   command_bind("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
427   command_bind("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
428   command_bind("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
429   command_bind("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
430
431   command_set_options("connect", "+silcnet");
432 }
433
434 void silc_server_deinit(void)
435 {
436   silc_servers_reconnect_deinit();
437
438   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
439   signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
440   signal_remove("send text", (SIGNAL_FUNC) event_text);
441   command_unbind("whois", (SIGNAL_FUNC) command_self);
442   command_unbind("whowas", (SIGNAL_FUNC) command_self);
443   command_unbind("nick", (SIGNAL_FUNC) command_self);
444   command_unbind("topic", (SIGNAL_FUNC) command_self);
445   command_unbind("cmode", (SIGNAL_FUNC) command_self);
446   command_unbind("cumode", (SIGNAL_FUNC) command_self);
447   command_unbind("users", (SIGNAL_FUNC) command_self);
448   command_unbind("list", (SIGNAL_FUNC) command_self);
449   command_unbind("oper", (SIGNAL_FUNC) command_self);
450   command_unbind("silcoper", (SIGNAL_FUNC) command_self);
451   command_unbind("umode", (SIGNAL_FUNC) command_self);
452   command_unbind("invite", (SIGNAL_FUNC) command_self);
453   command_unbind("kill", (SIGNAL_FUNC) command_self);
454   command_unbind("kick", (SIGNAL_FUNC) command_self);
455   command_unbind("info", (SIGNAL_FUNC) command_self);
456   command_unbind("ping", (SIGNAL_FUNC) command_self);
457   command_unbind("motd", (SIGNAL_FUNC) command_self);
458   command_unbind("ban", (SIGNAL_FUNC) command_self);
459   command_unbind("close", (SIGNAL_FUNC) command_self);
460   command_unbind("shutdown", (SIGNAL_FUNC) command_self);
461   command_unbind("getkey", (SIGNAL_FUNC) command_self);
462   command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
463 }