5 Author: Pekka Riikonen <priikone@silcnet.org>
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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 #include "silcerapp.hh"
23 #include <sys/utsname.h>
24 #include <glade/glade.h>
25 #include <libgnome/gnome-triggers.h>
26 #include <libgnome/gnome-util.h>
27 #include <libgnomeui/gnome-window-icon.h>
28 #include <gnome--/client.h>
30 #include "silcversion.h"
32 // Pointer to the application
33 SilcerApp *Silcer_App;
34 string package = "silcer";
35 string version = "1.0";
37 SilcClient silc_client;
38 SilcClientConnection silc_client_conn;
41 silc_create_key_pair(char *pkcs_name, int bits, char *path,
43 SilcPublicKey *ret_pub_key,
44 SilcPrivateKey *ret_prv_key)
47 SilcPublicKey pub_key;
48 SilcPrivateKey prv_key;
52 char pkfile[256], prvfile[256];
54 if (!pkcs_name || !path)
60 rng = silc_rng_alloc();
62 silc_rng_global_init(rng);
65 silc_pkcs_alloc((const unsigned char *)pkcs_name, &pkcs);
66 pkcs->pkcs->init(pkcs->context, bits, rng);
68 /* Save public key into file */
69 key = silc_pkcs_get_public_key(pkcs, &key_len);
70 pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
72 *ret_pub_key = pub_key;
74 memset(key, 0, sizeof(key_len));
77 /* Save private key into file */
78 key = silc_pkcs_get_private_key(pkcs, &key_len);
79 prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
80 *ret_prv_key = prv_key;
82 memset(key, 0, sizeof(key_len));
92 void silc_op_say(SilcClient client, SilcClientConnection conn,
93 SilcClientMessageType type, char *msg, ...)
99 str = g_strdup_vprintf(msg, va);
100 Silcer_App->_MainDialog->print((string)str);
106 void silc_channel_message(SilcClient client, SilcClientConnection conn,
107 SilcClientEntry sender, SilcChannelEntry channel,
108 SilcMessageFlags flags, char *msg)
110 Silcer_App->_MainDialog->print((string)msg, (string)sender->nickname);
114 void silc_private_message(SilcClient client, SilcClientConnection conn,
115 SilcClientEntry sender, SilcMessageFlags flags,
118 Silcer_App->_MainDialog->print((string)msg);
122 void silc_notify(SilcClient client, SilcClientConnection conn,
123 SilcNotifyType type, ...)
128 Silcer_App->_MainDialog->print((string)va_arg(va, char *));
133 void silc_connect(SilcClient client, SilcClientConnection conn, int success)
135 silc_client_conn = conn;
139 void silc_disconnect(SilcClient client, SilcClientConnection conn)
141 silc_client_conn = NULL;
145 void silc_auth_meth(SilcClient client,
146 SilcClientConnection conn,
147 char *hostname, SilcUInt16 port,
148 SilcGetAuthMeth completion, void *context)
150 completion(TRUE, SILC_AUTH_NONE, NULL, 0, context);
154 void silc_verify_public_key(SilcClient client, SilcClientConnection conn,
155 SilcSocketType conn_type, unsigned char *pk,
156 SilcUInt32 pk_len, SilcSKEPKType pk_type,
157 SilcVerifyPublicKey completion, void *context)
159 completion(TRUE, context);
163 void silc_command(SilcClient client, SilcClientConnection conn,
164 SilcClientCommandContext cmd_context, int success,
171 void silc_command_reply(SilcClient client, SilcClientConnection conn,
172 SilcCommandPayload cmd_payload, int success,
173 SilcCommand command, SilcCommandStatus status, ...)
178 /* SILC client operations */
179 SilcClientOperations ops = {
181 silc_channel_message,
182 silc_private_message,
189 silc_verify_public_key,
196 SILC_TASK_CALLBACK(connect_client)
198 SilcClient client = (SilcClient)context;
199 silc_client_connect_to_server(client, 706, "silc.silcnet.org", NULL);
202 SilcerApp::SilcerApp(int argc, char **argv)
203 : _GnomeApp(package, version, argc, argv),
204 _gclient(Gnome::Client::master_client())
206 // Save application pointer
209 // Initialize SILC stuff
211 silc_debug_hexdump = TRUE;
212 silc_log_set_debug_string("*client*,*net*,*ske*");
214 // Initialize SILC Client Library */
215 silc_client = silc_client_alloc(&ops, NULL, NULL, "SILC-1.0-0.6.2");
216 silc_client->realname = "Foo T. Bar";
217 silc_client->username = "foobar";
218 silc_client->hostname = "foo.bar.foobar.com";
219 silc_cipher_register_default();
220 silc_pkcs_register_default();
221 silc_hash_register_default();
222 silc_hmac_register_default();
225 // In real application at this point it would be of course checked
226 // whether ~/.silc direectory or something exists and key pair exists.
227 // If not then some firstsetup-wizard would be lauched that creates
228 // the keypair. In our example we'll always create a key pair. :(
229 silc_create_key_pair("rsa", 1024, "kk", "UN=foobar, "
230 "HN=foo.bar.foobar.com",
231 &silc_client->public_key, &silc_client->private_key);
233 // We are ready to initialize the SILC Client library.
234 silc_client_init(silc_client);
236 // Setup SILC scheduler as timeout task. This will handle the SILC
237 // client library every 50 milliseconds. It will actually make the
238 // SILC client work on background.
239 Gnome::Main::timeout.connect(slot(this, &SilcerApp::silc_scheduler), 50);
242 // This is now used to directly connect to silc.silcnet.org router
244 silc_schedule_task_add(silc_client->schedule, 0, connect_client,
245 silc_client, 0, 1, SILC_TASK_TIMEOUT,
246 SILC_TASK_PRI_NORMAL);
251 // Locate glade files
252 if (!g_file_exists(string(_SourceDir + "SilcerMainDlg.glade").c_str()))
254 if (!g_file_exists(string(_SourceDir + "SilcerMainDlg.glade").c_str()))
255 _SourceDir = "./ui/";
256 if (!g_file_exists(string(_SourceDir + "SilcerMainDlg.glade").c_str()))
257 _SourceDir = "./src/";
258 if (!g_file_exists(string(_SourceDir + "SilcerMainDlg.glade").c_str())) {
259 g_error("Could not find SilcerMainDlg.glade");
263 _MainDialog = new SilcerMainDlg();
266 SilcerApp::~SilcerApp()
271 void SilcerApp::run()
273 // Let the gnome app start processing messages
277 void SilcerApp::quit()
279 // Stop gtk/gnome message loop
284 GladeXML *SilcerApp::load_resource(const char *name)
286 return glade_xml_new(string(_SourceDir + name + ".glade").c_str(), name);
289 GladeXML *SilcerApp::load_resource(const char *name, const char *filename)
291 return glade_xml_new(string(_SourceDir + filename + ".glade").c_str(), name);
294 gint SilcerApp::silc_scheduler()
296 // Run the SILC client once, and return immediately. This function
297 // is called every 50 milliseconds by the Gnome main loop, to process
298 // SILC stuff. This function will read data, and write data to network,
299 // etc. Makes the client library tick! :)
300 silc_client_run_one(silc_client);