source: otr.c @ 9cc653c

Last change on this file since 9cc653c was 9cc653c, checked in by Sven Moritz Hallberg <pesco@…>, at 2010-09-19T19:12:49Z

let 'otr smpq' only work for smp init, not reply

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