Merged 0.7.99 irssi.
[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 "version_internal.h"
33 #include "version.h"
34
35 #include "signals.h"
36 #include "levels.h"
37 #include "settings.h"
38 #include "fe-common/core/printtext.h"
39 #include "fe-common/core/fe-channels.h"
40 #include "fe-common/core/keyboard.h"
41 #include "fe-common/silc/module-formats.h"
42
43 /* Command line option variables */
44 static bool opt_create_keypair = FALSE;
45 static bool opt_list_ciphers = FALSE;
46 static bool opt_list_hash = FALSE;
47 static bool opt_list_hmac = FALSE;
48 static bool opt_list_pkcs = FALSE;
49 static bool opt_version = FALSE;
50 static char *opt_debug = FALSE;
51 static char *opt_pkcs = NULL;
52 static char *opt_keyfile = NULL;
53 static int opt_bits = 0;
54
55 static int idletag;
56
57 SilcClient silc_client = NULL;
58 SilcClientConfig silc_config = NULL;
59 extern SilcClientOperations ops;
60 extern bool silc_debug;
61 extern bool silc_debug_hexdump;
62 #ifdef SILC_SIM
63 /* SIM (SILC Module) table */
64 SilcSimContext **sims = NULL;
65 uint32 sims_count = 0;
66 #endif
67
68 static int my_silc_scheduler(void)
69 {
70   silc_client_run_one(silc_client);
71   return 1;
72 }
73
74 static CHATNET_REC *create_chatnet(void)
75 {
76   return g_malloc0(sizeof(CHATNET_REC));
77 }
78
79 static SERVER_SETUP_REC *create_server_setup(void)
80 {
81   return g_malloc0(sizeof(SERVER_SETUP_REC));
82 }
83
84 static CHANNEL_SETUP_REC *create_channel_setup(void)
85 {
86   return g_malloc0(sizeof(CHANNEL_SETUP_REC));
87 }
88
89 static SERVER_CONNECT_REC *create_server_connect(void)
90 {
91   return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
92 }
93
94 /* Checks user information and saves them to the config file it they
95    do not exist there already. */
96
97 static void silc_init_userinfo(void)
98 {
99   const char *set, *nick, *user_name;
100   char *str;   
101         
102   /* check if nick/username/realname wasn't read from setup.. */
103   set = settings_get_str("real_name");
104   if (set == NULL || *set == '\0') {
105     str = g_getenv("SILCNAME");
106     if (!str)
107       str = g_getenv("IRCNAME");
108     settings_set_str("real_name",
109                      str != NULL ? str : g_get_real_name());
110   }
111  
112   /* username */
113   user_name = settings_get_str("user_name");
114   if (user_name == NULL || *user_name == '\0') {
115     str = g_getenv("SILCUSER");
116     if (!str)
117       str = g_getenv("IRCUSER");
118     settings_set_str("user_name",
119                      str != NULL ? str : g_get_user_name());
120     
121     user_name = settings_get_str("user_name");
122   }
123
124   /* nick */
125   nick = settings_get_str("nick");
126   if (nick == NULL || *nick == '\0') {
127     str = g_getenv("SILCNICK");
128     if (!str)
129       str = g_getenv("IRCNICK");
130     settings_set_str("nick", str != NULL ? str : user_name);
131     
132     nick = settings_get_str("nick");
133   }
134                 
135   /* alternate nick */
136   set = settings_get_str("alternate_nick");
137   if (set == NULL || *set == '\0') {
138     if (strlen(nick) < 9)
139       str = g_strconcat(nick, "_", NULL);
140     else { 
141       str = g_strdup(nick);
142       str[strlen(str)-1] = '_';
143     }
144     settings_set_str("alternate_nick", str);
145     g_free(str);
146   }
147
148   /* host name */
149   set = settings_get_str("hostname");
150   if (set == NULL || *set == '\0') {
151     str = g_getenv("SILCHOST");
152     if (!str)
153       str = g_getenv("IRCHOST");
154     if (str != NULL)
155       settings_set_str("hostname", str);
156   }
157 }
158
159 /* Log callbacks */
160
161 static bool silc_log_misc(SilcLogType type, char *message, void *context)
162 {
163   fprintf(stderr, "%s\n", message);
164   return TRUE;
165 }
166
167 static void silc_nickname_format_parse(const char *nickname,
168                                        char **ret_nickname)
169 {
170   silc_parse_userfqdn(nickname, ret_nickname, NULL);
171 }
172
173 /* Finalize init. Init finish signal calls this. */
174
175 void silc_core_init_finish(SERVER_REC *server)
176 {
177   CHAT_PROTOCOL_REC *rec;
178   SilcClientParams params;
179
180   if (opt_create_keypair == TRUE) {
181     /* Create new key pair and exit */
182     silc_cipher_register_default();
183     silc_pkcs_register_default();
184     silc_hash_register_default();
185     silc_hmac_register_default();
186     silc_client_create_key_pair(opt_pkcs, opt_bits, 
187                                 NULL, NULL, NULL, NULL, NULL);
188     exit(0);
189   }
190
191   if (opt_keyfile) {
192     /* Dump the key */
193     silc_cipher_register_default();
194     silc_pkcs_register_default();
195     silc_hash_register_default();
196     silc_hmac_register_default();
197     silc_client_show_key(opt_keyfile);
198     exit(0);
199   }
200
201   if (opt_list_ciphers) {
202     silc_cipher_register_default();
203     silc_client_list_ciphers();
204     exit(0);
205   }
206
207   if (opt_list_hash) {
208     silc_hash_register_default();
209     silc_client_list_hash_funcs();
210     exit(0);
211   }
212
213   if (opt_list_hmac) {
214     silc_hmac_register_default();
215     silc_client_list_hmacs();
216     exit(0);
217   }
218
219   if (opt_list_pkcs) {
220     silc_pkcs_register_default();
221     silc_client_list_pkcs();
222     exit(0);
223   }
224
225   if (opt_version) {
226     printf("SILC Secure Internet Live Conferencing, version %s "
227            "(base: SILC Toolkit %s)\n", silc_dist_version, silc_version);
228     printf("(c) 1997 - 2001 Pekka Riikonen <priikone@silcnet.org>\n");
229     exit(0); 
230   }
231
232   if (opt_debug) {
233     silc_debug = TRUE;
234     silc_debug_hexdump = TRUE;
235     silc_log_set_debug_string(opt_debug);
236     silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
237     silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
238     silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
239     silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
240 #ifndef SILC_DEBUG
241     fprintf(stdout, 
242             "Run-time debugging is not enabled. To enable it recompile\n"
243             "the client with --enable-debug configuration option.\n");
244 #endif
245   }
246
247   /* Do some irssi initializing */
248   settings_add_bool("server", "skip_motd", FALSE);
249   settings_add_str("server", "alternate_nick", NULL);
250   
251   /* Initialize the auto_addr variables Is "server" the best choice for
252    * this?  No existing category seems to apply.
253    */
254   settings_add_bool("server", "use_auto_addr", FALSE);
255   settings_add_str("server", "auto_bind_ip", "");
256   settings_add_str("server", "auto_public_ip", "");
257   settings_add_int("server", "auto_bind_port", 0);
258                                 
259   silc_init_userinfo();
260
261   /* Initialize client parameters */
262   memset(&params, 0, sizeof(params));
263   strcat(params.nickname_format, "%n@%h%a");
264   params.nickname_parse = silc_nickname_format_parse;
265
266   /* Allocate SILC client */
267   silc_client = silc_client_alloc(&ops, &params, NULL, silc_version_string);
268
269   /* Load local config file */
270   silc_config = silc_client_config_alloc(SILC_CLIENT_HOME_CONFIG_FILE);
271
272   /* Get user information */
273   silc_client->username = g_strdup(settings_get_str("user_name"));
274   silc_client->nickname = g_strdup(settings_get_str("nick"));
275   silc_client->hostname = silc_net_localhost();
276   silc_client->realname = g_strdup(settings_get_str("real_name"));
277
278   /* Register all configured ciphers, PKCS and hash functions. */
279   if (silc_config) {
280     silc_config->client = silc_client;
281     if (!silc_client_config_register_ciphers(silc_config))
282       silc_cipher_register_default();
283     if (!silc_client_config_register_pkcs(silc_config))
284       silc_pkcs_register_default();
285     if (!silc_client_config_register_hashfuncs(silc_config))
286       silc_hash_register_default();
287     if (!silc_client_config_register_hmacs(silc_config))
288       silc_hmac_register_default();
289   } else {
290     /* Register default ciphers, pkcs, hash funtions and hmacs. */
291     silc_cipher_register_default();
292     silc_pkcs_register_default();
293     silc_hash_register_default();
294     silc_hmac_register_default();
295   }
296
297   /* Check ~/.silc directory and public and private keys */
298   if (silc_client_check_silc_dir() == FALSE) {
299     idletag = -1;
300     return;
301   }
302
303   /* Load public and private key */
304   if (silc_client_load_keys(silc_client) == FALSE) {
305     idletag = -1;
306     return;
307   }
308
309   /* Initialize the SILC client */
310   if (!silc_client_init(silc_client)) {
311     idletag = -1;
312     return;
313   }
314
315   /* Register SILC to the irssi */
316   rec = g_new0(CHAT_PROTOCOL_REC, 1);
317   rec->name = "SILC";
318   rec->fullname = "Secure Internet Live Conferencing";
319   rec->chatnet = "silcnet";
320   rec->create_chatnet = create_chatnet;
321   rec->create_server_setup = create_server_setup;
322   rec->create_channel_setup = create_channel_setup;
323   rec->create_server_connect = create_server_connect;
324   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
325     silc_server_connect; 
326   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
327     silc_channel_create;
328   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
329     silc_query_create;
330   
331   chat_protocol_register(rec);
332   g_free(rec);
333
334   silc_server_init();
335   silc_channels_init();
336   silc_queries_init();
337
338   idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
339 }
340
341 /* Init SILC. Called from src/fe-text/silc.c */
342
343 void silc_core_init(void)
344 {
345   static struct poptOption options[] = {
346     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0, 
347       "Create new public key pair", NULL },
348     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0, 
349       "Set the PKCS of the public key pair", "PKCS" },
350     { "bits", 0, POPT_ARG_INT, &opt_bits, 0, 
351       "Set the length of the public key pair", "VALUE" },
352     { "show-key", 'S', POPT_ARG_STRING, &opt_keyfile, 0, 
353       "Show the contents of the public key", "FILE" },
354     { "list-ciphers", 'C', POPT_ARG_NONE, &opt_list_ciphers, 0,
355       "List supported ciphers", NULL },
356     { "list-hash-funcs", 'H', POPT_ARG_NONE, &opt_list_hash, 0,
357       "List supported hash functions", NULL },
358     { "list-hmacs", 'H', POPT_ARG_NONE, &opt_list_hmac, 0,
359       "List supported HMACs", NULL },
360     { "list-pkcs", 'P', POPT_ARG_NONE, &opt_list_pkcs, 0,
361       "List supported PKCSs", NULL },
362     { "debug", 'd', POPT_ARG_STRING, &opt_debug, 0,
363       "Enable debugging", "STRING" },
364     { "version", 'V', POPT_ARG_NONE, &opt_version, 0,
365       "Show version", NULL },
366     { NULL, '\0', 0, NULL }
367   };
368
369   signal_add("irssi init finished", (SIGNAL_FUNC) silc_core_init_finish);
370
371   args_register(options);
372 }
373
374 /* Deinit SILC. Called from src/fe-text/silc.c */
375
376 void silc_core_deinit(void)
377 {
378   if (idletag != -1) {
379     signal_emit("chat protocol deinit", 1,
380                 chat_protocol_find("SILC"));
381     
382     silc_server_deinit();
383     silc_channels_deinit();
384     silc_queries_deinit();
385     
386     chat_protocol_unregister("SILC");
387     
388     g_source_remove(idletag);
389   }
390   
391   g_free(silc_client->username);
392   g_free(silc_client->realname);
393   silc_client_free(silc_client);
394 }