source: otr.c @ 81a15da

Last change on this file since 81a15da was 098a75b, checked in by dequis <dx@…>, at 2015-03-22T13:35:08Z

Fix a bunch of memory leaks

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