source: otr.c @ d3b99b6

Last change on this file since d3b99b6 was 47ab9a9, checked in by dequis <dx@…>, at 2015-11-27T23:42:00Z

misc.c: Add a str_reject_chars function, use it in otr_filter_colors

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