-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotr-plugin.c
2075 lines (1754 loc) · 61.8 KB
/
otr-plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
f * Off-the-Record Messaging plugin for pidgin
* Copyright (C) 2004-2014 Ian Goldberg, Rob Smits,
* Chris Alexander, Willy Lew,
* Lisa Du, Nikita Borisov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* config.h */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* system headers */
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/* libgcrypt headers */
#include <gcrypt.h>
/* purple headers */
#include "pidgin.h"
#include "notify.h"
#include "version.h"
#include "util.h"
#include "debug.h"
#include "core.h"
#ifdef USING_GTK
/* purple GTK headers */
#include "gtkplugin.h"
#endif
#ifdef ENABLE_NLS
#ifdef WIN32
/* On Win32, include win32dep.h from pidgin for correct definition
* of LOCALEDIR */
#include "win32dep.h"
#endif /* WIN32 */
/* internationalisation header */
#include <glib/gi18n-lib.h>
#else /* ENABLE_NLS */
#define _(x) (x)
#define N_(x) (x)
#endif /* ENABLE_NLS */
/* libotr headers */
#include <libotr/privkey.h>
#include <libotr/proto.h>
#include <libotr/tlv.h>
#include <libotr/message.h>
#include <libotr/userstate.h>
#include <libotr/instag.h>
/* DIMOKAS */
#include <libotr/chat_info.h>
#include <libotr/chat_protocol.h>
#include <libotr/chat_token.h>
#include <libotr/chat_types.h>
#include <libotr/chat_fingerprint.h>
/* ******* */
/* purple-otr headers */
#include "ui.h"
#include "dialogs.h"
#include "otr-plugin.h"
#ifdef USING_GTK
/* purple-otr GTK headers */
#include <glib.h>
#include "gtk-ui.h"
#include "gtk-dialog.h"
/* Controls a beta warning/expiry dialog */
#define BETA_DIALOG 0
#if BETA_DIALOG && defined USING_GTK /* Only for beta */
#include "gtkblist.h"
#endif
#endif
/* If we're using glib on Windows, we need to use g_fopen to open files.
* On other platforms, it's also safe to use it. If we're not using
* glib, just use fopen. */
#ifdef USING_GTK
/* If we're cross-compiling, this might be wrong, so fix it. */
#ifdef WIN32
#undef G_OS_UNIX
#define G_OS_WIN32
#endif
#include <glib/gstdio.h>
#else
#define g_fopen fopen
#endif
PurplePlugin *otrg_plugin_handle;
/* We'll only use the one OtrlUserState. */
OtrlUserState otrg_plugin_userstate = NULL;
/* GLib HashTable for storing the maximum message size for various
* protocols. */
GHashTable* mms_table = NULL;
/* Send an IM from the given account to the given recipient. Display an
* error dialog if that account isn't currently logged in. */
void otrg_plugin_inject_message(PurpleAccount *account, const char *recipient,
const char *message)
{
PurpleConnection *connection;
connection = purple_account_get_connection(account);
if (!connection) {
const char *protocol = purple_account_get_protocol_id(account);
const char *accountname = purple_account_get_username(account);
PurplePlugin *p = purple_find_prpl(protocol);
char *msg = g_strdup_printf(_("You are not currently connected to "
"account %s (%s)."), accountname,
(p && p->info->name) ? p->info->name : _("Unknown"));
otrg_dialog_notify_error(accountname, protocol, recipient,
_("Not connected"), msg, NULL);
g_free(msg);
return;
}
serv_send_im(connection, recipient, message, 0);
}
/* DIKOMAS */
int chat_token_to_chat_id(otrl_chat_token_t token)
{
return (int) token;
}
PurpleConversation *chat_info_to_purple_conversation(const OtrlChatInfoPtr info)
{
PurpleAccount *account;
PurpleConnection *connection;
PurpleConversation *conv;
char *accountname = NULL, *protocol = NULL;
otrl_chat_token_t chat_token;
int chat_id;
accountname = otrl_chat_info_get_accountname(info);
protocol = otrl_chat_info_get_protocol(info);
chat_token = otrl_chat_info_get_chat_token(info);
account = purple_accounts_find(accountname, protocol);
if(!account) { goto error; }
connection = purple_account_get_connection(account);
if(!connection) { goto error; }
chat_id = chat_token_to_chat_id(chat_token);
conv = purple_find_chat(connection, chat_id);
if(!conv) { goto error; }
return conv;
error:
return NULL;
}
int plugin_chat_inject_message(const OtrlChatInfoPtr info, const char *message)
{
PurpleConnection *connection;
PurpleAccount *account;
char *accountname = NULL, *protocol = NULL;
otrl_chat_token_t chat_token;
int chat_id, err;
accountname = otrl_chat_info_get_accountname(info);
protocol = otrl_chat_info_get_protocol(info);
chat_token = otrl_chat_info_get_chat_token(info);
account = purple_accounts_find(accountname, protocol);
if(!account) { goto error;}
chat_id = chat_token_to_chat_id(chat_token);
connection = purple_account_get_connection(account);
if (!connection) { goto error; }
err = serv_chat_send(connection, chat_id, message, 0);
if(err < 0) { goto error; }
return 0;
error:
return 1;
}
static int plugin_chat_inject_message_cb(void *opdata, const OtrlChatInfoPtr info, const char *message)
{
return plugin_chat_inject_message(info , message);
}
int chat_display_notification(const OtrlChatInfoPtr info, const char *notification)
{
PurpleConversation *conv;
conv = chat_info_to_purple_conversation(info);
if(!conv) { goto error; }
purple_conversation_write(conv, NULL, notification, PURPLE_MESSAGE_SYSTEM, time(NULL));
return 0;
error:
purple_debug_info("otr", "MPOTR: chat_display_notification: error\n");
return 1;
}
void chat_display_notification_cb(void *opdata, const OtrlChatInfoPtr info, const char *notification)
{
chat_display_notification(info, notification);
}
int chat_handle_event_with_notifiaction(const OtrlChatInfoPtr info, const OtrlChatEventPtr event)
{
OtrlChatEventType type;
OtrlChatEventParticipantDataPtr part_data;
OtrlChatEventMessageDataPtr msg_data;
gchar *msg = NULL;
char *username = NULL, *message = NULL;
int err;
type = otrl_chat_event_get_type(event);
switch(type) {
case OTRL_CHAT_EVENT_OFFER_RECEIVED:
part_data = otrl_chat_event_get_data(event);
username = otrl_chat_event_participant_data_get_username(part_data);
msg = g_strdup_printf (_("%s has requested a private session."), username);
if(!msg) { goto error; }
break;
case OTRL_CHAT_EVENT_STARTING:
msg = g_strdup_printf (_("Initializing a new private chat session..."));
if(!msg) { goto error; }
break;
case OTRL_CHAT_EVENT_STARTED:
msg = g_strdup_printf (_("The private session has started successfully."));
if(!msg) { goto error; }
break;
case OTRL_CHAT_EVENT_UNVERIFIED_PARTICIPANT:
part_data = otrl_chat_event_get_data(event);
username = otrl_chat_event_participant_data_get_username(part_data);
msg = g_strdup_printf (_("You have not verified the fingerprint of user: %s."), username);
if(!msg) { goto error; }
break;
case OTRL_CHAT_EVENT_PLAINTEXT_RECEIVED:
msg_data = otrl_chat_event_get_data(event);
username = otrl_chat_event_message_data_get_username(msg_data);
message = otrl_chat_event_message_data_get_message(msg_data);
msg = g_strdup_printf (_("<b>The following message received from %s was <i>not</i> encrypted: [</b>%s<b>]</b>"), username, message);
if(!msg) { goto error; }
break;
case OTRL_CHAT_EVENT_PRIVATE_RECEIVED:
part_data = otrl_chat_event_get_data(event);
username = otrl_chat_event_participant_data_get_username(part_data);
msg = g_strdup_printf (_("%s sent an encrypted message, while not in a private session."), username);
if(!msg) { goto error; }
break;
case OTRL_CHAT_EVENT_CONSENSUS_BROKEN:
part_data = otrl_chat_event_get_data(event);
username = otrl_chat_event_participant_data_get_username(part_data);
msg = g_strdup_printf (_("You do NOT have consensus with %s."), username);
if(!msg) { goto error; }
break;
case OTRL_CHAT_EVENT_FINISHED:
msg = g_strdup_printf (_("The private session was finished."));
if(!msg) { goto error; }
break;
default:
goto error;
}
err = chat_display_notification(info, msg);
if(err) { goto error_with_msg; }
g_free(msg);
return 0;
error_with_msg:
g_free(msg);
error:
return 1;
}
void chat_handle_event(const OtrlChatInfoPtr info, const OtrlChatEventPtr event)
{
OtrlChatEventType type;
type = otrl_chat_event_get_type(event);
switch(type) {
case OTRL_CHAT_EVENT_OFFER_RECEIVED:
case OTRL_CHAT_EVENT_STARTING:
case OTRL_CHAT_EVENT_STARTED:
case OTRL_CHAT_EVENT_UNVERIFIED_PARTICIPANT:
case OTRL_CHAT_EVENT_PLAINTEXT_RECEIVED:
case OTRL_CHAT_EVENT_PRIVATE_RECEIVED:
case OTRL_CHAT_EVENT_CONSENSUS_BROKEN:
case OTRL_CHAT_EVENT_FINISHED:
chat_handle_event_with_notifiaction(info, event);
break;
default:
return;
}
}
void chat_handle_event_cb(void *odata, const OtrlChatInfoPtr info, const OtrlChatEventPtr event)
{
chat_handle_event(info, event);
}
char **chat_get_participants(const OtrlChatInfoPtr info, unsigned int *size)
{
PurpleConversation *conv;
GList *userlist;
guint length;
unsigned int i;
char **usernames;
conv = chat_info_to_purple_conversation(info);
if(!conv) { goto error; }
userlist = purple_conv_chat_get_users(PURPLE_CONV_CHAT(conv));
if(!userlist) { goto error; }
length = g_list_length(userlist);
if(length == 0) { goto error; }
usernames = malloc(length * sizeof *usernames);
if(!usernames) { goto error; }
for(i=0; i<length; usernames[i++] = NULL);
for (i = 0; userlist != NULL; userlist = userlist->next, i++) {
PurpleConvChatBuddy *chatBuddy = userlist->data;
if(!chatBuddy) { goto error_with_usernames; }
char *participantName = chatBuddy->name;
if(!participantName) { goto error_with_usernames; }
usernames[i] = strdup(participantName);
if(!usernames[i]) { goto error_with_usernames; }
}
*size = length;
return usernames;
error_with_usernames:
purple_debug_info("otr", "MPOTR: chat_get_participants: error_with_usernames\n");
for(i=0; i<length; i++) {
free(usernames[i]);
free(usernames);
}
error:
purple_debug_info("otr", "MPOTR: chat_get_participants: error\n");
return NULL;
}
char **chat_get_participants_cb(void *opdata, const const OtrlChatInfoPtr info, unsigned int *size)
{
return chat_get_participants(info, size);
}
// TODO maybe this is of no use since its the same as otrg_plugin_chat_id_key_generate_new
// just check that WIN32 are added in the latter
void chat_privkey_create(const char *accountname, const char *protocol)
{
//OtrgDialogWaitHandle waithandle;
//#ifndef WIN32
// mode_t mask;
//#endif /* WIN32 */
// FILE *privf;
// gchar *privkeyfile = g_build_filename(purple_user_dir(), CHATPRIVKEYFNAME, NULL);
// if (!privkeyfile) {
// fprintf(stderr, _("Out of memory building filenames!\n"));
// return;
// }
//#ifndef WIN32
// mask = umask (0077);
//#endif /* WIN32 */
// privf = g_fopen(privkeyfile, "w+b");
//#ifndef WIN32
// umask (mask);
//#endif /* WIN32 */
// g_free(privkeyfile);
// if (!privf) {
// fprintf(stderr, _("Could not write private key file\n"));
// return;
// }
//waithandle = otrg_dialog_private_key_wait_start(accountname, protocol);
/* Generate the key */
//otrl_privkey_generate_FILEp(otrg_plugin_userstate, privf, accountname, protocol);
//otrl_chat_privkeydh_generate_FILEp(otrg_plugin_userstate, privf, accountname, protocol);
// otrl_chat_protocol_id_key_generate_new(otrg_plugin_userstate, &ui_ops, accountname, protocol);
// fclose(privf);
//otrg_ui_update_fingerprint();
/* Mark the dialog as done. */
//otrg_dialog_private_key_wait_done(waithandle);
otrg_plugin_chat_id_key_generate_new(accountname, protocol);
}
void chat_privkey_create_cb(void *opdata, const char *accountname, const char *protocol)
{
chat_privkey_create(accountname, protocol);
}
void chat_fingerprints_write()
{
gchar *chatfingerfile = g_build_filename(purple_user_dir(), CHATFINGERFNAME, NULL);
FILE *chatfingerf = g_fopen(chatfingerfile, "wb");
g_free(chatfingerfile);
if(chatfingerf) {
otrl_chat_protocol_fingerprints_write_file(otrg_plugin_userstate, chatfingerf);
fclose(chatfingerf);
} else {
purple_debug_info("otr", "MPOTR: otr_plugin_load: error reading trusted fingerprints file\n");
}
}
void chat_fingerprints_write_cb(void *opdata)
{
chat_fingerprints_write();
}
void chat_privkeys_write()
{
purple_debug_info("otr", "MPOTR: chat_privkeys_write: start\n");
gchar *chatprivkeyfile = g_build_filename(purple_user_dir(), CHATPRIVKEYFNAME, NULL);
FILE *chatprivkeyf = g_fopen(chatprivkeyfile, "wb");
g_free(chatprivkeyfile);
if(chatprivkeyf) {
otrl_chat_protocol_id_keys_write_file(otrg_plugin_userstate, chatprivkeyf);
fclose(chatprivkeyf);
} else {
purple_debug_info("otr", "MPOTR: otr_plugin_load: error reading trusted fingerprints file\n");
}
purple_debug_info("otr", "MPOTR: chat_privkeys_write: end\n");
}
void chat_privkeys_write_cb(void *opdata)
{
chat_privkeys_write();
}
void chat_info_refresh(const OtrlChatInfoPtr info)
{
PurpleConversation *conv;
OtrlChatPrivacyLevel privacy_level;
privacy_level = otrl_chat_info_get_privacy_level(info);
conv = chat_info_to_purple_conversation(info);
if(!conv) { goto error; }
otrg_dialog_chat_gui_refresh(conv, privacy_level);
error:
return;
}
void chat_info_refresh_cb(void *opdata, const OtrlChatInfoPtr info)
{
chat_info_refresh(info);
}
/* ******* */
/* Display a notification message for a particular accountname /
* protocol / username conversation. */
static void notify(void *opdata, OtrlNotifyLevel level,
const char *accountname, const char *protocol, const char *username,
const char *title, const char *primary, const char *secondary)
{
PurpleNotifyMsgType purplelevel = PURPLE_NOTIFY_MSG_ERROR;
switch (level) {
case OTRL_NOTIFY_ERROR:
purplelevel = PURPLE_NOTIFY_MSG_ERROR;
break;
case OTRL_NOTIFY_WARNING:
purplelevel = PURPLE_NOTIFY_MSG_WARNING;
break;
case OTRL_NOTIFY_INFO:
purplelevel = PURPLE_NOTIFY_MSG_INFO;
break;
}
otrg_dialog_notify_message(purplelevel, accountname, protocol,
username, title, primary, secondary);
}
/* Display an OTR control message for a particular accountname /
* protocol / username conversation. If force_create is non-zero and
* if the corresponding conversation window is not present, a new
* conversation window will be created and the message will be displayed
* there. If the message cannot be displayed, try notify() instead and
* return 1. Otherwise return 0 if message is successfully displayed. */
static int display_otr_message_or_notify(void *opdata, const char *accountname,
const char *protocol, const char *username, const char *msg,
int force_create, OtrlNotifyLevel level, const char *title,
const char *primary, const char *secondary)
{
if (otrg_dialog_display_otr_message(accountname, protocol,
username, msg, force_create)) {
notify(opdata, level, accountname, protocol, username, title, primary,
secondary);
return 1;
} else return 0;
}
static void log_message(void *opdata, const char *message)
{
purple_debug_info("otr", "%s", message);
}
static OtrlPolicy policy_cb(void *opdata, ConnContext *context)
{
PurpleAccount *account;
OtrlPolicy policy = OTRL_POLICY_DEFAULT;
OtrgUiPrefs prefs;
if (!context) return policy;
account = purple_accounts_find(context->accountname, context->protocol);
if (!account) return policy;
otrg_ui_get_prefs(&prefs, account, context->username);
return prefs.policy;
}
/* Generate a private key for the given accountname/protocol */
void otrg_plugin_create_privkey(const char *accountname,
const char *protocol)
{
//OtrgDialogWaitHandle waithandle;
#ifndef WIN32
mode_t mask;
#endif /* WIN32 */
FILE *privf;
gchar *privkeyfile = g_build_filename(purple_user_dir(),
PRIVKEYFNAME, NULL);
if (!privkeyfile) {
fprintf(stderr, _("Out of memory building filenames!\n"));
return;
}
#ifndef WIN32
mask = umask (0077);
#endif /* WIN32 */
privf = g_fopen(privkeyfile, "w+b");
#ifndef WIN32
umask (mask);
#endif /* WIN32 */
g_free(privkeyfile);
if (!privf) {
fprintf(stderr, _("Could not write private key file\n"));
return;
}
//waithandle = otrg_dialog_private_key_wait_start(accountname, protocol);
/* Generate the key */
otrl_privkey_generate_FILEp(otrg_plugin_userstate, privf,
accountname, protocol);
fclose(privf);
otrg_ui_update_fingerprint();
/* Mark the dialog as done. */
//otrg_dialog_private_key_wait_done(waithandle);
}
static void create_privkey_cb(void *opdata, const char *accountname,
const char *protocol)
{
otrg_plugin_create_privkey(accountname, protocol);
}
/* Generate a instance tag for the given accountname/protocol */
void otrg_plugin_create_instag(const char *accountname,
const char *protocol)
{
FILE *instagf;
gchar *instagfile = g_build_filename(purple_user_dir(), INSTAGFNAME, NULL);
if (!instagfile) {
fprintf(stderr, _("Out of memory building filenames!\n"));
return;
}
instagf = g_fopen(instagfile, "w+b");
g_free(instagfile);
if (!instagf) {
fprintf(stderr, _("Could not write private key file\n"));
return;
}
/* Generate the instag */
otrl_instag_generate_FILEp(otrg_plugin_userstate, instagf,
accountname, protocol);
fclose(instagf);
}
static void create_instag_cb(void *opdata, const char *accountname,
const char *protocol)
{
otrg_plugin_create_instag(accountname, protocol);
}
static int is_logged_in_cb(void *opdata, const char *accountname,
const char *protocol, const char *recipient)
{
PurpleAccount *account;
PurpleBuddy *buddy;
account = purple_accounts_find(accountname, protocol);
if (!account) return -1;
buddy = purple_find_buddy(account, recipient);
if (!buddy) return -1;
return (PURPLE_BUDDY_IS_ONLINE(buddy));
}
static void inject_message_cb(void *opdata, const char *accountname,
const char *protocol, const char *recipient, const char *message)
{
PurpleAccount *account = purple_accounts_find(accountname, protocol);
if (!account) {
PurplePlugin *p = purple_find_prpl(protocol);
char *msg = g_strdup_printf(_("Unknown account %s (%s)."),
accountname,
(p && p->info->name) ? p->info->name : _("Unknown"));
otrg_dialog_notify_error(accountname, protocol, recipient,
_("Unknown account"), msg, NULL);
g_free(msg);
return;
}
otrg_plugin_inject_message(account, recipient, message);
}
static void update_context_list_cb(void *opdata)
{
otrg_ui_update_keylist();
}
static void confirm_fingerprint_cb(void *opdata, OtrlUserState us,
const char *accountname, const char *protocol, const char *username,
unsigned char fingerprint[20])
{
otrg_dialog_unknown_fingerprint(us, accountname, protocol, username,
fingerprint);
}
static void write_fingerprints_cb(void *opdata)
{
otrg_plugin_write_fingerprints();
otrg_ui_update_keylist();
otrg_dialog_resensitize_all();
}
static void gone_secure_cb(void *opdata, ConnContext *context)
{
otrg_dialog_connected(context);
}
static void gone_insecure_cb(void *opdata, ConnContext *context)
{
otrg_dialog_disconnected(context);
}
static void still_secure_cb(void *opdata, ConnContext *context, int is_reply)
{
if (is_reply == 0) {
otrg_dialog_stillconnected(context);
}
}
static int max_message_size_cb(void *opdata, ConnContext *context)
{
void* lookup_result = g_hash_table_lookup(mms_table, context->protocol);
if (!lookup_result)
return 0;
else
return *((int*)lookup_result);
}
static const char* otr_error_message_cb(void *opdata, ConnContext *context,
OtrlErrorCode err_code)
{
char *err_msg = NULL;
switch (err_code)
{
case OTRL_ERRCODE_NONE :
break;
case OTRL_ERRCODE_ENCRYPTION_ERROR :
err_msg = g_strdup(_("Error occurred encrypting message."));
break;
case OTRL_ERRCODE_MSG_NOT_IN_PRIVATE :
if (context) {
err_msg = g_strdup_printf(_("You sent encrypted data to %s, who"
" wasn't expecting it."), context->accountname);
}
break;
case OTRL_ERRCODE_MSG_UNREADABLE :
err_msg =
g_strdup(_("You transmitted an unreadable encrypted message."));
break;
case OTRL_ERRCODE_MSG_MALFORMED :
err_msg = g_strdup(_("You transmitted a malformed data message."));
break;
}
return err_msg;
}
static void otr_error_message_free_cb(void *opdata, const char *err_msg)
{
if (err_msg) g_free((char*)err_msg);
}
static const char *resent_msg_prefix_cb(void *opdata, ConnContext *context)
{
return g_strdup(_("[resent]"));
}
static void resent_msg_prefix_free_cb(void *opdata, const char *prefix)
{
if (prefix) g_free((char*)prefix);
}
static void handle_smp_event_cb(void *opdata, OtrlSMPEvent smp_event,
ConnContext *context, unsigned short progress_percent,
char *question)
{
if (!context) return;
switch (smp_event)
{
case OTRL_SMPEVENT_NONE :
break;
case OTRL_SMPEVENT_ASK_FOR_SECRET :
otrg_dialog_socialist_millionaires(context);
break;
case OTRL_SMPEVENT_ASK_FOR_ANSWER :
otrg_dialog_socialist_millionaires_q(context, question);
break;
case OTRL_SMPEVENT_CHEATED :
otrg_plugin_abort_smp(context);
/* FALLTHROUGH */
case OTRL_SMPEVENT_IN_PROGRESS :
case OTRL_SMPEVENT_SUCCESS :
case OTRL_SMPEVENT_FAILURE :
case OTRL_SMPEVENT_ABORT :
otrg_dialog_update_smp(context,
smp_event, ((gdouble)progress_percent)/100.0);
break;
case OTRL_SMPEVENT_ERROR :
otrg_plugin_abort_smp(context);
break;
}
}
/* Treat this event like other incoming messages. This allows message
* notification events to get properly triggered. */
static void emit_msg_received(ConnContext *context, const char* message) {
PurpleConversation *conv = otrg_plugin_userinfo_to_conv(
context->accountname, context->protocol, context->username, 1);
PurpleMessageFlags flags = PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_SYSTEM
| PURPLE_MESSAGE_NOTIFY;
PurpleAccount * account = purple_conversation_get_account(conv);
purple_signal_emit(purple_conversations_get_handle(), "received-im-msg",
account, context->username, message, conv, flags);
}
static void handle_msg_event_cb(void *opdata, OtrlMessageEvent msg_event,
ConnContext *context, const char* message, gcry_error_t err)
{
PurpleConversation *conv = NULL;
gchar *buf;
OtrlMessageEvent * last_msg_event;
if (!context) return;
conv = otrg_plugin_context_to_conv(context, 1);
last_msg_event = g_hash_table_lookup(conv->data, "otr-last_msg_event");
switch (msg_event)
{
case OTRL_MSGEVENT_NONE:
break;
case OTRL_MSGEVENT_ENCRYPTION_REQUIRED:
buf = g_strdup_printf(_("You attempted to send an "
"unencrypted message to %s"), context->username);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, _("Attempting to"
" start a private conversation..."), 1, OTRL_NOTIFY_WARNING,
_("OTR Policy Violation"), buf,
_("Unencrypted messages to this recipient are "
"not allowed. Attempting to start a private "
"conversation.\n\nYour message will be "
"retransmitted when the private conversation "
"starts."));
g_free(buf);
break;
case OTRL_MSGEVENT_ENCRYPTION_ERROR:
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, _("An error occurred "
"when encrypting your message. The message was not sent."),
1, OTRL_NOTIFY_ERROR, _("Error encrypting message"),
_("An error occurred when encrypting your message"),
_("The message was not sent."));
break;
case OTRL_MSGEVENT_CONNECTION_ENDED:
buf = g_strdup_printf(_("%s has already closed his/her private "
"connection to you"), context->username);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, _("Your message "
"was not sent. Either end your private conversation, "
"or restart it."), 1, OTRL_NOTIFY_ERROR,
_("Private connection closed"), buf,
_("Your message was not sent. Either close your "
"private connection to him, or refresh it."));
g_free(buf);
break;
case OTRL_MSGEVENT_SETUP_ERROR:
if (!err) {
err = GPG_ERR_INV_VALUE;
}
switch(gcry_err_code(err)) {
case GPG_ERR_INV_VALUE:
buf = g_strdup(_("Error setting up private "
"conversation: Malformed message received"));
break;
default:
buf = g_strdup_printf(_("Error setting up private "
"conversation: %s"), gcry_strerror(err));
break;
}
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, buf, 1,
OTRL_NOTIFY_ERROR, _("OTR Error"), buf, NULL);
g_free(buf);
break;
case OTRL_MSGEVENT_MSG_REFLECTED:
display_otr_message_or_notify(opdata,
context->accountname, context->protocol,
context->username,
_("We are receiving our own OTR messages. "
"You are either trying to talk to yourself, "
"or someone is reflecting your messages back "
"at you."), 1, OTRL_NOTIFY_ERROR,
_("OTR Error"), _("We are receiving our own OTR messages."),
_("You are either trying to talk to yourself, "
"or someone is reflecting your messages back "
"at you."));
break;
case OTRL_MSGEVENT_MSG_RESENT:
buf = g_strdup_printf(_("<b>The last message to %s was resent."
"</b>"), context->username);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, buf, 1,
OTRL_NOTIFY_INFO, _("Message resent"), buf, NULL);
g_free(buf);
break;
case OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE:
buf = g_strdup_printf(_("<b>The encrypted message received from "
"%s is unreadable, as you are not currently communicating "
"privately.</b>"), context->username);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, buf, 1,
OTRL_NOTIFY_INFO, _("Unreadable message"), buf, NULL);
g_free(buf);
break;
case OTRL_MSGEVENT_RCVDMSG_UNREADABLE:
buf = g_strdup_printf(_("We received an unreadable "
"encrypted message from %s."), context->username);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, buf, 1,
OTRL_NOTIFY_ERROR, _("OTR Error"), buf, NULL);
g_free(buf);
break;
case OTRL_MSGEVENT_RCVDMSG_MALFORMED:
buf = g_strdup_printf(_("We received a malformed data "
"message from %s."), context->username);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, buf, 1,
OTRL_NOTIFY_ERROR, _("OTR Error"), buf, NULL);
g_free(buf);
break;
case OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD:
buf = g_strdup_printf(_("Heartbeat received from %s.\n"),
context->username);
log_message(opdata, buf);
g_free(buf);
break;
case OTRL_MSGEVENT_LOG_HEARTBEAT_SENT:
buf = g_strdup_printf(_("Heartbeat sent to %s.\n"),
context->username);
log_message(opdata, buf);
g_free(buf);
break;
case OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR:
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, message, 1,
OTRL_NOTIFY_ERROR, _("OTR Error"), message, NULL);
break;
case OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED:
buf = g_strdup_printf(_("<b>The following message received "
"from %s was <i>not</i> encrypted: [</b>%s<b>]</b>"),
context->username, message);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, buf, 1,
OTRL_NOTIFY_INFO, _("Received unencrypted message"),
buf, NULL);
emit_msg_received(context, buf);
g_free(buf);
break;
case OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED:
buf = g_strdup_printf(_("Unrecognized OTR message received "
"from %s.\n"), context->username);
log_message(opdata, buf);
g_free(buf);
break;
case OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE:
if (*last_msg_event == msg_event) {
break;
}
buf = g_strdup_printf(_("%s has sent a message intended for a "
"different session. If you are logged in multiple times, "
"another session may have received the message."),
context->username);
display_otr_message_or_notify(opdata, context->accountname,
context->protocol, context->username, buf, 1,
OTRL_NOTIFY_INFO, _("Received message for a different "
"session"), buf, NULL);
g_free(buf);
break;
}
*last_msg_event = msg_event;
}
#ifdef DUMP_RECEIVED_SYMKEY
static void dump_data(const unsigned char *d, size_t l)
{
size_t i;
for (i=0;i<l;++i) printf("%02x", d[i]);
}
static void received_symkey_cb(void *opdata, ConnContext *context,
unsigned int use, const unsigned char *usedata,
size_t usedatalen, const unsigned char *symkey)
{
printf("Received symkey use: %08x\nKey: ", use);
dump_data(symkey, OTRL_EXTRAKEY_BYTES);
printf("\nUsedata: ");
dump_data(usedata, usedatalen);
printf("\n\n");
}
#endif
static guint otrg_plugin_timerid = 0;