updates.
[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_debug = FALSE;
46 static char *opt_pkcs = NULL;
47 static char *opt_keyfile = NULL;
48 static int opt_bits = 0;
49
50 static int idletag;
51
52 SilcClient silc_client = NULL;
53 SilcClientConfig silc_config = NULL;
54 extern SilcClientOperations ops;
55 extern int silc_debug;
56 #ifdef SILC_SIM
57 /* SIM (SILC Module) table */
58 SilcSimContext **sims = NULL;
59 uint32 sims_count = 0;
60 #endif
61
62 static int my_silc_scheduler(void)
63 {
64   silc_schedule_one(0);
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 /* 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 : g_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 : g_get_user_name());
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     if (strlen(nick) < 9)
133       str = g_strconcat(nick, "_", NULL);
134     else { 
135       str = g_strdup(nick);
136       str[strlen(str)-1] = '_';
137     }
138     settings_set_str("alternate_nick", str);
139     g_free(str);
140   }
141
142   /* host name */
143   set = settings_get_str("hostname");
144   if (set == NULL || *set == '\0') {
145     str = g_getenv("SILCHOST");
146     if (!str)
147       str = g_getenv("IRCHOST");
148     if (str != NULL)
149       settings_set_str("hostname", str);
150   }
151 }
152
153 /* Log callbacks */
154
155 static void silc_log_info(char *message)
156 {
157   fprintf(stderr, "%s\n", message);
158 }
159
160 static void silc_log_warning(char *message)
161 {
162   fprintf(stderr, "%s\n", message);
163 }
164
165 static void silc_log_error(char *message)
166 {
167   fprintf(stderr, "%s\n", message);
168 }
169
170 /* Init SILC. Called from src/fe-text/silc.c */
171
172 void silc_core_init(void)
173 {
174   static struct poptOption options[] = {
175     { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0, 
176       "Create new public key pair", NULL },
177     { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0, 
178       "Set the PKCS of the public key pair", "PKCS" },
179     { "bits", 0, POPT_ARG_INT, &opt_bits, 0, 
180       "Set the length of the public key pair", "VALUE" },
181     { "show-key", 'S', POPT_ARG_STRING, &opt_keyfile, 0, 
182       "Show the contents of the public key", "FILE" },
183     { "debug", 'd', POPT_ARG_NONE, &opt_debug, 0,
184       "Enable debugging", NULL },
185     { NULL, '\0', 0, NULL }
186   };
187
188   args_register(options);
189 }
190
191 /* Finalize init. Called from src/fe-text/silc.c */
192
193 void silc_core_init_finish(void)
194 {
195   CHAT_PROTOCOL_REC *rec;
196
197   if (opt_create_keypair == TRUE) {
198     /* Create new key pair and exit */
199     silc_cipher_register_default();
200     silc_pkcs_register_default();
201     silc_hash_register_default();
202     silc_hmac_register_default();
203     silc_client_create_key_pair(opt_pkcs, opt_bits, 
204                                 NULL, NULL, NULL, NULL, NULL);
205     exit(0);
206   }
207
208   if (opt_keyfile) {
209     /* Dump the key */
210     silc_cipher_register_default();
211     silc_pkcs_register_default();
212     silc_hash_register_default();
213     silc_hmac_register_default();
214     silc_client_show_key(opt_keyfile);
215     exit(0);
216   }
217
218   silc_debug = opt_debug;
219   silc_log_set_callbacks(silc_log_info, silc_log_warning,
220                          silc_log_error, NULL);
221
222   /* Do some irssi initializing */
223   settings_add_bool("server", "skip_motd", FALSE);
224   settings_add_str("server", "alternate_nick", NULL);
225   silc_init_userinfo();
226
227   /* Allocate SILC client */
228   silc_client = silc_client_alloc(&ops, NULL);
229
230   /* Load local config file */
231   silc_config = silc_client_config_alloc(SILC_CLIENT_HOME_CONFIG_FILE);
232
233   /* Get user information */
234   silc_client->username = g_strdup(settings_get_str("user_name"));
235   silc_client->hostname = silc_net_localhost();
236   silc_client->realname = g_strdup(settings_get_str("real_name"));
237
238   /* Register all configured ciphers, PKCS and hash functions. */
239   if (silc_config) {
240     silc_config->client = silc_client;
241     if (!silc_client_config_register_ciphers(silc_config))
242       silc_cipher_register_default();
243     if (!silc_client_config_register_pkcs(silc_config))
244       silc_pkcs_register_default();
245     if (!silc_client_config_register_hashfuncs(silc_config))
246       silc_hash_register_default();
247     if (!silc_client_config_register_hmacs(silc_config))
248       silc_hmac_register_default();
249   } else {
250     /* Register default ciphers, pkcs, hash funtions and hmacs. */
251     silc_cipher_register_default();
252     silc_pkcs_register_default();
253     silc_hash_register_default();
254     silc_hmac_register_default();
255   }
256
257   /* Check ~/.silc directory and public and private keys */
258   if (silc_client_check_silc_dir() == FALSE) {
259     idletag = -1;
260     return;
261   }
262
263   /* Load public and private key */
264   if (silc_client_load_keys(silc_client) == FALSE) {
265     idletag = -1;
266     return;
267   }
268
269   /* Initialize the SILC client */
270   if (!silc_client_init(silc_client)) {
271     idletag = -1;
272     return;
273   }
274
275   /* Register SILC to the irssi */
276   rec = g_new0(CHAT_PROTOCOL_REC, 1);
277   rec->name = "SILC";
278   rec->fullname = "Secure Internet Live Conferencing";
279   rec->chatnet = "silcnet";
280   rec->create_chatnet = create_chatnet;
281   rec->create_server_setup = create_server_setup;
282   rec->create_channel_setup = create_channel_setup;
283   rec->create_server_connect = create_server_connect;
284   rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
285     silc_server_connect; 
286   rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
287     silc_channel_create;
288   rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
289     silc_query_create;
290   
291   chat_protocol_register(rec);
292   g_free(rec);
293
294   silc_server_init();
295   silc_channels_init();
296   silc_queries_init();
297
298   idletag = g_timeout_add(50, (GSourceFunc) my_silc_scheduler, NULL);
299 }
300
301 /* Deinit SILC. Called from src/fe-text/silc.c */
302
303 void silc_core_deinit(void)
304 {
305   if (idletag != -1) {
306     signal_emit("chat protocol deinit", 1,
307                 chat_protocol_find("SILC"));
308     
309     silc_server_deinit();
310     silc_channels_deinit();
311     silc_queries_deinit();
312     
313     chat_protocol_unregister("SILC");
314     
315     g_source_remove(idletag);
316   }
317   
318   g_free(silc_client->username);
319   g_free(silc_client->realname);
320   silc_client_free(silc_client);
321 }