source: protocols/msn/msn_util.c @ d550358

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

msn: remove unsupported commands, OIMs and dead code

  • Property mode set to 100644
File size: 10.6 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';
[5ebff60]57
58        if (!((bu = bee_user_by_handle(ic->bee, ic, who)) ||
59              (bu = bee_user_new(ic->bee, ic, who, 0))) ||
60            !(bd = bu->data) || bd->flags & list) {
[193dc74]61                return 1;
[5ebff60]62        }
63
[193dc74]64        bd->flags |= list;
[5ebff60]65
66        if (list == MSN_BUDDY_FL) {
67                msn_soap_ab_contact_add(ic, bu);
68        } else {
69                msn_soap_memlist_edit(ic, who, TRUE, list);
70        }
71
72        if ((adl = adlrml_entry(who, list))) {
73                int st = msn_ns_write(ic, -1, "ADL %d %zd\r\n%s",
74                                      ++md->trId, strlen(adl), adl);
75                g_free(adl);
76
[64768d4]77                return st;
[193dc74]78        }
[5ebff60]79
[6ddb223]80        return 1;
[b7d3cc34]81}
82
[5ebff60]83int msn_buddy_list_remove(struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *group)
[b7d3cc34]84{
[0da65d5]85        struct msn_data *md = ic->proto_data;
[64768d4]86        char groupid[8];
[193dc74]87        bee_user_t *bu;
88        struct msn_buddy_data *bd;
89        char *adl;
[5ebff60]90
[8b01217]91        *groupid = '\0';
[5ebff60]92
93        if (!(bu = bee_user_by_handle(ic->bee, ic, who)) ||
94            !(bd = bu->data) || !(bd->flags & list)) {
[193dc74]95                return 1;
[5ebff60]96        }
97
[193dc74]98        bd->flags &= ~list;
[5ebff60]99
100        if (list == MSN_BUDDY_FL) {
101                msn_soap_ab_contact_del(ic, bu);
102        } else {
103                msn_soap_memlist_edit(ic, who, FALSE, list);
104        }
105
106        if ((adl = adlrml_entry(who, list))) {
107                int st = msn_ns_write(ic, -1, "RML %d %zd\r\n%s",
108                                      ++md->trId, strlen(adl), adl);
109                g_free(adl);
110
[64768d4]111                return st;
[193dc74]112        }
[5ebff60]113
[6ddb223]114        return 1;
[b7d3cc34]115}
116
[5ebff60]117struct msn_buddy_ask_data {
[0da65d5]118        struct im_connection *ic;
[b7d3cc34]119        char *handle;
120        char *realname;
121};
122
[098a75b]123static void msn_buddy_ask_free(void *data)
124{
125        struct msn_buddy_ask_data *bla = data;
126
127        g_free(bla->handle);
128        g_free(bla->realname);
129        g_free(bla);
130}
131
[5ebff60]132static void msn_buddy_ask_yes(void *data)
[b7d3cc34]133{
[9143aeb]134        struct msn_buddy_ask_data *bla = data;
[5ebff60]135
136        msn_buddy_list_add(bla->ic, MSN_BUDDY_AL, bla->handle, bla->realname, NULL);
137
138        imcb_ask_add(bla->ic, bla->handle, NULL);
139
[098a75b]140        msn_buddy_ask_free(bla);
[b7d3cc34]141}
142
[5ebff60]143static void msn_buddy_ask_no(void *data)
[b7d3cc34]144{
[9143aeb]145        struct msn_buddy_ask_data *bla = data;
[5ebff60]146
147        msn_buddy_list_add(bla->ic, MSN_BUDDY_BL, bla->handle, bla->realname, NULL);
148
[098a75b]149        msn_buddy_ask_free(bla);
[b7d3cc34]150}
151
[5ebff60]152void msn_buddy_ask(bee_user_t *bu)
[b7d3cc34]153{
[e5854a8]154        struct msn_buddy_ask_data *bla;
155        struct msn_buddy_data *bd = bu->data;
[b7d3cc34]156        char buf[1024];
[5ebff60]157
158        if (!(bd->flags & MSN_BUDDY_PL)) {
[e5854a8]159                return;
[5ebff60]160        }
161
162        bla = g_new0(struct msn_buddy_ask_data, 1);
[e5854a8]163        bla->ic = bu->ic;
[5ebff60]164        bla->handle = g_strdup(bu->handle);
165        bla->realname = g_strdup(bu->fullname);
166
167        g_snprintf(buf, sizeof(buf),
168                   "The user %s (%s) wants to add you to his/her buddy list.",
169                   bu->handle, bu->fullname);
[098a75b]170
171        imcb_ask_with_free(bu->ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no, msn_buddy_ask_free);
[b7d3cc34]172}
173
174/* *NOT* thread-safe, but that's not a problem for now... */
[5ebff60]175char **msn_linesplit(char *line)
[b7d3cc34]176{
177        static char **ret = NULL;
178        static int size = 3;
179        int i, n = 0;
[5ebff60]180
181        if (ret == NULL) {
182                ret = g_new0(char*, size);
183        }
184
185        for (i = 0; line[i] && line[i] == ' '; i++) {
186                ;
187        }
188        if (line[i]) {
[b7d3cc34]189                ret[n++] = line + i;
[5ebff60]190                for (i++; line[i]; i++) {
191                        if (line[i] == ' ') {
[b7d3cc34]192                                line[i] = 0;
[5ebff60]193                        } else if (line[i] != ' ' && !line[i - 1]) {
[b7d3cc34]194                                ret[n++] = line + i;
[5ebff60]195                        }
196
197                        if (n >= size) {
198                                ret = g_renew(char*, ret, size += 2);
199                        }
[b7d3cc34]200                }
201        }
202        ret[n] = NULL;
[5ebff60]203
204        return(ret);
[b7d3cc34]205}
206
207/* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give
208   commands, but sometimes they give additional data (payload). This function tries to handle
209   this all in a nice way and send all data to the right places. */
210
211/* Return values: -1: Read error, abort connection.
212                   0: Command reported error; Abort *immediately*. (The connection does not exist anymore)
213                   1: OK */
214
[11e42dc]215int msn_handler(struct msn_data *h)
[b7d3cc34]216{
[11e42dc]217        struct im_connection *ic = h->ic;
[b7d3cc34]218        int st;
[5ebff60]219
220        h->rxq = g_renew(char, h->rxq, h->rxlen + 1024);
221        st = read(h->fd, h->rxq + h->rxlen, 1024);
[b7d3cc34]222        h->rxlen += st;
[5ebff60]223
224        if (st <= 0) {
[693aca0]225                fprintf(stderr, "\n\x1b[92m<<< [closed]\x1b[97m ");
[5ebff60]226                return(-1);
227        }
228
229        if (getenv("BITLBEE_DEBUG")) {
[693aca0]230                fprintf(stderr, "\n\x1b[92m<<< ");
[5ebff60]231                write(2, h->rxq + h->rxlen - st, st);
[693aca0]232                fprintf(stderr, "\x1b[97m");
[523fb23]233        }
[5ebff60]234
235        while (st) {
[b7d3cc34]236                int i;
[5ebff60]237
238                if (h->msglen == 0) {
239                        for (i = 0; i < h->rxlen; i++) {
240                                if (h->rxq[i] == '\r' || h->rxq[i] == '\n') {
[b7d3cc34]241                                        char *cmd_text, **cmd;
242                                        int count;
[5ebff60]243
244                                        cmd_text = g_strndup(h->rxq, i);
245                                        cmd = msn_linesplit(cmd_text);
246                                        for (count = 0; cmd[count]; count++) {
247                                                ;
248                                        }
[11e42dc]249                                        st = msn_ns_command(h, cmd, count);
[5ebff60]250                                        g_free(cmd_text);
251
[b7d3cc34]252                                        /* If the connection broke, don't continue. We don't even exist anymore. */
[5ebff60]253                                        if (!st) {
254                                                return(0);
255                                        }
256
257                                        if (h->msglen) {
258                                                h->cmd_text = g_strndup(h->rxq, i);
259                                        }
260
[b7d3cc34]261                                        /* Skip to the next non-emptyline */
[5ebff60]262                                        while (i < h->rxlen && (h->rxq[i] == '\r' || h->rxq[i] == '\n')) {
263                                                i++;
264                                        }
265
[b7d3cc34]266                                        break;
267                                }
268                        }
[5ebff60]269
[b7d3cc34]270                        /* If we reached the end of the buffer, there's still an incomplete command there.
271                           Return and wait for more data. */
[5ebff60]272                        if (i == h->rxlen && h->rxq[i - 1] != '\r' && h->rxq[i - 1] != '\n') {
[b7d3cc34]273                                break;
[5ebff60]274                        }
275                } else {
[b7d3cc34]276                        char *msg, **cmd;
277                        int count;
[5ebff60]278
[b7d3cc34]279                        /* Do we have the complete message already? */
[5ebff60]280                        if (h->msglen > h->rxlen) {
[b7d3cc34]281                                break;
[5ebff60]282                        }
283
284                        msg = g_strndup(h->rxq, h->msglen);
285                        cmd = msn_linesplit(h->cmd_text);
286                        for (count = 0; cmd[count]; count++) {
287                                ;
288                        }
289
[11e42dc]290                        st = msn_ns_message(h, msg, h->msglen, cmd, count);
[5ebff60]291                        g_free(msg);
292                        g_free(h->cmd_text);
[b7d3cc34]293                        h->cmd_text = NULL;
[5ebff60]294
295                        if (!st) {
296                                return(0);
297                        }
298
[b7d3cc34]299                        i = h->msglen;
300                        h->msglen = 0;
301                }
[5ebff60]302
[b7d3cc34]303                /* More data after this block? */
[5ebff60]304                if (i < h->rxlen) {
[b7d3cc34]305                        char *tmp;
[5ebff60]306
307                        tmp = g_memdup(h->rxq + i, h->rxlen - i);
308                        g_free(h->rxq);
[b7d3cc34]309                        h->rxq = tmp;
310                        h->rxlen -= i;
311                        i = 0;
[5ebff60]312                } else {
313                        /* If not, reset the rx queue and get lost. */
314                        g_free(h->rxq);
315                        h->rxq = g_new0(char, 1);
[b7d3cc34]316                        h->rxlen = 0;
[5ebff60]317                        return(1);
[b7d3cc34]318                }
319        }
[5ebff60]320
321        return(1);
[b7d3cc34]322}
[54794b8]323
[21029d0]324/* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */
[5ebff60]325char *msn_p11_challenge(char *challenge)
[21029d0]326{
327        char *output, buf[256];
328        md5_state_t md5c;
329        unsigned char md5Hash[16], *newHash;
330        unsigned int *md5Parts, *chlStringParts, newHashParts[5];
331        long long nHigh = 0, nLow = 0;
332        int i, n;
333
334        /* Create the MD5 hash */
335        md5_init(&md5c);
[5ebff60]336        md5_append(&md5c, (unsigned char *) challenge, strlen(challenge));
337        md5_append(&md5c, (unsigned char *) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY));
[21029d0]338        md5_finish(&md5c, md5Hash);
339
340        /* Split it into four integers */
[5ebff60]341        md5Parts = (unsigned int *) md5Hash;
342        for (i = 0; i < 4; i++) {
[523fb23]343                md5Parts[i] = GUINT32_TO_LE(md5Parts[i]);
[5ebff60]344
[21029d0]345                /* & each integer with 0x7FFFFFFF */
346                /* and save one unmodified array for later */
347                newHashParts[i] = md5Parts[i];
348                md5Parts[i] &= 0x7FFFFFFF;
349        }
[5ebff60]350
[21029d0]351        /* make a new string and pad with '0' */
[5ebff60]352        n = g_snprintf(buf, sizeof(buf) - 5, "%s%s00000000", challenge, MSNP11_PROD_ID);
[21029d0]353        /* truncate at an 8-byte boundary */
[5ebff60]354        buf[n &= ~7] = '\0';
355
[21029d0]356        /* split into integers */
[5ebff60]357        chlStringParts = (unsigned int *) buf;
358
[21029d0]359        /* this is magic */
[5ebff60]360        for (i = 0; i < (n / 4) - 1; i += 2) {
[21029d0]361                long long temp;
362
[523fb23]363                chlStringParts[i]   = GUINT32_TO_LE(chlStringParts[i]);
[5ebff60]364                chlStringParts[i + 1] = GUINT32_TO_LE(chlStringParts[i + 1]);
[21029d0]365
[5ebff60]366                temp  =
367                        (md5Parts[0] *
368                         (((0x0E79A9C1 *
369                            (long long) chlStringParts[i]) % 0x7FFFFFFF) + nHigh) + md5Parts[1]) % 0x7FFFFFFF;
370                nHigh =
371                        (md5Parts[2] *
372                         (((long long) chlStringParts[i + 1] + temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF;
[21029d0]373                nLow  = nLow + nHigh + temp;
374        }
[5ebff60]375        nHigh = (nHigh + md5Parts[1]) % 0x7FFFFFFF;
376        nLow = (nLow + md5Parts[3]) % 0x7FFFFFFF;
377
[21029d0]378        newHashParts[0] ^= nHigh;
379        newHashParts[1] ^= nLow;
380        newHashParts[2] ^= nHigh;
381        newHashParts[3] ^= nLow;
[5ebff60]382
[21029d0]383        /* swap more bytes if big endian */
[5ebff60]384        for (i = 0; i < 4; i++) {
385                newHashParts[i] = GUINT32_TO_LE(newHashParts[i]);
386        }
387
[21029d0]388        /* make a string of the parts */
[5ebff60]389        newHash = (unsigned char *) newHashParts;
390
[21029d0]391        /* convert to hexadecimal */
392        output = g_new(char, 33);
[5ebff60]393        for (i = 0; i < 16; i++) {
[21029d0]394                sprintf(output + i * 2, "%02x", newHash[i]);
[5ebff60]395        }
396
[21029d0]397        return output;
398}
[ca7de3a]399
[5ebff60]400gint msn_domaintree_cmp(gconstpointer a_, gconstpointer b_)
[ca7de3a]401{
402        const char *a = a_, *b = b_;
403        gint ret;
[5ebff60]404
405        if (!(a = strchr(a, '@')) || !(b = strchr(b, '@')) ||
406            (ret = strcmp(a, b)) == 0) {
407                ret = strcmp(a_, b_);
408        }
409
[ca7de3a]410        return ret;
411}
[ff27648]412
[5ebff60]413struct msn_group *msn_group_by_name(struct im_connection *ic, const char *name)
[ff27648]414{
415        struct msn_data *md = ic->proto_data;
416        GSList *l;
[5ebff60]417
418        for (l = md->groups; l; l = l->next) {
[ff27648]419                struct msn_group *mg = l->data;
[5ebff60]420
421                if (g_strcasecmp(mg->name, name) == 0) {
[ff27648]422                        return mg;
[5ebff60]423                }
[ff27648]424        }
[5ebff60]425
[ff27648]426        return NULL;
427}
428
[5ebff60]429struct msn_group *msn_group_by_id(struct im_connection *ic, const char *id)
[ff27648]430{
431        struct msn_data *md = ic->proto_data;
432        GSList *l;
[5ebff60]433
434        for (l = md->groups; l; l = l->next) {
[ff27648]435                struct msn_group *mg = l->data;
[5ebff60]436
437                if (g_strcasecmp(mg->id, id) == 0) {
[ff27648]438                        return mg;
[5ebff60]439                }
[ff27648]440        }
[5ebff60]441
[ff27648]442        return NULL;
443}
[e0e1546]444
[5ebff60]445int msn_ns_set_display_name(struct im_connection *ic, const char *value)
[e0e1546]446{
[d80822c]447        // TODO, implement this through msn_set_away's method
448        return 1;
[e0e1546]449}
[79bb7e4]450
[5ebff60]451const char *msn_normalize_handle(const char *handle)
[79bb7e4]452{
[5ebff60]453        if (strncmp(handle, "1:", 2) == 0) {
[79bb7e4]454                return handle + 2;
[5ebff60]455        } else {
[79bb7e4]456                return handle;
[5ebff60]457        }
[79bb7e4]458}
Note: See TracBrowser for help on using the repository browser.