source: otr.c @ cc17b76

Last change on this file since cc17b76 was cc17b76, checked in by Wilmer van der Gaast <wilmer@…>, at 2014-01-20T22:00:53Z

Merging fix from Flexo for #1109.

  • Property mode set to 100644
File size: 54.3 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/*
8  OTR support (cf. http://www.cypherpunks.ca/otr/)
9 
10  (c) 2008-2011,2013 Sven Moritz Hallberg <pesco@khjk.org>
11  funded by stonedcoder.org
12   
13  files used to store OTR data:
14    <configdir>/<nick>.otr_keys
15    <configdir>/<nick>.otr_fprints
16    <configdir>/<nick>.otr_instags  <- don't copy this one between hosts
17   
18  top-level todos: (search for TODO for more ;-))
19    integrate otr_load/otr_save with existing storage backends
20    per-account policy settings
21    per-user policy settings
22    add a way to select recipient instance
23*/
24
25/*
26  This program is free software; you can redistribute it and/or modify
27  it under the terms of the GNU General Public License as published by
28  the Free Software Foundation; either version 2 of the License, or
29  (at your option) any later version.
30
31  This program is distributed in the hope that it will be useful,
32  but WITHOUT ANY WARRANTY; without even the implied warranty of
33  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  GNU General Public License for more details.
35
36  You should have received a copy of the GNU General Public License with
37  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
38  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
39  Suite 330, Boston, MA  02111-1307  USA
40*/
41
42#include "bitlbee.h"
43#include "irc.h"
44#include "otr.h"
45#include <sys/types.h>
46#include <sys/wait.h>
47#include <unistd.h>
48#include <assert.h>
49#include <signal.h>
50
51
52/** OTR interface routines for the OtrlMessageAppOps struct: **/
53
54OtrlPolicy op_policy(void *opdata, ConnContext *context);
55
56void op_create_privkey(void *opdata, const char *accountname, const char *protocol);
57
58int op_is_logged_in(void *opdata, const char *accountname, const char *protocol,
59        const char *recipient);
60
61void op_inject_message(void *opdata, const char *accountname, const char *protocol,
62        const char *recipient, const char *message);
63
64void op_new_fingerprint(void *opdata, OtrlUserState us, const char *accountname,
65        const char *protocol, const char *username, unsigned char fingerprint[20]);
66
67void op_write_fingerprints(void *opdata);
68
69void op_gone_secure(void *opdata, ConnContext *context);
70
71void op_gone_insecure(void *opdata, ConnContext *context);
72
73void op_still_secure(void *opdata, ConnContext *context, int is_reply);
74
75void op_log_message(void *opdata, const char *message);
76
77int op_max_message_size(void *opdata, ConnContext *context);
78
79const char *op_account_name(void *opdata, const char *account, const char *protocol);
80
81void op_create_instag(void *opdata, const char *account, const char *protocol);
82
83void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ,
84        char **dst, const char *src);
85void op_convert_free(void *opdata, ConnContext *ctx, char *msg);
86
87void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx,
88        unsigned short percent, char *question);
89
90void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx,
91        const char *message, gcry_error_t err);
92
93const char *op_otr_error_message(void *opdata, ConnContext *ctx,
94        OtrlErrorCode err_code);
95
96/** otr sub-command handlers: **/
97
98static void cmd_otr(irc_t *irc, char **args);
99void cmd_otr_connect(irc_t *irc, char **args);
100void cmd_otr_disconnect(irc_t *irc, char **args);
101void cmd_otr_reconnect(irc_t *irc, char **args);
102void cmd_otr_smp(irc_t *irc, char **args);
103void cmd_otr_smpq(irc_t *irc, char **args);
104void cmd_otr_trust(irc_t *irc, char **args);
105void cmd_otr_info(irc_t *irc, char **args);
106void cmd_otr_keygen(irc_t *irc, char **args);
107void cmd_otr_forget(irc_t *irc, char **args);
108
109const command_t otr_commands[] = {
110        { "connect",     1, &cmd_otr_connect,    0 },
111        { "disconnect",  1, &cmd_otr_disconnect, 0 },
112        { "reconnect",   1, &cmd_otr_reconnect,  0 },
113        { "smp",         2, &cmd_otr_smp,        0 },
114        { "smpq",        3, &cmd_otr_smpq,       0 },
115        { "trust",       6, &cmd_otr_trust,      0 },
116        { "info",        0, &cmd_otr_info,       0 },
117        { "keygen",      1, &cmd_otr_keygen,     0 },
118        { "forget",      2, &cmd_otr_forget,     0 },
119        { NULL }
120};
121
122typedef struct {
123        void *fst;
124        void *snd;
125} pair_t;       
126
127static OtrlMessageAppOps otr_ops;   /* collects interface functions required by OTR */
128
129
130/** misc. helpers/subroutines: **/
131
132/* check whether we are already generating a key for a given account */
133int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol);
134
135/* start background process to generate a (new) key for a given account */
136void otr_keygen(irc_t *irc, const char *handle, const char *protocol);
137
138/* main function for the forked keygen slave */
139void keygen_child_main(OtrlUserState us, int infd, int outfd);
140
141/* mainloop handler for when a keygen finishes */
142gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond);
143
144/* copy the contents of file a to file b, overwriting it if it exists */
145void copyfile(const char *a, const char *b);
146
147/* read one line of input from a stream, excluding trailing newline */
148void myfgets(char *s, int size, FILE *stream);
149
150/* some yes/no handlers */
151void yes_keygen(void *data);
152void yes_forget_fingerprint(void *data);
153void yes_forget_context(void *data);
154void yes_forget_key(void *data);
155
156/* timeout handler that calls otrl_message_poll */
157gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond);
158
159/* helper to make sure accountname and protocol match the incoming "opdata" */
160struct im_connection *check_imc(void *opdata, const char *accountname,
161        const char *protocol);
162
163/* determine the nick for a given handle/protocol pair
164   returns "handle/protocol" if not found */
165const char *peernick(irc_t *irc, const char *handle, const char *protocol);
166
167/* turn a hexadecimal digit into its numerical value */
168int hexval(char a);
169
170/* determine the irc_user_t for a given handle/protocol pair
171   returns NULL if not found */
172irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol);
173
174/* show an otr-related message to the user */
175void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...);
176
177/* write an otr-related message to the system log */
178void log_otr_message(void *opdata, const char *fmt, ...);
179
180/* combined handler for the 'otr smp' and 'otr smpq' commands */
181void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question,
182                const char *secret);
183
184/* update flags within the irc_user structure to reflect OTR status of context */
185void otr_update_uflags(ConnContext *context, irc_user_t *u);
186
187/* update op/voice flag of given user according to encryption state and settings
188   returns 0 if neither op_buddies nor voice_buddies is set to "encrypted",
189   i.e. msgstate should be announced seperately */
190int otr_update_modeflags(irc_t *irc, irc_user_t *u);
191
192/* show general info about the OTR subsystem; called by 'otr info' */
193void show_general_otr_info(irc_t *irc);
194
195/* show info about a given OTR context */
196void show_otr_context_info(irc_t *irc, ConnContext *ctx);
197
198/* show the list of fingerprints associated with a given context */
199void show_fingerprints(irc_t *irc, ConnContext *ctx);
200
201/* find a fingerprint by prefix (given as any number of hex strings) */
202Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args);
203
204/* find a private key by fingerprint prefix (given as any number of hex strings) */
205OtrlPrivKey *match_privkey(irc_t *irc, const char **args);
206
207/* check whether a string is safe to use in a path component */
208int strsane(const char *s);
209
210/* functions to be called for certain events */
211static const struct irc_plugin otr_plugin;
212
213
214/*** routines declared in otr.h: ***/
215
216#ifdef OTR_BI
217#define init_plugin otr_init
218#endif
219
220void init_plugin(void)
221{
222        OTRL_INIT;
223       
224        /* fill global OtrlMessageAppOps */
225        otr_ops.policy = &op_policy;
226        otr_ops.create_privkey = &op_create_privkey;
227        otr_ops.is_logged_in = &op_is_logged_in;
228        otr_ops.inject_message = &op_inject_message;
229        otr_ops.update_context_list = NULL;
230        otr_ops.new_fingerprint = &op_new_fingerprint;
231        otr_ops.write_fingerprints = &op_write_fingerprints;
232        otr_ops.gone_secure = &op_gone_secure;
233        otr_ops.gone_insecure = &op_gone_insecure;
234        otr_ops.still_secure = &op_still_secure;
235        otr_ops.max_message_size = &op_max_message_size;
236        otr_ops.account_name = &op_account_name;
237        otr_ops.account_name_free = NULL;
238
239        /* stuff added with libotr 4.0.0 */
240        otr_ops.received_symkey = NULL;         /* we don't use the extra key */
241        otr_ops.otr_error_message = &op_otr_error_message;
242        otr_ops.otr_error_message_free = NULL;
243        otr_ops.resent_msg_prefix = NULL;       /* default: [resent] */
244        otr_ops.resent_msg_prefix_free = NULL;
245        otr_ops.handle_smp_event = &op_handle_smp_event;
246        otr_ops.handle_msg_event = &op_handle_msg_event;
247        otr_ops.create_instag = &op_create_instag;
248        otr_ops.convert_msg = &op_convert_msg;
249        otr_ops.convert_free = &op_convert_free;
250        otr_ops.timer_control = NULL;           /* we just poll */
251               
252        root_command_add( "otr", 1, cmd_otr, 0 );
253        register_irc_plugin( &otr_plugin );
254}
255
256gboolean otr_irc_new(irc_t *irc)
257{
258        set_t *s;
259        GSList *l;
260       
261        irc->otr = g_new0(otr_t, 1);
262        irc->otr->us = otrl_userstate_create();
263       
264        s = set_add( &irc->b->set, "otr_color_encrypted", "true", set_eval_bool, irc );
265       
266        s = set_add( &irc->b->set, "otr_policy", "opportunistic", set_eval_list, irc );
267        l = g_slist_prepend( NULL, "never" );
268        l = g_slist_prepend( l, "opportunistic" );
269        l = g_slist_prepend( l, "manual" );
270        l = g_slist_prepend( l, "always" );
271        s->eval_data = l;
272
273        s = set_add( &irc->b->set, "otr_does_html", "true", set_eval_bool, irc );
274       
275        /* regularly call otrl_message_poll */
276        gint definterval = otrl_message_poll_get_default_interval(irc->otr->us);
277        irc->otr->timer = b_timeout_add(definterval, ev_message_poll, irc->otr);
278
279        return TRUE;
280}
281
282void otr_irc_free(irc_t *irc)
283{
284        otr_t *otr = irc->otr;
285        b_event_remove(otr->timer);
286        otrl_userstate_free(otr->us);
287        if(otr->keygen) {
288                kill(otr->keygen, SIGTERM);
289                waitpid(otr->keygen, NULL, 0);
290                /* TODO: remove stale keygen tempfiles */
291        }
292        if(otr->to)
293                fclose(otr->to);
294        if(otr->from)
295                fclose(otr->from);
296        while(otr->todo) {
297                kg_t *p = otr->todo;
298                otr->todo = p->next;
299                g_free(p);
300        }
301        g_free(otr);
302}
303
304void otr_load(irc_t *irc)
305{
306        char s[512];
307        account_t *a;
308        gcry_error_t e;
309        gcry_error_t enoent = gcry_error_from_errno(ENOENT);
310        int kg=0;
311
312        if(strsane(irc->user->nick)) {
313                g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick);
314                e = otrl_privkey_read(irc->otr->us, s);
315                if(e && e!=enoent) {
316                        irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
317                }
318                g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
319                e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL);
320                if(e && e!=enoent) {
321                        irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
322                }
323                g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, irc->user->nick);
324                e = otrl_instag_read(irc->otr->us, s);
325                if(e && e!=enoent) {
326                        irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e));
327                }
328        }
329       
330        /* check for otr keys on all accounts */
331        for(a=irc->b->accounts; a; a=a->next) {
332                kg = otr_check_for_key(a) || kg;
333        }
334        if(kg) {
335                irc_rootmsg(irc, "Notice: "
336                        "The accounts above do not have OTR encryption keys associated with them, yet. "
337                        "These keys are now being generated in the background. "
338                        "You will be notified as they are completed. "
339                        "It is not necessary to wait; "
340                        "BitlBee can be used normally during key generation. "
341                        "You may safely ignore this message if you don't know what OTR is. ;)");
342        }
343}
344
345void otr_save(irc_t *irc)
346{
347        char s[512];
348        gcry_error_t e;
349
350        if(strsane(irc->user->nick)) {
351                g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick);
352                e = otrl_privkey_write_fingerprints(irc->otr->us, s);
353                if(e) {
354                        irc_rootmsg(irc, "otr save: %s: %s", s, gcry_strerror(e));
355                }
356                chmod(s, 0600);
357        }
358}
359
360void otr_remove(const char *nick)
361{
362        char s[512];
363       
364        if(strsane(nick)) {
365                g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick);
366                unlink(s);
367                g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick);
368                unlink(s);
369        }
370}
371
372void otr_rename(const char *onick, const char *nnick)
373{
374        char s[512], t[512];
375       
376        if(strsane(nnick) && strsane(onick)) {
377                g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick);
378                g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick);
379                rename(s,t);
380                g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick);
381                g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick);
382                rename(s,t);
383        }
384}
385
386int otr_check_for_key(account_t *a)
387{
388        irc_t *irc = a->bee->ui_data;
389        OtrlPrivKey *k;
390       
391        /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
392        if(a->prpl->options & OPT_NOOTR) {
393                return 0;
394        }
395       
396        k = otrl_privkey_find(irc->otr->us, a->user, a->prpl->name);
397        if(k) {
398                irc_rootmsg(irc, "otr: %s/%s ready", a->user, a->prpl->name);
399                return 0;
400        } if(keygen_in_progress(irc, a->user, a->prpl->name)) {
401                irc_rootmsg(irc, "otr: keygen for %s/%s already in progress", a->user, a->prpl->name);
402                return 0;
403        } else {
404                irc_rootmsg(irc, "otr: starting background keygen for %s/%s", a->user, a->prpl->name);
405                otr_keygen(irc, a->user, a->prpl->name);
406                return 1;
407        }
408}
409
410char *otr_filter_msg_in(irc_user_t *iu, char *msg, int flags)
411{
412        int ignore_msg;
413        char *newmsg = NULL;
414        OtrlTLV *tlvs = NULL;
415        irc_t *irc = iu->irc;
416        struct im_connection *ic = iu->bu->ic;
417       
418        /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
419        if(ic->acc->prpl->options & OPT_NOOTR) {
420                return msg;
421        }
422       
423        ignore_msg = otrl_message_receiving(irc->otr->us, &otr_ops, ic,
424                ic->acc->user, ic->acc->prpl->name, iu->bu->handle, msg, &newmsg,
425                &tlvs, NULL, NULL, NULL);
426
427        if(ignore_msg) {
428                /* this was an internal OTR protocol message */
429                return NULL;
430        } else if(!newmsg) {
431                /* this was a non-OTR message */
432                return msg;
433        } else {
434                /* we're done with the original msg, which will be caller-freed. */
435                return newmsg;
436        }
437}
438
439char *otr_filter_msg_out(irc_user_t *iu, char *msg, int flags)
440{       
441        int st;
442        char *otrmsg = NULL;
443        char *emsg = msg;           /* the message as we hand it to libotr */
444        ConnContext *ctx = NULL;
445        irc_t *irc = iu->irc;
446        struct im_connection *ic = iu->bu->ic;
447        otrl_instag_t instag = OTRL_INSTAG_RECENT; // XXX?
448
449        /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
450        if(ic->acc->prpl->options & OPT_NOOTR) {
451                return msg;
452        }
453
454        st = otrl_message_sending(irc->otr->us, &otr_ops, ic,
455                ic->acc->user, ic->acc->prpl->name, iu->bu->handle, instag,
456                emsg, NULL, &otrmsg, OTRL_FRAGMENT_SEND_SKIP, &ctx, NULL, NULL);
457        /* in libotr 4.0.0 with OTRL_FRAGMENT_SEND_ALL, otrmsg must be passed
458         * but the value it gets carries no meaning. it can be set even though
459         * the message has been injected.
460         * With OTRL_FRAGMENT_SEND_SKIP, libotr doesn't handle the sending, it's
461         * up to us.
462         */
463
464        if(emsg != msg) {
465                g_free(emsg);   /* we're done with this one */
466                emsg = NULL;
467        }
468        if(st) {
469                irc_rootmsg(irc, "Error encrypting text for OTR: %d", st);
470        }
471       
472        if (otrmsg != NULL) {
473                return otrmsg;
474        }
475        return emsg;
476}
477
478static const struct irc_plugin otr_plugin =
479{
480        otr_irc_new,
481        otr_irc_free,
482        otr_filter_msg_out,
483        otr_filter_msg_in,
484        otr_load,
485        otr_save,
486        otr_remove,
487};
488
489static void cmd_otr(irc_t *irc, char **args)
490{
491        const command_t *cmd;
492       
493        if(!args[0])
494                return;
495       
496        if(!args[1])
497                return;
498       
499        for(cmd=otr_commands; cmd->command; cmd++) {
500                if(strcmp(cmd->command, args[1]) == 0)
501                        break;
502        }
503       
504        if(!cmd->command) {
505                irc_rootmsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02",
506                        args[0], args[1]);
507                return;
508        }
509       
510        if(!args[cmd->required_parameters+1]) {
511                irc_rootmsg(irc, "%s %s: not enough arguments (%d req.)",
512                        args[0], args[1], cmd->required_parameters);
513                return;
514        }
515       
516        cmd->execute(irc, args+1);
517}
518
519
520/*** OTR "MessageAppOps" callbacks for global.otr_ui: ***/
521
522OtrlPolicy op_policy(void *opdata, ConnContext *context)
523{
524        struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol);
525        irc_t *irc = ic->bee->ui_data;
526        const char *p;
527       
528        /* policy override during keygen: if we're missing the key for context but are currently
529           generating it, then that's as much as we can do. => temporarily return NEVER. */
530        if(keygen_in_progress(irc, context->accountname, context->protocol) &&
531           !otrl_privkey_find(irc->otr->us, context->accountname, context->protocol))
532                return OTRL_POLICY_NEVER;
533
534        p = set_getstr(&ic->bee->set, "otr_policy");
535        if(!strcmp(p, "never"))
536                return OTRL_POLICY_NEVER;
537        if(!strcmp(p, "opportunistic"))
538                return OTRL_POLICY_OPPORTUNISTIC;
539        if(!strcmp(p, "manual"))
540                return OTRL_POLICY_MANUAL;
541        if(!strcmp(p, "always"))
542                return OTRL_POLICY_ALWAYS;
543       
544        return OTRL_POLICY_OPPORTUNISTIC;
545}
546
547void op_create_privkey(void *opdata, const char *accountname,
548        const char *protocol)
549{
550        struct im_connection *ic = check_imc(opdata, accountname, protocol);
551        irc_t *irc = ic->bee->ui_data;
552       
553        /* will fail silently if keygen already in progress */
554        otr_keygen(irc, accountname, protocol);
555}
556
557int op_is_logged_in(void *opdata, const char *accountname,
558        const char *protocol, const char *recipient)
559{
560        struct im_connection *ic = check_imc(opdata, accountname, protocol);
561        bee_user_t *bu;
562
563        /* lookup the irc_user_t for the given recipient */
564        bu = bee_user_by_handle(ic->bee, ic, recipient);
565        if(bu) {
566                if(bu->flags & BEE_USER_ONLINE)
567                        return 1;
568                else
569                        return 0;
570        } else {
571                return -1;
572        }
573}
574
575void op_inject_message(void *opdata, const char *accountname,
576        const char *protocol, const char *recipient, const char *message)
577{
578        struct im_connection *ic = check_imc(opdata, accountname, protocol);
579        irc_t *irc = ic->bee->ui_data;
580
581        if (strcmp(accountname, recipient) == 0) {
582                /* huh? injecting messages to myself? */
583                irc_rootmsg(irc, "note to self: %s", message);
584        } else {
585                /* need to drop some consts here :-( */
586                /* TODO: get flags into op_inject_message?! */
587                ic->acc->prpl->buddy_msg(ic, (char *)recipient, (char *)message, 0);
588                /* ignoring return value :-/ */
589        }
590}
591
592void op_new_fingerprint(void *opdata, OtrlUserState us,
593        const char *accountname, const char *protocol,
594        const char *username, unsigned char fingerprint[20])
595{
596        struct im_connection *ic = check_imc(opdata, accountname, protocol);
597        irc_t *irc = ic->bee->ui_data;
598        irc_user_t *u = peeruser(irc, username, protocol);
599        char hunam[45];         /* anybody looking? ;-) */
600       
601        otrl_privkey_hash_to_human(hunam, fingerprint);
602        if(u) {
603                irc_usernotice(u, "new fingerprint: %s", hunam);
604        } else {
605                /* this case shouldn't normally happen */
606                irc_rootmsg(irc, "new fingerprint for %s/%s: %s",
607                        username, protocol, hunam);
608        }
609}
610
611void op_write_fingerprints(void *opdata)
612{
613        struct im_connection *ic = (struct im_connection *)opdata;
614        irc_t *irc = ic->bee->ui_data;
615
616        otr_save(irc);
617}
618
619void op_gone_secure(void *opdata, ConnContext *context)
620{
621        struct im_connection *ic =
622                check_imc(opdata, context->accountname, context->protocol);
623        irc_user_t *u;
624        irc_t *irc = ic->bee->ui_data;
625
626        u = peeruser(irc, context->username, context->protocol);
627        if(!u) {
628                log_message(LOGLVL_ERROR,
629                        "BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!",
630                        context->username, context->protocol, context->accountname);
631                return;
632        }
633       
634        otr_update_uflags(context, u);
635        if(!otr_update_modeflags(irc, u)) {
636                char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
637                irc_usernotice(u, "conversation is now off the record (%s)", trust);
638        }
639}
640
641void op_gone_insecure(void *opdata, ConnContext *context)
642{
643        /* XXX on 'otr disconnect', this gets called for every instance and we
644         * get the message multiple times... */
645
646        struct im_connection *ic =
647                check_imc(opdata, context->accountname, context->protocol);
648        irc_t *irc = ic->bee->ui_data;
649        irc_user_t *u;
650
651        u = peeruser(irc, context->username, context->protocol);
652        if(!u) {
653                log_message(LOGLVL_ERROR,
654                        "BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!",
655                        context->username, context->protocol, context->accountname);
656                return;
657        }
658        otr_update_uflags(context, u);
659        if(!otr_update_modeflags(irc, u))
660                irc_usernotice(u, "conversation is now in cleartext");
661}
662
663void op_still_secure(void *opdata, ConnContext *context, int is_reply)
664{
665        struct im_connection *ic =
666                check_imc(opdata, context->accountname, context->protocol);
667        irc_t *irc = ic->bee->ui_data;
668        irc_user_t *u;
669
670        u = peeruser(irc, context->username, context->protocol);
671        if(!u) {
672                log_message(LOGLVL_ERROR,
673                        "BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!",
674                        context->username, context->protocol, context->accountname);
675                return;
676        }
677
678        otr_update_uflags(context, u);
679        if(!otr_update_modeflags(irc, u)) {
680                char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!";
681                irc_usernotice(u, "otr connection has been refreshed (%s)", trust);
682        }
683}
684
685int op_max_message_size(void *opdata, ConnContext *context)
686{
687        struct im_connection *ic =
688                check_imc(opdata, context->accountname, context->protocol);
689 
690        return ic->acc->prpl->mms;
691}
692
693const char *op_account_name(void *opdata, const char *account, const char *protocol)
694{
695        struct im_connection *ic = (struct im_connection *)opdata;
696        irc_t *irc = ic->bee->ui_data;
697
698        return peernick(irc, account, protocol);
699}
700
701void op_create_instag(void *opdata, const char *account, const char *protocol)
702{
703        struct im_connection *ic =
704                check_imc(opdata, account, protocol);
705        irc_t *irc = ic->bee->ui_data;
706        gcry_error_t e;
707        char s[512];
708 
709        g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir,
710                irc->user->nick);
711        e = otrl_instag_generate(irc->otr->us, s, account, protocol);
712        if(e) {
713                irc_rootmsg(irc, "otr: %s/%s: otrl_instag_generate failed: %s",
714                        account, protocol, gcry_strerror(e));
715        }
716}
717
718void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ,
719        char **dst, const char *src)
720{
721        struct im_connection *ic =
722                check_imc(opdata, ctx->accountname, ctx->protocol);
723        irc_t *irc = ic->bee->ui_data;
724        irc_user_t *iu = peeruser(irc, ctx->username, ctx->protocol);
725
726        if(typ == OTRL_CONVERT_RECEIVING) {
727                char *msg = g_strdup(src);
728
729                /* HTML decoding */
730                if(set_getbool(&ic->bee->set, "otr_does_html") &&
731                   !(ic->flags & OPT_DOES_HTML) &&
732                   set_getbool(&ic->bee->set, "strip_html")) {
733                        strip_html(msg);
734                        *dst = msg;
735                }
736
737                /* coloring */
738                if(set_getbool(&ic->bee->set, "otr_color_encrypted")) {
739                        int color;                /* color according to f'print trust */
740                        char *pre="", *sep="";    /* optional parts */
741                        const char *trust = ctx->active_fingerprint->trust;
742
743                        if(trust && trust[0] != '\0')
744                                color=3;   /* green */
745                        else
746                                color=5;   /* red */
747
748                        /* in a query window, keep "/me " uncolored at the beginning */
749                        if(g_strncasecmp(msg, "/me ", 4) == 0
750                           && irc_user_msgdest(iu) == irc->user->nick) {
751                                msg += 4;  /* skip */
752                                pre = "/me ";
753                        }
754
755                        /* comma in first place could mess with the color code */
756                        if(msg[0] == ',') {
757                            /* insert a space between color spec and message */
758                            sep = " ";
759                        }
760
761                        *dst = g_strdup_printf("%s\x03%.2d%s%s\x0F", pre,
762                                color, sep, msg);
763                        g_free(msg);
764                }
765        } else {
766                /* HTML encoding */
767                /* consider OTR plaintext to be HTML if otr_does_html is set */
768                if(ctx && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED &&
769                   set_getbool(&ic->bee->set, "otr_does_html") &&
770                   (g_strncasecmp(src, "<html>", 6) != 0)) {
771                        *dst = escape_html(src);
772                }
773        }
774}
775
776void op_convert_free(void *opdata, ConnContext *ctx, char *msg)
777{
778        g_free(msg);
779}
780       
781/* Socialist Millionaires' Protocol */
782void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx,
783        unsigned short percent, char *question)
784{
785        struct im_connection *ic =
786                check_imc(opdata, ctx->accountname, ctx->protocol);
787        irc_t *irc = ic->bee->ui_data;
788        OtrlUserState us = irc->otr->us;
789        irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol);
790
791        if(!u) return;
792
793        switch(ev) {
794        case OTRL_SMPEVENT_ASK_FOR_SECRET:
795                irc_rootmsg(irc, "smp: initiated by %s"
796                        " - respond with \x02otr smp %s <secret>\x02",
797                        u->nick, u->nick);
798                break;
799        case OTRL_SMPEVENT_ASK_FOR_ANSWER:
800                irc_rootmsg(irc, "smp: initiated by %s with question: \x02\"%s\"\x02", u->nick,
801                        question);
802                irc_rootmsg(irc, "smp: respond with \x02otr smp %s <answer>\x02",
803                        u->nick);
804                break;
805        case OTRL_SMPEVENT_CHEATED:
806                irc_rootmsg(irc, "smp %s: opponent violated protocol, aborting",
807                        u->nick);
808                otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx);
809                otrl_sm_state_free(ctx->smstate);
810                break;
811        case OTRL_SMPEVENT_NONE:
812                break;
813        case OTRL_SMPEVENT_IN_PROGRESS:
814                break;
815        case OTRL_SMPEVENT_SUCCESS:
816                if(ctx->smstate->received_question) {
817                        irc_rootmsg(irc, "smp %s: correct answer, you are trusted",
818                                u->nick);
819                } else {
820                        irc_rootmsg(irc, "smp %s: secrets proved equal, fingerprint trusted",
821                                u->nick);
822                }
823                otrl_sm_state_free(ctx->smstate);
824                break;
825        case OTRL_SMPEVENT_FAILURE:
826                if(ctx->smstate->received_question) {
827                        irc_rootmsg(irc, "smp %s: wrong answer, you are not trusted",
828                                u->nick);
829                } else {
830                        irc_rootmsg(irc, "smp %s: secrets did not match, fingerprint not trusted",
831                                u->nick);
832                }
833                otrl_sm_state_free(ctx->smstate);
834                break;
835        case OTRL_SMPEVENT_ABORT:
836                irc_rootmsg(irc, "smp: received abort from %s", u->nick);
837                otrl_sm_state_free(ctx->smstate);
838                break;
839        case OTRL_SMPEVENT_ERROR:
840                irc_rootmsg(irc, "smp %s: protocol error, aborting",
841                        u->nick);
842                otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx);
843                otrl_sm_state_free(ctx->smstate);
844                break;
845        }
846}
847
848void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx,
849        const char *message, gcry_error_t err)
850{
851        switch(ev) {
852        case OTRL_MSGEVENT_ENCRYPTION_REQUIRED:
853                display_otr_message(opdata, ctx,
854                        "policy requires encryption - message not sent");
855                break;
856        case OTRL_MSGEVENT_ENCRYPTION_ERROR:
857                display_otr_message(opdata, ctx,
858                        "error during encryption - message not sent");
859                break;
860        case OTRL_MSGEVENT_CONNECTION_ENDED:
861                display_otr_message(opdata, ctx,
862                        "other end has disconnected OTR - "
863                        "close connection or reconnect!");
864                break;
865        case OTRL_MSGEVENT_SETUP_ERROR:
866                display_otr_message(opdata, ctx,
867                        "OTR connection failed: %s", gcry_strerror(err));
868                break;
869        case OTRL_MSGEVENT_MSG_REFLECTED:
870                display_otr_message(opdata, ctx,
871                        "received our own OTR message (!?)");
872                break;
873        case OTRL_MSGEVENT_MSG_RESENT:
874                display_otr_message(opdata, ctx,
875                        "the previous message was resent");
876                break;
877        case OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE:
878                display_otr_message(opdata, ctx,
879                        "unexpected encrypted message received");
880                break;
881        case OTRL_MSGEVENT_RCVDMSG_UNREADABLE:
882                display_otr_message(opdata, ctx,
883                        "unreadable encrypted message received");
884                break;
885        case OTRL_MSGEVENT_RCVDMSG_MALFORMED:
886                display_otr_message(opdata, ctx,
887                        "malformed OTR message received");
888                break;
889        case OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD:
890                if(global.conf->verbose) {
891                        log_otr_message(opdata, "%s/%s: heartbeat received",
892                                ctx->accountname, ctx->protocol);
893                }
894                break;
895        case OTRL_MSGEVENT_LOG_HEARTBEAT_SENT:
896                if(global.conf->verbose) {
897                        log_otr_message(opdata, "%s/%s: heartbeat sent",
898                                ctx->accountname, ctx->protocol);
899                }
900                break;
901        case OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR:
902                display_otr_message(opdata, ctx,
903                        "OTR error message received: %s", message);
904                break;
905        case OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED:
906                display_otr_message(opdata, ctx,
907                        "unencrypted message received: %s", message);
908                break;
909        case OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED:
910                display_otr_message(opdata, ctx,
911                        "unrecognized OTR message received");
912                break;
913        case OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE:
914                display_otr_message(opdata, ctx,
915                        "OTR message for a different instance received");
916                break;
917        default:
918                /* shouldn't happen */
919                break;
920        }
921}
922
923const char *op_otr_error_message(void *opdata, ConnContext *ctx,
924        OtrlErrorCode err_code)
925{
926        switch(err_code) {
927        case OTRL_ERRCODE_ENCRYPTION_ERROR:
928                return "i failed to encrypt a message";
929        case OTRL_ERRCODE_MSG_NOT_IN_PRIVATE:
930                return "you sent an encrypted message i didn't expect";
931        case OTRL_ERRCODE_MSG_UNREADABLE:
932                return "could not read encrypted message";
933        case OTRL_ERRCODE_MSG_MALFORMED:
934                return "you sent a malformed OTR message";
935        default:
936                return "i suffered an unexpected OTR error";
937        }
938}
939
940
941
942/*** OTR sub-command handlers ***/
943
944void cmd_otr_reconnect(irc_t *irc, char **args)
945{
946        cmd_otr_disconnect(irc, args);
947        cmd_otr_connect(irc, args);
948}
949
950void cmd_otr_disconnect(irc_t *irc, char **args)
951{
952        irc_user_t *u;
953
954        u = irc_user_by_name(irc, args[1]);
955        if(!u || !u->bu || !u->bu->ic) {
956                irc_rootmsg(irc, "%s: unknown user", args[1]);
957                return;
958        }
959       
960        /* XXX we disconnect all instances; is that what we want? */
961        otrl_message_disconnect_all_instances(irc->otr->us, &otr_ops,
962                u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, u->bu->handle);
963       
964        /* for some reason, libotr (4.0.0) doesn't do this itself: */
965        if(!(u->flags & IRC_USER_OTR_ENCRYPTED))
966                return;
967
968        ConnContext *ctx, *p;
969        ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
970                u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
971        if(!ctx) { /* huh? */
972                u->flags &= ( IRC_USER_OTR_ENCRYPTED | IRC_USER_OTR_TRUSTED );
973                return;
974        }
975
976        for(p=ctx; p && p->m_context == ctx->m_context; p=p->next)
977                op_gone_insecure(u->bu->ic, p);
978}
979
980void cmd_otr_connect(irc_t *irc, char **args)
981{
982        irc_user_t *u;
983
984        u = irc_user_by_name(irc, args[1]);
985        if(!u || !u->bu || !u->bu->ic) {
986                irc_rootmsg(irc, "%s: unknown user", args[1]);
987                return;
988        }
989        if(!(u->bu->flags & BEE_USER_ONLINE)) {
990                irc_rootmsg(irc, "%s is offline", args[1]);
991                return;
992        }
993       
994        /* passing this through the filter so it goes through libotr which
995         * will replace the simple query string with a proper one */
996        otr_filter_msg_out(u, "?OTR?", 0);
997}
998
999void cmd_otr_smp(irc_t *irc, char **args)
1000{
1001        otr_smp_or_smpq(irc, args[1], NULL, args[2]);   /* no question */
1002}
1003
1004void cmd_otr_smpq(irc_t *irc, char **args)
1005{
1006        otr_smp_or_smpq(irc, args[1], args[2], args[3]);
1007}
1008
1009void cmd_otr_trust(irc_t *irc, char **args)
1010{
1011        irc_user_t *u;
1012        ConnContext *ctx;
1013        unsigned char raw[20];
1014        Fingerprint *fp;
1015        int i,j;
1016       
1017        u = irc_user_by_name(irc, args[1]);
1018        if(!u || !u->bu || !u->bu->ic) {
1019                irc_rootmsg(irc, "%s: unknown user", args[1]);
1020                return;
1021        }
1022       
1023        ctx = otrl_context_find(irc->otr->us, u->bu->handle,
1024                u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
1025        if(!ctx) {
1026                irc_rootmsg(irc, "%s: no otr context with user", args[1]);
1027                return;
1028        }
1029       
1030        /* convert given fingerprint to raw representation */
1031        for(i=0; i<5; i++) {
1032                for(j=0; j<4; j++) {
1033                        char *p = args[2+i]+(2*j);
1034                        char *q = p+1;
1035                        int x, y;
1036                       
1037                        if(!*p || !*q) {
1038                                irc_rootmsg(irc, "failed: truncated fingerprint block %d", i+1);
1039                                return;
1040                        }
1041                       
1042                        x = hexval(*p);
1043                        y = hexval(*q);
1044                        if(x<0) {
1045                                irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+1, i+1);
1046                                return;
1047                        }
1048                        if(y<0) {
1049                                irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+2, i+1);
1050                                return;
1051                        }
1052
1053                        raw[i*4+j] = x*16 + y;
1054                }
1055        }
1056        fp = otrl_context_find_fingerprint(ctx, raw, 0, NULL);
1057        if(!fp) {
1058                irc_rootmsg(irc, "failed: no such fingerprint for %s", args[1]);
1059        } else {
1060                char *trust = args[7] ? args[7] : "affirmed";
1061                otrl_context_set_trust(fp, trust);
1062                irc_rootmsg(irc, "fingerprint match, trust set to \"%s\"", trust);
1063                if(u->flags & IRC_USER_OTR_ENCRYPTED)
1064                        u->flags |= IRC_USER_OTR_TRUSTED;
1065                otr_update_modeflags(irc, u);
1066        }
1067}
1068
1069void cmd_otr_info(irc_t *irc, char **args)
1070{
1071        if(!args[1]) {
1072                show_general_otr_info(irc);
1073        } else {
1074                char *arg = g_strdup(args[1]);
1075                char *myhandle, *handle=NULL, *protocol;
1076                ConnContext *ctx;
1077               
1078                /* interpret arg as 'user/protocol/account' if possible */
1079                protocol = strchr(arg, '/');
1080                myhandle = NULL;
1081                if(protocol) {
1082                        *(protocol++) = '\0';
1083                        myhandle = strchr(protocol, '/');
1084                }
1085                if(protocol && myhandle) {
1086                        *(myhandle++) = '\0';
1087                        handle = arg;
1088                        ctx = otrl_context_find(irc->otr->us, handle, myhandle, protocol, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
1089                        if(!ctx) {
1090                                irc_rootmsg(irc, "no such context");
1091                                g_free(arg);
1092                                return;
1093                        }
1094                } else {
1095                        irc_user_t *u = irc_user_by_name(irc, args[1]);
1096                        if(!u || !u->bu || !u->bu->ic) {
1097                                irc_rootmsg(irc, "%s: unknown user", args[1]);
1098                                g_free(arg);
1099                                return;
1100                        }
1101                        ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
1102                                u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
1103                        if(!ctx) {
1104                                irc_rootmsg(irc, "no otr context with %s", args[1]);
1105                                g_free(arg);
1106                                return;
1107                        }
1108                }
1109       
1110                /* show how we resolved the (nick) argument, if we did */
1111                if(handle!=arg) {
1112                        irc_rootmsg(irc, "%s is %s/%s; we are %s/%s to them", args[1],
1113                                ctx->username, ctx->protocol, ctx->accountname, ctx->protocol);
1114                }
1115                show_otr_context_info(irc, ctx);
1116                g_free(arg);
1117        }
1118}
1119
1120void cmd_otr_keygen(irc_t *irc, char **args)
1121{
1122        int i, n;
1123        account_t *a;
1124       
1125        n = atoi(args[1]);
1126        if(n<0 || (!n && strcmp(args[1], "0"))) {
1127                irc_rootmsg(irc, "%s: invalid account number", args[1]);
1128                return;
1129        }
1130       
1131        a = irc->b->accounts;
1132        for(i=0; i<n && a; i++, a=a->next);
1133        if(!a) {
1134                irc_rootmsg(irc, "%s: no such account", args[1]);
1135                return;
1136        }
1137       
1138        if(keygen_in_progress(irc, a->user, a->prpl->name)) {
1139                irc_rootmsg(irc, "keygen for account %d already in progress", n);
1140                return;
1141        }
1142       
1143        if(otrl_privkey_find(irc->otr->us, a->user, a->prpl->name)) {
1144                char *s = g_strdup_printf("account %d already has a key, replace it?", n);
1145                query_add(irc, NULL, s, yes_keygen, NULL, NULL, a);
1146                g_free(s);
1147        } else {
1148                otr_keygen(irc, a->user, a->prpl->name);
1149        }
1150}
1151
1152void yes_forget_fingerprint(void *data)
1153{
1154        pair_t *p = (pair_t *)data;
1155        irc_t *irc = (irc_t *)p->fst;
1156        Fingerprint *fp = (Fingerprint *)p->snd;
1157
1158        g_free(p);
1159       
1160        if(fp == fp->context->active_fingerprint) {
1161                irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first");
1162                return;
1163        }
1164               
1165        otrl_context_forget_fingerprint(fp, 0);
1166}
1167
1168void yes_forget_context(void *data)
1169{
1170        pair_t *p = (pair_t *)data;
1171        irc_t *irc = (irc_t *)p->fst;
1172        ConnContext *ctx = (ConnContext *)p->snd;
1173
1174        g_free(p);
1175
1176        // XXX forget all contexts
1177       
1178        if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
1179                irc_rootmsg(irc, "active otr connection with %s, terminate it first",
1180                        peernick(irc, ctx->username, ctx->protocol));
1181                return;
1182        }
1183               
1184        if(ctx->msgstate == OTRL_MSGSTATE_FINISHED)
1185                otrl_context_force_plaintext(ctx);
1186        otrl_context_forget(ctx);
1187}
1188
1189void yes_forget_key(void *data)
1190{
1191        OtrlPrivKey *key = (OtrlPrivKey *)data;
1192       
1193        otrl_privkey_forget(key);
1194        /* Hm, libotr doesn't seem to offer a function for explicitly /writing/
1195           keyfiles. So the key will be back on the next load... */
1196        /* TODO: Actually erase forgotten keys from storage? */
1197}
1198
1199void cmd_otr_forget(irc_t *irc, char **args)
1200{
1201        if(!strcmp(args[1], "fingerprint"))
1202        {
1203                irc_user_t *u;
1204                ConnContext *ctx;
1205                Fingerprint *fp;
1206                char human[54];
1207                char *s;
1208                pair_t *p;
1209               
1210                if(!args[3]) {
1211                        irc_rootmsg(irc, "otr %s %s: not enough arguments (2 req.)", args[0], args[1]);
1212                        return;
1213                }
1214               
1215                /* TODO: allow context specs ("user/proto/account") in 'otr forget fingerprint'? */
1216                u = irc_user_by_name(irc, args[2]);
1217                if(!u || !u->bu || !u->bu->ic) {
1218                        irc_rootmsg(irc, "%s: unknown user", args[2]);
1219                        return;
1220                }
1221               
1222                ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
1223                        u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
1224                if(!ctx) {
1225                        irc_rootmsg(irc, "no otr context with %s", args[2]);
1226                        return;
1227                }
1228               
1229                fp = match_fingerprint(irc, ctx, ((const char **)args)+3);
1230                if(!fp) {
1231                        /* match_fingerprint does error messages */
1232                        return;
1233                }
1234               
1235                if(fp == ctx->active_fingerprint) {
1236                        irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first");
1237                        return;
1238                }
1239               
1240                otrl_privkey_hash_to_human(human, fp->fingerprint);
1241                s = g_strdup_printf("about to forget fingerprint %s, are you sure?", human);
1242                p = g_malloc(sizeof(pair_t));
1243                if(!p)
1244                        return;
1245                p->fst = irc;
1246                p->snd = fp;
1247                query_add(irc, NULL, s, yes_forget_fingerprint, NULL, NULL, p);
1248                g_free(s);
1249        }
1250       
1251        else if(!strcmp(args[1], "context"))
1252        {
1253                irc_user_t *u;
1254                ConnContext *ctx;
1255                char *s;
1256                pair_t *p;
1257               
1258                /* TODO: allow context specs ("user/proto/account") in 'otr forget contex'? */
1259                u = irc_user_by_name(irc, args[2]);
1260                if(!u || !u->bu || !u->bu->ic) {
1261                        irc_rootmsg(irc, "%s: unknown user", args[2]);
1262                        return;
1263                }
1264               
1265                ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user,
1266                        u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
1267                if(!ctx) {
1268                        irc_rootmsg(irc, "no otr context with %s", args[2]);
1269                        return;
1270                }
1271               
1272                if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
1273                        irc_rootmsg(irc, "active otr connection with %s, terminate it first", args[2]);
1274                        return;
1275                }
1276               
1277                s = g_strdup_printf("about to forget otr data about %s, are you sure?", args[2]);
1278                p = g_malloc(sizeof(pair_t));
1279                if(!p)
1280                        return;
1281                p->fst = irc;
1282                p->snd = ctx;
1283                query_add(irc, NULL, s, yes_forget_context, NULL, NULL, p);
1284                g_free(s);
1285        }
1286       
1287        else if(!strcmp(args[1], "key"))
1288        {
1289                OtrlPrivKey *key;
1290                char *s;
1291               
1292                key = match_privkey(irc, ((const char **)args)+2);
1293                if(!key) {
1294                        /* match_privkey does error messages */
1295                        return;
1296                }
1297               
1298                s = g_strdup_printf("about to forget the private key for %s/%s, are you sure?",
1299                        key->accountname, key->protocol);
1300                query_add(irc, NULL, s, yes_forget_key, NULL, NULL, key);
1301                g_free(s);
1302        }
1303       
1304        else
1305        {
1306                irc_rootmsg(irc, "otr %s: unknown subcommand \"%s\", see \x02help otr forget\x02",
1307                        args[0], args[1]);
1308        }
1309}
1310
1311
1312/*** local helpers / subroutines: ***/
1313
1314void log_otr_message(void *opdata, const char *fmt, ...)
1315{
1316        va_list va;
1317
1318        va_start(va, fmt);
1319        char *msg = g_strdup_vprintf(fmt, va);
1320        va_end(va);
1321       
1322        log_message(LOGLVL_INFO, "otr: %s", msg);
1323}
1324
1325void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...)
1326{
1327        struct im_connection *ic =
1328                check_imc(opdata, ctx->accountname, ctx->protocol);
1329        irc_t *irc = ic->bee->ui_data;
1330        irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol);
1331        va_list va;
1332
1333        va_start(va, fmt);
1334        char *msg = g_strdup_vprintf(fmt, va);
1335        va_end(va);
1336
1337        if(u) {
1338                /* display as a notice from this particular user */
1339                irc_usernotice(u, "%s", msg);
1340        } else {
1341                irc_rootmsg(irc, "[otr] %s", msg);
1342        }
1343
1344        g_free(msg);
1345}
1346
1347/* combined handler for the 'otr smp' and 'otr smpq' commands */
1348void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question,
1349                const char *secret)
1350{
1351        irc_user_t *u;
1352        ConnContext *ctx;
1353        otrl_instag_t instag = OTRL_INSTAG_RECENT;  // XXX
1354
1355        u = irc_user_by_name(irc, nick);
1356        if(!u || !u->bu || !u->bu->ic) {
1357                irc_rootmsg(irc, "%s: unknown user", nick);
1358                return;
1359        }
1360        if(!(u->bu->flags & BEE_USER_ONLINE)) {
1361                irc_rootmsg(irc, "%s is offline", nick);
1362                return;
1363        }
1364       
1365        ctx = otrl_context_find(irc->otr->us, u->bu->handle,
1366                u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, instag, 0, NULL, NULL, NULL);
1367        if(!ctx || ctx->msgstate != OTRL_MSGSTATE_ENCRYPTED) {
1368                irc_rootmsg(irc, "smp: otr inactive with %s, try \x02otr connect"
1369                                " %s\x02", nick, nick);
1370                return;
1371        }
1372
1373        if(ctx->smstate->nextExpected != OTRL_SMP_EXPECT1) {
1374                log_message(LOGLVL_INFO,
1375                        "SMP already in phase %d, sending abort before reinitiating",
1376                        ctx->smstate->nextExpected+1);
1377                otrl_message_abort_smp(irc->otr->us, &otr_ops, u->bu->ic, ctx);
1378                otrl_sm_state_free(ctx->smstate);
1379        }
1380
1381        if(question) {
1382                /* this was 'otr smpq', just initiate */
1383                irc_rootmsg(irc, "smp: initiating with %s...", u->nick);
1384                otrl_message_initiate_smp_q(irc->otr->us, &otr_ops, u->bu->ic, ctx,
1385                        question, (unsigned char *)secret, strlen(secret));
1386                /* smp is now in EXPECT2 */
1387        } else {
1388                /* this was 'otr smp', initiate or reply */
1389                /* warning: the following assumes that smstates are cleared whenever an SMP
1390                   is completed or aborted! */ 
1391                if(ctx->smstate->secret == NULL) {
1392                        irc_rootmsg(irc, "smp: initiating with %s...", u->nick);
1393                        otrl_message_initiate_smp(irc->otr->us, &otr_ops,
1394                                u->bu->ic, ctx, (unsigned char *)secret, strlen(secret));
1395                        /* smp is now in EXPECT2 */
1396                } else {
1397                        /* if we're still in EXPECT1 but smstate is initialized, we must have
1398                           received the SMP1, so let's issue a response */
1399                        irc_rootmsg(irc, "smp: responding to %s...", u->nick);
1400                        otrl_message_respond_smp(irc->otr->us, &otr_ops,
1401                                u->bu->ic, ctx, (unsigned char *)secret, strlen(secret));
1402                        /* smp is now in EXPECT3 */
1403                }
1404        }
1405}
1406
1407/* timeout handler that calls otrl_message_poll */
1408gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond)
1409{
1410        otr_t *otr = data;
1411
1412        if(otr && otr->us)
1413                otrl_message_poll(otr->us, &otr_ops, NULL);
1414
1415        return TRUE;    /* cycle timer */
1416}
1417
1418/* helper to assert that account and protocol names given to ops below always
1419   match the im_connection passed through as opdata */
1420struct im_connection *check_imc(void *opdata, const char *accountname,
1421        const char *protocol)
1422{
1423        struct im_connection *ic = (struct im_connection *)opdata;
1424
1425        /* libotr 4.0.0 has a bug where it doesn't set opdata, so we catch
1426         * that and try to find the desired connection in the global list. */
1427        if(!ic) {
1428                GSList *l;
1429                for(l=get_connections(); l; l=l->next) {
1430                        ic = l->data;
1431                        if(strcmp(accountname, ic->acc->user) == 0 &&
1432                           strcmp(protocol, ic->acc->prpl->name) == 0)
1433                                break;
1434                }
1435                assert(l != NULL);  /* a match should always be found */
1436                if(!l)
1437                        return NULL;
1438        }
1439
1440        if (strcmp(accountname, ic->acc->user) != 0) {
1441                log_message(LOGLVL_WARNING,
1442                        "otr: internal account name mismatch: '%s' vs '%s'",
1443                        accountname, ic->acc->user);
1444        }
1445        if (strcmp(protocol, ic->acc->prpl->name) != 0) {
1446                log_message(LOGLVL_WARNING,
1447                        "otr: internal protocol name mismatch: '%s' vs '%s'",
1448                        protocol, ic->acc->prpl->name);
1449        }
1450       
1451        return ic;
1452}
1453
1454irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol)
1455{
1456        GSList *l;
1457       
1458        for(l=irc->b->users; l; l = l->next) {
1459                bee_user_t *bu = l->data;
1460                struct prpl *prpl;
1461                if(!bu->ui_data || !bu->ic || !bu->handle)
1462                        continue;
1463                prpl = bu->ic->acc->prpl;
1464                if(strcmp(prpl->name, protocol) == 0
1465                        && prpl->handle_cmp(bu->handle, handle) == 0) {
1466                        return bu->ui_data;
1467                }
1468        }
1469       
1470        return NULL;
1471}
1472
1473int hexval(char a)
1474{
1475        int x=tolower(a);
1476       
1477        if(x>='a' && x<='f')
1478                x = x - 'a' + 10;
1479        else if(x>='0' && x<='9')
1480                x = x - '0';
1481        else
1482                return -1;
1483       
1484        return x;
1485}
1486
1487const char *peernick(irc_t *irc, const char *handle, const char *protocol)
1488{
1489        static char fallback[512];
1490       
1491        irc_user_t *u = peeruser(irc, handle, protocol);
1492        if(u) {
1493                return u->nick;
1494        } else {
1495                g_snprintf(fallback, 511, "%s/%s", handle, protocol);
1496                return fallback;
1497        }
1498}
1499
1500void otr_update_uflags(ConnContext *context, irc_user_t *u)
1501{
1502        const char *trust;
1503
1504        if(context->active_fingerprint) {
1505                u->flags |= IRC_USER_OTR_ENCRYPTED;
1506
1507                trust = context->active_fingerprint->trust;
1508                if(trust && trust[0])
1509                        u->flags |= IRC_USER_OTR_TRUSTED;
1510                else
1511                        u->flags &= ~IRC_USER_OTR_TRUSTED;
1512        } else {
1513                u->flags &= ~IRC_USER_OTR_ENCRYPTED;
1514        }
1515}
1516
1517int otr_update_modeflags(irc_t *irc, irc_user_t *u)
1518{
1519        return 0;
1520}
1521
1522void show_fingerprints(irc_t *irc, ConnContext *ctx)
1523{
1524        char human[45];
1525        Fingerprint *fp;
1526        const char *trust;
1527        int count=0;
1528       
1529        for(fp=&ctx->fingerprint_root; fp; fp=fp->next) {
1530                if(!fp->fingerprint)
1531                        continue;
1532                count++;
1533                otrl_privkey_hash_to_human(human, fp->fingerprint);
1534                if(!fp->trust || fp->trust[0] == '\0') {
1535                        trust="untrusted";
1536                } else {
1537                        trust=fp->trust;
1538                }
1539                if(fp == ctx->active_fingerprint) {
1540                        irc_rootmsg(irc, "    \x02%s (%s)\x02", human, trust);
1541                } else {
1542                        irc_rootmsg(irc, "    %s (%s)", human, trust);
1543                }
1544        }
1545        if(count==0)
1546                irc_rootmsg(irc, "    (none)");
1547}
1548
1549Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args)
1550{
1551        Fingerprint *fp, *fp2;
1552        char human[45];
1553        char prefix[45], *p;
1554        int n;
1555        int i,j;
1556       
1557        /* assemble the args into a prefix in standard "human" form */
1558        n=0;
1559        p=prefix;
1560        for(i=0; args[i]; i++) {
1561                for(j=0; args[i][j]; j++) {
1562                        char c = toupper(args[i][j]);
1563                       
1564                        if(n>=40) {
1565                                irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40");
1566                                return NULL;
1567                        }
1568                       
1569                        if( (c>='A' && c<='F') || (c>='0' && c<='9') ) {
1570                                *(p++) = c;
1571                        } else {
1572                                irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1);
1573                                return NULL;
1574                        }
1575                       
1576                        n++;
1577                        if(n%8 == 0)
1578                                *(p++) = ' ';
1579                }
1580        }
1581        *p = '\0';
1582       
1583        /* find first fingerprint with the given prefix */
1584        n = strlen(prefix);
1585        for(fp=&ctx->fingerprint_root; fp; fp=fp->next) {
1586                if(!fp->fingerprint)
1587                        continue;
1588                otrl_privkey_hash_to_human(human, fp->fingerprint);
1589                if(!strncmp(prefix, human, n))
1590                        break;
1591        }
1592        if(!fp) {
1593                irc_rootmsg(irc, "%s: no match", prefix);
1594                return NULL;
1595        }
1596       
1597        /* make sure the match, if any, is unique */
1598        for(fp2=fp->next; fp2; fp2=fp2->next) {
1599                if(!fp2->fingerprint)
1600                        continue;
1601                otrl_privkey_hash_to_human(human, fp2->fingerprint);
1602                if(!strncmp(prefix, human, n))
1603                        break;
1604        }
1605        if(fp2) {
1606                irc_rootmsg(irc, "%s: multiple matches", prefix);
1607                return NULL;
1608        }
1609       
1610        return fp;
1611}
1612
1613OtrlPrivKey *match_privkey(irc_t *irc, const char **args)
1614{
1615        OtrlPrivKey *k, *k2;
1616        char human[45];
1617        char prefix[45], *p;
1618        int n;
1619        int i,j;
1620       
1621        /* assemble the args into a prefix in standard "human" form */
1622        n=0;
1623        p=prefix;
1624        for(i=0; args[i]; i++) {
1625                for(j=0; args[i][j]; j++) {
1626                        char c = toupper(args[i][j]);
1627                       
1628                        if(n>=40) {
1629                                irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40");
1630                                return NULL;
1631                        }
1632                       
1633                        if( (c>='A' && c<='F') || (c>='0' && c<='9') ) {
1634                                *(p++) = c;
1635                        } else {
1636                                irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1);
1637                                return NULL;
1638                        }
1639                       
1640                        n++;
1641                        if(n%8 == 0)
1642                                *(p++) = ' ';
1643                }
1644        }
1645        *p = '\0';
1646       
1647        /* find first key which matches the given prefix */
1648        n = strlen(prefix);
1649        for(k=irc->otr->us->privkey_root; k; k=k->next) {
1650                p = otrl_privkey_fingerprint(irc->otr->us, human, k->accountname, k->protocol);
1651                if(!p) /* gah! :-P */
1652                        continue;
1653                if(!strncmp(prefix, human, n))
1654                        break;
1655        }
1656        if(!k) {
1657                irc_rootmsg(irc, "%s: no match", prefix);
1658                return NULL;
1659        }
1660       
1661        /* make sure the match, if any, is unique */
1662        for(k2=k->next; k2; k2=k2->next) {
1663                p = otrl_privkey_fingerprint(irc->otr->us, human, k2->accountname, k2->protocol);
1664                if(!p) /* gah! :-P */
1665                        continue;
1666                if(!strncmp(prefix, human, n))
1667                        break;
1668        }
1669        if(k2) {
1670                irc_rootmsg(irc, "%s: multiple matches", prefix);
1671                return NULL;
1672        }
1673       
1674        return k;
1675}
1676
1677void show_general_otr_info(irc_t *irc)
1678{
1679        ConnContext *ctx;
1680        OtrlPrivKey *key;
1681        char human[45];
1682        kg_t *kg;
1683
1684        /* list all privkeys (including ones being generated) */
1685        irc_rootmsg(irc, "\x1fprivate keys:\x1f");
1686        for(key=irc->otr->us->privkey_root; key; key=key->next) {
1687                const char *hash;
1688               
1689                switch(key->pubkey_type) {
1690                case OTRL_PUBKEY_TYPE_DSA:
1691                        irc_rootmsg(irc, "  %s/%s - DSA", key->accountname, key->protocol);
1692                        break;
1693                default:
1694                        irc_rootmsg(irc, "  %s/%s - type %d", key->accountname, key->protocol,
1695                                key->pubkey_type);
1696                }
1697
1698                /* No, it doesn't make much sense to search for the privkey again by
1699                   account/protocol, but libotr currently doesn't provide a direct routine
1700                   for hashing a given 'OtrlPrivKey'... */
1701                hash = otrl_privkey_fingerprint(irc->otr->us, human, key->accountname, key->protocol);
1702                if(hash) /* should always succeed */
1703                        irc_rootmsg(irc, "    %s", human);
1704        }
1705        if(irc->otr->sent_accountname) {
1706                irc_rootmsg(irc, "  %s/%s - DSA", irc->otr->sent_accountname,
1707                        irc->otr->sent_protocol);
1708                irc_rootmsg(irc, "    (being generated)");
1709        }
1710        for(kg=irc->otr->todo; kg; kg=kg->next) {
1711                irc_rootmsg(irc, "  %s/%s - DSA", kg->accountname, kg->protocol);
1712                irc_rootmsg(irc, "    (queued)");
1713        }
1714        if(key == irc->otr->us->privkey_root &&
1715           !irc->otr->sent_accountname &&
1716           kg == irc->otr->todo)
1717                irc_rootmsg(irc, "  (none)");
1718
1719        /* list all contexts */
1720        /* XXX remove this, or split off as its own command */
1721        /* XXX show instags? */
1722        irc_rootmsg(irc, "%s", "");
1723        irc_rootmsg(irc, "\x1f" "connection contexts:\x1f (bold=currently encrypted)");
1724        for(ctx=irc->otr->us->context_root; ctx; ctx=ctx->next) {\
1725                irc_user_t *u;
1726                char *userstring;
1727               
1728                u = peeruser(irc, ctx->username, ctx->protocol);
1729                if(u)
1730                        userstring = g_strdup_printf("%s/%s/%s (%s)",
1731                                ctx->username, ctx->protocol, ctx->accountname, u->nick);
1732                else
1733                        userstring = g_strdup_printf("%s/%s/%s",
1734                                ctx->username, ctx->protocol, ctx->accountname);
1735               
1736                if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
1737                        irc_rootmsg(irc, \x02%s\x02", userstring);
1738                } else {
1739                        irc_rootmsg(irc, "  %s", userstring);
1740                }
1741               
1742                g_free(userstring);
1743        }
1744        if(ctx == irc->otr->us->context_root)
1745                irc_rootmsg(irc, "  (none)");
1746}
1747
1748void show_otr_context_info(irc_t *irc, ConnContext *ctx)
1749{
1750        // XXX show all instags/subcontexts
1751
1752        switch(ctx->otr_offer) {
1753        case OFFER_NOT:
1754                irc_rootmsg(irc, "  otr offer status: none sent");
1755                break;
1756        case OFFER_SENT:
1757                irc_rootmsg(irc, "  otr offer status: awaiting reply");
1758                break;
1759        case OFFER_ACCEPTED:
1760                irc_rootmsg(irc, "  otr offer status: accepted our offer");
1761                break;
1762        case OFFER_REJECTED:
1763                irc_rootmsg(irc, "  otr offer status: ignored our offer");
1764                break;
1765        default:
1766                irc_rootmsg(irc, "  otr offer status: %d", ctx->otr_offer);
1767        }
1768
1769        switch(ctx->msgstate) {
1770        case OTRL_MSGSTATE_PLAINTEXT:
1771                irc_rootmsg(irc, "  connection state: cleartext");
1772                break;
1773        case OTRL_MSGSTATE_ENCRYPTED:
1774                irc_rootmsg(irc, "  connection state: encrypted (v%d)", ctx->protocol_version);
1775                break;
1776        case OTRL_MSGSTATE_FINISHED:
1777                irc_rootmsg(irc, "  connection state: shut down");
1778                break;
1779        default:
1780                irc_rootmsg(irc, "  connection state: %d", ctx->msgstate);
1781        }
1782
1783        irc_rootmsg(irc, "  fingerprints: (bold=active)");     
1784        show_fingerprints(irc, ctx);
1785}
1786
1787int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol)
1788{
1789        kg_t *kg;
1790       
1791        if(!irc->otr->sent_accountname || !irc->otr->sent_protocol)
1792                return 0;
1793
1794        /* are we currently working on this key? */
1795        if(!strcmp(handle, irc->otr->sent_accountname) &&
1796           !strcmp(protocol, irc->otr->sent_protocol))
1797                return 1;
1798       
1799        /* do we have it queued for later? */
1800        for(kg=irc->otr->todo; kg; kg=kg->next) {
1801                if(!strcmp(handle, kg->accountname) &&
1802                   !strcmp(protocol, kg->protocol))
1803                        return 1;
1804        }
1805       
1806        return 0;
1807}
1808
1809void otr_keygen(irc_t *irc, const char *handle, const char *protocol)
1810{
1811        /* do nothing if a key for the requested account is already being generated */
1812        if(keygen_in_progress(irc, handle, protocol))
1813                return;
1814
1815        /* see if we already have a keygen child running. if not, start one and put a
1816           handler on its output. */
1817        if(!irc->otr->keygen || waitpid(irc->otr->keygen, NULL, WNOHANG)) {
1818                pid_t p;
1819                int to[2], from[2];
1820                FILE *tof, *fromf;
1821               
1822                if(pipe(to) < 0 || pipe(from) < 0) {
1823                        irc_rootmsg(irc, "otr keygen: couldn't create pipe: %s", strerror(errno));
1824                        return;
1825                }
1826               
1827                tof = fdopen(to[1], "w");
1828                fromf = fdopen(from[0], "r");
1829                if(!tof || !fromf) {
1830                        irc_rootmsg(irc, "otr keygen: couldn't streamify pipe: %s", strerror(errno));
1831                        return;
1832                }
1833               
1834                p = fork();
1835                if(p<0) {
1836                        irc_rootmsg(irc, "otr keygen: couldn't fork: %s", strerror(errno));
1837                        return;
1838                }
1839               
1840                if(!p) {
1841                        /* child process */
1842                        signal(SIGTERM, exit);
1843                        keygen_child_main(irc->otr->us, to[0], from[1]);
1844                        exit(0);
1845                }
1846               
1847                irc->otr->keygen = p;
1848                irc->otr->to = tof;
1849                irc->otr->from = fromf;
1850                irc->otr->sent_accountname = NULL;
1851                irc->otr->sent_protocol = NULL;
1852                irc->otr->todo = NULL;
1853                b_input_add(from[0], B_EV_IO_READ, keygen_finish_handler, irc);
1854        }
1855       
1856        /* is the keygen slave currently working? */
1857        if(irc->otr->sent_accountname) {
1858                /* enqueue our job for later transmission */
1859                kg_t **kg = &irc->otr->todo;
1860                while(*kg)
1861                        kg=&((*kg)->next);
1862                *kg = g_new0(kg_t, 1);
1863                (*kg)->accountname = g_strdup(handle);
1864                (*kg)->protocol = g_strdup(protocol);
1865        } else {
1866                /* send our job over and remember it */
1867                fprintf(irc->otr->to, "%s\n%s\n", handle, protocol);
1868                fflush(irc->otr->to);
1869                irc->otr->sent_accountname = g_strdup(handle);
1870                irc->otr->sent_protocol = g_strdup(protocol);
1871        }
1872}
1873
1874void keygen_child_main(OtrlUserState us, int infd, int outfd)
1875{
1876        FILE *input, *output;
1877        char filename[128], accountname[512], protocol[512];
1878        gcry_error_t e;
1879        int tempfd;
1880       
1881        input = fdopen(infd, "r");
1882        output = fdopen(outfd, "w");
1883       
1884        while(!feof(input) && !ferror(input) && !feof(output) && !ferror(output)) {
1885                myfgets(accountname, 512, input);
1886                myfgets(protocol, 512, input);
1887               
1888                strncpy(filename, "/tmp/bitlbee-XXXXXX", 128);
1889                tempfd = mkstemp(filename);
1890                close(tempfd);
1891
1892                e = otrl_privkey_generate(us, filename, accountname, protocol);
1893                if(e) {
1894                        fprintf(output, "\n");  /* this means failure */
1895                        fprintf(output, "otr keygen: %s\n", gcry_strerror(e));
1896                        unlink(filename);
1897                } else {
1898                        fprintf(output, "%s\n", filename);
1899                        fprintf(output, "otr keygen for %s/%s complete\n", accountname, protocol);
1900                }
1901                fflush(output);
1902        }
1903       
1904        fclose(input);
1905        fclose(output);
1906}
1907
1908gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond)
1909{
1910        irc_t *irc = (irc_t *)data;
1911        char filename[512], msg[512];
1912
1913        myfgets(filename, 512, irc->otr->from);
1914        myfgets(msg, 512, irc->otr->from);
1915       
1916        irc_rootmsg(irc, "%s", msg);
1917        if(filename[0]) {
1918                if(strsane(irc->user->nick)) {
1919                        char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick);
1920                        char *tmp = g_strdup_printf("%s.new", kf);
1921                        copyfile(filename, tmp);
1922                        unlink(filename);
1923                        rename(tmp,kf);
1924                        otrl_privkey_read(irc->otr->us, kf);
1925                        g_free(kf);
1926                        g_free(tmp);
1927                } else {
1928                        otrl_privkey_read(irc->otr->us, filename);
1929                        unlink(filename);
1930                }
1931        }
1932       
1933        /* forget this job */
1934        g_free(irc->otr->sent_accountname);
1935        g_free(irc->otr->sent_protocol);
1936        irc->otr->sent_accountname = NULL;
1937        irc->otr->sent_protocol = NULL;
1938       
1939        /* see if there are any more in the queue */
1940        if(irc->otr->todo) {
1941                kg_t *p = irc->otr->todo;
1942                /* send the next one over */
1943                fprintf(irc->otr->to, "%s\n%s\n", p->accountname, p->protocol);
1944                fflush(irc->otr->to);
1945                irc->otr->sent_accountname = p->accountname;
1946                irc->otr->sent_protocol = p->protocol;
1947                irc->otr->todo = p->next;
1948                g_free(p);
1949                return TRUE;   /* keep watching */
1950        } else {
1951                /* okay, the slave is idle now, so kill him */
1952                fclose(irc->otr->from);
1953                fclose(irc->otr->to);
1954                irc->otr->from = irc->otr->to = NULL;
1955                kill(irc->otr->keygen, SIGTERM);
1956                waitpid(irc->otr->keygen, NULL, 0);
1957                irc->otr->keygen = 0;
1958                return FALSE;  /* unregister ourselves */
1959        }
1960}
1961
1962void copyfile(const char *a, const char *b)
1963{
1964        int fda, fdb;
1965        int n;
1966        char buf[1024];
1967       
1968        fda = open(a, O_RDONLY);
1969        fdb = open(b, O_WRONLY | O_CREAT | O_TRUNC, 0600);
1970       
1971        while((n=read(fda, buf, 1024)) > 0)
1972                write(fdb, buf, n);
1973       
1974        close(fda);
1975        close(fdb);     
1976}
1977
1978void myfgets(char *s, int size, FILE *stream)
1979{
1980        if(!fgets(s, size, stream)) {
1981                s[0] = '\0';
1982        } else {
1983                int n = strlen(s);
1984                if(n>0 && s[n-1] == '\n')
1985                        s[n-1] = '\0';
1986        }
1987}
1988
1989void yes_keygen(void *data)
1990{
1991        account_t *acc = (account_t *)data;
1992        irc_t *irc = acc->bee->ui_data;
1993       
1994        if(keygen_in_progress(irc, acc->user, acc->prpl->name)) {
1995                irc_rootmsg(irc, "keygen for %s/%s already in progress",
1996                        acc->user, acc->prpl->name);
1997        } else {
1998                irc_rootmsg(irc, "starting background keygen for %s/%s",
1999                        acc->user, acc->prpl->name);
2000                irc_rootmsg(irc, "you will be notified when it completes");
2001                otr_keygen(irc, acc->user, acc->prpl->name);
2002        }
2003}
2004
2005/* check whether a string is safe to use in a path component */
2006int strsane(const char *s)
2007{
2008        return strpbrk(s, "/\\") == NULL;
2009}
2010
2011/* vim: set noet ts=4 sw=4: */
Note: See TracBrowser for help on using the repository browser.