Changeset 29ff5c2 for otr.c


Ignore:
Timestamp:
2015-11-21T00:01:50Z (8 years ago)
Author:
dequis <dx@…>
Parents:
e4f08bf (diff), 8fdeaa5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feat/hip-cat

File:
1 edited

Legend:

Unmodified
Added
Removed
  • otr.c

    re4f08bf r29ff5c2  
    187187/* update op/voice flag of given user according to encryption state and settings
    188188   returns 0 if neither op_buddies nor voice_buddies is set to "encrypted",
    189    i.e. msgstate should be announced seperately */
     189   i.e. msgstate should be announced separately */
    190190int otr_update_modeflags(irc_t *irc, irc_user_t *u);
    191191
     
    216216void otr_disconnect_all(irc_t *irc);
    217217
     218/* modifies string in-place, replacing \x03 with '?',
     219   as a quick way to prevent remote users from messing with irc colors */
     220static char *otr_filter_colors(char *msg);
     221
    218222/* functions to be called for certain events */
    219223static const struct irc_plugin otr_plugin;
     224
     225#define OTR_COLOR_TRUSTED "03"     /* green */
     226#define OTR_COLOR_UNTRUSTED "05"   /* red */
    220227
    221228/*** routines declared in otr.h: ***/
     
    434441
    435442        /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
    436         if (ic->acc->prpl->options & OPT_NOOTR) {
     443        if (ic->acc->prpl->options & OPT_NOOTR ||
     444            iu->bu->flags & BEE_USER_NOOTR) {
    437445                return msg;
    438446        }
     
    451459        } else if (!newmsg) {
    452460                /* this was a non-OTR message */
    453                 return msg;
     461                return otr_filter_colors(msg);
    454462        } else {
    455463                /* we're done with the original msg, which will be caller-freed. */
     
    472480
    473481        /* don't do OTR on certain (not classic IM) protocols, e.g. twitter */
    474         if (ic->acc->prpl->options & OPT_NOOTR) {
     482        if (ic->acc->prpl->options & OPT_NOOTR ||
     483            iu->bu->flags & BEE_USER_NOOTR) {
    475484                return msg;
    476485        }
     
    742751}
    743752
     753static char *otr_filter_colors(char *msg) {
     754        int i;
     755        for (i = 0; msg[i]; i++) {
     756                if (msg[i] == '\x02' || msg[i] == '\x03') {
     757                        msg[i] = '?';
     758                }
     759        }
     760        return msg;
     761}
     762
     763/* returns newly allocated string */
     764static char *otr_color_encrypted(char *msg, char *color, gboolean is_query) {
     765        char **lines;
     766        GString *out;
     767        int i;
     768
     769        lines = g_strsplit(msg, "\n", -1);
     770
     771        /* up to 4 extra chars per line (e.g., '\x03' + ("03"|"05") + ' ') */
     772        out = g_string_sized_new(strlen(msg) + g_strv_length(lines) * 4);
     773       
     774        for (i = 0; lines[i]; i++) {
     775                char *line = lines[i];
     776
     777                if (i != 0) {
     778                        g_string_append_c(out, '\n');
     779
     780                } else if (is_query && g_strncasecmp(line, "/me ", 4) == 0) {
     781                        /* in a query window, keep "/me " uncolored at the beginning */
     782                        line += 4;
     783                        g_string_append(out, "/me ");
     784                }
     785
     786                g_string_append_c(out, '\x03');
     787                g_string_append(out, color);
     788
     789                /* comma in first place could mess with the color code */
     790                if (line[0] == ',') {
     791                        /* insert a space between color spec and message */
     792                        g_string_append_c(out, ' ');
     793                }
     794
     795                g_string_append(out, otr_filter_colors(line));
     796        }
     797
     798        g_strfreev(lines);
     799
     800        return g_string_free(out, FALSE);
     801}
     802
    744803void op_convert_msg(void *opdata, ConnContext *ctx, OtrlConvertType typ,
    745804                    char **dst, const char *src)
     
    752811        if (typ == OTRL_CONVERT_RECEIVING) {
    753812                char *msg = g_strdup(src);
    754                 char *buf = msg;
    755813
    756814                /* HTML decoding */
     
    759817                    set_getbool(&ic->bee->set, "strip_html")) {
    760818                        strip_html(msg);
     819
     820                        /* msg is borrowed by *dst (unless the next if decides to color it) */
    761821                        *dst = msg;
    762822                }
     
    764824                /* coloring */
    765825                if (set_getbool(&ic->bee->set, "otr_color_encrypted")) {
    766                         int color;                /* color according to f'print trust */
    767                         char *pre = "", *sep = "";    /* optional parts */
    768826                        const char *trust = ctx->active_fingerprint->trust;
    769 
    770                         if (trust && trust[0] != '\0') {
    771                                 color = 3;   /* green */
    772                         } else {
    773                                 color = 5;   /* red */
    774 
    775                         }
    776                         /* in a query window, keep "/me " uncolored at the beginning */
    777                         if (g_strncasecmp(msg, "/me ", 4) == 0
    778                             && irc_user_msgdest(iu) == irc->user->nick) {
    779                                 msg += 4;  /* skip */
    780                                 pre = "/me ";
    781                         }
    782 
    783                         /* comma in first place could mess with the color code */
    784                         if (msg[0] == ',') {
    785                                 /* insert a space between color spec and message */
    786                                 sep = " ";
    787                         }
    788 
    789                         *dst = g_strdup_printf("%s\x03%.2d%s%s\x0F", pre,
    790                                                color, sep, msg);
    791                         g_free(buf);
     827                        char *color = (trust && *trust) ? OTR_COLOR_TRUSTED : OTR_COLOR_UNTRUSTED;
     828                        gboolean is_query = (irc_user_msgdest(iu) == irc->user->nick);
     829
     830                        /* the return value of otr_color_encrypted() is borrowed by *dst */
     831                        *dst = otr_color_encrypted(msg, color, is_query);
     832
     833                        /* this branch doesn't need msg */
     834                        g_free(msg);
    792835                }
    793836        } else {
     
    13571400
    13581401        if (u) {
    1359                 /* display as a notice from this particular user */
    1360                 irc_usernotice(u, "%s", msg);
     1402                /* just show this as a regular message */
     1403                irc_usermsg(u, "<<\002OTR\002>> %s", msg);
    13611404        } else {
    13621405                irc_rootmsg(irc, "[otr] %s", msg);
     
    16951738        *p = '\0';
    16961739
     1740        /* remove trailing whitespace */
     1741        g_strchomp(prefix);
     1742
    16971743        /* find first key which matches the given prefix */
    16981744        n = strlen(prefix);
Note: See TracChangeset for help on using the changeset viewer.