forked from bitlbee/bitlbee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otr.c
2168 lines (1841 loc) · 59.3 KB
/
otr.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
/********************************************************************\
* BitlBee -- An IRC to other IM-networks gateway *
* *
* Copyright 2002-2012 Wilmer van der Gaast and others *
\********************************************************************/
/*
OTR support (cf. http://www.cypherpunks.ca/otr/)
(c) 2008-2011,2013 Sven Moritz Hallberg <pesco@khjk.org>
funded by stonedcoder.org
files used to store OTR data:
<configdir>/<nick>.otr_keys
<configdir>/<nick>.otr_fprints
<configdir>/<nick>.otr_instags <- don't copy this one between hosts
top-level todos: (search for TODO for more ;-))
integrate otr_load/otr_save with existing storage backends
per-account policy settings
per-user policy settings
add a way to select recipient instance
*/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 with
the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "bitlbee.h"
#include "irc.h"
#include "otr.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
/** OTR interface routines for the OtrlMessageAppOps struct: **/
OtrlPolicy op_policy(void *opdata, ConnContext *context);
void op_create_privkey(void *opdata, const char *accountname, const char *protocol);
int op_is_logged_in(void *opdata, const char *accountname, const char *protocol,
const char *recipient);
void op_inject_message(void *opdata, const char *accountname, const char *protocol,
const char *recipient, const char *message);
void op_new_fingerprint(void *opdata, OtrlUserState us, const char *accountname,
const char *protocol, const char *username, unsigned char fingerprint[20]);
void op_write_fingerprints(void *opdata);
void op_gone_secure(void *opdata, ConnContext *context);
void op_gone_insecure(void *opdata, ConnContext *context);
void op_still_secure(void *opdata, ConnContext *context, int is_reply);
void op_log_message(void *opdata, const char *message);
int op_max_message_size(void *opdata, ConnContext *context);
const char *op_account_name(void *opdata, const char *account, const char *protocol);
void op_create_instag(void *opdata, const char *account, const char *protocol);
void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ,
char **dst, const char *src);
void op_convert_free(void *opdata, ConnContext *ctx, char *msg);
void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx,
unsigned short percent, char *question);
void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx,
const char *message, gcry_error_t err);
const char *op_otr_error_message(void *opdata, ConnContext *ctx,
OtrlErrorCode err_code);
/** otr sub-command handlers: **/
static void cmd_otr(irc_t *irc, char **args);
void cmd_otr_connect(irc_t *irc, char **args);
void cmd_otr_disconnect(irc_t *irc, char **args);
void cmd_otr_reconnect(irc_t *irc, char **args);
void cmd_otr_smp(irc_t *irc, char **args);
void cmd_otr_smpq(irc_t *irc, char **args);
void cmd_otr_trust(irc_t *irc, char **args);
void cmd_otr_info(irc_t *irc, char **args);
void cmd_otr_keygen(irc_t *irc, char **args);
void cmd_otr_forget(irc_t *irc, char **args);
const command_t otr_commands[] = {
{ "connect", 1, &cmd_otr_connect, 0 },
{ "disconnect", 1, &cmd_otr_disconnect, 0 },
{ "reconnect", 1, &cmd_otr_reconnect, 0 },
{ "smp", 2, &cmd_otr_smp, 0 },
{ "smpq", 3, &cmd_otr_smpq, 0 },
{ "trust", 6, &cmd_otr_trust, 0 },
{ "info", 0, &cmd_otr_info, 0 },
{ "keygen", 1, &cmd_otr_keygen, 0 },
{ "forget", 2, &cmd_otr_forget, 0 },
{ NULL }
};
typedef struct {
void *fst;
void *snd;
} pair_t;
static OtrlMessageAppOps otr_ops; /* collects interface functions required by OTR */
/** misc. helpers/subroutines: **/
/* check whether we are already generating a key for a given account */
int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol);
/* start background process to generate a (new) key for a given account */
void otr_keygen(irc_t *irc, const char *handle, const char *protocol);
/* main function for the forked keygen slave */
void keygen_child_main(OtrlUserState us, int infd, int outfd);
/* mainloop handler for when a keygen finishes */
gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond);
/* copy the contents of file a to file b, overwriting it if it exists */
void copyfile(const char *a, const char *b);
/* read one line of input from a stream, excluding trailing newline */
void myfgets(char *s, int size, FILE *stream);
/* some yes/no handlers */
void yes_keygen(void *data);
void yes_forget_fingerprint(void *data);
void yes_forget_context(void *data);
void yes_forget_key(void *data);
/* timeout handler that calls otrl_message_poll */
gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond);
/* helper to make sure accountname and protocol match the incoming "opdata" */
struct im_connection *check_imc(void *opdata, const char *accountname,
const char *protocol);
/* determine the nick for a given handle/protocol pair
returns "handle/protocol" if not found */
const char *peernick(irc_t *irc, const char *handle, const char *protocol);
/* turn a hexadecimal digit into its numerical value */
int hexval(char a);
/* determine the irc_user_t for a given handle/protocol pair
returns NULL if not found */
irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol);
/* show an otr-related message to the user */
void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...);
/* write an otr-related message to the system log */
void log_otr_message(void *opdata, const char *fmt, ...);
/* combined handler for the 'otr smp' and 'otr smpq' commands */
void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question,
const char *secret);
/* update flags within the irc_user structure to reflect OTR status of context */
void otr_update_uflags(ConnContext *context, irc_user_t *u);
/* update op/voice flag of given user according to encryption state and settings
returns 0 if neither op_buddies nor voice_buddies is set to "encrypted",
i.e. msgstate should be announced separately */
int otr_update_modeflags(irc_t *irc, irc_user_t *u);
/* show general info about the OTR subsystem; called by 'otr info' */
void show_general_otr_info(irc_t *irc);
/* show info about a given OTR context and subcontexts/instances. bestctx
may be either NULL or preferred destination context (this is hilighted
in the output as being the target for a message) */
void show_otr_context_info(irc_t *irc, ConnContext *ctx, ConnContext *bestctx);
/* show the list of fingerprints associated with a given context */
void show_fingerprints(irc_t *irc, ConnContext *ctx);
/* find a fingerprint by prefix (given as any number of hex strings) */
Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args);
/* find a private key by fingerprint prefix (given as any number of hex strings) */
OtrlPrivKey *match_privkey(irc_t *irc, const char **args);
/* check whether a string is safe to use in a path component */
int strsane(const char *s);
/* close the OTR connection with the given buddy */
gboolean otr_disconnect_user(irc_t *irc, irc_user_t *u);
/* close all active OTR connections */
void otr_disconnect_all(irc_t *irc);
/* modifies string in-place, replacing \x03 with '?',
as a quick way to prevent remote users from messing with irc colors */
static char *otr_filter_colors(char *msg);
/* functions to be called for certain events */
static const struct irc_plugin otr_plugin;
#define OTR_COLOR_TRUSTED "03" /* green */
#define OTR_COLOR_UNTRUSTED "05" /* red */
/*** routines declared in otr.h: ***/
#ifdef OTR_BI
#define init_plugin otr_init
#endif
void init_plugin(void)
{
OTRL_INIT;
/* fill global OtrlMessageAppOps */
otr_ops.policy = &op_policy;
otr_ops.create_privkey = &op_create_privkey;
otr_ops.is_logged_in = &op_is_logged_in;
otr_ops.inject_message = &op_inject_message;
otr_ops.update_context_list = NULL;
otr_ops.new_fingerprint = &op_new_fingerprint;
otr_ops.write_fingerprints = &op_write_fingerprints;
otr_ops.gone_secure = &op_gone_secure;
otr_ops.gone_insecure = &op_gone_insecure;
otr_ops.still_secure = &op_still_secure;
otr_ops.max_message_size = &op_max_message_size;
otr_ops.account_name = &op_account_name;
otr_ops.account_name_free = NULL;
/* stuff added with libotr 4.0.0 */
otr_ops.received_symkey = NULL; /* we don't use the extra key */
otr_ops.otr_error_message = &op_otr_error_message;
otr_ops.otr_error_message_free = NULL;
otr_ops.resent_msg_prefix = NULL; /* default: [resent] */
otr_ops.resent_msg_prefix_free = NULL;
otr_ops.handle_smp_event = &op_handle_smp_event;
otr_ops.handle_msg_event = &op_handle_msg_event;
otr_ops.create_instag = &op_create_instag;
otr_ops.convert_msg = &op_convert_msg;
otr_ops.convert_free = &op_convert_free;
otr_ops.timer_control = NULL; /* we just poll */
root_command_add("otr", 1, cmd_otr, 0);
register_irc_plugin(&otr_plugin);
}
gboolean otr_irc_new(irc_t *irc)
{
set_t *s;
GSList *l;
irc->otr = g_new0(otr_t, 1);
irc->otr->us = otrl_userstate_create();
s = set_add(&irc->b->set, "otr_color_encrypted", "true", set_eval_bool, irc);
s = set_add(&irc->b->set, "otr_policy", "opportunistic", set_eval_list, irc);
l = g_slist_prepend(NULL, "never");
l = g_slist_prepend(l, "opportunistic");
l = g_slist_prepend(l, "manual");
l = g_slist_prepend(l, "always");
s->eval_data = l;
s = set_add(&irc->b->set, "otr_does_html", "true", set_eval_bool, irc);
/* regularly call otrl_message_poll */
gint definterval = otrl_message_poll_get_default_interval(irc->otr->us);
irc->otr->timer = b_timeout_add(definterval, ev_message_poll, irc->otr);
return TRUE;
}
void otr_irc_free(irc_t *irc)
{
set_t *s;
otr_t *otr = irc->otr;
otr_disconnect_all(irc);
b_event_remove(otr->timer);
otrl_userstate_free(otr->us);
s = set_find(&irc->b->set, "otr_policy");
g_slist_free(s->eval_data);
if (otr->keygen) {
kill(otr->keygen, SIGTERM);
waitpid(otr->keygen, NULL, 0);
/* TODO: remove stale keygen tempfiles */
}
if (otr->to) {
fclose(otr->to);
}
if (otr->from) {
fclose(otr->from);
}
while (otr->todo) {
kg_t *p = otr->todo;
otr->todo = p->next;
g_free(p);
}
g_free(otr);
}
void otr_load(irc_t *irc)
{
char s[512];
account_t *a;
gcry_error_t e;
gcry_error_t enoent = gcry_error_from_errno(ENOENT);
int kg = 0;
if (strsane(irc->user->nick)) {
g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick);
e = otrl_privkey_read(irc->otr->us, s);
if (e && e != enoent) {
irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
}
g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL);
if (e && e != enoent) {
irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
}
g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, irc->user->nick);
e = otrl_instag_read(irc->otr->us, s);
if (e && e != enoent) {
irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
}
}
/* check for otr keys on all accounts */
for (a = irc->b->accounts; a; a = a->next) {
kg = otr_check_for_key(a) || kg;
}
if (kg) {
irc_rootmsg(irc, "Notice: "
"The accounts above do not have OTR encryption keys associated with them, yet. "
"These keys are now being generated in the background. "
"You will be notified as they are completed. "
"It is not necessary to wait; "
"BitlBee can be used normally during key generation. "
"You may safely ignore this message if you don't know what OTR is. ;)");
}
}
void otr_save(irc_t *irc)
{
char s[512];
gcry_error_t e;
if (strsane(irc->user->nick)) {
g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
e = otrl_privkey_write_fingerprints(irc->otr->us, s);
if (e) {
irc_rootmsg(irc, "otr save: %s: %s", s, gcry_strerror(e));
}
chmod(s, 0600);
}
}
void otr_remove(const char *nick)
{
char s[512];
if (strsane(nick)) {
g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick);
unlink(s);
g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick);
unlink(s);
}
}
void otr_rename(const char *onick, const char *nnick)
{
char s[512], t[512];
if (strsane(nnick) && strsane(onick)) {
g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick);
g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick);
rename(s, t);
g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick);
g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick);
rename(s, t);
}
}
int otr_check_for_key(account_t *a)
{
irc_t *irc = a->bee->ui_data;
OtrlPrivKey *k;
/* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
if (a->prpl->options & OPT_NOOTR) {
return 0;
}
k = otrl_privkey_find(irc->otr->us, a->user, a->prpl->name);
if (k) {
irc_rootmsg(irc, "otr: %s/%s ready", a->user, a->prpl->name);
return 0;
}
if (keygen_in_progress(irc, a->user, a->prpl->name)) {
irc_rootmsg(irc, "otr: keygen for %s/%s already in progress", a->user, a->prpl->name);
return 0;
} else {
irc_rootmsg(irc, "otr: starting background keygen for %s/%s", a->user, a->prpl->name);
otr_keygen(irc, a->user, a->prpl->name);
return 1;
}
}
char *otr_filter_msg_in(irc_user_t *iu, char *msg, int flags)
{
int ignore_msg;
char *newmsg = NULL;
OtrlTLV *tlvs = NULL;
irc_t *irc = iu->irc;
struct im_connection *ic = iu->bu->ic;
/* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
if (ic->acc->prpl->options & OPT_NOOTR) {
return msg;
}
ignore_msg = otrl_message_receiving(irc->otr->us, &otr_ops, ic,
ic->acc->user, ic->acc->prpl->name, iu->bu->handle, msg, &newmsg,
&tlvs, NULL, NULL, NULL);
if (tlvs) {
otrl_tlv_free(tlvs);
}
if (ignore_msg) {
/* this was an internal OTR protocol message */
return NULL;
} else if (!newmsg) {
/* this was a non-OTR message */
return otr_filter_colors(msg);
} else {
/* we're done with the original msg, which will be caller-freed. */
return newmsg;
}
}
char *otr_filter_msg_out(irc_user_t *iu, char *msg, int flags)
{
int st;
char *otrmsg = NULL;
ConnContext *ctx = NULL;
irc_t *irc = iu->irc;
struct im_connection *ic = iu->bu->ic;
otrl_instag_t instag = OTRL_INSTAG_BEST; // XXX?
/* NB: in libotr 4.0.0 OTRL_INSTAG_RECENT will cause a null-pointer deref
* in otrl_message_sending with newly-added OTR contexts.
*/
/* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
if (ic->acc->prpl->options & OPT_NOOTR) {
return msg;
}
st = otrl_message_sending(irc->otr->us, &otr_ops, ic,
ic->acc->user, ic->acc->prpl->name, iu->bu->handle, instag,
msg, NULL, &otrmsg, OTRL_FRAGMENT_SEND_ALL_BUT_LAST, &ctx, NULL, NULL);
if (otrmsg && otrmsg != msg) {
/* libotr wants us to replace our message */
/* NB: caller will free old msg */
msg = st ? NULL : g_strdup(otrmsg);
otrl_message_free(otrmsg);
}
if (st) {
irc_usernotice(iu, "otr: error handling outgoing message: %d", st);
msg = NULL; /* do not send plaintext! */
}
return msg;
}
static const struct irc_plugin otr_plugin =
{
otr_irc_new,
otr_irc_free,
otr_filter_msg_out,
otr_filter_msg_in,
otr_load,
otr_save,
otr_remove,
};
static void cmd_otr(irc_t *irc, char **args)
{
const command_t *cmd;
if (!args[0]) {
return;
}
if (!args[1]) {
return;
}
for (cmd = otr_commands; cmd->command; cmd++) {
if (strcmp(cmd->command, args[1]) == 0) {
break;
}
}
if (!cmd->command) {
irc_rootmsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02",
args[0], args[1]);
return;
}
if (!args[cmd->required_parameters + 1]) {
irc_rootmsg(irc, "%s %s: not enough arguments (%d req.)",
args[0], args[1], cmd->required_parameters);
return;
}
cmd->execute(irc, args + 1);
}
/*** OTR "MessageAppOps" callbacks for global.otr_ui: ***/
OtrlPolicy op_policy(void *opdata, ConnContext *context)
{
struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol);
irc_t *irc = ic->bee->ui_data;
const char *p;
/* policy override during keygen: if we're missing the key for context but are currently
generating it, then that's as much as we can do. => temporarily return NEVER. */
if (keygen_in_progress(irc, context->accountname, context->protocol) &&
!otrl_privkey_find(irc->otr->us, context->accountname, context->protocol)) {
return OTRL_POLICY_NEVER;
}
p = set_getstr(&ic->bee->set, "otr_policy");
if (!strcmp(p, "never")) {
return OTRL_POLICY_NEVER;
}
if (!strcmp(p, "opportunistic")) {
return OTRL_POLICY_OPPORTUNISTIC;
}
if (!strcmp(p, "manual")) {
return OTRL_POLICY_MANUAL;
}
if (!strcmp(p, "always")) {
return OTRL_POLICY_ALWAYS;
}
return OTRL_POLICY_OPPORTUNISTIC;
}
void op_create_privkey(void *opdata, const char *accountname,
const char *protocol)
{
struct im_connection *ic = check_imc(opdata, accountname, protocol);
irc_t *irc = ic->bee->ui_data;
/* will fail silently if keygen already in progress */
otr_keygen(irc, accountname, protocol);
}
int op_is_logged_in(void *opdata, const char *accountname,
const char *protocol, const char *recipient)
{
struct im_connection *ic = check_imc(opdata, accountname, protocol);
bee_user_t *bu;
/* lookup the irc_user_t for the given recipient */
bu = bee_user_by_handle(ic->bee, ic, recipient);
if (bu) {
if (bu->flags & BEE_USER_ONLINE) {
return 1;
} else {
return 0;
}
} else {
return -1;
}
}
void op_inject_message(void *opdata, const char *accountname,
const char *protocol, const char *recipient, const char *message)
{
struct im_connection *ic = check_imc(opdata, accountname, protocol);
irc_t *irc = ic->bee->ui_data;
if (strcmp(accountname, recipient) == 0) {
/* huh? injecting messages to myself? */
irc_rootmsg(irc, "note to self: %s", message);
} else {
/* need to drop some consts here :-( */
/* TODO: get flags into op_inject_message?! */
ic->acc->prpl->buddy_msg(ic, (char *) recipient, (char *) message, 0);
/* ignoring return value :-/ */
}
}
void op_new_fingerprint(void *opdata, OtrlUserState us,
const char *accountname, const char *protocol,
const char *username, unsigned char fingerprint[20])
{
struct im_connection *ic = check_imc(opdata, accountname, protocol);
irc_t *irc = ic->bee->ui_data;
irc_user_t *u = peeruser(irc, username, protocol);
char hunam[45]; /* anybody looking? ;-) */
otrl_privkey_hash_to_human(hunam, fingerprint);
if (u) {
irc_usernotice(u, "new fingerprint: %s", hunam);
} else {
/* this case shouldn't normally happen */
irc_rootmsg(irc, "new fingerprint for %s/%s: %s",
username, protocol, hunam);
}
}
void op_write_fingerprints(void *opdata)
{
struct im_connection *ic = (struct im_connection *) opdata;
irc_t *irc = ic->bee->ui_data;
otr_save(irc);
}
void op_gone_secure(void *opdata, ConnContext *context)
{
struct im_connection *ic =
check_imc(opdata, context->accountname, context->protocol);
irc_user_t *u;
irc_t *irc = ic->bee->ui_data;
u = peeruser(irc, context->username, context->protocol);
if (!u) {
log_message(LOGLVL_ERROR,
"BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!",
context->username, context->protocol, context->accountname);
return;
}
otr_update_uflags(context, u);
if (!otr_update_modeflags(irc, u)) {
char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
irc_usernotice(u, "conversation is now off the record (%s)", trust);
}
}
void op_gone_insecure(void *opdata, ConnContext *context)
{
struct im_connection *ic =
check_imc(opdata, context->accountname, context->protocol);
irc_t *irc = ic->bee->ui_data;
irc_user_t *u;
u = peeruser(irc, context->username, context->protocol);
if (!u) {
log_message(LOGLVL_ERROR,
"BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!",
context->username, context->protocol, context->accountname);
return;
}
otr_update_uflags(context, u);
if (!otr_update_modeflags(irc, u)) {
irc_usernotice(u, "conversation is now in cleartext");
}
}
void op_still_secure(void *opdata, ConnContext *context, int is_reply)
{
struct im_connection *ic =
check_imc(opdata, context->accountname, context->protocol);
irc_t *irc = ic->bee->ui_data;
irc_user_t *u;
u = peeruser(irc, context->username, context->protocol);
if (!u) {
log_message(LOGLVL_ERROR,
"BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!",
context->username, context->protocol, context->accountname);
return;
}
otr_update_uflags(context, u);
if (!otr_update_modeflags(irc, u)) {
char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
irc_usernotice(u, "otr connection has been refreshed (%s)", trust);
}
}
int op_max_message_size(void *opdata, ConnContext *context)
{
struct im_connection *ic =
check_imc(opdata, context->accountname, context->protocol);
return ic->acc->prpl->mms;
}
const char *op_account_name(void *opdata, const char *account, const char *protocol)
{
struct im_connection *ic = (struct im_connection *) opdata;
irc_t *irc = ic->bee->ui_data;
return peernick(irc, account, protocol);
}
void op_create_instag(void *opdata, const char *account, const char *protocol)
{
struct im_connection *ic =
check_imc(opdata, account, protocol);
irc_t *irc = ic->bee->ui_data;
gcry_error_t e;
char s[512];
g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir,
irc->user->nick);
e = otrl_instag_generate(irc->otr->us, s, account, protocol);
if (e) {
irc_rootmsg(irc, "otr: %s/%s: otrl_instag_generate failed: %s",
account, protocol, gcry_strerror(e));
}
}
static char *otr_filter_colors(char *msg) {
int i;
for (i = 0; msg[i]; i++) {
if (msg[i] == '\x03') {
msg[i] = '?';
}
}
return msg;
}
/* returns newly allocated string */
static char *otr_color_encrypted(char *msg, char *color, gboolean is_query) {
char **lines;
GString *out;
int i;
lines = g_strsplit(msg, "\n", -1);
/* up to 4 extra chars per line (e.g., '\x03' + ("03"|"05") + ' ') */
out = g_string_sized_new(strlen(msg) + g_strv_length(lines) * 4);
for (i = 0; lines[i]; i++) {
char *line = lines[i];
if (i != 0) {
g_string_append_c(out, '\n');
} else if (is_query && g_strncasecmp(line, "/me ", 4) == 0) {
/* in a query window, keep "/me " uncolored at the beginning */
line += 4;
g_string_append(out, "/me ");
}
g_string_append_c(out, '\x03');
g_string_append(out, color);
/* comma in first place could mess with the color code */
if (line[0] == ',') {
/* insert a space between color spec and message */
g_string_append_c(out, ' ');
}
g_string_append(out, otr_filter_colors(line));
}
g_strfreev(lines);
return g_string_free(out, FALSE);
}
void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ,
char **dst, const char *src)
{
struct im_connection *ic =
check_imc(opdata, ctx->accountname, ctx->protocol);
irc_t *irc = ic->bee->ui_data;
irc_user_t *iu = peeruser(irc, ctx->username, ctx->protocol);
if (typ == OTRL_CONVERT_RECEIVING) {
char *msg = g_strdup(src);
/* HTML decoding */
if (set_getbool(&ic->bee->set, "otr_does_html") &&
!(ic->flags & OPT_DOES_HTML) &&
set_getbool(&ic->bee->set, "strip_html")) {
strip_html(msg);
/* msg is borrowed by *dst (unless the next if decides to color it) */
*dst = msg;
}
/* coloring */
if (set_getbool(&ic->bee->set, "otr_color_encrypted")) {
const char *trust = ctx->active_fingerprint->trust;
char *color = (trust && *trust) ? OTR_COLOR_TRUSTED : OTR_COLOR_UNTRUSTED;
gboolean is_query = (irc_user_msgdest(iu) == irc->user->nick);
/* the return value of otr_color_encrypted() is borrowed by *dst */
*dst = otr_color_encrypted(msg, color, is_query);
/* this branch doesn't need msg */
g_free(msg);
}
} else {
/* HTML encoding */
/* consider OTR plaintext to be HTML if otr_does_html is set */
if (ctx && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED &&
set_getbool(&ic->bee->set, "otr_does_html") &&
(g_strncasecmp(src, "<html>", 6) != 0)) {
*dst = escape_html(src);
}
}
}
void op_convert_free(void *opdata, ConnContext *ctx, char *msg)
{
g_free(msg);
}
/* Socialist Millionaires' Protocol */
void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx,
unsigned short percent, char *question)
{
struct im_connection *ic =
check_imc(opdata, ctx->accountname, ctx->protocol);
irc_t *irc = ic->bee->ui_data;
OtrlUserState us = irc->otr->us;
irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol);
if (!u) {
return;
}
switch (ev) {
case OTRL_SMPEVENT_ASK_FOR_SECRET:
irc_rootmsg(irc, "smp: initiated by %s"
" - respond with \x02otr smp %s <secret>\x02",
u->nick, u->nick);
break;
case OTRL_SMPEVENT_ASK_FOR_ANSWER:
irc_rootmsg(irc, "smp: initiated by %s with question: \x02\"%s\"\x02", u->nick,
question);
irc_rootmsg(irc, "smp: respond with \x02otr smp %s <answer>\x02",
u->nick);
break;
case OTRL_SMPEVENT_CHEATED:
irc_rootmsg(irc, "smp %s: opponent violated protocol, aborting",
u->nick);
otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx);
otrl_sm_state_free(ctx->smstate);
break;
case OTRL_SMPEVENT_NONE:
break;
case OTRL_SMPEVENT_IN_PROGRESS:
break;
case OTRL_SMPEVENT_SUCCESS:
if (ctx->smstate->received_question) {
irc_rootmsg(irc, "smp %s: correct answer, you are trusted",
u->nick);
} else {
irc_rootmsg(irc, "smp %s: secrets proved equal, fingerprint trusted",
u->nick);
}
otrl_sm_state_free(ctx->smstate);
break;
case OTRL_SMPEVENT_FAILURE:
if (ctx->smstate->received_question) {
irc_rootmsg(irc, "smp %s: wrong answer, you are not trusted",
u->nick);
} else {
irc_rootmsg(irc, "smp %s: secrets did not match, fingerprint not trusted",
u->nick);
}
otrl_sm_state_free(ctx->smstate);
break;
case OTRL_SMPEVENT_ABORT:
irc_rootmsg(irc, "smp: received abort from %s", u->nick);
otrl_sm_state_free(ctx->smstate);
break;
case OTRL_SMPEVENT_ERROR:
irc_rootmsg(irc, "smp %s: protocol error, aborting",
u->nick);
otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx);
otrl_sm_state_free(ctx->smstate);
break;
}
}
void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx,
const char *message, gcry_error_t err)
{
switch (ev) {
case OTRL_MSGEVENT_ENCRYPTION_REQUIRED:
display_otr_message(opdata, ctx,
"policy requires encryption - message not sent");
break;
case OTRL_MSGEVENT_ENCRYPTION_ERROR:
display_otr_message(opdata, ctx,
"error during encryption - message not sent");
break;
case OTRL_MSGEVENT_CONNECTION_ENDED:
display_otr_message(opdata, ctx,
"other end has disconnected OTR - "
"close connection or reconnect!");
break;
case OTRL_MSGEVENT_SETUP_ERROR:
display_otr_message(opdata, ctx,
"OTR connection failed: %s", gcry_strerror(err));
break;
case OTRL_MSGEVENT_MSG_REFLECTED:
display_otr_message(opdata, ctx,
"received our own OTR message (!?)");
break;
case OTRL_MSGEVENT_MSG_RESENT:
display_otr_message(opdata, ctx,
"the previous message was resent");
break;
case OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE:
display_otr_message(opdata, ctx,
"unexpected encrypted message received");
break;
case OTRL_MSGEVENT_RCVDMSG_UNREADABLE:
display_otr_message(opdata, ctx,
"unreadable encrypted message received");
break;
case OTRL_MSGEVENT_RCVDMSG_MALFORMED:
display_otr_message(opdata, ctx,
"malformed OTR message received");
break;
case OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD:
if (global.conf->verbose) {
log_otr_message(opdata, "%s/%s: heartbeat received",
ctx->accountname, ctx->protocol);
}
break;
case OTRL_MSGEVENT_LOG_HEARTBEAT_SENT:
if (global.conf->verbose) {
log_otr_message(opdata, "%s/%s: heartbeat sent",
ctx->accountname, ctx->protocol);
}
break;
case OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR:
display_otr_message(opdata, ctx,
"OTR error message received: %s", message);
break;
case OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED:
display_otr_message(opdata, ctx,
"unencrypted message received: %s", message);
break;
case OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED:
display_otr_message(opdata, ctx,
"unrecognized OTR message received");
break;
case OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE:
display_otr_message(opdata, ctx,
"OTR message for a different instance received");
break;
default:
/* shouldn't happen */
break;
}
}
const char *op_otr_error_message(void *opdata, ConnContext *ctx,
OtrlErrorCode err_code)
{
switch (err_code) {
case OTRL_ERRCODE_ENCRYPTION_ERROR:
return "i failed to encrypt a message";
case OTRL_ERRCODE_MSG_NOT_IN_PRIVATE: