Fixed the USERS & JOIN command replys, added silc-expandos.c to
[silc.git] / apps / irssi / src / silc / core / silc-core.c
1 /*
2
3   silc-core.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2001 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 #include "module.h"
22 #include "chat-protocols.h"
23 #include "args.h"
24
25 #include "chatnets.h"
26 #include "servers-setup.h"
27 #include "channels-setup.h"
28 #include "silc-servers.h"
29 #include "silc-channels.h"
30 #include "silc-queries.h"
31 #include "silc-nicklist.h"
32 #include "silcversion.h"
33
34 #include "signals.h"
35 #include "levels.h"
36 #include "settings.h"
37 #include "fe-common/core/printtext.h"
38 #include "fe-common/core/fe-channels.h"
39 #include "fe-common/core/keyboard.h"
40 #include "fe-common/silc/module-formats.h"
41
42 /* Command line option variables */
43 static bool opt_create_keypair = FALSE;
44 static char *opt_pkcs = NULL;
45 static int opt_bits = 0;
46
47 static int idletag;
48
49 SilcClient silc_client = NULL;
50 extern SilcClientOperations ops;
51 extern bool silc_debug;
52 extern bool silc_debug_hexdump;
53 #ifdef SILC_SIM
54 /* SIM (SILC Module) table */
55 SilcSimContext **sims = NULL;
56 SilcUInt32 sims_count = 0;
57 #endif
58
59 void silc_expandos_init(void);
60 void silc_expandos_deinit(void);
61
62 static int my_silc_scheduler(void)
63 {
64   silc_client_run_one(silc_client);
65   return 1;
66 }
67
68 static CHATNET_REC *create_chatnet(void)
69 {
70   return g_malloc0(sizeof(CHATNET_REC));
71 }
72
73 static SERVER_SETUP_REC *create_server_setup(void)
74 {
75   return g_malloc0(sizeof(SERVER_SETUP_REC));
76 }
77
78 static CHANNEL_SETUP_REC *create_channel_setup(void)
79 {
80   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
81 }
82
83 static SERVER_CONNECT_REC *create_server_connect(void)
84 {
85   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
86 }
87
88 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
89 {
90
91 }
92
93 /* Checks user information and saves them to the config file it they
94    do not exist there already. */
95
96 static void silc_init_userinfo(void)
97 {
98   const char *set, *nick, *user_name;
99   char *str;   
100         
101   /* check if nick/username/realname wasn't read from setup.. */
102   set = settings_get_str("real_name");
103   if (set == NULL || *set == '\0') {
104     str = g_getenv("SILCNAME");
105     if (!str)
106       str = g_getenv("IRCNAME");
107     settings_set_str("real_name",
108                      str != NULL ? str : g_get_real_name());
109   }
110  
111   /* username */
112   user_name = settings_get_str("user_name");
113   if (user_name == NULL || *user_name == '\0') {
114     str = g_getenv("SILCUSER");
115     if (!str)
116       str = g_getenv("IRCUSER");
117     settings_set_str("user_name",
118                      str != NULL ? str : g_get_user_name());
119     
120     user_name = settings_get_str("user_name");
121   }
122
123   /* nick */
124   nick = settings_get_str("nick");
125   if (nick == NULL || *nick == '\0') {
126     str = g_getenv("SILCNICK");
127     if (!str)
128       str = g_getenv("IRCNICK");
129     settings_set_str("nick", str != NULL ? str : user_name);
130     
131     nick = settings_get_str("nick");
132   }
133                 
134   /* alternate nick */
135   set = settings_get_str("alternate_nick");
136   if (set == NULL || *set == '\0') {
137     if (strlen(nick) < 9)
138       str = g_strconcat(nick, "_", NULL);
139     else { 
140       str = g_strdup(nick);
141       str[strlen(str)-1] = '_';
142     }
143     settings_set_str("alternate_nick", str);
144     g_free(str);
145   }
146
147   /* host name */
148   set = settings_get_str("hostname");
149   if (set == NULL || *set == '\0') {
150     str = g_getenv("SILCHOST");
151     if (!str)
152       str = g_getenv("IRCHOST");
153     if (str != NULL)
154       settings_set_str("hostname", str);
155   }
156 }
157
158 /* Log callbacks */
159
160 static bool silc_log_misc(SilcLogType type, char *message, void *context)
161 {
162   fprintf(stderr, "%s\n", message);
163   return TRUE;
164 }
165
166 static void silc_nickname_format_parse(const char *nickname,
167                                        char **ret_nickname)
168 {
169   silc_parse_userfqdn(nickname, ret_nickname, NULL);
170 }
171
172 static void silc_register_cipher(SilcClient client, const char *cipher)
173 {
174   int i;
175
176   if (cipher) {
177     for (i = 0; silc_default_ciphers[i].name; i++)
178       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
179         silc_cipher_register((SilcCipherObject *)&silc_default_ciphers[i]);
180         break;
181       }
182     
183     if (!silc_cipher_is_supported(cipher)) {
184       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
185       exit(1);
186     }
187   }
188
189   /* Register other defaults */
190   silc_cipher_register_default();
191 }
192
193 static void silc_register_hash(SilcClient client, const char *hash)
194 {
195   int i;
196
197   if (hash) {
198     for (i = 0; silc_default_hash[i].name; i++)
199       if (!strcmp(silc_default_hash[i].name, hash)) {
200         silc_hash_register((SilcHashObject *)&silc_default_hash[i]);
201         break;
202       }
203     
204     if (!silc_hash_is_supported(hash)) {
205       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
206       exit(1);
207     }
208   }
209
210   /* Register other defaults */
211   silc_hash_register_default();
212 }
213
214 static void silc_register_hmac(SilcClient client, const char *hmac)
215 {
216   int i;
217
218   if (hmac) {
219     for (i = 0; silc_default_hmacs[i].name; i++)
220       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
221         silc_hmac_register((SilcHmacObject *)&silc_default_hmacs[i]);
222         break;
223       }
224     
225     if (!silc_hmac_is_supported(hmac)) {
226       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
227       exit(1);
228     }
229   }
230
231   /* Register other defaults */
232   silc_hmac_register_default();
233 }
234
235 /* Finalize init. Init finish signal calls this. */
236
237 void silc_opt_callback(poptContext con, 
238                        enum poptCallbackReason reason,
239                        const struct poptOption *opt,
240                        const char *arg, void *data)
241 {
242   if (strcmp(opt->longName, "show-key") == 0) {
243     /* Dump the key */
244     silc_cipher_register_default();
245     silc_pkcs_register_default();
246     silc_hash_register_default();
247     silc_hmac_register_default();
248     silc_client_show_key((char *)arg);
249     exit(0);
250   }
251
252   if (strcmp(opt->longName, "list-ciphers") == 0) {
253     silc_cipher_register_default();
254     silc_client_list_ciphers();
255     exit(0);
256   }
257
258   if (strcmp(opt->longName, "list-hash-funcs") == 0) {
259     silc_hash_register_default();
260     silc_client_list_hash_funcs();
261     exit(0);
262   }
263
264   if (strcmp(opt->longName, "list-hmacs") == 0) {
265     silc_hmac_register_default();
266     silc_client_list_hmacs();
267     exit(0);
268   }
269
270   if (strcmp(opt->longName, "list-pkcs") == 0) {
271     silc_pkcs_register_default();
272     silc_client_list_pkcs();
273     exit(0);
274   }
275
276   if (strcmp(opt->longName, "debug") == 0) {
277     silc_debug = TRUE;
278     silc_debug_hexdump = TRUE;
279     silc_log_set_debug_string(arg);
280     silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
281     silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
282     silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
283     silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
284 #ifndef SILC_DEBUG
285     fprintf(stdout, 
286             "Run-time debugging is not enabled. To enable it recompile\n"
287             "the client with --enable-debug configuration option.\n");
288     sleep(1);
289 #endif
290   }
291 }
292
293 static void sig_init_read_settings(void)
294 {
295   if (opt_create_keypair) {
296     /* Create new key pair and exit */
297     silc_cipher_register_default();
298     silc_pkcs_register_default();
299     silc_hash_register_default();
300     silc_hmac_register_default();
301     silc_client_create_key_pair(opt_pkcs, opt_bits, 
302                                 NULL, NULL, NULL, NULL, NULL);
303     exit(0);
304   }
305 }
306
307 /* Init SILC. Called from src/fe-text/silc.c */
308
309 void silc_core_init(void)
310 {
311   static struct poptOption silc_options[] = {
312     { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
313     { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
314       "Show the contents of the public key", "FILE" },
315     { "list-ciphers", 'c', POPT_ARG_NONE, NULL, 0,
316       "List supported ciphers", NULL },
317     { "list-hash-funcs", 'H', POPT_ARG_NONE, NULL, 0,
318       "List supported hash functions", NULL },
319     { "list-hmacs", 'M', POPT_ARG_NONE, NULL, 0,
320       "List supported HMACs", NULL },
321     { "list-pkcs", 'P', POPT_ARG_NONE, NULL, 0,
322       "List supported PKCSs", NULL },
323     { "debug", 'd', POPT_ARG_STRING, NULL, 0,
324       "Enable debugging", "STRING" },
325     { NULL, '\0', 0, NULL }
326   };
327
328   static struct poptOption options[] = {
329     { NULL, '\0', POPT_ARG_INCLUDE_TABLE, silc_options, 0, NULL, NULL },
330     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0,
331       "Create new public key pair", NULL },
332     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
333       "Set the PKCS of the public key pair", "PKCS" },
334     { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
335       "Set the length of the public key pair", "VALUE" },
336     { NULL, '\0', 0, NULL }
337   };
338
339   CHAT_PROTOCOL_REC *rec;
340   SilcClientParams params;
341   const char *def_cipher, *def_hash, *def_hmac;
342
343   args_register(options);
344   signal_add("irssi init read settings", (SIGNAL_FUNC) sig_init_read_settings);
345
346   /* Settings */
347   settings_add_bool("server", "skip_motd", FALSE);
348   settings_add_str("server", "alternate_nick", NULL);
349   settings_add_bool("server", "use_auto_addr", FALSE);
350   settings_add_str("server", "auto_bind_ip", "");
351   settings_add_str("server", "auto_public_ip", "");
352   settings_add_int("server", "auto_bind_port", 0);
353   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
354   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
355   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
356   settings_add_int("server", "key_exchange_timeout_secs", 120);
357   settings_add_int("server", "key_exchange_rekey_secs", 3600);
358   settings_add_int("server", "connauth_request_secs", 2);
359
360   silc_init_userinfo();
361
362   /* Initialize client parameters */
363   memset(&params, 0, sizeof(params));
364   strcat(params.nickname_format, "%n@%h%a");
365   params.nickname_parse = silc_nickname_format_parse;
366   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
367   params.connauth_request_secs = settings_get_int("connauth_request_secs");
368
369   /* Allocate SILC client */
370   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
371
372   /* Get the ciphers and stuff from config file */
373   def_cipher = settings_get_str("crypto_default_cipher");
374   def_hash = settings_get_str("crypto_default_hash");
375   def_hmac = settings_get_str("crypto_default_hmac");
376   silc_register_cipher(silc_client, def_cipher);
377   silc_register_hash(silc_client, def_hash);
378   silc_register_hmac(silc_client, def_hmac);
379   silc_pkcs_register_default();
380
381   /* Get user information */
382   silc_client->username = g_strdup(settings_get_str("user_name"));
383   silc_client->nickname = g_strdup(settings_get_str("nick"));
384   silc_client->hostname = silc_net_localhost();
385   silc_client->realname = g_strdup(settings_get_str("real_name"));
386
387   /* Check ~/.silc directory and public and private keys */
388   if (silc_client_check_silc_dir() == FALSE) {
389     idletag = -1;
390     return;
391   }
392
393   /* Load public and private key */
394   if (silc_client_load_keys(silc_client) == FALSE) {
395     idletag = -1;
396     return;
397   }
398
399   /* Initialize the SILC client */
400   if (!silc_client_init(silc_client)) {
401     idletag = -1;
402     return;
403   }
404
405   /* Register SILC to the irssi */
406   rec = g_new0(CHAT_PROTOCOL_REC, 1);
407   rec->name = "SILC";
408   rec->fullname = "Secure Internet Live Conferencing";
409   rec->chatnet = "silcnet";
410   rec->create_chatnet = create_chatnet;
411   rec->create_server_setup = create_server_setup;
412   rec->create_channel_setup = create_channel_setup;
413   rec->create_server_connect = create_server_connect;
414   rec->destroy_server_connect = destroy_server_connect;
415   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
416     silc_server_connect; 
417   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
418     silc_channel_create;
419   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
420     silc_query_create;
421   
422   chat_protocol_register(rec);
423   g_free(rec);
424
425   silc_server_init();
426   silc_channels_init();
427   silc_queries_init();
428   silc_expandos_init();
429
430   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
431
432   module_register("silc", "core");
433 }
434
435 /* Deinit SILC. Called from src/fe-text/silc.c */
436
437 void silc_core_deinit(void)
438 {
439   if (idletag != -1) {
440     signal_emit("chat protocol deinit", 1,
441                 chat_protocol_find("SILC"));
442     
443     silc_server_deinit();
444     silc_channels_deinit();
445     silc_queries_deinit();
446     silc_expandos_deinit();
447     
448     chat_protocol_unregister("SILC");
449     
450     g_source_remove(idletag);
451   }
452   
453   g_free(silc_client->username);
454   g_free(silc_client->realname);
455   silc_client_free(silc_client);
456 }