From 5ebff60479fc7a9f7f50ac03b124c91d4e6ebe11 Mon Sep 17 00:00:00 2001 From: Indent Date: Thu, 19 Feb 2015 02:47:20 -0300 Subject: Reindent everything to K&R style with tabs Used uncrustify, with the configuration file in ./doc/uncrustify.cfg Commit author set to "Indent " so that it's easier to skip while doing git blame. --- otr.c | 1036 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 542 insertions(+), 494 deletions(-) (limited to 'otr.c') diff --git a/otr.c b/otr.c index 71b70f19..cadb4842 100644 --- a/otr.c +++ b/otr.c @@ -1,4 +1,4 @@ - /********************************************************************\ +/********************************************************************\ * BitlBee -- An IRC to other IM-networks gateway * * * * Copyright 2002-2012 Wilmer van der Gaast and others * @@ -6,15 +6,15 @@ /* OTR support (cf. http://www.cypherpunks.ca/otr/) - + (c) 2008-2011,2013 Sven Moritz Hallberg funded by stonedcoder.org - + files used to store OTR data: /.otr_keys /.otr_fprints /.otr_instags <- don't copy this one between hosts - + top-level todos: (search for TODO for more ;-)) integrate otr_load/otr_save with existing storage backends per-account policy settings @@ -56,13 +56,13 @@ OtrlPolicy op_policy(void *opdata, ConnContext *context); void op_create_privkey(void *opdata, const char *accountname, const char *protocol); int op_is_logged_in(void *opdata, const char *accountname, const char *protocol, - const char *recipient); + const char *recipient); void op_inject_message(void *opdata, const char *accountname, const char *protocol, - const char *recipient, const char *message); + const char *recipient, const char *message); void op_new_fingerprint(void *opdata, OtrlUserState us, const char *accountname, - const char *protocol, const char *username, unsigned char fingerprint[20]); + const char *protocol, const char *username, unsigned char fingerprint[20]); void op_write_fingerprints(void *opdata); @@ -81,17 +81,17 @@ const char *op_account_name(void *opdata, const char *account, const char *proto void op_create_instag(void *opdata, const char *account, const char *protocol); void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ, - char **dst, const char *src); + char **dst, const char *src); void op_convert_free(void *opdata, ConnContext *ctx, char *msg); void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, - unsigned short percent, char *question); + unsigned short percent, char *question); void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx, - const char *message, gcry_error_t err); + const char *message, gcry_error_t err); const char *op_otr_error_message(void *opdata, ConnContext *ctx, - OtrlErrorCode err_code); + OtrlErrorCode err_code); /** otr sub-command handlers: **/ @@ -122,7 +122,7 @@ const command_t otr_commands[] = { typedef struct { void *fst; void *snd; -} pair_t; +} pair_t; static OtrlMessageAppOps otr_ops; /* collects interface functions required by OTR */ @@ -158,7 +158,7 @@ gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond); /* helper to make sure accountname and protocol match the incoming "opdata" */ struct im_connection *check_imc(void *opdata, const char *accountname, - const char *protocol); + const char *protocol); /* determine the nick for a given handle/protocol pair returns "handle/protocol" if not found */ @@ -179,7 +179,7 @@ void log_otr_message(void *opdata, const char *fmt, ...); /* combined handler for the 'otr smp' and 'otr smpq' commands */ void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question, - const char *secret); + const char *secret); /* update flags within the irc_user structure to reflect OTR status of context */ void otr_update_uflags(ConnContext *context, irc_user_t *u); @@ -225,7 +225,7 @@ static const struct irc_plugin otr_plugin; void init_plugin(void) { OTRL_INIT; - + /* fill global OtrlMessageAppOps */ otr_ops.policy = &op_policy; otr_ops.create_privkey = &op_create_privkey; @@ -252,31 +252,31 @@ void init_plugin(void) otr_ops.create_instag = &op_create_instag; otr_ops.convert_msg = &op_convert_msg; otr_ops.convert_free = &op_convert_free; - otr_ops.timer_control = NULL; /* we just poll */ - - root_command_add( "otr", 1, cmd_otr, 0 ); - register_irc_plugin( &otr_plugin ); + otr_ops.timer_control = NULL; /* we just poll */ + + root_command_add("otr", 1, cmd_otr, 0); + register_irc_plugin(&otr_plugin); } gboolean otr_irc_new(irc_t *irc) { set_t *s; GSList *l; - + irc->otr = g_new0(otr_t, 1); irc->otr->us = otrl_userstate_create(); - - s = set_add( &irc->b->set, "otr_color_encrypted", "true", set_eval_bool, irc ); - - s = set_add( &irc->b->set, "otr_policy", "opportunistic", set_eval_list, irc ); - l = g_slist_prepend( NULL, "never" ); - l = g_slist_prepend( l, "opportunistic" ); - l = g_slist_prepend( l, "manual" ); - l = g_slist_prepend( l, "always" ); + + s = set_add(&irc->b->set, "otr_color_encrypted", "true", set_eval_bool, irc); + + s = set_add(&irc->b->set, "otr_policy", "opportunistic", set_eval_list, irc); + l = g_slist_prepend(NULL, "never"); + l = g_slist_prepend(l, "opportunistic"); + l = g_slist_prepend(l, "manual"); + l = g_slist_prepend(l, "always"); s->eval_data = l; - s = set_add( &irc->b->set, "otr_does_html", "true", set_eval_bool, irc ); - + s = set_add(&irc->b->set, "otr_does_html", "true", set_eval_bool, irc); + /* regularly call otrl_message_poll */ gint definterval = otrl_message_poll_get_default_interval(irc->otr->us); irc->otr->timer = b_timeout_add(definterval, ev_message_poll, irc->otr); @@ -287,19 +287,22 @@ gboolean otr_irc_new(irc_t *irc) void otr_irc_free(irc_t *irc) { otr_t *otr = irc->otr; + otr_disconnect_all(irc); b_event_remove(otr->timer); otrl_userstate_free(otr->us); - if(otr->keygen) { + if (otr->keygen) { kill(otr->keygen, SIGTERM); waitpid(otr->keygen, NULL, 0); /* TODO: remove stale keygen tempfiles */ } - if(otr->to) + if (otr->to) { fclose(otr->to); - if(otr->from) + } + if (otr->from) { fclose(otr->from); - while(otr->todo) { + } + while (otr->todo) { kg_t *p = otr->todo; otr->todo = p->next; g_free(p); @@ -313,38 +316,38 @@ void otr_load(irc_t *irc) account_t *a; gcry_error_t e; gcry_error_t enoent = gcry_error_from_errno(ENOENT); - int kg=0; + int kg = 0; - if(strsane(irc->user->nick)) { + if (strsane(irc->user->nick)) { g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, irc->user->nick); e = otrl_privkey_read(irc->otr->us, s); - if(e && e!=enoent) { + if (e && e != enoent) { irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); } g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick); e = otrl_privkey_read_fingerprints(irc->otr->us, s, NULL, NULL); - if(e && e!=enoent) { + if (e && e != enoent) { irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); } g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, irc->user->nick); e = otrl_instag_read(irc->otr->us, s); - if(e && e!=enoent) { + if (e && e != enoent) { irc_rootmsg(irc, "otr load: %s: %s", s, gcry_strerror(e)); } } - + /* check for otr keys on all accounts */ - for(a=irc->b->accounts; a; a=a->next) { + for (a = irc->b->accounts; a; a = a->next) { kg = otr_check_for_key(a) || kg; } - if(kg) { + if (kg) { irc_rootmsg(irc, "Notice: " - "The accounts above do not have OTR encryption keys associated with them, yet. " - "These keys are now being generated in the background. " - "You will be notified as they are completed. " - "It is not necessary to wait; " - "BitlBee can be used normally during key generation. " - "You may safely ignore this message if you don't know what OTR is. ;)"); + "The accounts above do not have OTR encryption keys associated with them, yet. " + "These keys are now being generated in the background. " + "You will be notified as they are completed. " + "It is not necessary to wait; " + "BitlBee can be used normally during key generation. " + "You may safely ignore this message if you don't know what OTR is. ;)"); } } @@ -353,10 +356,10 @@ void otr_save(irc_t *irc) char s[512]; gcry_error_t e; - if(strsane(irc->user->nick)) { + if (strsane(irc->user->nick)) { g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, irc->user->nick); e = otrl_privkey_write_fingerprints(irc->otr->us, s); - if(e) { + if (e) { irc_rootmsg(irc, "otr save: %s: %s", s, gcry_strerror(e)); } chmod(s, 0600); @@ -366,8 +369,8 @@ void otr_save(irc_t *irc) void otr_remove(const char *nick) { char s[512]; - - if(strsane(nick)) { + + if (strsane(nick)) { g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, nick); unlink(s); g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, nick); @@ -378,14 +381,14 @@ void otr_remove(const char *nick) void otr_rename(const char *onick, const char *nnick) { char s[512], t[512]; - - if(strsane(nnick) && strsane(onick)) { + + if (strsane(nnick) && strsane(onick)) { g_snprintf(s, 511, "%s%s.otr_keys", global.conf->configdir, onick); g_snprintf(t, 511, "%s%s.otr_keys", global.conf->configdir, nnick); - rename(s,t); + rename(s, t); g_snprintf(s, 511, "%s%s.otr_fprints", global.conf->configdir, onick); g_snprintf(t, 511, "%s%s.otr_fprints", global.conf->configdir, nnick); - rename(s,t); + rename(s, t); } } @@ -393,17 +396,18 @@ int otr_check_for_key(account_t *a) { irc_t *irc = a->bee->ui_data; OtrlPrivKey *k; - + /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ - if(a->prpl->options & OPT_NOOTR) { + if (a->prpl->options & OPT_NOOTR) { return 0; } - + k = otrl_privkey_find(irc->otr->us, a->user, a->prpl->name); - if(k) { + if (k) { irc_rootmsg(irc, "otr: %s/%s ready", a->user, a->prpl->name); return 0; - } if(keygen_in_progress(irc, a->user, a->prpl->name)) { + } + if (keygen_in_progress(irc, a->user, a->prpl->name)) { irc_rootmsg(irc, "otr: keygen for %s/%s already in progress", a->user, a->prpl->name); return 0; } else { @@ -420,20 +424,20 @@ char *otr_filter_msg_in(irc_user_t *iu, char *msg, int flags) OtrlTLV *tlvs = NULL; irc_t *irc = iu->irc; struct im_connection *ic = iu->bu->ic; - + /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ - if(ic->acc->prpl->options & OPT_NOOTR) { + if (ic->acc->prpl->options & OPT_NOOTR) { return msg; } - + ignore_msg = otrl_message_receiving(irc->otr->us, &otr_ops, ic, - ic->acc->user, ic->acc->prpl->name, iu->bu->handle, msg, &newmsg, - &tlvs, NULL, NULL, NULL); + ic->acc->user, ic->acc->prpl->name, iu->bu->handle, msg, &newmsg, + &tlvs, NULL, NULL, NULL); - if(ignore_msg) { + if (ignore_msg) { /* this was an internal OTR protocol message */ return NULL; - } else if(!newmsg) { + } else if (!newmsg) { /* this was a non-OTR message */ return msg; } else { @@ -443,36 +447,37 @@ char *otr_filter_msg_in(irc_user_t *iu, char *msg, int flags) } char *otr_filter_msg_out(irc_user_t *iu, char *msg, int flags) -{ +{ int st; char *otrmsg = NULL; ConnContext *ctx = NULL; irc_t *irc = iu->irc; struct im_connection *ic = iu->bu->ic; otrl_instag_t instag = OTRL_INSTAG_BEST; // XXX? + /* NB: in libotr 4.0.0 OTRL_INSTAG_RECENT will cause a null-pointer deref * in otrl_message_sending with newly-added OTR contexts. */ /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */ - if(ic->acc->prpl->options & OPT_NOOTR) { + if (ic->acc->prpl->options & OPT_NOOTR) { return msg; } st = otrl_message_sending(irc->otr->us, &otr_ops, ic, - ic->acc->user, ic->acc->prpl->name, iu->bu->handle, instag, - msg, NULL, &otrmsg, OTRL_FRAGMENT_SEND_ALL_BUT_LAST, &ctx, NULL, NULL); + ic->acc->user, ic->acc->prpl->name, iu->bu->handle, instag, + msg, NULL, &otrmsg, OTRL_FRAGMENT_SEND_ALL_BUT_LAST, &ctx, NULL, NULL); - if(otrmsg && otrmsg != msg) { + if (otrmsg && otrmsg != msg) { /* libotr wants us to replace our message */ /* NB: caller will free old msg */ msg = g_strdup(otrmsg); otrl_message_free(otrmsg); } - - if(st) { + + if (st) { irc_usernotice(iu, "otr: error handling outgoing message: %d", st); - msg = NULL; /* do not send plaintext! */ + msg = NULL; /* do not send plaintext! */ } return msg; @@ -492,31 +497,34 @@ static const struct irc_plugin otr_plugin = static void cmd_otr(irc_t *irc, char **args) { const command_t *cmd; - - if(!args[0]) + + if (!args[0]) { return; - - if(!args[1]) + } + + if (!args[1]) { return; - - for(cmd=otr_commands; cmd->command; cmd++) { - if(strcmp(cmd->command, args[1]) == 0) + } + + for (cmd = otr_commands; cmd->command; cmd++) { + if (strcmp(cmd->command, args[1]) == 0) { break; + } } - - if(!cmd->command) { + + if (!cmd->command) { irc_rootmsg(irc, "%s: unknown subcommand \"%s\", see \x02help otr\x02", - args[0], args[1]); + args[0], args[1]); return; } - - if(!args[cmd->required_parameters+1]) { + + if (!args[cmd->required_parameters + 1]) { irc_rootmsg(irc, "%s %s: not enough arguments (%d req.)", - args[0], args[1], cmd->required_parameters); + args[0], args[1], cmd->required_parameters); return; } - - cmd->execute(irc, args+1); + + cmd->execute(irc, args + 1); } @@ -527,56 +535,62 @@ OtrlPolicy op_policy(void *opdata, ConnContext *context) struct im_connection *ic = check_imc(opdata, context->accountname, context->protocol); irc_t *irc = ic->bee->ui_data; const char *p; - + /* policy override during keygen: if we're missing the key for context but are currently generating it, then that's as much as we can do. => temporarily return NEVER. */ - if(keygen_in_progress(irc, context->accountname, context->protocol) && - !otrl_privkey_find(irc->otr->us, context->accountname, context->protocol)) + if (keygen_in_progress(irc, context->accountname, context->protocol) && + !otrl_privkey_find(irc->otr->us, context->accountname, context->protocol)) { return OTRL_POLICY_NEVER; + } p = set_getstr(&ic->bee->set, "otr_policy"); - if(!strcmp(p, "never")) + if (!strcmp(p, "never")) { return OTRL_POLICY_NEVER; - if(!strcmp(p, "opportunistic")) + } + if (!strcmp(p, "opportunistic")) { return OTRL_POLICY_OPPORTUNISTIC; - if(!strcmp(p, "manual")) + } + if (!strcmp(p, "manual")) { return OTRL_POLICY_MANUAL; - if(!strcmp(p, "always")) + } + if (!strcmp(p, "always")) { return OTRL_POLICY_ALWAYS; - + } + return OTRL_POLICY_OPPORTUNISTIC; } void op_create_privkey(void *opdata, const char *accountname, - const char *protocol) + const char *protocol) { struct im_connection *ic = check_imc(opdata, accountname, protocol); irc_t *irc = ic->bee->ui_data; - + /* will fail silently if keygen already in progress */ otr_keygen(irc, accountname, protocol); } int op_is_logged_in(void *opdata, const char *accountname, - const char *protocol, const char *recipient) + const char *protocol, const char *recipient) { struct im_connection *ic = check_imc(opdata, accountname, protocol); bee_user_t *bu; /* lookup the irc_user_t for the given recipient */ bu = bee_user_by_handle(ic->bee, ic, recipient); - if(bu) { - if(bu->flags & BEE_USER_ONLINE) + if (bu) { + if (bu->flags & BEE_USER_ONLINE) { return 1; - else + } else { return 0; + } } else { return -1; } } void op_inject_message(void *opdata, const char *accountname, - const char *protocol, const char *recipient, const char *message) + const char *protocol, const char *recipient, const char *message) { struct im_connection *ic = check_imc(opdata, accountname, protocol); irc_t *irc = ic->bee->ui_data; @@ -587,33 +601,33 @@ void op_inject_message(void *opdata, const char *accountname, } else { /* need to drop some consts here :-( */ /* TODO: get flags into op_inject_message?! */ - ic->acc->prpl->buddy_msg(ic, (char *)recipient, (char *)message, 0); + ic->acc->prpl->buddy_msg(ic, (char *) recipient, (char *) message, 0); /* ignoring return value :-/ */ } } void op_new_fingerprint(void *opdata, OtrlUserState us, - const char *accountname, const char *protocol, - const char *username, unsigned char fingerprint[20]) + const char *accountname, const char *protocol, + const char *username, unsigned char fingerprint[20]) { struct im_connection *ic = check_imc(opdata, accountname, protocol); irc_t *irc = ic->bee->ui_data; irc_user_t *u = peeruser(irc, username, protocol); - char hunam[45]; /* anybody looking? ;-) */ - + char hunam[45]; /* anybody looking? ;-) */ + otrl_privkey_hash_to_human(hunam, fingerprint); - if(u) { + if (u) { irc_usernotice(u, "new fingerprint: %s", hunam); } else { /* this case shouldn't normally happen */ irc_rootmsg(irc, "new fingerprint for %s/%s: %s", - username, protocol, hunam); + username, protocol, hunam); } } void op_write_fingerprints(void *opdata) { - struct im_connection *ic = (struct im_connection *)opdata; + struct im_connection *ic = (struct im_connection *) opdata; irc_t *irc = ic->bee->ui_data; otr_save(irc); @@ -622,20 +636,20 @@ void op_write_fingerprints(void *opdata) void op_gone_secure(void *opdata, ConnContext *context) { struct im_connection *ic = - check_imc(opdata, context->accountname, context->protocol); + check_imc(opdata, context->accountname, context->protocol); irc_user_t *u; irc_t *irc = ic->bee->ui_data; u = peeruser(irc, context->username, context->protocol); - if(!u) { + if (!u) { log_message(LOGLVL_ERROR, - "BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!", - context->username, context->protocol, context->accountname); + "BUG: otr.c: op_gone_secure: irc_user_t for %s/%s/%s not found!", + context->username, context->protocol, context->accountname); return; } - + otr_update_uflags(context, u); - if(!otr_update_modeflags(irc, u)) { + if (!otr_update_modeflags(irc, u)) { char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!"; irc_usernotice(u, "conversation is now off the record (%s)", trust); } @@ -644,39 +658,40 @@ void op_gone_secure(void *opdata, ConnContext *context) void op_gone_insecure(void *opdata, ConnContext *context) { struct im_connection *ic = - check_imc(opdata, context->accountname, context->protocol); + check_imc(opdata, context->accountname, context->protocol); irc_t *irc = ic->bee->ui_data; irc_user_t *u; u = peeruser(irc, context->username, context->protocol); - if(!u) { + if (!u) { log_message(LOGLVL_ERROR, - "BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!", - context->username, context->protocol, context->accountname); + "BUG: otr.c: op_gone_insecure: irc_user_t for %s/%s/%s not found!", + context->username, context->protocol, context->accountname); return; } otr_update_uflags(context, u); - if(!otr_update_modeflags(irc, u)) + if (!otr_update_modeflags(irc, u)) { irc_usernotice(u, "conversation is now in cleartext"); + } } void op_still_secure(void *opdata, ConnContext *context, int is_reply) { struct im_connection *ic = - check_imc(opdata, context->accountname, context->protocol); + check_imc(opdata, context->accountname, context->protocol); irc_t *irc = ic->bee->ui_data; irc_user_t *u; u = peeruser(irc, context->username, context->protocol); - if(!u) { + if (!u) { log_message(LOGLVL_ERROR, - "BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!", - context->username, context->protocol, context->accountname); + "BUG: otr.c: op_still_secure: irc_user_t for %s/%s/%s not found!", + context->username, context->protocol, context->accountname); return; } otr_update_uflags(context, u); - if(!otr_update_modeflags(irc, u)) { + if (!otr_update_modeflags(irc, u)) { char *trust = u->flags & IRC_USER_OTR_TRUSTED ? "trusted" : "untrusted!"; irc_usernotice(u, "otr connection has been refreshed (%s)", trust); } @@ -685,14 +700,14 @@ void op_still_secure(void *opdata, ConnContext *context, int is_reply) int op_max_message_size(void *opdata, ConnContext *context) { struct im_connection *ic = - check_imc(opdata, context->accountname, context->protocol); - + check_imc(opdata, context->accountname, context->protocol); + return ic->acc->prpl->mms; } const char *op_account_name(void *opdata, const char *account, const char *protocol) { - struct im_connection *ic = (struct im_connection *)opdata; + struct im_connection *ic = (struct im_connection *) opdata; irc_t *irc = ic->bee->ui_data; return peernick(irc, account, protocol); @@ -701,74 +716,75 @@ const char *op_account_name(void *opdata, const char *account, const char *proto void op_create_instag(void *opdata, const char *account, const char *protocol) { struct im_connection *ic = - check_imc(opdata, account, protocol); + check_imc(opdata, account, protocol); irc_t *irc = ic->bee->ui_data; gcry_error_t e; char s[512]; - + g_snprintf(s, 511, "%s%s.otr_instags", global.conf->configdir, - irc->user->nick); + irc->user->nick); e = otrl_instag_generate(irc->otr->us, s, account, protocol); - if(e) { + if (e) { irc_rootmsg(irc, "otr: %s/%s: otrl_instag_generate failed: %s", - account, protocol, gcry_strerror(e)); + account, protocol, gcry_strerror(e)); } } void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ, - char **dst, const char *src) + char **dst, const char *src) { struct im_connection *ic = - check_imc(opdata, ctx->accountname, ctx->protocol); + check_imc(opdata, ctx->accountname, ctx->protocol); irc_t *irc = ic->bee->ui_data; irc_user_t *iu = peeruser(irc, ctx->username, ctx->protocol); - if(typ == OTRL_CONVERT_RECEIVING) { + if (typ == OTRL_CONVERT_RECEIVING) { char *msg = g_strdup(src); char *buf = msg; /* HTML decoding */ - if(set_getbool(&ic->bee->set, "otr_does_html") && - !(ic->flags & OPT_DOES_HTML) && - set_getbool(&ic->bee->set, "strip_html")) { + if (set_getbool(&ic->bee->set, "otr_does_html") && + !(ic->flags & OPT_DOES_HTML) && + set_getbool(&ic->bee->set, "strip_html")) { strip_html(msg); *dst = msg; } /* coloring */ - if(set_getbool(&ic->bee->set, "otr_color_encrypted")) { + if (set_getbool(&ic->bee->set, "otr_color_encrypted")) { int color; /* color according to f'print trust */ - char *pre="", *sep=""; /* optional parts */ + char *pre = "", *sep = ""; /* optional parts */ const char *trust = ctx->active_fingerprint->trust; - if(trust && trust[0] != '\0') - color=3; /* green */ - else - color=5; /* red */ + if (trust && trust[0] != '\0') { + color = 3; /* green */ + } else { + color = 5; /* red */ + } /* in a query window, keep "/me " uncolored at the beginning */ - if(g_strncasecmp(msg, "/me ", 4) == 0 - && irc_user_msgdest(iu) == irc->user->nick) { + if (g_strncasecmp(msg, "/me ", 4) == 0 + && irc_user_msgdest(iu) == irc->user->nick) { msg += 4; /* skip */ pre = "/me "; } /* comma in first place could mess with the color code */ - if(msg[0] == ',') { - /* insert a space between color spec and message */ - sep = " "; + if (msg[0] == ',') { + /* insert a space between color spec and message */ + sep = " "; } *dst = g_strdup_printf("%s\x03%.2d%s%s\x0F", pre, - color, sep, msg); + color, sep, msg); g_free(buf); } } else { /* HTML encoding */ /* consider OTR plaintext to be HTML if otr_does_html is set */ - if(ctx && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED && - set_getbool(&ic->bee->set, "otr_does_html") && - (g_strncasecmp(src, "", 6) != 0)) { + if (ctx && ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED && + set_getbool(&ic->bee->set, "otr_does_html") && + (g_strncasecmp(src, "", 6) != 0)) { *dst = escape_html(src); } } @@ -778,34 +794,36 @@ void op_convert_free(void *opdata, ConnContext *ctx, char *msg) { g_free(msg); } - + /* Socialist Millionaires' Protocol */ void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, - unsigned short percent, char *question) + unsigned short percent, char *question) { struct im_connection *ic = - check_imc(opdata, ctx->accountname, ctx->protocol); + check_imc(opdata, ctx->accountname, ctx->protocol); irc_t *irc = ic->bee->ui_data; OtrlUserState us = irc->otr->us; irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol); - if(!u) return; + if (!u) { + return; + } - switch(ev) { + switch (ev) { case OTRL_SMPEVENT_ASK_FOR_SECRET: irc_rootmsg(irc, "smp: initiated by %s" - " - respond with \x02otr smp %s \x02", - u->nick, u->nick); + " - respond with \x02otr smp %s \x02", + u->nick, u->nick); break; case OTRL_SMPEVENT_ASK_FOR_ANSWER: irc_rootmsg(irc, "smp: initiated by %s with question: \x02\"%s\"\x02", u->nick, - question); + question); irc_rootmsg(irc, "smp: respond with \x02otr smp %s \x02", - u->nick); + u->nick); break; case OTRL_SMPEVENT_CHEATED: irc_rootmsg(irc, "smp %s: opponent violated protocol, aborting", - u->nick); + u->nick); otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx); otrl_sm_state_free(ctx->smstate); break; @@ -814,32 +832,32 @@ void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, case OTRL_SMPEVENT_IN_PROGRESS: break; case OTRL_SMPEVENT_SUCCESS: - if(ctx->smstate->received_question) { + if (ctx->smstate->received_question) { irc_rootmsg(irc, "smp %s: correct answer, you are trusted", - u->nick); + u->nick); } else { irc_rootmsg(irc, "smp %s: secrets proved equal, fingerprint trusted", - u->nick); + u->nick); } otrl_sm_state_free(ctx->smstate); break; case OTRL_SMPEVENT_FAILURE: - if(ctx->smstate->received_question) { + if (ctx->smstate->received_question) { irc_rootmsg(irc, "smp %s: wrong answer, you are not trusted", - u->nick); + u->nick); } else { irc_rootmsg(irc, "smp %s: secrets did not match, fingerprint not trusted", - u->nick); + u->nick); } otrl_sm_state_free(ctx->smstate); break; case OTRL_SMPEVENT_ABORT: - irc_rootmsg(irc, "smp: received abort from %s", u->nick); + irc_rootmsg(irc, "smp: received abort from %s", u->nick); otrl_sm_state_free(ctx->smstate); break; case OTRL_SMPEVENT_ERROR: irc_rootmsg(irc, "smp %s: protocol error, aborting", - u->nick); + u->nick); otrl_message_abort_smp(us, &otr_ops, u->bu->ic, ctx); otrl_sm_state_free(ctx->smstate); break; @@ -847,73 +865,73 @@ void op_handle_smp_event(void *opdata, OtrlSMPEvent ev, ConnContext *ctx, } void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx, - const char *message, gcry_error_t err) + const char *message, gcry_error_t err) { - switch(ev) { + switch (ev) { case OTRL_MSGEVENT_ENCRYPTION_REQUIRED: display_otr_message(opdata, ctx, - "policy requires encryption - message not sent"); + "policy requires encryption - message not sent"); break; case OTRL_MSGEVENT_ENCRYPTION_ERROR: display_otr_message(opdata, ctx, - "error during encryption - message not sent"); + "error during encryption - message not sent"); break; case OTRL_MSGEVENT_CONNECTION_ENDED: display_otr_message(opdata, ctx, - "other end has disconnected OTR - " - "close connection or reconnect!"); + "other end has disconnected OTR - " + "close connection or reconnect!"); break; case OTRL_MSGEVENT_SETUP_ERROR: display_otr_message(opdata, ctx, - "OTR connection failed: %s", gcry_strerror(err)); + "OTR connection failed: %s", gcry_strerror(err)); break; case OTRL_MSGEVENT_MSG_REFLECTED: display_otr_message(opdata, ctx, - "received our own OTR message (!?)"); + "received our own OTR message (!?)"); break; case OTRL_MSGEVENT_MSG_RESENT: display_otr_message(opdata, ctx, - "the previous message was resent"); + "the previous message was resent"); break; case OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE: display_otr_message(opdata, ctx, - "unexpected encrypted message received"); + "unexpected encrypted message received"); break; case OTRL_MSGEVENT_RCVDMSG_UNREADABLE: display_otr_message(opdata, ctx, - "unreadable encrypted message received"); + "unreadable encrypted message received"); break; case OTRL_MSGEVENT_RCVDMSG_MALFORMED: display_otr_message(opdata, ctx, - "malformed OTR message received"); + "malformed OTR message received"); break; case OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD: - if(global.conf->verbose) { + if (global.conf->verbose) { log_otr_message(opdata, "%s/%s: heartbeat received", - ctx->accountname, ctx->protocol); + ctx->accountname, ctx->protocol); } break; case OTRL_MSGEVENT_LOG_HEARTBEAT_SENT: - if(global.conf->verbose) { + if (global.conf->verbose) { log_otr_message(opdata, "%s/%s: heartbeat sent", - ctx->accountname, ctx->protocol); + ctx->accountname, ctx->protocol); } break; case OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR: display_otr_message(opdata, ctx, - "OTR error message received: %s", message); + "OTR error message received: %s", message); break; case OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED: display_otr_message(opdata, ctx, - "unencrypted message received: %s", message); + "unencrypted message received: %s", message); break; case OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED: display_otr_message(opdata, ctx, - "unrecognized OTR message received"); + "unrecognized OTR message received"); break; case OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE: display_otr_message(opdata, ctx, - "OTR message for a different instance received"); + "OTR message for a different instance received"); break; default: /* shouldn't happen */ @@ -922,9 +940,9 @@ void op_handle_msg_event(void *opdata, OtrlMessageEvent ev, ConnContext *ctx, } const char *op_otr_error_message(void *opdata, ConnContext *ctx, - OtrlErrorCode err_code) + OtrlErrorCode err_code) { - switch(err_code) { + switch (err_code) { case OTRL_ERRCODE_ENCRYPTION_ERROR: return "i failed to encrypt a message"; case OTRL_ERRCODE_MSG_NOT_IN_PRIVATE: @@ -952,15 +970,16 @@ void cmd_otr_disconnect(irc_t *irc, char **args) { irc_user_t *u; - if(!strcmp("*", args[1])) { + if (!strcmp("*", args[1])) { otr_disconnect_all(irc); irc_rootmsg(irc, "all conversations are now in cleartext"); } else { u = irc_user_by_name(irc, args[1]); - if(otr_disconnect_user(irc, u)) + if (otr_disconnect_user(irc, u)) { irc_usernotice(u, "conversation is now in cleartext"); - else + } else { irc_rootmsg(irc, "%s: unknown user", args[1]); + } } } @@ -970,32 +989,33 @@ void cmd_otr_connect(irc_t *irc, char **args) char *msg, *query = "?OTR?"; u = irc_user_by_name(irc, args[1]); - if(!u || !u->bu || !u->bu->ic) { + if (!u || !u->bu || !u->bu->ic) { irc_rootmsg(irc, "%s: unknown user", args[1]); return; } - if(!(u->bu->flags & BEE_USER_ONLINE)) { + if (!(u->bu->flags & BEE_USER_ONLINE)) { irc_rootmsg(irc, "%s is offline", args[1]); return; } - + /* passing this through the filter so it goes through libotr which * will replace the simple query string with a proper one */ msg = otr_filter_msg_out(u, query, 0); /* send the message */ - if(msg) { + if (msg) { u->bu->ic->acc->prpl->buddy_msg(u->bu->ic, u->bu->handle, msg, 0); /* XXX flags? */ /* XXX error message? */ - if(msg != query) + if (msg != query) { g_free(msg); + } } } void cmd_otr_smp(irc_t *irc, char **args) { - otr_smp_or_smpq(irc, args[1], NULL, args[2]); /* no question */ + otr_smp_or_smpq(irc, args[1], NULL, args[2]); /* no question */ } void cmd_otr_smpq(irc_t *irc, char **args) @@ -1009,105 +1029,108 @@ void cmd_otr_trust(irc_t *irc, char **args) ConnContext *ctx; unsigned char raw[20]; Fingerprint *fp; - int i,j; - + int i, j; + u = irc_user_by_name(irc, args[1]); - if(!u || !u->bu || !u->bu->ic) { + if (!u || !u->bu || !u->bu->ic) { irc_rootmsg(irc, "%s: unknown user", args[1]); return; } - + ctx = otrl_context_find(irc->otr->us, u->bu->handle, - u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); - if(!ctx) { + u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, + NULL); + if (!ctx) { irc_rootmsg(irc, "%s: no otr context with user", args[1]); return; } - + /* convert given fingerprint to raw representation */ - for(i=0; i<5; i++) { - for(j=0; j<4; j++) { - char *p = args[2+i]+(2*j); - char *q = p+1; + for (i = 0; i < 5; i++) { + for (j = 0; j < 4; j++) { + char *p = args[2 + i] + (2 * j); + char *q = p + 1; int x, y; - - if(!*p || !*q) { - irc_rootmsg(irc, "failed: truncated fingerprint block %d", i+1); + + if (!*p || !*q) { + irc_rootmsg(irc, "failed: truncated fingerprint block %d", i + 1); return; } - + x = hexval(*p); y = hexval(*q); - if(x<0) { - irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+1, i+1); + if (x < 0) { + irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2 * j + 1, i + 1); return; } - if(y<0) { - irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2*j+2, i+1); + if (y < 0) { + irc_rootmsg(irc, "failed: %d. hex digit of block %d out of range", 2 * j + 2, i + 1); return; } - raw[i*4+j] = x*16 + y; + raw[i * 4 + j] = x * 16 + y; } } fp = otrl_context_find_fingerprint(ctx, raw, 0, NULL); - if(!fp) { + if (!fp) { irc_rootmsg(irc, "failed: no such fingerprint for %s", args[1]); } else { char *trust = args[7] ? args[7] : "affirmed"; otrl_context_set_trust(fp, trust); irc_rootmsg(irc, "fingerprint match, trust set to \"%s\"", trust); - if(u->flags & IRC_USER_OTR_ENCRYPTED) + if (u->flags & IRC_USER_OTR_ENCRYPTED) { u->flags |= IRC_USER_OTR_TRUSTED; + } otr_update_modeflags(irc, u); } } void cmd_otr_info(irc_t *irc, char **args) { - if(!args[1]) { + if (!args[1]) { show_general_otr_info(irc); } else { char *arg = g_strdup(args[1]); - char *myhandle, *handle=NULL, *protocol; + char *myhandle, *handle = NULL, *protocol; ConnContext *ctx; - + /* interpret arg as 'user/protocol/account' if possible */ protocol = strchr(arg, '/'); myhandle = NULL; - if(protocol) { + if (protocol) { *(protocol++) = '\0'; myhandle = strchr(protocol, '/'); } - if(protocol && myhandle) { + if (protocol && myhandle) { *(myhandle++) = '\0'; handle = arg; - ctx = otrl_context_find(irc->otr->us, handle, myhandle, protocol, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); - if(!ctx) { + ctx = otrl_context_find(irc->otr->us, handle, myhandle, protocol, OTRL_INSTAG_MASTER, 0, NULL, + NULL, NULL); + if (!ctx) { irc_rootmsg(irc, "no such context"); g_free(arg); return; } } else { irc_user_t *u = irc_user_by_name(irc, args[1]); - if(!u || !u->bu || !u->bu->ic) { + if (!u || !u->bu || !u->bu->ic) { irc_rootmsg(irc, "%s: unknown user", args[1]); g_free(arg); return; } ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, - u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); - if(!ctx) { + u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); + if (!ctx) { irc_rootmsg(irc, "no otr context with %s", args[1]); g_free(arg); return; } } - + /* show how we resolved the (nick) argument, if we did */ - if(handle!=arg) { + if (handle != arg) { irc_rootmsg(irc, "%s is %s/%s; we are %s/%s to them", args[1], - ctx->username, ctx->protocol, ctx->accountname, ctx->protocol); + ctx->username, ctx->protocol, ctx->accountname, ctx->protocol); } show_otr_context_info(irc, ctx); g_free(arg); @@ -1117,18 +1140,18 @@ void cmd_otr_info(irc_t *irc, char **args) void cmd_otr_keygen(irc_t *irc, char **args) { account_t *a; - + if ((a = account_get(irc->b, args[1])) == NULL) { irc_rootmsg(irc, "Could not find account `%s'.", args[1]); return; } - - if(keygen_in_progress(irc, a->user, a->prpl->name)) { + + if (keygen_in_progress(irc, a->user, a->prpl->name)) { irc_rootmsg(irc, "keygen for account `%s' already in progress", a->tag); return; } - - if(otrl_privkey_find(irc->otr->us, a->user, a->prpl->name)) { + + if (otrl_privkey_find(irc->otr->us, a->user, a->prpl->name)) { char *s = g_strdup_printf("account `%s' already has a key, replace it?", a->tag); query_add(irc, NULL, s, yes_keygen, NULL, NULL, a); g_free(s); @@ -1139,45 +1162,46 @@ void cmd_otr_keygen(irc_t *irc, char **args) void yes_forget_fingerprint(void *data) { - pair_t *p = (pair_t *)data; - irc_t *irc = (irc_t *)p->fst; - Fingerprint *fp = (Fingerprint *)p->snd; + pair_t *p = (pair_t *) data; + irc_t *irc = (irc_t *) p->fst; + Fingerprint *fp = (Fingerprint *) p->snd; g_free(p); - - if(fp == fp->context->active_fingerprint) { + + if (fp == fp->context->active_fingerprint) { irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first"); return; } - + otrl_context_forget_fingerprint(fp, 0); } void yes_forget_context(void *data) { - pair_t *p = (pair_t *)data; - irc_t *irc = (irc_t *)p->fst; - ConnContext *ctx = (ConnContext *)p->snd; + pair_t *p = (pair_t *) data; + irc_t *irc = (irc_t *) p->fst; + ConnContext *ctx = (ConnContext *) p->snd; g_free(p); // XXX forget all contexts - - if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { + + if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { irc_rootmsg(irc, "active otr connection with %s, terminate it first", - peernick(irc, ctx->username, ctx->protocol)); + peernick(irc, ctx->username, ctx->protocol)); return; } - - if(ctx->msgstate == OTRL_MSGSTATE_FINISHED) + + if (ctx->msgstate == OTRL_MSGSTATE_FINISHED) { otrl_context_force_plaintext(ctx); + } otrl_context_forget(ctx); } void yes_forget_key(void *data) { - OtrlPrivKey *key = (OtrlPrivKey *)data; - + OtrlPrivKey *key = (OtrlPrivKey *) data; + otrl_privkey_forget(key); /* Hm, libotr doesn't seem to offer a function for explicitly /writing/ keyfiles. So the key will be back on the next load... */ @@ -1186,113 +1210,105 @@ void yes_forget_key(void *data) void cmd_otr_forget(irc_t *irc, char **args) { - if(!strcmp(args[1], "fingerprint")) - { + if (!strcmp(args[1], "fingerprint")) { irc_user_t *u; ConnContext *ctx; Fingerprint *fp; char human[54]; char *s; pair_t *p; - - if(!args[3]) { + + if (!args[3]) { irc_rootmsg(irc, "otr %s %s: not enough arguments (2 req.)", args[0], args[1]); return; } - + /* TODO: allow context specs ("user/proto/account") in 'otr forget fingerprint'? */ u = irc_user_by_name(irc, args[2]); - if(!u || !u->bu || !u->bu->ic) { + if (!u || !u->bu || !u->bu->ic) { irc_rootmsg(irc, "%s: unknown user", args[2]); return; } - + ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, - u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); - if(!ctx) { + u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); + if (!ctx) { irc_rootmsg(irc, "no otr context with %s", args[2]); return; } - - fp = match_fingerprint(irc, ctx, ((const char **)args)+3); - if(!fp) { + + fp = match_fingerprint(irc, ctx, ((const char **) args) + 3); + if (!fp) { /* match_fingerprint does error messages */ return; } - - if(fp == ctx->active_fingerprint) { + + if (fp == ctx->active_fingerprint) { irc_rootmsg(irc, "that fingerprint is active, terminate otr connection first"); return; } - + otrl_privkey_hash_to_human(human, fp->fingerprint); s = g_strdup_printf("about to forget fingerprint %s, are you sure?", human); p = g_malloc(sizeof(pair_t)); - if(!p) + if (!p) { return; + } p->fst = irc; p->snd = fp; query_add(irc, NULL, s, yes_forget_fingerprint, NULL, NULL, p); g_free(s); - } - - else if(!strcmp(args[1], "context")) - { + } else if (!strcmp(args[1], "context")) { irc_user_t *u; ConnContext *ctx; char *s; pair_t *p; - + /* TODO: allow context specs ("user/proto/account") in 'otr forget contex'? */ u = irc_user_by_name(irc, args[2]); - if(!u || !u->bu || !u->bu->ic) { + if (!u || !u->bu || !u->bu->ic) { irc_rootmsg(irc, "%s: unknown user", args[2]); return; } - + ctx = otrl_context_find(irc->otr->us, u->bu->handle, u->bu->ic->acc->user, - u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); - if(!ctx) { + u->bu->ic->acc->prpl->name, OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL); + if (!ctx) { irc_rootmsg(irc, "no otr context with %s", args[2]); return; } - - if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { + + if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { irc_rootmsg(irc, "active otr connection with %s, terminate it first", args[2]); return; } - + s = g_strdup_printf("about to forget otr data about %s, are you sure?", args[2]); p = g_malloc(sizeof(pair_t)); - if(!p) + if (!p) { return; + } p->fst = irc; p->snd = ctx; query_add(irc, NULL, s, yes_forget_context, NULL, NULL, p); g_free(s); - } - - else if(!strcmp(args[1], "key")) - { + } else if (!strcmp(args[1], "key")) { OtrlPrivKey *key; char *s; - - key = match_privkey(irc, ((const char **)args)+2); - if(!key) { + + key = match_privkey(irc, ((const char **) args) + 2); + if (!key) { /* match_privkey does error messages */ return; } - + s = g_strdup_printf("about to forget the private key for %s/%s, are you sure?", - key->accountname, key->protocol); + key->accountname, key->protocol); query_add(irc, NULL, s, yes_forget_key, NULL, NULL, key); g_free(s); - } - - else - { + } else { irc_rootmsg(irc, "otr %s: unknown subcommand \"%s\", see \x02help otr forget\x02", - args[0], args[1]); + args[0], args[1]); } } @@ -1306,14 +1322,14 @@ void log_otr_message(void *opdata, const char *fmt, ...) va_start(va, fmt); char *msg = g_strdup_vprintf(fmt, va); va_end(va); - + log_message(LOGLVL_INFO, "otr: %s", msg); } void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...) { struct im_connection *ic = - check_imc(opdata, ctx->accountname, ctx->protocol); + check_imc(opdata, ctx->accountname, ctx->protocol); irc_t *irc = ic->bee->ui_data; irc_user_t *u = peeruser(irc, ctx->username, ctx->protocol); va_list va; @@ -1322,7 +1338,7 @@ void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...) char *msg = g_strdup_vprintf(fmt, va); va_end(va); - if(u) { + if (u) { /* display as a notice from this particular user */ irc_usernotice(u, "%s", msg); } else { @@ -1334,59 +1350,59 @@ void display_otr_message(void *opdata, ConnContext *ctx, const char *fmt, ...) /* combined handler for the 'otr smp' and 'otr smpq' commands */ void otr_smp_or_smpq(irc_t *irc, const char *nick, const char *question, - const char *secret) + const char *secret) { irc_user_t *u; ConnContext *ctx; otrl_instag_t instag = OTRL_INSTAG_BEST; // XXX u = irc_user_by_name(irc, nick); - if(!u || !u->bu || !u->bu->ic) { + if (!u || !u->bu || !u->bu->ic) { irc_rootmsg(irc, "%s: unknown user", nick); return; } - if(!(u->bu->flags & BEE_USER_ONLINE)) { + if (!(u->bu->flags & BEE_USER_ONLINE)) { irc_rootmsg(irc, "%s is offline", nick); return; } - + ctx = otrl_context_find(irc->otr->us, u->bu->handle, - u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, instag, 0, NULL, NULL, NULL); - if(!ctx || ctx->msgstate != OTRL_MSGSTATE_ENCRYPTED) { + u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, instag, 0, NULL, NULL, NULL); + if (!ctx || ctx->msgstate != OTRL_MSGSTATE_ENCRYPTED) { irc_rootmsg(irc, "smp: otr inactive with %s, try \x02otr connect" - " %s\x02", nick, nick); + " %s\x02", nick, nick); return; } - if(ctx->smstate->nextExpected != OTRL_SMP_EXPECT1) { + if (ctx->smstate->nextExpected != OTRL_SMP_EXPECT1) { log_message(LOGLVL_INFO, - "SMP already in phase %d, sending abort before reinitiating", - ctx->smstate->nextExpected+1); + "SMP already in phase %d, sending abort before reinitiating", + ctx->smstate->nextExpected + 1); otrl_message_abort_smp(irc->otr->us, &otr_ops, u->bu->ic, ctx); otrl_sm_state_free(ctx->smstate); } - if(question) { + if (question) { /* this was 'otr smpq', just initiate */ irc_rootmsg(irc, "smp: initiating with %s...", u->nick); otrl_message_initiate_smp_q(irc->otr->us, &otr_ops, u->bu->ic, ctx, - question, (unsigned char *)secret, strlen(secret)); + question, (unsigned char *) secret, strlen(secret)); /* smp is now in EXPECT2 */ } else { /* this was 'otr smp', initiate or reply */ /* warning: the following assumes that smstates are cleared whenever an SMP - is completed or aborted! */ - if(ctx->smstate->secret == NULL) { + is completed or aborted! */ + if (ctx->smstate->secret == NULL) { irc_rootmsg(irc, "smp: initiating with %s...", u->nick); otrl_message_initiate_smp(irc->otr->us, &otr_ops, - u->bu->ic, ctx, (unsigned char *)secret, strlen(secret)); + u->bu->ic, ctx, (unsigned char *) secret, strlen(secret)); /* smp is now in EXPECT2 */ } else { /* if we're still in EXPECT1 but smstate is initialized, we must have received the SMP1, so let's issue a response */ irc_rootmsg(irc, "smp: responding to %s...", u->nick); otrl_message_respond_smp(irc->otr->us, &otr_ops, - u->bu->ic, ctx, (unsigned char *)secret, strlen(secret)); + u->bu->ic, ctx, (unsigned char *) secret, strlen(secret)); /* smp is now in EXPECT3 */ } } @@ -1397,87 +1413,93 @@ gboolean ev_message_poll(gpointer data, gint fd, b_input_condition cond) { otr_t *otr = data; - if(otr && otr->us) + if (otr && otr->us) { otrl_message_poll(otr->us, &otr_ops, NULL); + } - return TRUE; /* cycle timer */ + return TRUE; /* cycle timer */ } /* helper to assert that account and protocol names given to ops below always match the im_connection passed through as opdata */ struct im_connection *check_imc(void *opdata, const char *accountname, - const char *protocol) + const char *protocol) { - struct im_connection *ic = (struct im_connection *)opdata; + struct im_connection *ic = (struct im_connection *) opdata; /* libotr 4.0.0 has a bug where it doesn't set opdata, so we catch * that and try to find the desired connection in the global list. */ - if(!ic) { + if (!ic) { GSList *l; - for(l=get_connections(); l; l=l->next) { + for (l = get_connections(); l; l = l->next) { ic = l->data; - if(strcmp(accountname, ic->acc->user) == 0 && - strcmp(protocol, ic->acc->prpl->name) == 0) + if (strcmp(accountname, ic->acc->user) == 0 && + strcmp(protocol, ic->acc->prpl->name) == 0) { break; + } } assert(l != NULL); /* a match should always be found */ - if(!l) + if (!l) { return NULL; + } } if (strcmp(accountname, ic->acc->user) != 0) { log_message(LOGLVL_WARNING, - "otr: internal account name mismatch: '%s' vs '%s'", - accountname, ic->acc->user); + "otr: internal account name mismatch: '%s' vs '%s'", + accountname, ic->acc->user); } if (strcmp(protocol, ic->acc->prpl->name) != 0) { log_message(LOGLVL_WARNING, - "otr: internal protocol name mismatch: '%s' vs '%s'", - protocol, ic->acc->prpl->name); + "otr: internal protocol name mismatch: '%s' vs '%s'", + protocol, ic->acc->prpl->name); } - + return ic; } irc_user_t *peeruser(irc_t *irc, const char *handle, const char *protocol) { GSList *l; - - for(l=irc->b->users; l; l = l->next) { + + for (l = irc->b->users; l; l = l->next) { bee_user_t *bu = l->data; struct prpl *prpl; - if(!bu->ui_data || !bu->ic || !bu->handle) + if (!bu->ui_data || !bu->ic || !bu->handle) { continue; + } prpl = bu->ic->acc->prpl; - if(strcmp(prpl->name, protocol) == 0 - && prpl->handle_cmp(bu->handle, handle) == 0) { + if (strcmp(prpl->name, protocol) == 0 + && prpl->handle_cmp(bu->handle, handle) == 0) { return bu->ui_data; } } - + return NULL; } int hexval(char a) { - int x=g_ascii_tolower(a); - - if(x>='a' && x<='f') + int x = g_ascii_tolower(a); + + if (x >= 'a' && x <= 'f') { x = x - 'a' + 10; - else if(x>='0' && x<='9') + } else if (x >= '0' && x <= '9') { x = x - '0'; - else + } else { return -1; - + } + return x; } const char *peernick(irc_t *irc, const char *handle, const char *protocol) { static char fallback[512]; - + irc_user_t *u = peeruser(irc, handle, protocol); - if(u) { + + if (u) { return u->nick; } else { g_snprintf(fallback, 511, "%s/%s", handle, protocol); @@ -1489,14 +1511,15 @@ void otr_update_uflags(ConnContext *context, irc_user_t *u) { const char *trust; - if(context->active_fingerprint) { + if (context->active_fingerprint) { u->flags |= IRC_USER_OTR_ENCRYPTED; trust = context->active_fingerprint->trust; - if(trust && trust[0]) + if (trust && trust[0]) { u->flags |= IRC_USER_OTR_TRUSTED; - else + } else { u->flags &= ~IRC_USER_OTR_TRUSTED; + } } else { u->flags &= ~IRC_USER_OTR_ENCRYPTED; } @@ -1512,26 +1535,28 @@ void show_fingerprints(irc_t *irc, ConnContext *ctx) char human[45]; Fingerprint *fp; const char *trust; - int count=0; - - for(fp=&ctx->fingerprint_root; fp; fp=fp->next) { - if(!fp->fingerprint) + int count = 0; + + for (fp = &ctx->fingerprint_root; fp; fp = fp->next) { + if (!fp->fingerprint) { continue; + } count++; otrl_privkey_hash_to_human(human, fp->fingerprint); - if(!fp->trust || fp->trust[0] == '\0') { - trust="untrusted"; + if (!fp->trust || fp->trust[0] == '\0') { + trust = "untrusted"; } else { - trust=fp->trust; + trust = fp->trust; } - if(fp == ctx->active_fingerprint) { + if (fp == ctx->active_fingerprint) { irc_rootmsg(irc, " \x02%s (%s)\x02", human, trust); } else { irc_rootmsg(irc, " %s (%s)", human, trust); } } - if(count==0) + if (count == 0) { irc_rootmsg(irc, " (none)"); + } } Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args) @@ -1540,61 +1565,66 @@ Fingerprint *match_fingerprint(irc_t *irc, ConnContext *ctx, const char **args) char human[45]; char prefix[45], *p; int n; - int i,j; - + int i, j; + /* assemble the args into a prefix in standard "human" form */ - n=0; - p=prefix; - for(i=0; args[i]; i++) { - for(j=0; args[i][j]; j++) { + n = 0; + p = prefix; + for (i = 0; args[i]; i++) { + for (j = 0; args[i][j]; j++) { char c = g_ascii_toupper(args[i][j]); - - if(n>=40) { + + if (n >= 40) { irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40"); return NULL; } - - if( (c>='A' && c<='F') || (c>='0' && c<='9') ) { + + if ((c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')) { *(p++) = c; } else { - irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1); + irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i + 1); return NULL; } - + n++; - if(n%8 == 0) + if (n % 8 == 0) { *(p++) = ' '; + } } } *p = '\0'; - + /* find first fingerprint with the given prefix */ n = strlen(prefix); - for(fp=&ctx->fingerprint_root; fp; fp=fp->next) { - if(!fp->fingerprint) + for (fp = &ctx->fingerprint_root; fp; fp = fp->next) { + if (!fp->fingerprint) { continue; + } otrl_privkey_hash_to_human(human, fp->fingerprint); - if(!strncmp(prefix, human, n)) + if (!strncmp(prefix, human, n)) { break; + } } - if(!fp) { + if (!fp) { irc_rootmsg(irc, "%s: no match", prefix); return NULL; } - + /* make sure the match, if any, is unique */ - for(fp2=fp->next; fp2; fp2=fp2->next) { - if(!fp2->fingerprint) + for (fp2 = fp->next; fp2; fp2 = fp2->next) { + if (!fp2->fingerprint) { continue; + } otrl_privkey_hash_to_human(human, fp2->fingerprint); - if(!strncmp(prefix, human, n)) + if (!strncmp(prefix, human, n)) { break; + } } - if(fp2) { + if (fp2) { irc_rootmsg(irc, "%s: multiple matches", prefix); return NULL; } - + return fp; } @@ -1604,61 +1634,66 @@ OtrlPrivKey *match_privkey(irc_t *irc, const char **args) char human[45]; char prefix[45], *p; int n; - int i,j; - + int i, j; + /* assemble the args into a prefix in standard "human" form */ - n=0; - p=prefix; - for(i=0; args[i]; i++) { - for(j=0; args[i][j]; j++) { + n = 0; + p = prefix; + for (i = 0; args[i]; i++) { + for (j = 0; args[i][j]; j++) { char c = g_ascii_toupper(args[i][j]); - - if(n>=40) { + + if (n >= 40) { irc_rootmsg(irc, "too many fingerprint digits given, expected at most 40"); return NULL; } - - if( (c>='A' && c<='F') || (c>='0' && c<='9') ) { + + if ((c >= 'A' && c <= 'F') || (c >= '0' && c <= '9')) { *(p++) = c; } else { - irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i+1); + irc_rootmsg(irc, "invalid hex digit '%c' in block %d", args[i][j], i + 1); return NULL; } - + n++; - if(n%8 == 0) + if (n % 8 == 0) { *(p++) = ' '; + } } } *p = '\0'; - + /* find first key which matches the given prefix */ n = strlen(prefix); - for(k=irc->otr->us->privkey_root; k; k=k->next) { + for (k = irc->otr->us->privkey_root; k; k = k->next) { p = otrl_privkey_fingerprint(irc->otr->us, human, k->accountname, k->protocol); - if(!p) /* gah! :-P */ + if (!p) { /* gah! :-P */ continue; - if(!strncmp(prefix, human, n)) + } + if (!strncmp(prefix, human, n)) { break; + } } - if(!k) { + if (!k) { irc_rootmsg(irc, "%s: no match", prefix); return NULL; } - + /* make sure the match, if any, is unique */ - for(k2=k->next; k2; k2=k2->next) { + for (k2 = k->next; k2; k2 = k2->next) { p = otrl_privkey_fingerprint(irc->otr->us, human, k2->accountname, k2->protocol); - if(!p) /* gah! :-P */ + if (!p) { /* gah! :-P */ continue; - if(!strncmp(prefix, human, n)) + } + if (!strncmp(prefix, human, n)) { break; + } } - if(k2) { + if (k2) { irc_rootmsg(irc, "%s: multiple matches", prefix); return NULL; } - + return k; } @@ -1671,73 +1706,77 @@ void show_general_otr_info(irc_t *irc) /* list all privkeys (including ones being generated) */ irc_rootmsg(irc, "\x1fprivate keys:\x1f"); - for(key=irc->otr->us->privkey_root; key; key=key->next) { + for (key = irc->otr->us->privkey_root; key; key = key->next) { const char *hash; - - switch(key->pubkey_type) { + + switch (key->pubkey_type) { case OTRL_PUBKEY_TYPE_DSA: irc_rootmsg(irc, " %s/%s - DSA", key->accountname, key->protocol); break; default: irc_rootmsg(irc, " %s/%s - type %d", key->accountname, key->protocol, - key->pubkey_type); + key->pubkey_type); } /* No, it doesn't make much sense to search for the privkey again by account/protocol, but libotr currently doesn't provide a direct routine for hashing a given 'OtrlPrivKey'... */ hash = otrl_privkey_fingerprint(irc->otr->us, human, key->accountname, key->protocol); - if(hash) /* should always succeed */ + if (hash) { /* should always succeed */ irc_rootmsg(irc, " %s", human); + } } - if(irc->otr->sent_accountname) { + if (irc->otr->sent_accountname) { irc_rootmsg(irc, " %s/%s - DSA", irc->otr->sent_accountname, - irc->otr->sent_protocol); + irc->otr->sent_protocol); irc_rootmsg(irc, " (being generated)"); } - for(kg=irc->otr->todo; kg; kg=kg->next) { + for (kg = irc->otr->todo; kg; kg = kg->next) { irc_rootmsg(irc, " %s/%s - DSA", kg->accountname, kg->protocol); irc_rootmsg(irc, " (queued)"); } - if(key == irc->otr->us->privkey_root && - !irc->otr->sent_accountname && - kg == irc->otr->todo) + if (key == irc->otr->us->privkey_root && + !irc->otr->sent_accountname && + kg == irc->otr->todo) { irc_rootmsg(irc, " (none)"); + } /* list all contexts */ /* XXX remove this, or split off as its own command */ /* XXX show instags? */ irc_rootmsg(irc, "%s", ""); irc_rootmsg(irc, "\x1f" "connection contexts:\x1f (bold=currently encrypted)"); - for(ctx=irc->otr->us->context_root; ctx; ctx=ctx->next) {\ + for (ctx = irc->otr->us->context_root; ctx; ctx = ctx->next) { \ irc_user_t *u; char *userstring; - + u = peeruser(irc, ctx->username, ctx->protocol); - if(u) + if (u) { userstring = g_strdup_printf("%s/%s/%s (%s)", - ctx->username, ctx->protocol, ctx->accountname, u->nick); - else + ctx->username, ctx->protocol, ctx->accountname, u->nick); + } else { userstring = g_strdup_printf("%s/%s/%s", - ctx->username, ctx->protocol, ctx->accountname); - - if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { + ctx->username, ctx->protocol, ctx->accountname); + } + + if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { irc_rootmsg(irc, " \x02%s\x02", userstring); } else { irc_rootmsg(irc, " %s", userstring); } - + g_free(userstring); } - if(ctx == irc->otr->us->context_root) + if (ctx == irc->otr->us->context_root) { irc_rootmsg(irc, " (none)"); + } } void show_otr_context_info(irc_t *irc, ConnContext *ctx) { // XXX show all instags/subcontexts - switch(ctx->otr_offer) { + switch (ctx->otr_offer) { case OFFER_NOT: irc_rootmsg(irc, " otr offer status: none sent"); break; @@ -1754,7 +1793,7 @@ void show_otr_context_info(irc_t *irc, ConnContext *ctx) irc_rootmsg(irc, " otr offer status: %d", ctx->otr_offer); } - switch(ctx->msgstate) { + switch (ctx->msgstate) { case OTRL_MSGSTATE_PLAINTEXT: irc_rootmsg(irc, " connection state: cleartext"); break; @@ -1768,70 +1807,74 @@ void show_otr_context_info(irc_t *irc, ConnContext *ctx) irc_rootmsg(irc, " connection state: %d", ctx->msgstate); } - irc_rootmsg(irc, " fingerprints: (bold=active)"); + irc_rootmsg(irc, " fingerprints: (bold=active)"); show_fingerprints(irc, ctx); } int keygen_in_progress(irc_t *irc, const char *handle, const char *protocol) { kg_t *kg; - - if(!irc->otr->sent_accountname || !irc->otr->sent_protocol) + + if (!irc->otr->sent_accountname || !irc->otr->sent_protocol) { return 0; + } /* are we currently working on this key? */ - if(!strcmp(handle, irc->otr->sent_accountname) && - !strcmp(protocol, irc->otr->sent_protocol)) + if (!strcmp(handle, irc->otr->sent_accountname) && + !strcmp(protocol, irc->otr->sent_protocol)) { return 1; - + } + /* do we have it queued for later? */ - for(kg=irc->otr->todo; kg; kg=kg->next) { - if(!strcmp(handle, kg->accountname) && - !strcmp(protocol, kg->protocol)) + for (kg = irc->otr->todo; kg; kg = kg->next) { + if (!strcmp(handle, kg->accountname) && + !strcmp(protocol, kg->protocol)) { return 1; + } } - + return 0; } void otr_keygen(irc_t *irc, const char *handle, const char *protocol) { /* do nothing if a key for the requested account is already being generated */ - if(keygen_in_progress(irc, handle, protocol)) + if (keygen_in_progress(irc, handle, protocol)) { return; + } /* see if we already have a keygen child running. if not, start one and put a handler on its output. */ - if(!irc->otr->keygen || waitpid(irc->otr->keygen, NULL, WNOHANG)) { + if (!irc->otr->keygen || waitpid(irc->otr->keygen, NULL, WNOHANG)) { pid_t p; int to[2], from[2]; FILE *tof, *fromf; - - if(pipe(to) < 0 || pipe(from) < 0) { + + if (pipe(to) < 0 || pipe(from) < 0) { irc_rootmsg(irc, "otr keygen: couldn't create pipe: %s", strerror(errno)); return; } - + tof = fdopen(to[1], "w"); fromf = fdopen(from[0], "r"); - if(!tof || !fromf) { + if (!tof || !fromf) { irc_rootmsg(irc, "otr keygen: couldn't streamify pipe: %s", strerror(errno)); return; } - + p = fork(); - if(p<0) { + if (p < 0) { irc_rootmsg(irc, "otr keygen: couldn't fork: %s", strerror(errno)); return; } - - if(!p) { + + if (!p) { /* child process */ signal(SIGTERM, exit); keygen_child_main(irc->otr->us, to[0], from[1]); exit(0); } - + irc->otr->keygen = p; irc->otr->to = tof; irc->otr->from = fromf; @@ -1840,13 +1883,14 @@ void otr_keygen(irc_t *irc, const char *handle, const char *protocol) irc->otr->todo = NULL; b_input_add(from[0], B_EV_IO_READ, keygen_finish_handler, irc); } - + /* is the keygen slave currently working? */ - if(irc->otr->sent_accountname) { + if (irc->otr->sent_accountname) { /* enqueue our job for later transmission */ kg_t **kg = &irc->otr->todo; - while(*kg) - kg=&((*kg)->next); + while (*kg) { + kg = &((*kg)->next); + } *kg = g_new0(kg_t, 1); (*kg)->accountname = g_strdup(handle); (*kg)->protocol = g_strdup(protocol); @@ -1865,20 +1909,20 @@ void keygen_child_main(OtrlUserState us, int infd, int outfd) char filename[128], accountname[512], protocol[512]; gcry_error_t e; int tempfd; - + input = fdopen(infd, "r"); output = fdopen(outfd, "w"); - - while(!feof(input) && !ferror(input) && !feof(output) && !ferror(output)) { + + while (!feof(input) && !ferror(input) && !feof(output) && !ferror(output)) { myfgets(accountname, 512, input); myfgets(protocol, 512, input); - + strncpy(filename, "/tmp/bitlbee-XXXXXX", 128); tempfd = mkstemp(filename); close(tempfd); e = otrl_privkey_generate(us, filename, accountname, protocol); - if(e) { + if (e) { fprintf(output, "\n"); /* this means failure */ fprintf(output, "otr keygen: %s\n", gcry_strerror(e)); unlink(filename); @@ -1888,27 +1932,27 @@ void keygen_child_main(OtrlUserState us, int infd, int outfd) } fflush(output); } - + fclose(input); fclose(output); } gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond) { - irc_t *irc = (irc_t *)data; + irc_t *irc = (irc_t *) data; char filename[512], msg[512]; myfgets(filename, 512, irc->otr->from); myfgets(msg, 512, irc->otr->from); - + irc_rootmsg(irc, "%s", msg); - if(filename[0]) { - if(strsane(irc->user->nick)) { + if (filename[0]) { + if (strsane(irc->user->nick)) { char *kf = g_strdup_printf("%s%s.otr_keys", global.conf->configdir, irc->user->nick); char *tmp = g_strdup_printf("%s.new", kf); copyfile(filename, tmp); unlink(filename); - rename(tmp,kf); + rename(tmp, kf); otrl_privkey_read(irc->otr->us, kf); g_free(kf); g_free(tmp); @@ -1917,15 +1961,15 @@ gboolean keygen_finish_handler(gpointer data, gint fd, b_input_condition cond) unlink(filename); } } - + /* forget this job */ g_free(irc->otr->sent_accountname); g_free(irc->otr->sent_protocol); irc->otr->sent_accountname = NULL; irc->otr->sent_protocol = NULL; - + /* see if there are any more in the queue */ - if(irc->otr->todo) { + if (irc->otr->todo) { kg_t *p = irc->otr->todo; /* send the next one over */ fprintf(irc->otr->to, "%s\n%s\n", p->accountname, p->protocol); @@ -1952,39 +1996,41 @@ void copyfile(const char *a, const char *b) int fda, fdb; int n; char buf[1024]; - + fda = open(a, O_RDONLY); fdb = open(b, O_WRONLY | O_CREAT | O_TRUNC, 0600); - - while((n=read(fda, buf, 1024)) > 0) + + while ((n = read(fda, buf, 1024)) > 0) { write(fdb, buf, n); - + } + close(fda); - close(fdb); + close(fdb); } void myfgets(char *s, int size, FILE *stream) { - if(!fgets(s, size, stream)) { + if (!fgets(s, size, stream)) { s[0] = '\0'; } else { int n = strlen(s); - if(n>0 && s[n-1] == '\n') - s[n-1] = '\0'; + if (n > 0 && s[n - 1] == '\n') { + s[n - 1] = '\0'; + } } } void yes_keygen(void *data) { - account_t *acc = (account_t *)data; + account_t *acc = (account_t *) data; irc_t *irc = acc->bee->ui_data; - - if(keygen_in_progress(irc, acc->user, acc->prpl->name)) { + + if (keygen_in_progress(irc, acc->user, acc->prpl->name)) { irc_rootmsg(irc, "keygen for %s/%s already in progress", - acc->user, acc->prpl->name); + acc->user, acc->prpl->name); } else { irc_rootmsg(irc, "starting background keygen for %s/%s", - acc->user, acc->prpl->name); + acc->user, acc->prpl->name); irc_rootmsg(irc, "you will be notified when it completes"); otr_keygen(irc, acc->user, acc->prpl->name); } @@ -1999,13 +2045,15 @@ int strsane(const char *s) /* close the OTR connection with the given buddy */ gboolean otr_disconnect_user(irc_t *irc, irc_user_t *u) { - if(!u || !u->bu || !u->bu->ic) + if (!u || !u->bu || !u->bu->ic) { return FALSE; + } /* XXX we disconnect all instances; is that what we want? */ otrl_message_disconnect_all_instances(irc->otr->us, &otr_ops, - u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, u->bu->handle); - + u->bu->ic, u->bu->ic->acc->user, u->bu->ic->acc->prpl->name, + u->bu->handle); + u->flags &= ~IRC_USER_OTR_TRUSTED; u->flags &= ~IRC_USER_OTR_ENCRYPTED; otr_update_modeflags(irc, u); @@ -2019,8 +2067,8 @@ void otr_disconnect_all(irc_t *irc) irc_user_t *u; ConnContext *ctx; - for(ctx=irc->otr->us->context_root; ctx; ctx=ctx->next) { - if(ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { + for (ctx = irc->otr->us->context_root; ctx; ctx = ctx->next) { + if (ctx->msgstate == OTRL_MSGSTATE_ENCRYPTED) { u = peeruser(irc, ctx->username, ctx->protocol); (void) otr_disconnect_user(irc, u); } -- cgit v1.2.3