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