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 "silc-nicklist.h"
41 #include "window-item-def.h"
42
43 #include "fe-common/core/printtext.h"
44 #include "fe-common/silc/module-formats.h"
45
46 void silc_servers_reconnect_init(void);
47 void silc_servers_reconnect_deinit(void);
48
49 static void silc_send_channel(SILC_SERVER_REC *server,
50                               char *channel, char *msg)
51 {
52   SILC_CHANNEL_REC *rec;
53   
54   rec = silc_channel_find(server, channel);
55   if (rec == NULL || rec->entry == NULL)
56     return;
57   
58   silc_client_send_channel_message(silc_client, server->conn, rec->entry, 
59                                    NULL, 0, msg, strlen(msg), TRUE);
60 }
61
62 typedef struct {
63   char *nick;
64   char *msg;
65   SILC_SERVER_REC *server;
66 } PRIVMSG_REC;
67
68 /* Callback function that sends the private message if the client was
69    resolved from the server. */
70
71 static void silc_send_msg_clients(SilcClient client,
72                                   SilcClientConnection conn,
73                                   SilcClientEntry *clients,
74                                   uint32 clients_count,
75                                   void *context)
76 {
77   PRIVMSG_REC *rec = context;
78   SILC_SERVER_REC *server = rec->server;
79   SilcClientEntry target;
80   char *nickname = NULL;
81
82   if (!clients_count) {
83     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", rec->nick);
84   } else {
85     if (clients_count > 1) {
86       silc_parse_userfqdn(rec->nick, &nickname, NULL);
87
88       /* Find the correct one. The rec->nick might be a formatted nick
89          so this will find the correct one. */
90       clients = silc_client_get_clients_local(silc_client, server->conn, 
91                                               nickname, rec->nick, 
92                                               &clients_count);
93       if (!clients) {
94         printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", 
95                   rec->nick);
96         silc_free(nickname);
97         goto out;
98       }
99       silc_free(nickname);
100     }
101
102     target = clients[0];
103
104     /* Send the private message */
105     silc_client_send_private_message(client, conn, target, 0,
106                                      rec->msg, strlen(rec->msg),
107                                      TRUE);
108   }
109   
110  out:
111   g_free(rec->nick);
112   g_free(rec->msg);
113   g_free(rec);
114 }
115
116 static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
117 {
118   PRIVMSG_REC *rec;
119   SilcClientEntry *clients;
120   uint32 clients_count;
121   char *nickname = NULL;
122
123   if (!silc_parse_userfqdn(nick, &nickname, NULL)) {
124     printformat_module("fe-common/silc", server, NULL,
125                        MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick);
126     return;
127   }
128
129   /* Find client entry */
130   clients = silc_client_get_clients_local(silc_client, server->conn, 
131                                           nickname, nick, &clients_count);
132   silc_free(nickname);
133
134   if (!clients) {
135     rec = g_new0(PRIVMSG_REC, 1);
136     rec->nick = g_strdup(nick);
137     rec->msg = g_strdup(msg);
138     rec->server = server;
139
140     /* Could not find client with that nick, resolve it from server. */
141     silc_client_get_clients(silc_client, server->conn,
142                             nickname, NULL, silc_send_msg_clients, rec);
143     return;
144   }
145
146   /* Send the private message directly */
147   silc_client_send_private_message(silc_client, server->conn, 
148                                    clients[0], 0, msg, strlen(msg), TRUE);
149 }
150
151 static int isnickflag_func(char flag)
152 {
153   return flag == '@' || flag == '+';
154 }
155
156 static int ischannel_func(const char *data)
157 {
158   return *data == '#';
159 }
160
161 const char *get_nick_flags(void)
162 {
163   return "@\0\0";
164 }
165
166 static void send_message(SILC_SERVER_REC *server, char *target, char *msg)
167 {
168   g_return_if_fail(server != NULL);
169   g_return_if_fail(target != NULL);
170   g_return_if_fail(msg != NULL);
171
172   if (*target == '#')
173     silc_send_channel(server, target, msg);
174   else
175     silc_send_msg(server, target, msg);
176 }
177
178 static void sig_connected(SILC_SERVER_REC *server)
179 {
180   SilcClientConnection conn;
181   int fd;
182
183   if (!IS_SILC_SERVER(server))
184     return;
185
186   conn = silc_client_add_connection(silc_client,
187                                     server->connrec->address,
188                                     server->connrec->port,
189                                     server);
190   server->conn = conn;
191         
192   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
193   if (!silc_client_start_key_exchange(silc_client, conn, fd)) {
194     /* some internal error occured */
195     server_disconnect(SERVER(server));
196     signal_stop();
197     return;
198   }
199
200   server->isnickflag = isnickflag_func;
201   server->ischannel = ischannel_func;
202   server->get_nick_flags = get_nick_flags;
203   server->send_message = (void *) send_message;
204 }
205
206 static void sig_disconnected(SILC_SERVER_REC *server)
207 {
208   if (!IS_SILC_SERVER(server))
209     return;
210   
211   if (server->conn && server->conn->sock != NULL) {
212     silc_client_close_connection(silc_client, NULL, server->conn);
213     
214     /* SILC closes the handle */
215     g_io_channel_unref(net_sendbuffer_handle(server->handle));
216     net_sendbuffer_destroy(server->handle, FALSE);
217     server->handle = NULL;
218   }
219 }
220
221 SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn)
222 {
223   SILC_SERVER_REC *server;
224
225   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
226   if (conn->address == NULL || *conn->address == '\0') 
227     return NULL;
228   if (conn->nick == NULL || *conn->nick == '\0') {
229     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
230               "Cannot connect: nickname is not set");
231     return NULL;
232   }
233
234   server = g_new0(SILC_SERVER_REC, 1);
235   server->chat_type = SILC_PROTOCOL;
236   server->connrec = conn;
237   if (server->connrec->port <= 0) 
238     server->connrec->port = 706;
239
240   if (!server_start_connect((SERVER_REC *) server)) {
241     server_connect_free(SERVER_CONNECT(conn));
242     g_free(server);
243     return NULL;
244   }
245
246   return server;
247 }
248
249 /* Return a string of all channels in server in server->channels_join() 
250    format */
251
252 char *silc_server_get_channels(SILC_SERVER_REC *server)
253 {
254   GSList *tmp;
255   GString *chans;
256   char *ret;
257
258   g_return_val_if_fail(server != NULL, FALSE);
259
260   chans = g_string_new(NULL);
261   for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
262     CHANNEL_REC *channel = tmp->data;
263     
264     g_string_sprintfa(chans, "%s,", channel->name);
265   }
266
267   if (chans->len > 0)
268     g_string_truncate(chans, chans->len-1);
269
270   ret = chans->str;
271   g_string_free(chans, FALSE);
272   
273   return ret;
274 }
275
276 /* Syntaxes of all SILC commands for HELP files (the help file generation
277    will snoop these from here). */
278
279 /* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
280 /* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
281 /* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] [-pubkey|<passwd>] */
282 /* SYNTAX: GETKEY <nickname or server name> */
283 /* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
284 /* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
285 /* SYNTAX: KEY MSG <nickname> set|unset|list|agreement|negotiate [<arguments>] */
286 /* SYNTAX: KEY CHANNEL <channel> set|unset|list|agreement|negotiate [<arguments>] */
287 /* SYNTAX: KICK <channel> <nickname>[@<hostname>] [<comment>] */
288 /* SYNTAX: KILL <nickname>[@<hostname>] [<comment>] */
289 /* SYNTAX: OPER <username> [-pubkey] */
290 /* SYNTAX: SILCOPER <username> [-pubkey] */
291 /* SYNTAX: TOPIC <channel> [<topic>] */
292 /* SYNTAX: UMODE +|-<modes> */
293 /* SYNTAX: WHOIS <nickname>[@<hostname>] [<count>] */
294 /* SYNTAX: WHOWAS <nickname>[@<hostname>] [<count>] */
295 /* SYNTAX: CLOSE <server> [<port>] */
296 /* SYNTAX: SHUTDOWN */
297 /* SYNTAX: MOTD [<server>] */
298 /* SYNTAX: LIST [<channel>] */
299 /* SYNTAX: ME <message> */
300 /* SYNTAX: ACTION <channel> <message> */
301 /* SYNTAX: AWAY [<message>] */
302 /* SYNTAX: INFO [<server>] */
303 /* SYNTAX: NICK <nickname> */
304 /* SYNTAX: NOTICE <message> */
305 /* SYNTAX: PART [<channel>] */
306 /* SYNTAX: PING */
307 /* SYNTAX: SCONNECT <server> [<port>] */
308 /* SYNTAX: USERS <channel> */
309
310 void silc_command_exec(SILC_SERVER_REC *server,
311                        const char *command, const char *args)
312 {
313   uint32 argc = 0;
314   unsigned char **argv;
315   uint32 *argv_lens, *argv_types;
316   char *data, *tmpcmd;
317   SilcClientCommand *cmd;
318   SilcClientCommandContext ctx;
319
320   g_return_if_fail(server != NULL);
321
322   tmpcmd = g_strdup(command); 
323   g_strup(tmpcmd);
324   cmd = silc_client_command_find(tmpcmd);
325   g_free(tmpcmd);
326   if (cmd == NULL)
327     return;
328
329   /* Now parse all arguments */
330   data = g_strconcat(command, " ", args, NULL);
331   silc_parse_command_line(data, &argv, &argv_lens,
332                           &argv_types, &argc, cmd->max_args);
333   g_free(data);
334
335   /* Allocate command context. This and its internals must be free'd
336      by the command routine receiving it. */
337   ctx = silc_client_command_alloc();
338   ctx->client = silc_client;
339   ctx->conn = server->conn;
340   ctx->command = cmd;
341   ctx->argc = argc;
342   ctx->argv = argv;
343   ctx->argv_lens = argv_lens;
344   ctx->argv_types = argv_types;
345   
346   /* Execute command */
347   (*cmd->cb)(ctx, NULL);
348 }
349
350 /* Generic command function to call any SILC command directly. */
351
352 static void command_self(const char *data, SILC_SERVER_REC *server,
353                          WI_ITEM_REC *item)
354 {
355   if (!IS_SILC_SERVER(server) || !server->connected) {
356     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
357     return;
358   }
359
360   if (IS_SILC_CHANNEL(item)) {
361     SILC_CHANNEL_REC *chanrec;
362     chanrec = silc_channel_find(server, item->name);
363     if (chanrec)
364       server->conn->current_channel = chanrec->entry;
365   }
366
367   silc_command_exec(server, current_command, data);
368   signal_stop();
369 }
370
371 /* SCONNECT command.  Calls actually SILC's CONNECT command since Irssi
372    has CONNECT command for other purposes. */
373
374 static void command_sconnect(const char *data, SILC_SERVER_REC *server)
375 {
376   if (!IS_SILC_SERVER(server) || !server->connected) {
377     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
378     return;
379   }
380
381   silc_command_exec(server, "CONNECT", data);
382   signal_stop();
383 }
384
385 static void event_text(const char *line, SILC_SERVER_REC *server,
386                        WI_ITEM_REC *item)
387 {
388   char *str;
389
390   g_return_if_fail(line != NULL);
391
392   if (!IS_SILC_ITEM(item))
393     return;
394
395   str = g_strdup_printf("%s %s", item->name, line);
396   signal_emit("command msg", 3, str, server, item);
397   g_free(str);
398
399   signal_stop();
400 }
401
402 typedef struct {
403   SILC_SERVER_REC *server;
404   char *data;
405   WI_ITEM_REC *item;
406 } *FileGetClients;
407
408 SILC_CLIENT_CMD_FUNC(file_get_clients)
409 {
410   FileGetClients internal = (FileGetClients)context;
411   signal_emit("command file", 3, internal->data, internal->server,
412               internal->item);
413   silc_free(internal->data);
414   silc_free(internal);
415 }
416
417 static void command_file(const char *data, SILC_SERVER_REC *server,
418                          WI_ITEM_REC *item)
419 {
420   SilcClientConnection conn;
421   SilcClientEntry client_entry;
422   char *nickname, *tmp;
423   unsigned char **argv;
424   uint32 argc;
425   uint32 *argv_lens, *argv_types;
426   int type;
427
428   if (!server || !IS_SILC_SERVER(server) || !server->connected)
429     cmd_return_error(CMDERR_NOT_CONNECTED);
430
431   conn = server->conn;
432
433   /* Now parse all arguments */
434   tmp = g_strconcat("KEY", " ", data, NULL);
435   silc_parse_command_line(tmp, &argv, &argv_lens, &argv_types, &argc, 4);
436   g_free(tmp);
437
438   type = 0;
439   if (!strcasecmp(argv[1], "send"))
440     type = 1;
441   if (!strcasecmp(argv[1], "receive"))
442     type = 2;
443   
444   /* Parse the typed nickname. */
445   if (!silc_parse_userfqdn(argv[3], &nickname, NULL)) {
446     printformat_module("fe-common/silc", server, NULL,
447                        MSGLEVEL_CRAP, SILCTXT_BAD_NICK, argv[2]);
448     return;
449   }
450
451   /* Find client entry */
452   client_entry = silc_idlist_get_client(silc_client, conn, nickname, 
453                                         argv[3], TRUE);
454   if (!client_entry) {
455     FileGetClients inter = silc_calloc(1, sizeof(*inter));
456     inter->server = server;
457     inter->data = strdup(data);
458     inter->item = item;
459     
460     /* Client entry not found, it was requested thus mark this to be
461        pending command. */
462     silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, 
463                                 conn->cmd_ident, 
464                                 NULL, silc_client_command_file_get_clients, 
465                                 inter);
466     goto out;
467   }
468
469   switch (type) {
470   case 1:
471     silc_client_file_send(silc_client, conn, NULL, NULL, client_entry,
472                           argv[2]);
473     break;
474
475   case 2:
476     break;
477
478   default:
479     break;
480   }
481
482  out:
483   silc_free(nickname);
484 }
485
486 void silc_server_init(void)
487 {
488   silc_servers_reconnect_init();
489
490   signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
491   signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
492   signal_add("send text", (SIGNAL_FUNC) event_text);
493   command_bind("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
494   command_bind("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
495   command_bind("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
496   command_bind("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
497   command_bind("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
498   command_bind("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
499   command_bind("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
500   command_bind("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
501   command_bind("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
502   command_bind("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
503   command_bind("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
504   command_bind("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
505   command_bind("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
506   command_bind("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
507   command_bind("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
508   command_bind("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
509   command_bind("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
510   command_bind("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
511   command_bind("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
512   command_bind("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
513   command_bind("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
514   command_bind("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
515   command_bind("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
516
517   command_set_options("connect", "+silcnet");
518 }
519
520 void silc_server_deinit(void)
521 {
522   silc_servers_reconnect_deinit();
523
524   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
525   signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
526   signal_remove("send text", (SIGNAL_FUNC) event_text);
527   command_unbind("whois", (SIGNAL_FUNC) command_self);
528   command_unbind("whowas", (SIGNAL_FUNC) command_self);
529   command_unbind("nick", (SIGNAL_FUNC) command_self);
530   command_unbind("topic", (SIGNAL_FUNC) command_self);
531   command_unbind("cmode", (SIGNAL_FUNC) command_self);
532   command_unbind("cumode", (SIGNAL_FUNC) command_self);
533   command_unbind("users", (SIGNAL_FUNC) command_self);
534   command_unbind("list", (SIGNAL_FUNC) command_self);
535   command_unbind("oper", (SIGNAL_FUNC) command_self);
536   command_unbind("silcoper", (SIGNAL_FUNC) command_self);
537   command_unbind("umode", (SIGNAL_FUNC) command_self);
538   command_unbind("invite", (SIGNAL_FUNC) command_self);
539   command_unbind("kill", (SIGNAL_FUNC) command_self);
540   command_unbind("kick", (SIGNAL_FUNC) command_self);
541   command_unbind("info", (SIGNAL_FUNC) command_self);
542   command_unbind("ping", (SIGNAL_FUNC) command_self);
543   command_unbind("motd", (SIGNAL_FUNC) command_self);
544   command_unbind("ban", (SIGNAL_FUNC) command_self);
545   command_unbind("close", (SIGNAL_FUNC) command_self);
546   command_unbind("shutdown", (SIGNAL_FUNC) command_self);
547   command_unbind("getkey", (SIGNAL_FUNC) command_self);
548   command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
549   command_unbind("file", (SIGNAL_FUNC) command_file);
550 }