source: protocols/msn/msn_util.c @ 6e74911

Last change on this file since 6e74911 was 6e74911, checked in by dequis <dx@…>, at 2015-03-15T14:41:47Z

msn: implement PUT /user to set status as online

  • Property mode set to 100644
File size: 12.4 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
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;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#include "nogaim.h"
27#include "msn.h"
28#include "md5.h"
29#include "soap.h"
30#include <ctype.h>
31
32static char *adlrml_entry(const char *handle_, msn_buddy_flags_t list)
33{
34        char *domain, handle[strlen(handle_) + 1];
35
36        strcpy(handle, handle_);
37        if ((domain = strchr(handle, '@'))) {
38                *(domain++) = '\0';
39        } else {
40                return NULL;
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);
45}
46
47int msn_buddy_list_add(struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *realname,
48                       const char *group)
49{
50        struct msn_data *md = ic->proto_data;
51        char groupid[8];
52        bee_user_t *bu;
53        struct msn_buddy_data *bd;
54        char *adl;
55
56        *groupid = '\0';
57#if 0
58        if (group) {
59                int i;
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);
63                                break;
64                        }
65                }
66
67                if (*groupid == '\0') {
68                        /* Have to create this group, it doesn't exist yet. */
69                        struct msn_groupadd *ga;
70                        GSList *l;
71
72                        for (l = md->grpq; l; l = l->next) {
73                                ga = l->data;
74                                if (g_strcasecmp(ga->group, group) == 0) {
75                                        break;
76                                }
77                        }
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 {
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                }
97        }
98#endif
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) {
103                return 1;
104        }
105
106        bd->flags |= list;
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
119                return st;
120        }
121
122        return 1;
123}
124
125int msn_buddy_list_remove(struct im_connection *ic, msn_buddy_flags_t list, const char *who, const char *group)
126{
127        struct msn_data *md = ic->proto_data;
128        char groupid[8];
129        bee_user_t *bu;
130        struct msn_buddy_data *bd;
131        char *adl;
132
133        *groupid = '\0';
134#if 0
135        if (group) {
136                int i;
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);
140                                break;
141                        }
142                }
143        }
144#endif
145
146        if (!(bu = bee_user_by_handle(ic->bee, ic, who)) ||
147            !(bd = bu->data) || !(bd->flags & list)) {
148                return 1;
149        }
150
151        bd->flags &= ~list;
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
164                return st;
165        }
166
167        return 1;
168}
169
170struct msn_buddy_ask_data {
171        struct im_connection *ic;
172        char *handle;
173        char *realname;
174};
175
176static void msn_buddy_ask_yes(void *data)
177{
178        struct msn_buddy_ask_data *bla = data;
179
180        msn_buddy_list_add(bla->ic, MSN_BUDDY_AL, bla->handle, bla->realname, NULL);
181
182        imcb_ask_add(bla->ic, bla->handle, NULL);
183
184        g_free(bla->handle);
185        g_free(bla->realname);
186        g_free(bla);
187}
188
189static void msn_buddy_ask_no(void *data)
190{
191        struct msn_buddy_ask_data *bla = data;
192
193        msn_buddy_list_add(bla->ic, MSN_BUDDY_BL, bla->handle, bla->realname, NULL);
194
195        g_free(bla->handle);
196        g_free(bla->realname);
197        g_free(bla);
198}
199
200void msn_buddy_ask(bee_user_t *bu)
201{
202        struct msn_buddy_ask_data *bla;
203        struct msn_buddy_data *bd = bu->data;
204        char buf[1024];
205
206        if (!(bd->flags & MSN_BUDDY_PL)) {
207                return;
208        }
209
210        bla = g_new0(struct msn_buddy_ask_data, 1);
211        bla->ic = bu->ic;
212        bla->handle = g_strdup(bu->handle);
213        bla->realname = g_strdup(bu->fullname);
214
215        g_snprintf(buf, sizeof(buf),
216                   "The user %s (%s) wants to add you to his/her buddy list.",
217                   bu->handle, bu->fullname);
218        imcb_ask(bu->ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no);
219}
220
221/* *NOT* thread-safe, but that's not a problem for now... */
222char **msn_linesplit(char *line)
223{
224        static char **ret = NULL;
225        static int size = 3;
226        int i, n = 0;
227
228        if (ret == NULL) {
229                ret = g_new0(char*, size);
230        }
231
232        for (i = 0; line[i] && line[i] == ' '; i++) {
233                ;
234        }
235        if (line[i]) {
236                ret[n++] = line + i;
237                for (i++; line[i]; i++) {
238                        if (line[i] == ' ') {
239                                line[i] = 0;
240                        } else if (line[i] != ' ' && !line[i - 1]) {
241                                ret[n++] = line + i;
242                        }
243
244                        if (n >= size) {
245                                ret = g_renew(char*, ret, size += 2);
246                        }
247                }
248        }
249        ret[n] = NULL;
250
251        return(ret);
252}
253
254/* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give
255   commands, but sometimes they give additional data (payload). This function tries to handle
256   this all in a nice way and send all data to the right places. */
257
258/* Return values: -1: Read error, abort connection.
259                   0: Command reported error; Abort *immediately*. (The connection does not exist anymore)
260                   1: OK */
261
262int msn_handler(struct msn_data *h)
263{
264        struct im_connection *ic = h->ic;
265        int st;
266
267        h->rxq = g_renew(char, h->rxq, h->rxlen + 1024);
268        st = read(h->fd, h->rxq + h->rxlen, 1024);
269        h->rxlen += st;
270
271        if (st <= 0) {
272                fprintf(stderr, "\n\x1b[92m<<< [closed]\x1b[97m ");
273                return(-1);
274        }
275
276        if (getenv("BITLBEE_DEBUG")) {
277                fprintf(stderr, "\n\x1b[92m<<< ");
278                write(2, h->rxq + h->rxlen - st, st);
279                fprintf(stderr, "\x1b[97m");
280        }
281
282        while (st) {
283                int i;
284
285                if (h->msglen == 0) {
286                        for (i = 0; i < h->rxlen; i++) {
287                                if (h->rxq[i] == '\r' || h->rxq[i] == '\n') {
288                                        char *cmd_text, **cmd;
289                                        int count;
290
291                                        cmd_text = g_strndup(h->rxq, i);
292                                        cmd = msn_linesplit(cmd_text);
293                                        for (count = 0; cmd[count]; count++) {
294                                                ;
295                                        }
296                                        st = msn_ns_command(h, cmd, count);
297                                        g_free(cmd_text);
298
299                                        /* If the connection broke, don't continue. We don't even exist anymore. */
300                                        if (!st) {
301                                                return(0);
302                                        }
303
304                                        if (h->msglen) {
305                                                h->cmd_text = g_strndup(h->rxq, i);
306                                        }
307
308                                        /* Skip to the next non-emptyline */
309                                        while (i < h->rxlen && (h->rxq[i] == '\r' || h->rxq[i] == '\n')) {
310                                                i++;
311                                        }
312
313                                        break;
314                                }
315                        }
316
317                        /* If we reached the end of the buffer, there's still an incomplete command there.
318                           Return and wait for more data. */
319                        if (i == h->rxlen && h->rxq[i - 1] != '\r' && h->rxq[i - 1] != '\n') {
320                                break;
321                        }
322                } else {
323                        char *msg, **cmd;
324                        int count;
325
326                        /* Do we have the complete message already? */
327                        if (h->msglen > h->rxlen) {
328                                break;
329                        }
330
331                        msg = g_strndup(h->rxq, h->msglen);
332                        cmd = msn_linesplit(h->cmd_text);
333                        for (count = 0; cmd[count]; count++) {
334                                ;
335                        }
336
337                        st = msn_ns_message(h, msg, h->msglen, cmd, count);
338                        g_free(msg);
339                        g_free(h->cmd_text);
340                        h->cmd_text = NULL;
341
342                        if (!st) {
343                                return(0);
344                        }
345
346                        i = h->msglen;
347                        h->msglen = 0;
348                }
349
350                /* More data after this block? */
351                if (i < h->rxlen) {
352                        char *tmp;
353
354                        tmp = g_memdup(h->rxq + i, h->rxlen - i);
355                        g_free(h->rxq);
356                        h->rxq = tmp;
357                        h->rxlen -= i;
358                        i = 0;
359                } else {
360                        /* If not, reset the rx queue and get lost. */
361                        g_free(h->rxq);
362                        h->rxq = g_new0(char, 1);
363                        h->rxlen = 0;
364                        return(1);
365                }
366        }
367
368        return(1);
369}
370
371void msn_msgq_purge(struct im_connection *ic, GSList **list)
372{
373        struct msn_message *m;
374        GString *ret;
375        GSList *l;
376        int n = 0;
377
378        l = *list;
379        if (l == NULL) {
380                return;
381        }
382
383        m = l->data;
384        ret = g_string_sized_new(1024);
385        g_string_printf(ret, "Warning: Cleaning up MSN (switchboard) connection with unsent "
386                        "messages to %s:", m->who ? m->who : "unknown recipient");
387
388        while (l) {
389                m = l->data;
390
391                if (strncmp(m->text, "\r\r\r", 3) != 0) {
392                        g_string_append_printf(ret, "\n%s", m->text);
393                        n++;
394                }
395
396                g_free(m->who);
397                g_free(m->text);
398                g_free(m);
399
400                l = l->next;
401        }
402        g_slist_free(*list);
403        *list = NULL;
404
405        if (n > 0) {
406                imcb_log(ic, "%s", ret->str);
407        }
408        g_string_free(ret, TRUE);
409}
410
411/* Copied and heavily modified from http://tmsnc.sourceforge.net/chl.c */
412char *msn_p11_challenge(char *challenge)
413{
414        char *output, buf[256];
415        md5_state_t md5c;
416        unsigned char md5Hash[16], *newHash;
417        unsigned int *md5Parts, *chlStringParts, newHashParts[5];
418        long long nHigh = 0, nLow = 0;
419        int i, n;
420
421        /* Create the MD5 hash */
422        md5_init(&md5c);
423        md5_append(&md5c, (unsigned char *) challenge, strlen(challenge));
424        md5_append(&md5c, (unsigned char *) MSNP11_PROD_KEY, strlen(MSNP11_PROD_KEY));
425        md5_finish(&md5c, md5Hash);
426
427        /* Split it into four integers */
428        md5Parts = (unsigned int *) md5Hash;
429        for (i = 0; i < 4; i++) {
430                md5Parts[i] = GUINT32_TO_LE(md5Parts[i]);
431
432                /* & each integer with 0x7FFFFFFF */
433                /* and save one unmodified array for later */
434                newHashParts[i] = md5Parts[i];
435                md5Parts[i] &= 0x7FFFFFFF;
436        }
437
438        /* make a new string and pad with '0' */
439        n = g_snprintf(buf, sizeof(buf) - 5, "%s%s00000000", challenge, MSNP11_PROD_ID);
440        /* truncate at an 8-byte boundary */
441        buf[n &= ~7] = '\0';
442
443        /* split into integers */
444        chlStringParts = (unsigned int *) buf;
445
446        /* this is magic */
447        for (i = 0; i < (n / 4) - 1; i += 2) {
448                long long temp;
449
450                chlStringParts[i]   = GUINT32_TO_LE(chlStringParts[i]);
451                chlStringParts[i + 1] = GUINT32_TO_LE(chlStringParts[i + 1]);
452
453                temp  =
454                        (md5Parts[0] *
455                         (((0x0E79A9C1 *
456                            (long long) chlStringParts[i]) % 0x7FFFFFFF) + nHigh) + md5Parts[1]) % 0x7FFFFFFF;
457                nHigh =
458                        (md5Parts[2] *
459                         (((long long) chlStringParts[i + 1] + temp) % 0x7FFFFFFF) + md5Parts[3]) % 0x7FFFFFFF;
460                nLow  = nLow + nHigh + temp;
461        }
462        nHigh = (nHigh + md5Parts[1]) % 0x7FFFFFFF;
463        nLow = (nLow + md5Parts[3]) % 0x7FFFFFFF;
464
465        newHashParts[0] ^= nHigh;
466        newHashParts[1] ^= nLow;
467        newHashParts[2] ^= nHigh;
468        newHashParts[3] ^= nLow;
469
470        /* swap more bytes if big endian */
471        for (i = 0; i < 4; i++) {
472                newHashParts[i] = GUINT32_TO_LE(newHashParts[i]);
473        }
474
475        /* make a string of the parts */
476        newHash = (unsigned char *) newHashParts;
477
478        /* convert to hexadecimal */
479        output = g_new(char, 33);
480        for (i = 0; i < 16; i++) {
481                sprintf(output + i * 2, "%02x", newHash[i]);
482        }
483
484        return output;
485}
486
487gint msn_domaintree_cmp(gconstpointer a_, gconstpointer b_)
488{
489        const char *a = a_, *b = b_;
490        gint ret;
491
492        if (!(a = strchr(a, '@')) || !(b = strchr(b, '@')) ||
493            (ret = strcmp(a, b)) == 0) {
494                ret = strcmp(a_, b_);
495        }
496
497        return ret;
498}
499
500struct msn_group *msn_group_by_name(struct im_connection *ic, const char *name)
501{
502        struct msn_data *md = ic->proto_data;
503        GSList *l;
504
505        for (l = md->groups; l; l = l->next) {
506                struct msn_group *mg = l->data;
507
508                if (g_strcasecmp(mg->name, name) == 0) {
509                        return mg;
510                }
511        }
512
513        return NULL;
514}
515
516struct msn_group *msn_group_by_id(struct im_connection *ic, const char *id)
517{
518        struct msn_data *md = ic->proto_data;
519        GSList *l;
520
521        for (l = md->groups; l; l = l->next) {
522                struct msn_group *mg = l->data;
523
524                if (g_strcasecmp(mg->id, id) == 0) {
525                        return mg;
526                }
527        }
528
529        return NULL;
530}
531
532int msn_ns_set_display_name(struct im_connection *ic, const char *value)
533{
534        // TODO, implement this through msn_set_away's method
535        return 1;
536}
537
538const char *msn_normalize_handle(const char *handle)
539{
540        if (strncmp(handle, "1:", 2) == 0) {
541                return handle + 2;
542        } else {
543                return handle;
544        }
545}
Note: See TracBrowser for help on using the repository browser.