Integer type name change.
[silc.git] / apps / silcer / src / silcerapp.cc
1 /*
2
3   silcerapp.cc 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
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; version 2 of the License.
12
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.
17
18 */
19
20 #include "silcerapp.hh"
21 #include "gtkspell.h"
22
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>
29
30 #include "silcversion.h"
31
32 // Pointer to the application
33 SilcerApp *Silcer_App;
34 string package = "silcer";
35 string version = "1.0";
36
37 SilcClient silc_client;
38 SilcClientConnection silc_client_conn;
39
40 static int 
41 silc_create_key_pair(char *pkcs_name, int bits, char *path,
42                      char *identifier, 
43                      SilcPublicKey *ret_pub_key,
44                      SilcPrivateKey *ret_prv_key)
45 {
46   SilcPKCS pkcs;
47   SilcPublicKey pub_key;
48   SilcPrivateKey prv_key;
49   SilcRng rng;
50   unsigned char *key;
51   SilcUInt32 key_len;
52   char pkfile[256], prvfile[256];
53
54   if (!pkcs_name || !path)
55     return FALSE;
56
57   if (!bits)
58     bits = 1024;
59
60   rng = silc_rng_alloc();
61   silc_rng_init(rng);
62   silc_rng_global_init(rng);
63
64   /* Generate keys */
65   silc_pkcs_alloc((const unsigned char *)pkcs_name, &pkcs);
66   pkcs->pkcs->init(pkcs->context, bits, rng);
67
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,
71                                        key, key_len);
72   *ret_pub_key = pub_key;
73
74   memset(key, 0, sizeof(key_len));
75   silc_free(key);
76
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;
81
82   memset(key, 0, sizeof(key_len));
83   silc_free(key);
84
85   silc_rng_free(rng);
86   silc_pkcs_free(pkcs);
87
88   return TRUE;
89 }
90
91 static
92 void silc_op_say(SilcClient client, SilcClientConnection conn, 
93                  SilcClientMessageType type, char *msg, ...)
94 {
95   va_list va;
96   char *str;
97
98   va_start(va, msg);
99   str = g_strdup_vprintf(msg, va);
100   Silcer_App->_MainDialog->print((string)str);
101   g_free(str);
102   va_end(va);
103 }
104
105 static
106 void silc_channel_message(SilcClient client, SilcClientConnection conn, 
107                           SilcClientEntry sender, SilcChannelEntry channel, 
108                           SilcMessageFlags flags, char *msg)
109 {
110   Silcer_App->_MainDialog->print((string)msg, (string)sender->nickname);
111 }
112
113 static
114 void silc_private_message(SilcClient client, SilcClientConnection conn,
115                           SilcClientEntry sender, SilcMessageFlags flags,
116                           char *msg)
117 {
118   Silcer_App->_MainDialog->print((string)msg);
119 }
120
121 static
122 void silc_notify(SilcClient client, SilcClientConnection conn, 
123                  SilcNotifyType type, ...)
124 {
125   va_list va;
126   
127   va_start(va, type);
128   Silcer_App->_MainDialog->print((string)va_arg(va, char *));
129   va_end(va);
130 }
131
132 static
133 void silc_connect(SilcClient client, SilcClientConnection conn, int success)
134 {
135   silc_client_conn = conn;
136 }
137
138 static
139 void silc_disconnect(SilcClient client, SilcClientConnection conn)
140 {
141   silc_client_conn = NULL;
142 }
143
144 static
145 void silc_auth_meth(SilcClient client, 
146                     SilcClientConnection conn,
147                     char *hostname, SilcUInt16 port,
148                     SilcGetAuthMeth completion, void *context)
149 {
150   completion(TRUE, SILC_AUTH_NONE, NULL, 0, context);
151 }
152
153 static
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)
158 {
159   completion(TRUE, context);
160 }
161
162 static
163 void silc_command(SilcClient client, SilcClientConnection conn, 
164                   SilcClientCommandContext cmd_context, int success,
165                   SilcCommand command)
166 {
167
168 }
169
170 static
171 void silc_command_reply(SilcClient client, SilcClientConnection conn,
172                         SilcCommandPayload cmd_payload, int success,
173                         SilcCommand command, SilcCommandStatus status, ...)
174 {
175
176 }
177
178 /* SILC client operations */
179 SilcClientOperations ops = {
180   silc_op_say,
181   silc_channel_message,
182   silc_private_message,
183   silc_notify,
184   silc_command,
185   silc_command_reply,
186   silc_connect,
187   silc_disconnect,
188   silc_auth_meth,
189   silc_verify_public_key,
190   NULL,
191   NULL,
192   NULL,
193   NULL
194 };
195
196 SILC_TASK_CALLBACK(connect_client)
197 {
198   SilcClient client = (SilcClient)context;
199   silc_client_connect_to_server(client, 706, "silc.silcnet.org", NULL);
200 }
201
202 SilcerApp::SilcerApp(int argc, char **argv)
203   : _GnomeApp(package, version, argc, argv),
204   _gclient(Gnome::Client::master_client())
205 {
206   // Save application pointer
207   Silcer_App = this;
208
209   // Initialize SILC stuff
210   silc_debug = TRUE;
211   silc_debug_hexdump = TRUE;
212   silc_log_set_debug_string("*client*,*net*,*ske*");
213
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();
223
224   // XXXXX
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);
232
233   // We are ready to initialize the SILC Client library.
234   silc_client_init(silc_client);
235
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);
240
241   // XXXXX
242   // This is now used to directly connect to silc.silcnet.org router
243   // XXXXX
244   silc_schedule_task_add(silc_client->schedule, 0, connect_client, 
245                          silc_client, 0, 1, SILC_TASK_TIMEOUT, 
246                          SILC_TASK_PRI_NORMAL); 
247
248    // Initialize glade
249   glade_gnome_init();
250
251   // Locate glade files
252   if (!g_file_exists(string(_SourceDir + "SilcerMainDlg.glade").c_str()))
253     _SourceDir = "./";
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");
260     exit(-1);
261   }
262
263   _MainDialog = new SilcerMainDlg();
264 }
265
266 SilcerApp::~SilcerApp()
267 {
268   delete _MainDialog;
269 }
270
271 void SilcerApp::run()
272 {
273   // Let the gnome app start processing messages
274   Gnome::Main::run();
275 }
276
277 void SilcerApp::quit()
278 {
279   // Stop gtk/gnome message loop
280   Gnome::Main::quit();
281   delete Silcer_App;
282 }
283
284 GladeXML *SilcerApp::load_resource(const char *name)
285 {
286   return glade_xml_new(string(_SourceDir + name + ".glade").c_str(), name);
287 }
288
289 GladeXML *SilcerApp::load_resource(const char *name, const char *filename)
290 {
291   return glade_xml_new(string(_SourceDir + filename + ".glade").c_str(), name);
292 }
293
294 gint SilcerApp::silc_scheduler()
295 {
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);
301   return 1;
302 }