update updates-s-
[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)
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 } PRIVMSG_REC;
65
66 /* Callback function that sends the private message if the client was
67    resolved from the server. */
68
69 static void silc_send_msg_clients(SilcClient client,
70                                   SilcClientConnection conn,
71                                   SilcClientEntry *clients,
72                                   uint32 clients_count,
73                                   void *context)
74 {
75   PRIVMSG_REC *rec = context;
76   SilcClientEntry target;
77   
78   if (clients_count == 0) {
79     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Unknown nick: %s", rec->nick);
80   } else {
81     target = clients[0]; /* FIXME: not a good idea :) */
82     
83     silc_client_send_private_message(client, conn, target, 0,
84                                      rec->msg, strlen(rec->msg),
85                                      TRUE);
86   }
87   
88   g_free(rec->nick);
89   g_free(rec->msg);
90   g_free(rec);
91 }
92
93 static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
94 {
95   PRIVMSG_REC *rec;
96   SilcClientEntry client_entry;
97   uint32 num = 0;
98   char *nickname = NULL, *serv = NULL;
99   
100   if (!silc_parse_nickname(nick, &nickname, &serv, &num)) {
101     printformat_module("fe-common/silc", server, NULL,
102                        MSGLEVEL_CRAP, SILCTXT_BAD_NICK, nick);
103     return;
104   }
105
106   /* Find client entry */
107   client_entry = silc_idlist_get_client(silc_client, server->conn, 
108                                         nickname, serv, num, FALSE);
109   if (!client_entry) {
110     rec = g_new0(PRIVMSG_REC, 1);
111     rec->nick = g_strdup(nick);
112     rec->msg = g_strdup(msg);
113
114     /* Could not find client with that nick, resolve it from server. */
115     silc_client_get_clients(silc_client, server->conn,
116                             nickname, serv, silc_send_msg_clients, rec);
117     return;
118   }
119
120   /* Send the private message directly */
121   silc_client_send_private_message(silc_client, server->conn, client_entry, 0,
122                                    msg, strlen(msg), TRUE);
123 }
124
125 static int isnickflag_func(char flag)
126 {
127   return flag == '@' || flag == '+';
128 }
129
130 static int ischannel_func(const char *data)
131 {
132   return *data == '#';
133 }
134
135 const char *get_nick_flags(void)
136 {
137   return "@\0\0";
138 }
139
140 static void send_message(SILC_SERVER_REC *server, char *target, char *msg)
141 {
142   g_return_if_fail(server != NULL);
143   g_return_if_fail(target != NULL);
144   g_return_if_fail(msg != NULL);
145
146   if (*target == '#')
147     silc_send_channel(server, target, msg);
148   else
149     silc_send_msg(server, target, msg);
150 }
151
152 static void sig_connected(SILC_SERVER_REC *server)
153 {
154   SilcClientConnection conn;
155   int fd;
156
157   if (!IS_SILC_SERVER(server))
158     return;
159
160   conn = silc_client_add_connection(silc_client,
161                                     server->connrec->address,
162                                     server->connrec->port,
163                                     server);
164   server->conn = conn;
165         
166   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
167   if (!silc_client_start_key_exchange(silc_client, conn, fd)) {
168     /* some internal error occured */
169     server_disconnect(SERVER(server));
170     signal_stop();
171     return;
172   }
173
174   server->isnickflag = isnickflag_func;
175   server->ischannel = ischannel_func;
176   server->get_nick_flags = get_nick_flags;
177   server->send_message = (void *) send_message;
178 }
179
180 static void sig_disconnected(SILC_SERVER_REC *server)
181 {
182   if (!IS_SILC_SERVER(server) || server->conn == NULL)
183     return;
184   
185   if (server->conn->sock != NULL) {
186     silc_client_close_connection(silc_client, NULL, server->conn);
187     
188     /* SILC closes the handle */
189     g_io_channel_unref(net_sendbuffer_handle(server->handle));
190     net_sendbuffer_destroy(server->handle, FALSE);
191     server->handle = NULL;
192   }
193 }
194
195 SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn)
196 {
197   SILC_SERVER_REC *server;
198
199   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
200   if (conn->address == NULL || *conn->address == '\0') 
201     return NULL;
202   if (conn->nick == NULL || *conn->nick == '\0') {
203     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, 
204               "Cannot connect: nickname is not set");
205     return NULL;
206   }
207
208   server = g_new0(SILC_SERVER_REC, 1);
209   server->chat_type = SILC_PROTOCOL;
210   server->connrec = conn;
211   if (server->connrec->port <= 0) 
212     server->connrec->port = 706;
213
214   if (!server_start_connect((SERVER_REC *) server)) {
215     server_connect_free(SERVER_CONNECT(conn));
216     g_free(server);
217     return NULL;
218   }
219
220   return server;
221 }
222
223 /* Return a string of all channels in server in server->channels_join() 
224    format */
225
226 char *silc_server_get_channels(SILC_SERVER_REC *server)
227 {
228   GSList *tmp;
229   GString *chans;
230   char *ret;
231
232   g_return_val_if_fail(server != NULL, FALSE);
233
234   chans = g_string_new(NULL);
235   for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
236     CHANNEL_REC *channel = tmp->data;
237     
238     g_string_sprintfa(chans, "%s,", channel->name);
239   }
240
241   if (chans->len > 0)
242     g_string_truncate(chans, chans->len-1);
243
244   ret = chans->str;
245   g_string_free(chans, FALSE);
246   
247   return ret;
248 }
249
250 void silc_command_exec(SILC_SERVER_REC *server,
251                        const char *command, const char *args)
252 {
253   uint32 argc = 0;
254   unsigned char **argv;
255   uint32 *argv_lens, *argv_types;
256   char *data, *tmpcmd;
257   SilcClientCommand *cmd;
258   SilcClientCommandContext ctx;
259
260   g_return_if_fail(server != NULL);
261
262   tmpcmd = g_strdup(command); 
263   g_strup(tmpcmd);
264   cmd = silc_client_command_find(tmpcmd);
265   g_free(tmpcmd);
266   if (cmd == NULL)
267     return;
268
269   /* Now parse all arguments */
270   data = g_strconcat(command, " ", args, NULL);
271   silc_parse_command_line(data, &argv, &argv_lens,
272                           &argv_types, &argc, cmd->max_args);
273   g_free(data);
274
275   /* Allocate command context. This and its internals must be free'd
276      by the command routine receiving it. */
277   ctx = silc_client_command_alloc();
278   ctx->client = silc_client;
279   ctx->conn = server->conn;
280   ctx->command = cmd;
281   ctx->argc = argc;
282   ctx->argv = argv;
283   ctx->argv_lens = argv_lens;
284   ctx->argv_types = argv_types;
285   
286   /* Execute command */
287   (*cmd->cb)(ctx);
288 }
289
290 /* Generic command function to call any SILC command directly. */
291
292 static void command_self(const char *data, SILC_SERVER_REC *server)
293 {
294   if (!IS_SILC_SERVER(server) || !server->connected) {
295     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
296     return;
297   }
298
299   silc_command_exec(server, current_command, data);
300   signal_stop();
301 }
302
303 /* SCONNECT command.  Calls actually SILC's CONNECT command since Irssi
304    has CONNECT command for other purposes. */
305
306 static void command_sconnect(const char *data, SILC_SERVER_REC *server)
307 {
308   if (!IS_SILC_SERVER(server) || !server->connected) {
309     printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "Not connected to server");
310     return;
311   }
312
313   silc_command_exec(server, "CONNECT", data);
314   signal_stop();
315 }
316
317 static void event_text(const char *line, SILC_SERVER_REC *server,
318                        WI_ITEM_REC *item)
319 {
320   char *str;
321
322   g_return_if_fail(line != NULL);
323
324   if (!IS_SILC_ITEM(item))
325     return;
326
327   str = g_strdup_printf("%s %s", item->name, line);
328   signal_emit("command msg", 3, str, server, item);
329   g_free(str);
330
331   signal_stop();
332 }
333
334 void silc_server_init(void)
335 {
336   silc_servers_reconnect_init();
337
338   signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
339   signal_add("server disconnected", (SIGNAL_FUNC) sig_disconnected);
340   signal_add("send text", (SIGNAL_FUNC) event_text);
341   command_bind("whois", MODULE_NAME, (SIGNAL_FUNC) command_self);
342   command_bind("whowas", MODULE_NAME, (SIGNAL_FUNC) command_self);
343   command_bind("nick", MODULE_NAME, (SIGNAL_FUNC) command_self);
344   command_bind("topic", MODULE_NAME, (SIGNAL_FUNC) command_self);
345   command_bind("cmode", MODULE_NAME, (SIGNAL_FUNC) command_self);
346   command_bind("cumode", MODULE_NAME, (SIGNAL_FUNC) command_self);
347   command_bind("users", MODULE_NAME, (SIGNAL_FUNC) command_self);
348   command_bind("list", MODULE_NAME, (SIGNAL_FUNC) command_self);
349   command_bind("ban", MODULE_NAME, (SIGNAL_FUNC) command_self);
350   command_bind("oper", MODULE_NAME, (SIGNAL_FUNC) command_self);
351   command_bind("silcoper", MODULE_NAME, (SIGNAL_FUNC) command_self);
352   command_bind("umode", MODULE_NAME, (SIGNAL_FUNC) command_self);
353   command_bind("invite", MODULE_NAME, (SIGNAL_FUNC) command_self);
354   command_bind("kill", MODULE_NAME, (SIGNAL_FUNC) command_self);
355   command_bind("kick", MODULE_NAME, (SIGNAL_FUNC) command_self);
356   command_bind("info", MODULE_NAME, (SIGNAL_FUNC) command_self);
357   command_bind("ping", MODULE_NAME, (SIGNAL_FUNC) command_self);
358   command_bind("motd", MODULE_NAME, (SIGNAL_FUNC) command_self);
359   command_bind("close", MODULE_NAME, (SIGNAL_FUNC) command_self);
360   command_bind("shutdown", MODULE_NAME, (SIGNAL_FUNC) command_self);
361   command_bind("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
362   command_bind("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
363
364   command_set_options("connect", "+silcnet");
365 }
366
367 void silc_server_deinit(void)
368 {
369   silc_servers_reconnect_deinit();
370
371   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
372   signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
373   signal_remove("send text", (SIGNAL_FUNC) event_text);
374   command_unbind("whois", (SIGNAL_FUNC) command_self);
375   command_unbind("whowas", (SIGNAL_FUNC) command_self);
376   command_unbind("nick", (SIGNAL_FUNC) command_self);
377   command_unbind("topic", (SIGNAL_FUNC) command_self);
378   command_unbind("cmode", (SIGNAL_FUNC) command_self);
379   command_unbind("cumode", (SIGNAL_FUNC) command_self);
380   command_unbind("users", (SIGNAL_FUNC) command_self);
381   command_unbind("list", (SIGNAL_FUNC) command_self);
382   command_unbind("oper", (SIGNAL_FUNC) command_self);
383   command_unbind("silcoper", (SIGNAL_FUNC) command_self);
384   command_unbind("umode", (SIGNAL_FUNC) command_self);
385   command_unbind("invite", (SIGNAL_FUNC) command_self);
386   command_unbind("kill", (SIGNAL_FUNC) command_self);
387   command_unbind("kick", (SIGNAL_FUNC) command_self);
388   command_unbind("info", (SIGNAL_FUNC) command_self);
389   command_unbind("ping", (SIGNAL_FUNC) command_self);
390   command_unbind("motd", (SIGNAL_FUNC) command_self);
391   command_unbind("ban", (SIGNAL_FUNC) command_self);
392   command_unbind("close", (SIGNAL_FUNC) command_self);
393   command_unbind("shutdown", (SIGNAL_FUNC) command_self);
394   command_unbind("getkey", (SIGNAL_FUNC) command_self);
395   command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
396 }