5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2001 Pekka Riikonen
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.
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.
22 #include "chat-protocols.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"
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"
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;
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;
63 /* SIM (SILC Module) table */
64 SilcSimContext **sims = NULL;
65 uint32 sims_count = 0;
68 static int my_silc_scheduler(void)
70 silc_client_run_one(silc_client);
74 static CHATNET_REC *create_chatnet(void)
76 return g_malloc0(sizeof(CHATNET_REC));
79 static SERVER_SETUP_REC *create_server_setup(void)
81 return g_malloc0(sizeof(SERVER_SETUP_REC));
84 static CHANNEL_SETUP_REC *create_channel_setup(void)
86 return g_malloc0(sizeof(CHANNEL_SETUP_REC));
89 static SERVER_CONNECT_REC *create_server_connect(void)
91 return g_malloc0(sizeof(SILC_SERVER_CONNECT_REC));
94 /* Checks user information and saves them to the config file it they
95 do not exist there already. */
97 static void silc_init_userinfo(void)
99 const char *set, *nick, *user_name;
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");
107 str = g_getenv("IRCNAME");
108 settings_set_str("real_name",
109 str != NULL ? str : g_get_real_name());
113 user_name = settings_get_str("user_name");
114 if (user_name == NULL || *user_name == '\0') {
115 str = g_getenv("SILCUSER");
117 str = g_getenv("IRCUSER");
118 settings_set_str("user_name",
119 str != NULL ? str : g_get_user_name());
121 user_name = settings_get_str("user_name");
125 nick = settings_get_str("nick");
126 if (nick == NULL || *nick == '\0') {
127 str = g_getenv("SILCNICK");
129 str = g_getenv("IRCNICK");
130 settings_set_str("nick", str != NULL ? str : user_name);
132 nick = settings_get_str("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);
141 str = g_strdup(nick);
142 str[strlen(str)-1] = '_';
144 settings_set_str("alternate_nick", str);
149 set = settings_get_str("hostname");
150 if (set == NULL || *set == '\0') {
151 str = g_getenv("SILCHOST");
153 str = g_getenv("IRCHOST");
155 settings_set_str("hostname", str);
161 static bool silc_log_misc(SilcLogType type, char *message, void *context)
163 fprintf(stderr, "%s\n", message);
167 /* Init SILC. Called from src/fe-text/silc.c */
169 void silc_core_init(void)
171 static struct poptOption options[] = {
172 { "create-key-pair", 'C', POPT_ARG_NONE, &opt_create_keypair, 0,
173 "Create new public key pair", NULL },
174 { "pkcs", 0, POPT_ARG_STRING, &opt_pkcs, 0,
175 "Set the PKCS of the public key pair", "PKCS" },
176 { "bits", 0, POPT_ARG_INT, &opt_bits, 0,
177 "Set the length of the public key pair", "VALUE" },
178 { "show-key", 'S', POPT_ARG_STRING, &opt_keyfile, 0,
179 "Show the contents of the public key", "FILE" },
180 { "list-ciphers", 'C', POPT_ARG_NONE, &opt_list_ciphers, 0,
181 "List supported ciphers", NULL },
182 { "list-hash-funcs", 'H', POPT_ARG_NONE, &opt_list_hash, 0,
183 "List supported hash functions", NULL },
184 { "list-hmacs", 'H', POPT_ARG_NONE, &opt_list_hmac, 0,
185 "List supported HMACs", NULL },
186 { "list-pkcs", 'P', POPT_ARG_NONE, &opt_list_pkcs, 0,
187 "List supported PKCSs", NULL },
188 { "debug", 'd', POPT_ARG_STRING, &opt_debug, 0,
189 "Enable debugging", "STRING" },
190 { "version", 'V', POPT_ARG_NONE, &opt_version, 0,
191 "Show version", NULL },
192 { NULL, '\0', 0, NULL }
195 args_register(options);
198 static void silc_nickname_format_parse(const char *nickname,
201 silc_parse_userfqdn(nickname, ret_nickname, NULL);
204 /* Finalize init. Called from src/fe-text/silc.c */
206 void silc_core_init_finish(void)
208 CHAT_PROTOCOL_REC *rec;
209 SilcClientParams params;
211 if (opt_create_keypair == TRUE) {
212 /* Create new key pair and exit */
213 silc_cipher_register_default();
214 silc_pkcs_register_default();
215 silc_hash_register_default();
216 silc_hmac_register_default();
217 silc_client_create_key_pair(opt_pkcs, opt_bits,
218 NULL, NULL, NULL, NULL, NULL);
224 silc_cipher_register_default();
225 silc_pkcs_register_default();
226 silc_hash_register_default();
227 silc_hmac_register_default();
228 silc_client_show_key(opt_keyfile);
232 if (opt_list_ciphers) {
233 silc_cipher_register_default();
234 silc_client_list_ciphers();
239 silc_hash_register_default();
240 silc_client_list_hash_funcs();
245 silc_hmac_register_default();
246 silc_client_list_hmacs();
251 silc_pkcs_register_default();
252 silc_client_list_pkcs();
257 printf("SILC Secure Internet Live Conferencing, version %s "
258 "(base: SILC Toolkit %s)\n", silc_dist_version, silc_version);
259 printf("(c) 1997 - 2001 Pekka Riikonen <priikone@silcnet.org>\n");
265 silc_debug_hexdump = TRUE;
266 silc_log_set_debug_string(opt_debug);
267 silc_log_set_callback(SILC_LOG_INFO, silc_log_misc, NULL);
268 silc_log_set_callback(SILC_LOG_WARNING, silc_log_misc, NULL);
269 silc_log_set_callback(SILC_LOG_ERROR, silc_log_misc, NULL);
270 silc_log_set_callback(SILC_LOG_FATAL, silc_log_misc, NULL);
273 "Run-time debugging is not enabled. To enable it recompile\n"
274 "the client with --enable-debug configuration option.\n");
278 /* Do some irssi initializing */
279 settings_add_bool("server", "skip_motd", FALSE);
280 settings_add_str("server", "alternate_nick", NULL);
282 /* Initialize the auto_addr variables Is "server" the best choice for
283 * this? No existing category seems to apply.
285 settings_add_bool("server", "use_auto_addr", FALSE);
286 settings_add_str("server", "auto_bind_ip", "");
287 settings_add_str("server", "auto_public_ip", "");
288 settings_add_int("server", "auto_bind_port", 0);
290 silc_init_userinfo();
292 /* Initialize client parameters */
293 memset(¶ms, 0, sizeof(params));
294 strcat(params.nickname_format, "%n@%h%a");
295 params.nickname_parse = silc_nickname_format_parse;
297 /* Allocate SILC client */
298 silc_client = silc_client_alloc(&ops, ¶ms, NULL, silc_version_string);
300 /* Load local config file */
301 silc_config = silc_client_config_alloc(SILC_CLIENT_HOME_CONFIG_FILE);
303 /* Get user information */
304 silc_client->username = g_strdup(settings_get_str("user_name"));
305 silc_client->nickname = g_strdup(settings_get_str("nick"));
306 silc_client->hostname = silc_net_localhost();
307 silc_client->realname = g_strdup(settings_get_str("real_name"));
309 /* Register all configured ciphers, PKCS and hash functions. */
311 silc_config->client = silc_client;
312 if (!silc_client_config_register_ciphers(silc_config))
313 silc_cipher_register_default();
314 if (!silc_client_config_register_pkcs(silc_config))
315 silc_pkcs_register_default();
316 if (!silc_client_config_register_hashfuncs(silc_config))
317 silc_hash_register_default();
318 if (!silc_client_config_register_hmacs(silc_config))
319 silc_hmac_register_default();
321 /* Register default ciphers, pkcs, hash funtions and hmacs. */
322 silc_cipher_register_default();
323 silc_pkcs_register_default();
324 silc_hash_register_default();
325 silc_hmac_register_default();
328 /* Check ~/.silc directory and public and private keys */
329 if (silc_client_check_silc_dir() == FALSE) {
334 /* Load public and private key */
335 if (silc_client_load_keys(silc_client) == FALSE) {
340 /* Initialize the SILC client */
341 if (!silc_client_init(silc_client)) {
346 /* Register SILC to the irssi */
347 rec = g_new0(CHAT_PROTOCOL_REC, 1);
349 rec->fullname = "Secure Internet Live Conferencing";
350 rec->chatnet = "silcnet";
351 rec->create_chatnet = create_chatnet;
352 rec->create_server_setup = create_server_setup;
353 rec->create_channel_setup = create_channel_setup;
354 rec->create_server_connect = create_server_connect;
355 rec->server_connect = (SERVER_REC *(*) (SERVER_CONNECT_REC *))
357 rec->channel_create = (CHANNEL_REC *(*) (SERVER_REC *, const char *, int))
359 rec->query_create = (QUERY_REC *(*) (const char *, const char *, int))
362 chat_protocol_register(rec);
366 silc_channels_init();
369 idletag = g_timeout_add(5, (GSourceFunc) my_silc_scheduler, NULL);
372 /* Deinit SILC. Called from src/fe-text/silc.c */
374 void silc_core_deinit(void)
377 signal_emit("chat protocol deinit", 1,
378 chat_protocol_find("SILC"));
380 silc_server_deinit();
381 silc_channels_deinit();
382 silc_queries_deinit();
384 chat_protocol_unregister("SILC");
386 g_source_remove(idletag);
389 g_free(silc_client->username);
390 g_free(silc_client->realname);
391 silc_client_free(silc_client);