-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin-all.c
1884 lines (1601 loc) · 61.2 KB
/
plugin-all.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
/*
* Off-the-Record Messaging plugin for pidgin
* Copyright (C) 2004-2018 Ian Goldberg, Rob Smits,
* Chris Alexander, Willy Lew,
* Nikita Borisov
* The pidgin-otrng contributors
*
* 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
#include <glib.h>
/* system headers */
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/* libgcrypt headers */
#include <gcrypt.h>
/* purple headers */
#include <core.h>
#include <debug.h>
#include <notify.h>
#include <pidgin.h>
#include <pidginstock.h>
#include <util.h>
#include <version.h>
#ifdef USING_GTK
/* purple GTK headers */
#include <gtkplugin.h>
#endif
/* libotr headers */
#include <libotr/proto.h>
#include <libotr/instag.h>
#include <libotr/message.h>
#include <libotr/privkey.h>
#include <libotr/tlv.h>
#include <libotr/userstate.h>
/* pidgin-otrng headers */
#include "persistance.h"
#include "plugin-all.h"
#include "prekey-plugin.h"
#include "prekeys.h"
#include "profiles.h"
#ifdef USING_GTK
#include <glib.h>
/* pidgin-otrng GTK headers */
#include "fingerprint.h"
#include "gtk-dialog.h"
#include "gtk-ui.h"
#include "i18n.h"
#include "long_term_keys.h"
#include "pidgin-helpers.h"
#include "prekey-discovery.h"
#include "prekey-plugin-peers.h"
#include "prekey-plugin-shared.h"
#include <libotr-ng/alloc.h>
#include <libotr-ng/client_orchestration.h>
#include <libotr-ng/debug.h>
/* Controls a beta warning/expiry dialog */
#define BETA_DIALOG 0
#if defined USING_GTK
#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 *otrng_plugin_handle;
otrng_global_state_s *otrng_state = NULL;
/* GLib HashTable for storing the maximum message size for various
* protocols. */
GHashTable *otrng_max_message_size_table = NULL;
static void otrng_plugin_read_instance_tags_FILEp(FILE *instagf) {
if (otrng_failed(
otrng_global_state_instance_tags_read_from(otrng_state, instagf))) {
// TODO: react better on failure
return;
}
}
/* Send an IM from the given account to the given recipient. Display an
* error dialog if that account isn't currently logged in. */
void otrng_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"));
otrng_dialog_notify_error(accountname, protocol, recipient,
_("Not connected"), msg, NULL);
g_free(msg);
return;
}
serv_send_im(connection, recipient, message, 0);
}
/* 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;
}
otrng_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 (otrng_dialog_display_otr_message(accountname, protocol, username, msg,
force_create)) {
notify(opdata, level, accountname, protocol, username, title, primary,
secondary);
return 1;
}
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;
}
otrng_ui_get_prefs(&prefs, account, context->username);
return prefs.policy;
}
/* Generate a instance tag for the given accountname/protocol */
void otrng_plugin_create_instag(const PurpleAccount *account) {
FILE *instagf;
gchar *instagfile =
g_build_filename(purple_user_dir(), INSTAG_FILE_NAME, 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 instange tag file\n"));
return;
}
/* Generate the instag */
// TODO: check the return value
otrng_global_state_instag_generate_into(
otrng_state, purple_account_to_client_id(account), instagf);
fclose(instagf);
}
static void create_privkey_cb(void *opdata, const char *account_name,
const char *protocol_name) {
// This should never happen, so make this an empty callback for now
}
static void create_instag_cb(otrng_client_s *client) {
otrng_plugin_create_instag(client_id_to_purple_account(client->client_id));
}
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"));
otrng_dialog_notify_error(accountname, protocol, recipient,
_("Unknown account"), msg, NULL);
g_free(msg);
return;
}
otrng_plugin_inject_message(account, recipient, message);
}
static void update_context_list_cb(void *opdata) { otrng_ui_update_keylist(); }
static void still_secure_cb(void *opdata, ConnContext *context, int is_reply) {
if (is_reply == 0) {
otrng_dialog_stillconnected(context);
}
}
static int max_message_size_cb(void *opdata, ConnContext *context) {
void *lookup_result =
g_hash_table_lookup(otrng_max_message_size_table, context->protocol);
if (!lookup_result) {
return 0;
}
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);
}
}
/* 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 = otrng_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 = otrng_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 otrng_plugin_timerid = 0;
/* Called by the glib main loop, as set up by stop_start_timer */
static gboolean timer_fired_cb(gpointer data);
/* Stop the timer, if it's currently running. If interval > 0, start it
* to periodically fire every interval seconds. */
static void stop_start_timer(unsigned int interval) {
otrng_debug_enter("stop_start_timer");
if (otrng_plugin_timerid) {
g_source_remove(otrng_plugin_timerid);
otrng_plugin_timerid = 0;
}
if (interval > 0) {
otrng_plugin_timerid =
g_timeout_add_seconds(interval, timer_fired_cb, NULL);
}
otrng_debug_exit("stop_start_timer");
}
/* Called by libotr */
static void timer_control_cb(void *opdata, unsigned int interval) {
stop_start_timer(interval);
}
static OtrlMessageAppOps ui_ops = {policy_cb,
create_privkey_cb, // create_privkey_cb,
is_logged_in_cb,
inject_message_cb,
update_context_list_cb,
confirm_fingerprint_cb_v3,
write_fingerprints_cb_v3,
NULL, // gone_secure_cb
NULL, // gone_insecure_cb
still_secure_cb,
max_message_size_cb,
NULL, /* account_name */
NULL, /* account_name_free */
#ifdef DUMP_RECEIVED_SYMKEY
received_symkey_cb,
#else
NULL, /* received_symkey */
#endif
otr_error_message_cb,
otr_error_message_free_cb,
resent_msg_prefix_cb,
resent_msg_prefix_free_cb,
NULL, // handle_smp_event_cb,
handle_msg_event_cb,
NULL, // create_instag_cb
NULL, /* convert_data */
NULL, /* convert_data_free */
timer_control_cb};
/* Called by the glib main loop, as set up by stop_start_timer */
static gboolean timer_fired_cb(gpointer data) {
// TODO: There should be an equivalent for this
otrl_message_poll(otrng_state->user_state_v3, &ui_ops, NULL);
return TRUE;
}
typedef struct {
char *username;
char **message;
} prekey_client_offline_message_ctx_s;
int otrng_plugin_buddy_is_offline(PurpleAccount *account, PurpleBuddy *buddy) {
return buddy && purple_account_supports_offline_message(account, buddy) &&
!PURPLE_BUDDY_IS_ONLINE(buddy);
}
static void start_process_retrieving_prekey_ensembles(PurpleAccount *account,
otrng_client_s *client,
void *xctx) {
prekey_client_offline_message_ctx_s *c = xctx;
char *send_to_prekey_server = NULL;
char *domain;
otrng_debug_fprintf(stderr,
"Start process of retrieving a prekey ensemble for %s\n",
c->username);
otrng_prekey_plugin_add_to_mapped_prekey_ensembles_responses(
client, account, *c->message, c->username);
otrng_prekey_retrieve_prekeys(&send_to_prekey_server, client, c->username,
"4");
/* This can't fail, since we check this before going down the path */
domain = otrng_plugin_prekey_domain_for(account, c->username);
otrng_prekey_server_s *si =
otrng_prekey_get_server_identity_for(client, domain);
g_free(domain);
otrng_plugin_inject_message(account, si->identity, send_to_prekey_server);
free(send_to_prekey_server);
}
static void send_offline_message(char **message, const char *username,
PurpleAccount *account) {
prekey_client_offline_message_ctx_s *ctx =
malloc(sizeof(prekey_client_offline_message_ctx_s));
if (!ctx) {
return;
}
ctx->username = g_strdup(purple_normalize(account, username));
ctx->message = message;
otrng_plugin_ensure_server_identity(
account, ctx->username, start_process_retrieving_prekey_ensembles, ctx);
}
void otrng_plugin_send_non_interactive_auth(const char *username,
PurpleAccount *account) {
char **message = malloc(sizeof(char *));
if (!message) {
return;
}
*message = "\0";
send_offline_message(message, username, account);
return;
}
static void process_sending_im(PurpleAccount *account, char *who,
char **message, void *ctx) {
char *newmessage = NULL;
char *username = NULL;
// const char *accountname = purple_account_get_username(account);
// const char *protocol = purple_account_get_protocol_id(account);
// PurpleConversation * conv = NULL;
// otrl_instag_t instance;
if (!who || !message || !*message) {
return;
}
// conv = otrng_plugin_userinfo_to_conv(accountname, protocol, username, 1);
// instance = otrng_plugin_conv_to_selected_instag(conv, OTRL_INSTAG_BEST);
username = g_strdup(purple_normalize(account, who));
otrng_client_s *client = purple_account_to_otrng_client(account);
otrng_client_ensure_correct_state(client);
trigger_potential_publishing(client);
otrng_conversation_s *otr_conv =
otrng_client_get_conversation(0, username, client);
PurpleBuddy *buddy = purple_find_buddy(account, username);
if (otrng_plugin_buddy_is_offline(account, buddy) &&
!otrng_conversation_is_encrypted(otr_conv)) {
send_offline_message(message, username, account);
return;
}
otrng_result result =
otrng_client_send(&newmessage, *message, username, client);
// TODO: this message should be stored for retransmission
// TODO: this will never be true - we need to change otrng_client_send to
// accomodate this
/* if (result == OTRNG_CLIENT_RESULT_ERROR_NOT_ENCRYPTED) { */
/* return; */
/* } */
// TODO: if require encription
// if (err == ???) {
// /* Do not send out plain text */
// char *ourm = g_strdup("");
// free(*message);
// *message = ourm;
//}
if (otrng_succeeded(result)) {
free(*message);
*message = g_strdup(newmessage);
}
// TODO: This is probably because libotr use a different mechanism to allocate
// memory securely
otrl_message_free(newmessage);
g_free(username);
}
/* Abort the SMP protocol. Used when malformed or unexpected messages
* are received. */
void otrng_plugin_abort_smp(const otrng_plugin_conversation *conv) {
otrng_client_s *client = otrng_plugin_conversation_to_client(conv);
if (!client) {
return;
}
char *to_send = NULL;
if (otrng_failed(otrng_client_smp_abort(&to_send, conv->peer, client))) {
return; // ERROR?
}
PurpleConversation *purp_conv = NULL;
PurpleAccount *account = NULL;
purp_conv = otrng_plugin_userinfo_to_conv(conv->account, conv->protocol,
conv->peer, 1);
account = purple_conversation_get_account(purp_conv);
otrng_plugin_inject_message(account, conv->peer, to_send);
free(to_send);
}
// TODO: REMOVE
otrng_client_s *
otrng_plugin_conversation_to_client(const otrng_plugin_conversation *conv) {
return get_otrng_client(conv->protocol, conv->account);
}
otrng_plugin_conversation *
otrng_plugin_conversation_copy(const otrng_plugin_conversation *conv) {
otrng_plugin_conversation *ret = otrng_plugin_conversation_new(conv->conv);
if (!ret) {
return ret;
}
ret->their_instance_tag = conv->their_instance_tag;
ret->our_instance_tag = conv->our_instance_tag;
return ret;
}
otrng_plugin_conversation *
purple_conversation_to_plugin_conversation(const PurpleConversation *conv) {
otrng_conversation_s *otr_conv =
purple_conversation_to_otrng_conversation(conv);
return otrng_plugin_conversation_new(otr_conv->conn);
}
/* Start the Socialist Millionaires' Protocol over the current connection,
* using the given initial secret, and optionally a question to pass to
* the buddy. */
void otrng_plugin_start_smp(otrng_plugin_conversation *conv,
const unsigned char *question, const size_t q_len,
const unsigned char *secret, size_t secretlen) {
otrng_client_s *client = otrng_plugin_conversation_to_client(conv);
if (!client) {
return;
}
char *tosend = NULL;
if (otrng_failed(otrng_client_smp_start(&tosend, conv->peer, question, q_len,
secret, secretlen, client))) {
return; // ERROR?
}
PurpleConversation *purp_conv = NULL;
PurpleAccount *account = NULL;
purp_conv = otrng_plugin_userinfo_to_conv(conv->account, conv->protocol,
conv->peer, 1);
account = purple_conversation_get_account(purp_conv);
otrng_plugin_inject_message(account, conv->peer, tosend);
free(tosend);
}
/* Continue the Socialist Millionaires' Protocol over the current connection,
* using the given initial secret (ie finish step 2). */
void otrng_plugin_continue_smp(otrng_plugin_conversation *conv,
const unsigned char *secret, size_t secretlen) {
otrng_client_s *client = otrng_plugin_conversation_to_client(conv);
if (!client) {
return;
}
char *tosend = NULL;
if (otrng_failed(otrng_client_smp_respond(&tosend, conv->peer, secret,
secretlen, client))) {
return; // ERROR?
}
PurpleConversation *purp_conv = NULL;
PurpleAccount *account = NULL;
purp_conv = otrng_plugin_userinfo_to_conv(conv->account, conv->protocol,
conv->peer, 1);
account = purple_conversation_get_account(purp_conv);
otrng_plugin_inject_message(account, conv->peer, tosend);
free(tosend);
}
#define OTRG_PLUGIN_DEFAULT_QUERY \
"?OTRv34? Attempting to start an OTR conversation. If you don't have the " \
"plugin to support this, please install it."
void otrng_plugin_send_default_query(otrng_plugin_conversation *conv) {
PurpleConversation *purp_conv = NULL;
PurpleAccount *account = NULL;
char *msg;
char *username;
purp_conv = otrng_plugin_userinfo_to_conv(conv->account, conv->protocol,
conv->peer, 1);
// TODO: change this later, but it is so, so it does compile
account = purple_conversation_get_account(purp_conv);
username = g_strdup(
purple_normalize(account, purple_conversation_get_name(purp_conv)));
OtrgUiPrefs prefs;
otrng_ui_get_prefs(&prefs, account, username);
free(username);
otrng_client_s *client = get_otrng_client(conv->protocol, conv->account);
if (!client) {
return;
}
msg = otrng_client_init_message(
conv->peer,
"Attempting to start an OTR conversation. If you don't have the plugin "
"to support this, please install it.",
client);
otrng_plugin_inject_message(account, conv->peer,
msg ? msg : OTRG_PLUGIN_DEFAULT_QUERY);
free(msg);
}
/* Send the default OTR Query message to the correspondent of the given
* conversation. */
void otrng_plugin_send_default_query_conv(PurpleConversation *conv) {
PurpleAccount *account = NULL;
char *peer = NULL;
char *msg = NULL;
OtrgUiPrefs prefs;
account = purple_conversation_get_account(conv);
// accountname = purple_account_get_username(account);
otrng_client_s *client = purple_account_to_otrng_client(account);
peer =
g_strdup(purple_normalize(account, purple_conversation_get_name(conv)));
otrng_ui_get_prefs(&prefs, account, peer);
msg = otrng_client_init_message(
peer,
"Attempting to start an OTR conversation. If you don't have the plugin "
"to support this, please install it.",
client);
otrng_plugin_inject_message(account, peer,
msg ? msg : OTRG_PLUGIN_DEFAULT_QUERY);
free(peer);
free(msg);
}
static gboolean process_receiving_im(PurpleAccount *account, char **who,
char **message, PurpleConversation *conv,
PurpleMessageFlags *flags) {
char *username = NULL;
char *tosend = NULL;
char *todisplay = NULL;
otrng_bool should_ignore = otrng_false;
// OtrlTLV *tlvs = NULL;
// OtrlTLV *tlv = NULL;
// const char *accountname;
// const char *protocol;
if (!who || !*who || !message || !*message) {
return 0;
}
username = g_strdup(purple_normalize(account, *who));
otrng_client_s *client = purple_account_to_otrng_client(account);
otrng_client_receive(&tosend, &todisplay, *message, username, client,
&should_ignore);
// TODO: client might optionally pass a warning here
// TODO: this will likely not work correctly at all, since otrng_result
// doesn't have that kind of result
/* if (res == OTRNG_CLIENT_RESULT_ERROR_NOT_ENCRYPTED) { */
/* // TODO: Needs to free tosend AND todisplay */
/* return 1; */
/* } */
if (tosend) {
// TODO: Should this send to the original who or to the normalized who?
otrng_plugin_inject_message(account, username, tosend);
free(tosend);
}
if (todisplay) {
free(*message);
*message = g_strdup(todisplay);
} else {
/* If we're supposed to ignore this incoming message (because it's a
* protocol message), set it to NULL, so that other plugins that
* catch receiving-im-msg don't return 0, and cause it to be
* displayed anyway. */
free(*message);
*message = NULL;
}
free(username);
return should_ignore == otrng_true;
}
// TODO: Remove me
/* Find the ConnContext appropriate to a given PurpleConversation. */
ConnContext *otrng_plugin_conv_to_context(PurpleConversation *conv,
otrl_instag_t their_instance,
int force_create) {
PurpleAccount *account;
const char *username;
const char *accountname, *proto;
ConnContext *context;
if (!conv) {
return NULL;
}
account = purple_conversation_get_account(conv);
accountname = purple_account_get_username(account);
proto = purple_account_get_protocol_id(account);
username = purple_conversation_get_name(conv);
context =
otrl_context_find(otrng_state->user_state_v3, username, accountname,
proto, their_instance, force_create, NULL, NULL, NULL);
return context;
}
/* Given a PurpleConversation, return the selected instag */
otrl_instag_t otrng_plugin_conv_to_selected_instag(PurpleConversation *conv,
otrl_instag_t default_val) {
otrl_instag_t *selected_instance;
if (!conv || !conv->data) {
return default_val;
}
selected_instance = purple_conversation_get_data(conv, "otr-ui_selected_ctx");
if (!selected_instance) {
return default_val;
}