source: otr.c @ fbcb481

Last change on this file since fbcb481 was fbcb481, checked in by unknown <pesco@…>, at 2013-08-02T21:59:43Z

do 'otr connect' with the proper query message (generated by libotr)

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