Added "debug" and "debug_string" Irssi SILC client settings for
[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
54 void silc_expandos_init(void);
55 void silc_expandos_deinit(void);
56
57 static int my_silc_scheduler(void)
58 {
59   silc_client_run_one(silc_client);
60   return 1;
61 }
62
63 static CHATNET_REC *create_chatnet(void)
64 {
65   return g_malloc0(sizeof(CHATNET_REC));
66 }
67
68 static SERVER_SETUP_REC *create_server_setup(void)
69 {
70   return g_malloc0(sizeof(SERVER_SETUP_REC));
71 }
72
73 static CHANNEL_SETUP_REC *create_channel_setup(void)
74 {
75   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
76 }
77
78 static SERVER_CONNECT_REC *create_server_connect(void)
79 {
80   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
81 }
82
83 static void destroy_server_connect(SERVER_CONNECT_REC *conn)
84 {
85
86 }
87
88 /* Checks user information and saves them to the config file it they
89    do not exist there already. */
90
91 static void silc_init_userinfo(void)
92 {
93   const char *set, *nick, *user_name;
94   char *str;   
95         
96   /* check if nick/username/realname wasn't read from setup.. */
97   set = settings_get_str("real_name");
98   if (set == NULL || *set == '\0') {
99     str = g_getenv("SILCNAME");
100     if (!str)
101       str = g_getenv("IRCNAME");
102     settings_set_str("real_name",
103                      str != NULL ? str : silc_get_real_name());
104   }
105  
106   /* username */
107   user_name = settings_get_str("user_name");
108   if (user_name == NULL || *user_name == '\0') {
109     str = g_getenv("SILCUSER");
110     if (!str)
111       str = g_getenv("IRCUSER");
112     settings_set_str("user_name",
113                      str != NULL ? str : silc_get_username());
114
115     user_name = settings_get_str("user_name");
116   }
117
118   /* nick */
119   nick = settings_get_str("nick");
120   if (nick == NULL || *nick == '\0') {
121     str = g_getenv("SILCNICK");
122     if (!str)
123       str = g_getenv("IRCNICK");
124     settings_set_str("nick", str != NULL ? str : user_name);
125     
126     nick = settings_get_str("nick");
127   }
128                 
129   /* alternate nick */
130   set = settings_get_str("alternate_nick");
131   if (set == NULL || *set == '\0') {
132     str = g_strconcat(nick, "_", NULL);
133     settings_set_str("alternate_nick", str);
134     g_free(str);
135   }
136
137   /* host name */
138   set = settings_get_str("hostname");
139   if (set == NULL || *set == '\0') {
140     str = g_getenv("SILCHOST");
141     if (!str)
142       str = g_getenv("IRCHOST");
143     if (str != NULL)
144       settings_set_str("hostname", str);
145   }
146 }
147
148 #ifdef SILC_DEBUG
149 static bool i_debug;
150 static bool silc_irssi_debug_print(char *file, char *function, int line,
151                                    char *message, void *context)
152 {
153   printtext(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
154             "DEBUG: %s:%d:%s", function, line, message);
155   return TRUE;
156 }
157
158 static void sig_debug_setup_changed(void)
159 {
160   bool debug = settings_get_bool("debug");
161   if (debug) {
162     const char *debug_string = settings_get_str("debug_string");
163     i_debug = silc_debug = TRUE;
164     silc_log_set_debug_string(debug_string);
165     silc_log_set_debug_callbacks(silc_irssi_debug_print, NULL, NULL, NULL);
166     return;
167   }
168   if (i_debug)
169     silc_debug = FALSE;
170 }
171 #endif
172
173 /* Log callbacks */
174
175 static bool silc_log_misc(SilcLogType type, char *message, void *context)
176 {
177   fprintf(stderr, "%s\n", message);
178   return TRUE;
179 }
180
181 static void silc_nickname_format_parse(const char *nickname,
182                                        char **ret_nickname)
183 {
184   silc_parse_userfqdn(nickname, ret_nickname, NULL);
185 }
186
187 static void silc_register_cipher(SilcClient client, const char *cipher)
188 {
189   int i;
190
191   if (cipher) {
192     for (i = 0; silc_default_ciphers[i].name; i++)
193       if (!strcmp(silc_default_ciphers[i].name, cipher)) {
194         silc_cipher_register(&(silc_default_ciphers[i]));
195         break;
196       }
197     
198     if (!silc_cipher_is_supported(cipher)) {
199       SILC_LOG_ERROR(("Unknown cipher `%s'", cipher));
200       exit(1);
201     }
202   }
203
204   /* Register other defaults */
205   silc_cipher_register_default();
206 }
207
208 static void silc_register_hash(SilcClient client, const char *hash)
209 {
210   int i;
211
212   if (hash) {
213     for (i = 0; silc_default_hash[i].name; i++)
214       if (!strcmp(silc_default_hash[i].name, hash)) {
215         silc_hash_register(&(silc_default_hash[i]));
216         break;
217       }
218     
219     if (!silc_hash_is_supported(hash)) {
220       SILC_LOG_ERROR(("Unknown hash function `%s'", hash));
221       exit(1);
222     }
223   }
224
225   /* Register other defaults */
226   silc_hash_register_default();
227 }
228
229 static void silc_register_hmac(SilcClient client, const char *hmac)
230 {
231   int i;
232
233   if (hmac) {
234     for (i = 0; silc_default_hmacs[i].name; i++)
235       if (!strcmp(silc_default_hmacs[i].name, hmac)) {
236         silc_hmac_register(&(silc_default_hmacs[i]));
237         break;
238       }
239     
240     if (!silc_hmac_is_supported(hmac)) {
241       SILC_LOG_ERROR(("Unknown HMAC `%s'", hmac));
242       exit(1);
243     }
244   }
245
246   /* Register other defaults */
247   silc_hmac_register_default();
248 }
249
250 /* Finalize init. Init finish signal calls this. */
251
252 void silc_opt_callback(poptContext con, 
253                        enum poptCallbackReason reason,
254                        const struct poptOption *opt,
255                        const char *arg, void *data)
256 {
257   if (strcmp(opt->longName, "show-key") == 0) {
258     /* Dump the key */
259     silc_cipher_register_default();
260     silc_pkcs_register_default();
261     silc_hash_register_default();
262     silc_hmac_register_default();
263     silc_client_show_key((char *)arg);
264     exit(0);
265   }
266
267   if (strcmp(opt->longName, "list-ciphers") == 0) {
268     silc_cipher_register_default();
269     silc_client_list_ciphers();
270     exit(0);
271   }
272
273   if (strcmp(opt->longName, "list-hash-funcs") == 0) {
274     silc_hash_register_default();
275     silc_client_list_hash_funcs();
276     exit(0);
277   }
278
279   if (strcmp(opt->longName, "list-hmacs") == 0) {
280     silc_hmac_register_default();
281     silc_client_list_hmacs();
282     exit(0);
283   }
284
285   if (strcmp(opt->longName, "list-pkcs") == 0) {
286     silc_pkcs_register_default();
287     silc_client_list_pkcs();
288     exit(0);
289   }
290
291   if (strcmp(opt->longName, "debug") == 0) {
292     silc_debug = TRUE;
293     silc_debug_hexdump = TRUE;
294     silc_log_set_debug_string(arg);
295     silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
296     silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
297     silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
298     silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
299 #ifndef SILC_DEBUG
300     fprintf(stdout, 
301             "Run-time debugging is not enabled. To enable it recompile\n"
302             "the client with --enable-debug configuration option.\n");
303     sleep(1);
304 #endif
305   }
306 }
307
308 static void sig_init_read_settings(void)
309 {
310   if (opt_create_keypair) {
311     /* Create new key pair and exit */
312     silc_cipher_register_default();
313     silc_pkcs_register_default();
314     silc_hash_register_default();
315     silc_hmac_register_default();
316     silc_client_create_key_pair(opt_pkcs, opt_bits, 
317                                 NULL, NULL, NULL, NULL, NULL);
318     exit(0);
319   }
320 }
321
322 /* Init SILC. Called from src/fe-text/silc.c */
323
324 void silc_core_init(void)
325 {
326   static struct poptOption silc_options[] = {
327     { NULL, '\0', POPT_ARG_CALLBACK, (void *)&silc_opt_callback, '\0', NULL },
328     { "show-key", 'S', POPT_ARG_STRING, NULL, 0,
329       "Show the contents of the public key", "FILE" },
330     { "list-ciphers", 'c', POPT_ARG_NONE, NULL, 0,
331       "List supported ciphers", NULL },
332     { "list-hash-funcs", 'H', POPT_ARG_NONE, NULL, 0,
333       "List supported hash functions", NULL },
334     { "list-hmacs", 'M', POPT_ARG_NONE, NULL, 0,
335       "List supported HMACs", NULL },
336     { "list-pkcs", 'P', POPT_ARG_NONE, NULL, 0,
337       "List supported PKCSs", NULL },
338     { "debug", 'd', POPT_ARG_STRING, NULL, 0,
339       "Enable debugging", "STRING" },
340     { NULL, '\0', 0, NULL }
341   };
342
343   static struct poptOption options[] = {
344     { NULL, '\0', POPT_ARG_INCLUDE_TABLE, silc_options, 0, NULL, NULL },
345     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0,
346       "Create new public key pair", NULL },
347     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
348       "Set the PKCS of the public key pair", "PKCS" },
349     { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
350       "Set the length of the public key pair", "VALUE" },
351     { NULL, '\0', 0, NULL }
352   };
353
354   CHAT_PROTOCOL_REC *rec;
355   SilcClientParams params;
356   const char *def_cipher, *def_hash, *def_hmac;
357
358   args_register(options);
359   signal_add("irssi init read settings", (SIGNAL_FUNC) sig_init_read_settings);
360
361   /* Settings */
362   settings_add_bool("server", "skip_motd", FALSE);
363   settings_add_str("server", "alternate_nick", NULL);
364   settings_add_bool("server", "use_auto_addr", FALSE);
365   settings_add_str("server", "auto_bind_ip", "");
366   settings_add_str("server", "auto_public_ip", "");
367   settings_add_int("server", "auto_bind_port", 0);
368   settings_add_str("server", "crypto_default_cipher", SILC_DEFAULT_CIPHER);
369   settings_add_str("server", "crypto_default_hash", SILC_DEFAULT_HASH);
370   settings_add_str("server", "crypto_default_hmac", SILC_DEFAULT_HMAC);
371   settings_add_int("server", "key_exchange_timeout_secs", 120);
372   settings_add_int("server", "key_exchange_rekey_secs", 3600);
373   settings_add_int("server", "connauth_request_secs", 2);
374
375 #ifdef SILC_DEBUG
376   settings_add_bool("debug", "debug", FALSE);
377   settings_add_str("debug", "debug_string", "");
378   signal_add("setup changed", (SIGNAL_FUNC) sig_debug_setup_changed);
379 #endif
380
381   silc_init_userinfo();
382
383   /* Initialize client parameters */
384   memset(&params, 0, sizeof(params));
385   strcat(params.nickname_format, "%n@%h%a");
386   params.nickname_parse = silc_nickname_format_parse;
387   params.rekey_secs = settings_get_int("key_exchange_rekey_secs");
388   params.connauth_request_secs = settings_get_int("connauth_request_secs");
389
390   /* Allocate SILC client */
391   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
392
393   /* Get the ciphers and stuff from config file */
394   def_cipher = settings_get_str("crypto_default_cipher");
395   def_hash = settings_get_str("crypto_default_hash");
396   def_hmac = settings_get_str("crypto_default_hmac");
397   silc_register_cipher(silc_client, def_cipher);
398   silc_register_hash(silc_client, def_hash);
399   silc_register_hmac(silc_client, def_hmac);
400   silc_pkcs_register_default();
401
402   /* Get user information */
403   silc_client->username = g_strdup(settings_get_str("user_name"));
404   silc_client->nickname = g_strdup(settings_get_str("nick"));
405   silc_client->hostname = silc_net_localhost();
406   silc_client->realname = g_strdup(settings_get_str("real_name"));
407
408   /* Check ~/.silc directory and public and private keys */
409   if (silc_client_check_silc_dir() == FALSE) {
410     idletag = -1;
411     return;
412   }
413
414   /* Load public and private key */
415   if (silc_client_load_keys(silc_client) == FALSE) {
416     idletag = -1;
417     return;
418   }
419
420   /* Initialize the SILC client */
421   if (!silc_client_init(silc_client)) {
422     idletag = -1;
423     return;
424   }
425
426   /* Register SILC to the irssi */
427   rec = g_new0(CHAT_PROTOCOL_REC, 1);
428   rec->name = "SILC";
429   rec->fullname = "Secure Internet Live Conferencing";
430   rec->chatnet = "silcnet";
431   rec->create_chatnet = create_chatnet;
432   rec->create_server_setup = create_server_setup;
433   rec->create_channel_setup = create_channel_setup;
434   rec->create_server_connect = create_server_connect;
435   rec->destroy_server_connect = destroy_server_connect;
436   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
437     silc_server_connect; 
438   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
439     silc_channel_create;
440   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
441     silc_query_create;
442   
443   chat_protocol_register(rec);
444   g_free(rec);
445
446   silc_server_init();
447   silc_channels_init();
448   silc_queries_init();
449   silc_expandos_init();
450
451   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
452
453   module_register("silc", "core");
454 }
455
456 /* Deinit SILC. Called from src/fe-text/silc.c */
457
458 void silc_core_deinit(void)
459 {
460   if (idletag != -1) {
461     signal_emit("chat protocol deinit", 1,
462                 chat_protocol_find("SILC"));
463     signal_remove("irssi init read settings", 
464                   (SIGNAL_FUNC) sig_init_read_settings);
465 #ifdef SILC_DEBUG
466     signal_remove("setup changed", (SIGNAL_FUNC) sig_debug_setup_changed);
467 #endif
468     
469     silc_server_deinit();
470     silc_channels_deinit();
471     silc_queries_deinit();
472     silc_expandos_deinit();
473     
474     chat_protocol_unregister("SILC");
475     
476     g_source_remove(idletag);
477   }
478   
479   g_free(silc_client->username);
480   g_free(silc_client->realname);
481   silc_client_free(silc_client);
482 }