source: protocols/msn/msn_util.c @ 693aca0

Last change on this file since 693aca0 was 693aca0, checked in by dequis <dx@…>, at 2015-04-10T17:10:40Z

msn: colorful debug

  • Property mode set to 100644
File size: 12.7 KB
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[79bb7e4]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* MSN module - Miscellaneous utilities                                 */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#include "nogaim.h"
27#include "msn.h"
[21029d0]28#include "md5.h"
[193dc74]29#include "soap.h"
[b7d3cc34]30#include <ctype.h>
31
[5ebff60]32static char *adlrml_entry(const char *handle_, msn_buddy_flags_t list)
[193dc74]33{
[5ebff60]34        char *domain, handle[strlen(handle_) + 1];
35
36        strcpy(handle, handle_);
37        if ((domain = strchr(handle, '@'))) {
[d97f51b]38                *(domain++) = '\0';
[5ebff60]39        } else {
[193dc74]40                return NULL;
[5ebff60]41        }
42
43        return g_markup_printf_escaped("<ml><d n=\"%s\"><c n=\"%s\" l=\"%d\" t=\"1\"/></d></ml>",
44                                       domain, handle, list);
[193dc74]45}
46
[5ebff60]47int msn_buddy_list_add(struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *realname,
48                       const char *group)
[b7d3cc34]49{
[0da65d5]50        struct msn_data *md = ic->proto_data;
[64768d4]51        char groupid[8];
[193dc74]52        bee_user_t *bu;
53        struct msn_buddy_data *bd;
54        char *adl;
[5ebff60]55
[6acc033]56        *groupid = '\0';
[193dc74]57#if 0
[5ebff60]58        if (group) {
[6acc033]59                int i;
[5ebff60]60                for (i = 0; i < md->groupcount; i++) {
61                        if (g_strcasecmp(md->grouplist[i], group) == 0) {
62                                g_snprintf(groupid, sizeof(groupid), " %d", i);
[6acc033]63                                break;
64                        }
[5ebff60]65                }
66
67                if (*groupid == '\0') {
[70ac477]68                        /* Have to create this group, it doesn't exist yet. */
69                        struct msn_groupadd *ga;
70                        GSList *l;
[5ebff60]71
72                        for (l = md->grpq; l; l = l->next) {
[70ac477]73                                ga = l->data;
[5ebff60]74                                if (g_strcasecmp(ga->group, group) == 0) {
[70ac477]75                                        break;
[5ebff60]76                                }
[70ac477]77                        }
[5ebff60]78
79                        ga = g_new0(struct msn_groupadd, 1);
80                        ga->who = g_strdup(who);
81                        ga->group = g_strdup(group);
82                        md->grpq = g_slist_prepend(md->grpq, ga);
83
84                        if (l == NULL) {
85                                char groupname[strlen(group) + 1];
86                                strcpy(groupname, group);
87                                http_encode(groupname);
88                                g_snprintf(buf, sizeof(buf), "ADG %d %s %d\r\n", ++md->trId, groupname, 0);
89                                return msn_write(ic, buf, strlen(buf));
90                        } else {
[70ac477]91                                /* This can happen if the user's doing lots of adds to a
92                                   new group at once; we're still waiting for the server
93                                   to confirm group creation. */
94                                return 1;
95                        }
96                }
[b7d3cc34]97        }
[193dc74]98#endif
[5ebff60]99
100        if (!((bu = bee_user_by_handle(ic->bee, ic, who)) ||
101              (bu = bee_user_new(ic->bee, ic, who, 0))) ||
102            !(bd = bu->data) || bd->flags & list) {
[193dc74]103                return 1;
[5ebff60]104        }
105
[193dc74]106        bd->flags |= list;
[5ebff60]107
108        if (list == MSN_BUDDY_FL) {
109                msn_soap_ab_contact_add(ic, bu);
110        } else {
111                msn_soap_memlist_edit(ic, who, TRUE, list);
112        }
113
114        if ((adl = adlrml_entry(who, list))) {
115                int st = msn_ns_write(ic, -1, "ADL %d %zd\r\n%s",
116                                      ++md->trId, strlen(adl), adl);
117                g_free(adl);
118
[64768d4]119                return st;
[193dc74]120        }
[5ebff60]121
[6ddb223]122        return 1;
[b7d3cc34]123}
124
[5ebff60]125int msn_buddy_list_remove(struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *group)
[b7d3cc34]126{
[0da65d5]127        struct msn_data *md = ic->proto_data;
[64768d4]128        char groupid[8];
[193dc74]129        bee_user_t *bu;
130        struct msn_buddy_data *bd;
131        char *adl;
[5ebff60]132
[8b01217]133        *groupid = '\0';
[193dc74]134#if 0
[5ebff60]135        if (group) {
[8b01217]136                int i;
[5ebff60]137                for (i = 0; i < md->groupcount; i++) {
138                        if (g_strcasecmp(md->grouplist[i], group) == 0) {
139                                g_snprintf(groupid, sizeof(groupid), " %d", i);
[8b01217]140                                break;
141                        }
[5ebff60]142                }
[8b01217]143        }
[193dc74]144#endif
[5ebff60]145
146        if (!(bu = bee_user_by_handle(ic->bee, ic, who)) ||
147            !(bd = bu->data) || !(bd->flags & list)) {
[193dc74]148                return 1;
[5ebff60]149        }
150
[193dc74]151        bd->flags &= ~list;
[5ebff60]152
153        if (list == MSN_BUDDY_FL) {
154                msn_soap_ab_contact_del(ic, bu);
155        } else {
156                msn_soap_memlist_edit(ic, who, FALSE, list);
157        }
158
159        if ((adl = adlrml_entry(who, list))) {
160                int st = msn_ns_write(ic, -1, "RML %d %zd\r\n%s",
161                                      ++md->trId, strlen(adl), adl);
162                g_free(adl);
163
[64768d4]164                return st;
[193dc74]165        }
[5ebff60]166
[6ddb223]167        return 1;
[b7d3cc34]168}
169
[5ebff60]170struct msn_buddy_ask_data {
[0da65d5]171        struct im_connection *ic;
[b7d3cc34]172        char *handle;
173        char *realname;
174};
175
[098a75b]176static void msn_buddy_ask_free(void *data)
177{
178        struct msn_buddy_ask_data *bla = data;
179
180        g_free(bla->handle);
181        g_free(bla->realname);
182        g_free(bla);
183}
184
[5ebff60]185static void msn_buddy_ask_yes(void *data)
[b7d3cc34]186{
[9143aeb]187        struct msn_buddy_ask_data *bla = data;
[5ebff60]188
189        msn_buddy_list_add(bla->ic, MSN_BUDDY_AL, bla->handle, bla->realname, NULL);
190
191        imcb_ask_add(bla->ic, bla->handle, NULL);
192
[098a75b]193        msn_buddy_ask_free(bla);
[b7d3cc34]194}
195
[5ebff60]196static void msn_buddy_ask_no(void *data)
[b7d3cc34]197{
[9143aeb]198        struct msn_buddy_ask_data *bla = data;
[5ebff60]199
200        msn_buddy_list_add(bla->ic, MSN_BUDDY_BL, bla->handle, bla->realname, NULL);
201
[098a75b]202        msn_buddy_ask_free(bla);
[b7d3cc34]203}
204
[5ebff60]205void msn_buddy_ask(bee_user_t *bu)
[b7d3cc34]206{
[e5854a8]207        struct msn_buddy_ask_data *bla;
208        struct msn_buddy_data *bd = bu->data;
[b7d3cc34]209        char buf[1024];
[5ebff60]210
211        if (!(bd->flags & MSN_BUDDY_PL)) {
[e5854a8]212                return;
[5ebff60]213        }
214
215        bla = g_new0(struct msn_buddy_ask_data, 1);
[e5854a8]216        bla->ic = bu->ic;
[5ebff60]217        bla->handle = g_strdup(bu->handle);
218        bla->realname = g_strdup(bu->fullname);
219
220        g_snprintf(buf, sizeof(buf),
221                   "The user %s (%s) wants to add you to his/her buddy list.",
222                   bu->handle, bu->fullname);
[098a75b]223
224        imcb_ask_with_free(bu->ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no, msn_buddy_ask_free);
[b7d3cc34]225}
226
227/* *NOT* thread-safe, but that's not a problem for now... */
[5ebff60]228char **msn_linesplit(char *line)
[b7d3cc34]229{
230        static char **ret = NULL;
231        static int size = 3;
232        int i, n = 0;
[5ebff60]233
234        if (ret == NULL) {
235                ret = g_new0(char*, size);
236        }
237
238        for (i = 0; line[i] && line[i] == ' '; i++) {
239                ;
240        }
241        if (line[i]) {
[b7d3cc34]242                ret[n++] = line + i;
[5ebff60]243                for (i++; line[i]; i++) {
244                        if (line[i] == ' ') {
[b7d3cc34]245                                line[i] = 0;
[5ebff60]246                        } else if (line[i] != ' ' && !line[i - 1]) {
[b7d3cc34]247                                ret[n++] = line + i;
[5ebff60]248                        }
249
250                        if (n >= size) {
251                                ret = g_renew(char*, ret, size += 2);
252                        }
[b7d3cc34]253                }
254        }
255        ret[n] = NULL;
[5ebff60]256
257        return(ret);
[b7d3cc34]258}
259
260/* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give
261   commands, but sometimes they give additional data (payload). This function tries to handle
262   this all in a nice way and send all data to the right places. */
263
264/* Return values: -1: Read error, abort connection.
265                   0: Command reported error; Abort *immediately*. (The connection does not exist anymore)
266                   1: OK */
267
[5ebff60]268int msn_handler(struct msn_handler_data *h)
[b7d3cc34]269{
270        int st;
[5ebff60]271
272        h->rxq = g_renew(char, h->rxq, h->rxlen + 1024);
273        st = read(h->fd, h->rxq + h->rxlen, 1024);
[b7d3cc34]274        h->rxlen += st;
[5ebff60]275
276        if (st <= 0) {
[693aca0]277                fprintf(stderr, "\n\x1b[92m<<< [closed]\x1b[97m ");
[5ebff60]278                return(-1);
279        }
280
281        if (getenv("BITLBEE_DEBUG")) {
[693aca0]282                fprintf(stderr, "\n\x1b[92m<<< ");
[5ebff60]283                write(2, h->rxq + h->rxlen - st, st);
[693aca0]284                fprintf(stderr, "\x1b[97m");
[523fb23]285        }
[5ebff60]286
287        while (st) {
[b7d3cc34]288                int i;
[5ebff60]289
290                if (h->msglen == 0) {
291                        for (i = 0; i < h->rxlen; i++) {
292                                if (h->rxq[i] == '\r' || h->rxq[i] == '\n') {
[b7d3cc34]293                                        char *cmd_text, **cmd;
294                                        int count;
[5ebff60]295
296                                        cmd_text = g_strndup(h->rxq, i);
297                                        cmd = msn_linesplit(cmd_text);
298                                        for (count = 0; cmd[count]; count++) {
299                                                ;
300                                        }
301                                        st = h->exec_command(h, cmd, count);
302                                        g_free(cmd_text);
303
[b7d3cc34]304                                        /* If the connection broke, don't continue. We don't even exist anymore. */
[5ebff60]305                                        if (!st) {
306                                                return(0);
307                                        }
308
309                                        if (h->msglen) {
310                                                h->cmd_text = g_strndup(h->rxq, i);
311                                        }
312
[b7d3cc34]313                                        /* Skip to the next non-emptyline */
[5ebff60]314                                        while (i < h->rxlen && (h->rxq[i] == '\r' || h->rxq[i] == '\n')) {
315                                                i++;
316                                        }
317
[b7d3cc34]318                                        break;
319                                }
320                        }
[5ebff60]321
[b7d3cc34]322                        /* If we reached the end of the buffer, there's still an incomplete command there.
323                           Return and wait for more data. */
[5ebff60]324                        if (i == h->rxlen && h->rxq[i - 1] != '\r' && h->rxq[i - 1] != '\n') {
[b7d3cc34]325                                break;
[5ebff60]326                        }
327                } else {
[b7d3cc34]328                        char *msg, **cmd;
329                        int count;
[5ebff60]330
[b7d3cc34]331                        /* Do we have the complete message already? */
[5ebff60]332                        if (h->msglen > h->rxlen) {
[b7d3cc34]333                                break;
[5ebff60]334                        }
335
336                        msg = g_strndup(h->rxq, h->msglen);
337                        cmd = msn_linesplit(h->cmd_text);
338                        for (count = 0; cmd[count]; count++) {
339                                ;
340                        }
341
342                        st = h->exec_message(h, msg, h->msglen, cmd, count);
343                        g_free(msg);
344                        g_free(h->cmd_text);
[b7d3cc34]345                        h->cmd_text = NULL;
[5ebff60]346
347                        if (!st) {
348                                return(0);
349                        }
350
[b7d3cc34]351                        i = h->msglen;
352                        h->msglen = 0;
353                }
[5ebff60]354
[b7d3cc34]355                /* More data after this block? */
[5ebff60]356                if (i < h->rxlen) {
[b7d3cc34]357                        char *tmp;
[5ebff60]358
359                        tmp = g_memdup(h->rxq + i, h->rxlen - i);
360                        g_free(h->rxq);
[b7d3cc34]361                        h->rxq = tmp;
362                        h->rxlen -= i;
363                        i = 0;
[5ebff60]364                } else {
365                        /* If not, reset the rx queue and get lost. */
366                        g_free(h->rxq);
367                        h->rxq = g_new0(char, 1);
[b7d3cc34]368                        h->rxlen = 0;
[5ebff60]369                        return(1);
[b7d3cc34]370                }
371        }
[5ebff60]372
373        return(1);
[b7d3cc34]374}
[54794b8]375
[5ebff60]376void msn_msgq_purge(struct im_connection *ic, GSList **list)
[46dca11]377{
378        struct msn_message *m;
379        GString *ret;
380        GSList *l;
[4255320]381        int n = 0;
[5ebff60]382
[46dca11]383        l = *list;
[5ebff60]384        if (l == NULL) {
[46dca11]385                return;
[5ebff60]386        }
387
[46dca11]388        m = l->data;
[5ebff60]389        ret = g_string_sized_new(1024);
390        g_string_printf(ret, "Warning: Cleaning up MSN (switchboard) connection with unsent "
391                        "messages to %s:", m->who ? m->who : "unknown recipient");
392
393        while (l) {
[46dca11]394                m = l->data;
[5ebff60]395
396                if (strncmp(m->text, "\r\r\r", 3) != 0) {
397                        g_string_append_printf(ret, "\n%s", m->text);
398                        n++;
[4255320]399                }
[5ebff60]400
401                g_free(m->who);
402                g_free(m->text);
403                g_free(m);
404
[46dca11]405                l = l->next;
406        }
[5ebff60]407        g_slist_free(*list);
[46dca11]408        *list = NULL;
[5ebff60]409
410        if (n > 0) {
411                imcb_log(ic, "%s", ret->str);
412        }
413        g_string_free(ret, TRUE);
[46dca11]414}
[e3413cc]415
[21029d0]416/* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */
[5ebff60]417char *msn_p11_challenge(char *challenge)
[21029d0]418{
419        char *output, buf[256];
420        md5_state_t md5c;
421        unsigned char md5Hash[16], *newHash;
422        unsigned int *md5Parts, *chlStringParts, newHashParts[5];
423        long long nHigh = 0, nLow = 0;
424        int i, n;
425
426        /* Create the MD5 hash */
427        md5_init(&md5c);
[5ebff60]428        md5_append(&md5c, (unsigned char *) challenge, strlen(challenge));
429        md5_append(&md5c, (unsigned char *) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY));
[21029d0]430        md5_finish(&md5c, md5Hash);
431
432        /* Split it into four integers */
[5ebff60]433        md5Parts = (unsigned int *) md5Hash;
434        for (i = 0; i < 4; i++) {
[523fb23]435                md5Parts[i] = GUINT32_TO_LE(md5Parts[i]);
[5ebff60]436
[21029d0]437                /* & each integer with 0x7FFFFFFF */
438                /* and save one unmodified array for later */
439                newHashParts[i] = md5Parts[i];
440                md5Parts[i] &= 0x7FFFFFFF;
441        }
[5ebff60]442
[21029d0]443        /* make a new string and pad with '0' */
[5ebff60]444        n = g_snprintf(buf, sizeof(buf) - 5, "%s%s00000000", challenge, MSNP11_PROD_ID);
[21029d0]445        /* truncate at an 8-byte boundary */
[5ebff60]446        buf[n &= ~7] = '\0';
447
[21029d0]448        /* split into integers */
[5ebff60]449        chlStringParts = (unsigned int *) buf;
450
[21029d0]451        /* this is magic */
[5ebff60]452        for (i = 0; i < (n / 4) - 1; i += 2) {
[21029d0]453                long long temp;
454
[523fb23]455                chlStringParts[i]   = GUINT32_TO_LE(chlStringParts[i]);
[5ebff60]456                chlStringParts[i + 1] = GUINT32_TO_LE(chlStringParts[i + 1]);
[21029d0]457
[5ebff60]458                temp  =
459                        (md5Parts[0] *
460                         (((0x0E79A9C1 *
461                            (long long) chlStringParts[i]) % 0x7FFFFFFF) + nHigh) + md5Parts[1]) % 0x7FFFFFFF;
462                nHigh =
463                        (md5Parts[2] *
464                         (((long long) chlStringParts[i + 1] + temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF;
[21029d0]465                nLow  = nLow + nHigh + temp;
466        }
[5ebff60]467        nHigh = (nHigh + md5Parts[1]) % 0x7FFFFFFF;
468        nLow = (nLow + md5Parts[3]) % 0x7FFFFFFF;
469
[21029d0]470        newHashParts[0] ^= nHigh;
471        newHashParts[1] ^= nLow;
472        newHashParts[2] ^= nHigh;
473        newHashParts[3] ^= nLow;
[5ebff60]474
[21029d0]475        /* swap more bytes if big endian */
[5ebff60]476        for (i = 0; i < 4; i++) {
477                newHashParts[i] = GUINT32_TO_LE(newHashParts[i]);
478        }
479
[21029d0]480        /* make a string of the parts */
[5ebff60]481        newHash = (unsigned char *) newHashParts;
482
[21029d0]483        /* convert to hexadecimal */
484        output = g_new(char, 33);
[5ebff60]485        for (i = 0; i < 16; i++) {
[21029d0]486                sprintf(output + i * 2, "%02x", newHash[i]);
[5ebff60]487        }
488
[21029d0]489        return output;
490}
[ca7de3a]491
[5ebff60]492gint msn_domaintree_cmp(gconstpointer a_, gconstpointer b_)
[ca7de3a]493{
494        const char *a = a_, *b = b_;
495        gint ret;
[5ebff60]496
497        if (!(a = strchr(a, '@')) || !(b = strchr(b, '@')) ||
498            (ret = strcmp(a, b)) == 0) {
499                ret = strcmp(a_, b_);
500        }
501
[ca7de3a]502        return ret;
503}
[ff27648]504
[5ebff60]505struct msn_group *msn_group_by_name(struct im_connection *ic, const char *name)
[ff27648]506{
507        struct msn_data *md = ic->proto_data;
508        GSList *l;
[5ebff60]509
510        for (l = md->groups; l; l = l->next) {
[ff27648]511                struct msn_group *mg = l->data;
[5ebff60]512
513                if (g_strcasecmp(mg->name, name) == 0) {
[ff27648]514                        return mg;
[5ebff60]515                }
[ff27648]516        }
[5ebff60]517
[ff27648]518        return NULL;
519}
520
[5ebff60]521struct msn_group *msn_group_by_id(struct im_connection *ic, const char *id)
[ff27648]522{
523        struct msn_data *md = ic->proto_data;
524        GSList *l;
[5ebff60]525
526        for (l = md->groups; l; l = l->next) {
[ff27648]527                struct msn_group *mg = l->data;
[5ebff60]528
529                if (g_strcasecmp(mg->id, id) == 0) {
[ff27648]530                        return mg;
[5ebff60]531                }
[ff27648]532        }
[5ebff60]533
[ff27648]534        return NULL;
535}
[e0e1546]536
[5ebff60]537int msn_ns_set_display_name(struct im_connection *ic, const char *value)
[e0e1546]538{
539        struct msn_data *md = ic->proto_data;
[5ebff60]540        char fn[strlen(value) * 3 + 1];
541
542        strcpy(fn, value);
543        http_encode(fn);
544
[e0e1546]545        /* Note: We don't actually know if the server accepted the new name,
546           and won't give proper feedback yet if it doesn't. */
[5ebff60]547        return msn_ns_write(ic, -1, "PRP %d MFN %s\r\n", ++md->trId, fn);
[e0e1546]548}
[79bb7e4]549
[5ebff60]550const char *msn_normalize_handle(const char *handle)
[79bb7e4]551{
[5ebff60]552        if (strncmp(handle, "1:", 2) == 0) {
[79bb7e4]553                return handle + 2;
[5ebff60]554        } else {
[79bb7e4]555                return handle;
[5ebff60]556        }
[79bb7e4]557}
Note: See TracBrowser for help on using the repository browser.