135
136
silc_client_run_one(client);
146
/* A timeout occurred. Call SILC scheduler. */
149
silcpurple_scheduler_timeout(gpointer context)
151
SilcPurpleTask task = (SilcPurpleTask)context;
152
silc_client_run_one(task->sg->client);
153
silc_dlist_del(task->sg->tasks, task);
158
/* An fd task event occurred. Call SILC scheduler. */
161
silcpurple_scheduler_fd(gpointer data, gint fd, PurpleInputCondition cond)
163
SilcClient client = (SilcClient)data;
164
silc_client_run_one(client);
167
/* SILC Scheduler notify callback. This is called whenever task is added to
168
or deleted from SILC scheduler. It's also called when fd task events
169
change. Here we add same tasks to glib's main loop. */
172
silcpurple_scheduler(SilcSchedule schedule,
173
SilcBool added, SilcTask task,
174
SilcBool fd_task, SilcUInt32 fd,
176
long seconds, long useconds,
179
SilcClient client = (SilcClient)context;
180
PurpleConnection *gc = client->application;
181
SilcPurple sg = gc->proto_data;
182
SilcPurpleTask ptask = NULL;
186
/* Add fd or change fd events */
187
PurpleInputCondition e = 0;
189
silc_dlist_start(sg->tasks);
190
while ((ptask = silc_dlist_get(sg->tasks)))
191
if (ptask->fd == fd) {
192
purple_input_remove(ptask->tag);
196
if (event & SILC_TASK_READ)
197
e |= PURPLE_INPUT_READ;
198
if (event & SILC_TASK_WRITE)
199
e |= PURPLE_INPUT_WRITE;
203
ptask = silc_calloc(1, sizeof(*ptask));
205
silc_dlist_add(sg->tasks, ptask);
207
ptask->tag = purple_input_add(fd, e, silcpurple_scheduler_fd,
210
silc_dlist_del(sg->tasks, ptask);
215
ptask = silc_calloc(1, sizeof(*ptask));
217
ptask->tag = purple_timeout_add((seconds * 1000) +
219
silcpurple_scheduler_timeout,
221
silc_dlist_add(sg->tasks, ptask);
226
silc_dlist_start(sg->tasks);
227
while ((ptask = silc_dlist_get(sg->tasks)))
228
if (ptask->fd == fd) {
229
purple_input_remove(ptask->tag);
230
silc_dlist_del(sg->tasks, ptask);
237
#endif /* __SILC_TOOLKIT_VERSION */
140
240
silcpurple_connect_cb(SilcClient client, SilcClientConnection conn,
215
315
/* Close the connection */
216
316
if (!sg->detaching)
217
purple_connection_error(gc, _("Disconnected by server"));
317
purple_connection_error_reason(gc,
318
PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
319
_("Disconnected by server"));
219
321
/* TODO: Does this work correctly? Maybe we need to set wants_to_die? */
220
322
purple_account_disconnect(purple_connection_get_account(gc));
223
325
case SILC_CLIENT_CONN_ERROR:
224
purple_connection_error(gc, _("Error during connecting to SILC Server"));
326
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
327
_("Error during connecting to SILC Server"));
225
328
g_unlink(silcpurple_session_file(purple_account_get_username(sg->account)));
228
331
case SILC_CLIENT_CONN_ERROR_KE:
229
purple_connection_error(gc, _("Key Exchange failed"));
332
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR,
333
_("Key Exchange failed"));
232
336
case SILC_CLIENT_CONN_ERROR_AUTH:
233
purple_connection_error(gc, _("Authentication failed"));
337
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
338
_("Authentication failed"));
236
341
case SILC_CLIENT_CONN_ERROR_RESUME:
237
purple_connection_error(gc,
238
_("Resuming detached session failed. "
239
"Press Reconnect to create new connection."));
342
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
343
_("Resuming detached session failed. "
344
"Press Reconnect to create new connection."));
240
345
g_unlink(silcpurple_session_file(purple_account_get_username(sg->account)));
243
348
case SILC_CLIENT_CONN_ERROR_TIMEOUT:
244
purple_connection_error(gc, _("Connection Timeout"));
349
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
350
_("Connection Timeout"));
262
368
sg = gc->proto_data;
264
370
if (status != SILC_SOCKET_OK) {
265
purple_connection_error(gc, _("Connection failed"));
371
purple_connection_error_reason(gc,
372
PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
373
_("Connection failed"));
266
374
silc_pkcs_public_key_free(sg->public_key);
267
375
silc_pkcs_private_key_free(sg->private_key);
325
436
static void silcpurple_running(SilcClient client, void *context)
327
PurpleAccount *account = context;
328
PurpleConnection *gc = account->gc;
438
SilcPurple sg = context;
439
PurpleConnection *gc = sg->gc;
440
PurpleAccount *account = purple_connection_get_account(gc);
330
441
char pkd[256], prd[256];
332
sg = silc_calloc(1, sizeof(*sg));
335
memset(sg, 0, sizeof(*sg));
338
sg->account = account;
339
sg->scheduler = SILC_PTR_TO_32(gc->proto_data);
343
445
purple_connection_update_progress(gc, _("Connecting to SILC Server"), 1, 5);
349
451
(char *)purple_account_get_string(account, "private-key", prd),
350
452
(gc->password == NULL) ? "" : gc->password,
351
453
&sg->public_key, &sg->private_key)) {
352
g_snprintf(pkd, sizeof(pkd), _("Could not load SILC key pair"));
353
purple_connection_error(gc, pkd);
454
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
455
_("Could not load SILC key pair"));
354
456
gc->proto_data = NULL;
363
465
purple_account_get_int(account, "port", 706),
364
466
silcpurple_login_connected, gc) == NULL)
366
purple_connection_error(gc, _("Unable to create connection"));
468
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
469
_("Unable to create connection"));
367
470
gc->proto_data = NULL;
538
sg = silc_calloc(1, sizeof(*sg));
543
sg->account = account;
434
546
/* Init SILC client */
435
547
if (!silc_client_init(client, username, hostname, realname,
436
silcpurple_running, account)) {
437
gc->wants_to_die = TRUE;
438
purple_connection_error(gc, _("Cannot initialize SILC protocol"));
548
silcpurple_running, sg)) {
549
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
550
_("Cannot initialize SILC protocol"));
551
gc->proto_data = NULL;
442
556
/* Check the ~/.silc dir and create it, and new key pair if necessary. */
443
557
if (!silcpurple_check_silc_dir(gc)) {
444
gc->wants_to_die = TRUE;
445
purple_connection_error(gc, _("Error loading SILC key pair"));
558
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
559
_("Error loading SILC key pair"));
560
gc->proto_data = NULL;
565
#if __SILC_TOOLKIT_VERSION < SILC_VERSION(1,1,1)
449
566
/* Schedule SILC using Glib's event loop */
450
scheduler = purple_timeout_add(300, (GSourceFunc)silcpurple_scheduler, client);
451
gc->proto_data = SILC_32_TO_PTR(scheduler);
567
sg->scheduler = purple_timeout_add(300, (GSourceFunc)silcpurple_scheduler, client);
569
/* Run SILC scheduler */
570
sg->tasks = silc_dlist_init();
571
silc_schedule_set_notify(client->schedule, silcpurple_scheduler,
573
silc_client_run_one(client);
574
#endif /* __SILC_TOOLKIT_VERSION */
455
578
silcpurple_close_final(gpointer *context)
457
580
SilcPurple sg = (SilcPurple)context;
458
582
silc_client_stop(sg->client, NULL, NULL);
459
583
silc_client_free(sg->client);
585
silc_hash_free(sg->sha1hash);
461
587
silc_mime_assembler_free(sg->mimeass);
467
593
silcpurple_close(PurpleConnection *gc)
469
595
SilcPurple sg = gc->proto_data;
596
#if __SILC_TOOLKIT_VERSION >= SILC_VERSION(1,1,1)
598
#endif /* __SILC_TOOLKIT_VERSION */
471
600
g_return_if_fail(sg != NULL);
474
603
silc_client_command_call(sg->client, sg->conn, NULL,
475
"QUIT", "Download Pidgin: " PURPLE_WEBSITE, NULL);
604
"QUIT", "Download Pidgin: " PURPLE_WEBSITE,
478
608
silc_client_close_connection(sg->client, sg->conn);
610
#if __SILC_TOOLKIT_VERSION >= SILC_VERSION(1,1,1)
612
silc_client_run_one(sg->client);
613
silc_schedule_set_notify(sg->client->schedule, NULL, NULL);
615
silc_dlist_start(sg->tasks);
616
while ((task = silc_dlist_get(sg->tasks))) {
617
purple_input_remove(task->tag);
620
silc_dlist_uninit(sg->tasks);
621
#endif /* __SILC_TOOLKIT_VERSION */
480
623
purple_timeout_remove(sg->scheduler);
481
624
purple_timeout_add(1, (GSourceFunc)silcpurple_close_final, sg);
1155
1298
silc_dlist_start(list);
1156
1299
while ((buf = silc_dlist_get(list)) != SILC_LIST_END)
1157
1300
silc_client_send_private_message(client, conn,
1158
client_entry, im->flags, NULL,
1301
client_entry, im->flags, sg->sha1hash,
1160
1303
silc_buffer_len(buf));
1161
1304
silc_mime_partial_free(list);
1168
1311
/* Send the message */
1169
1312
silc_client_send_private_message(client, conn, client_entry, im->flags,
1170
NULL, (unsigned char *)im->message, im->message_len);
1313
sg->sha1hash, (unsigned char *)im->message, im->message_len);
1171
1314
purple_conv_im_write(PURPLE_CONV_IM(convo), conn->local_entry->nickname,
1172
1315
im->message, 0, time(NULL));
1259
1402
while ((buf = silc_dlist_get(list)) != SILC_LIST_END)
1261
1404
silc_client_send_private_message(client, conn,
1262
client_entry, mflags, NULL,
1405
client_entry, mflags, sg->sha1hash,
1264
1407
silc_buffer_len(buf));
1265
1408
silc_mime_partial_free(list);