source: protocols/msn/ns.c @ c408298

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

msn: add msn_queue_feed(), move read() out of msn_handler()

  • Property mode set to 100644
File size: 16.2 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 - Notification server callbacks                           */
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 <ctype.h>
27#include <sys/utsname.h>
28#include "nogaim.h"
29#include "msn.h"
30#include "md5.h"
31#include "sha1.h"
32#include "soap.h"
33#include "xmltree.h"
34
35static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond);
36static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond);
37
38static void msn_ns_send_adl_start(struct im_connection *ic);
39static void msn_ns_send_adl(struct im_connection *ic);
40
41int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...)
42{
43        struct msn_data *md = ic->proto_data;
44        va_list params;
45        char *out;
46        size_t len;
47        int st;
48
49        va_start(params, fmt);
50        out = g_strdup_vprintf(fmt, params);
51        va_end(params);
52
53        if (fd < 0) {
54                fd = md->fd;
55        }
56
57        if (getenv("BITLBEE_DEBUG")) {
58                fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, out);
59        }
60
61        len = strlen(out);
62        st = write(fd, out, len);
63        g_free(out);
64        if (st != len) {
65                imcb_error(ic, "Short write() to main server");
66                imc_logout(ic, TRUE);
67                return 0;
68        }
69
70        return 1;
71}
72
73gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
74{
75        struct msn_data *handler = ic->proto_data;
76
77        if (handler->fd >= 0) {
78                closesocket(handler->fd);
79        }
80
81        handler->fd = proxy_connect(host, port, msn_ns_connected, handler);
82        if (handler->fd < 0) {
83                imcb_error(ic, "Could not connect to server");
84                imc_logout(ic, TRUE);
85                return FALSE;
86        }
87
88        return TRUE;
89}
90
91static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
92{
93        struct msn_data *md = data;
94        struct msn_data *handler = md;
95        struct im_connection *ic = md->ic;
96
97        if (source == -1) {
98                imcb_error(ic, "Could not connect to server");
99                imc_logout(ic, TRUE);
100                return FALSE;
101        }
102
103        g_free(handler->rxq);
104        handler->rxlen = 0;
105        handler->rxq = g_new0(char, 1);
106
107        if (md->uuid == NULL) {
108                struct utsname name;
109                sha1_state_t sha[1];
110
111                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
112                sha1_init(sha);
113                sha1_append(sha, (void *) "BitlBee", 7);
114                if (uname(&name) == 0) {
115                        sha1_append(sha, (void *) name.nodename, strlen(name.nodename));
116                }
117                sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user));
118                md->uuid = sha1_random_uuid(sha);
119                memcpy(md->uuid, "b171be3e", 8);   /* :-P */
120        }
121
122        if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
123                handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler);
124                imcb_log(ic, "Connected to server, waiting for reply");
125        }
126
127        return FALSE;
128}
129
130void msn_ns_close(struct msn_data *handler)
131{
132        if (handler->fd >= 0) {
133                closesocket(handler->fd);
134                b_event_remove(handler->inpa);
135        }
136
137        handler->fd = handler->inpa = -1;
138        g_free(handler->rxq);
139        g_free(handler->cmd_text);
140
141        handler->rxlen = 0;
142        handler->rxq = NULL;
143        handler->cmd_text = NULL;
144}
145
146static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
147{
148        struct msn_data *handler = data;
149        struct im_connection *ic = handler->ic;
150        char bytes[1024];
151        int st;
152
153        st = read(handler->fd, bytes, 1024);
154        if (st <= 0) {
155                imcb_error(ic, "Error while reading from server");
156                imc_logout(ic, TRUE);
157                return FALSE;
158        }
159
160        msn_queue_feed(handler, bytes, st);
161
162        /* Ignore ret == 0, it's already disconnected then. */
163        msn_handler(handler);
164
165        return TRUE;
166       
167}
168
169int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts)
170{
171        struct im_connection *ic = handler->ic;
172        struct msn_data *md = handler;
173
174        if (num_parts == 0) {
175                /* Hrrm... Empty command...? Ignore? */
176                return(1);
177        }
178
179        if (strcmp(cmd[0], "VER") == 0) {
180                if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) {
181                        imcb_error(ic, "Unsupported protocol");
182                        imc_logout(ic, FALSE);
183                        return(0);
184                }
185
186                return(msn_ns_write(ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s VmVyc2lvbjogMQ0KWGZyQ291bnQ6IDINClhmclNlbnRVVENUaW1lOiA2MzU2MTQ3OTU5NzgzOTAwMDANCklzR2VvWGZyOiB0cnVlDQo=\r\n",
187                                    ++md->trId, ic->acc->user));
188        } else if (strcmp(cmd[0], "CVR") == 0) {
189                /* We don't give a damn about the information we just received */
190                return msn_ns_write(ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user);
191        } else if (strcmp(cmd[0], "XFR") == 0) {
192                char *server;
193                int port;
194
195                if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) {
196                        b_event_remove(handler->inpa);
197                        handler->inpa = -1;
198
199                        server = strchr(cmd[3], ':');
200                        if (!server) {
201                                imcb_error(ic, "Syntax error");
202                                imc_logout(ic, TRUE);
203                                return(0);
204                        }
205                        *server = 0;
206                        port = atoi(server + 1);
207                        server = cmd[3];
208
209                        imcb_log(ic, "Transferring to other server");
210                        return msn_ns_connect(ic, server, port);
211                } else {
212                        imcb_error(ic, "Syntax error");
213                        imc_logout(ic, TRUE);
214                        return(0);
215                }
216        } else if (strcmp(cmd[0], "USR") == 0) {
217                if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 &&
218                    strcmp(cmd[3], "S") == 0) {
219                        g_free(md->pp_policy);
220                        md->pp_policy = g_strdup(cmd[4]);
221                        msn_soap_passport_sso_request(ic, cmd[5]);
222                } else if (strcmp(cmd[2], "OK") == 0) {
223                        /* If the number after the handle is 0, the e-mail
224                           address is unverified, which means we can't change
225                           the display name. */
226                        if (cmd[4][0] == '0') {
227                                md->flags |= MSN_EMAIL_UNVERIFIED;
228                        }
229
230                        imcb_log(ic, "Authenticated, getting buddy list");
231                        msn_soap_memlist_request(ic);
232                } else {
233                        imcb_error(ic, "Unknown authentication type");
234                        imc_logout(ic, FALSE);
235                        return(0);
236                }
237        } else if (strcmp(cmd[0], "MSG") == 0) {
238                if (num_parts < 4) {
239                        imcb_error(ic, "Syntax error");
240                        imc_logout(ic, TRUE);
241                        return(0);
242                }
243
244                handler->msglen = atoi(cmd[3]);
245
246                if (handler->msglen <= 0) {
247                        imcb_error(ic, "Syntax error");
248                        imc_logout(ic, TRUE);
249                        return(0);
250                }
251        } else if (strcmp(cmd[0], "ADL") == 0) {
252                if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) {
253                        msn_ns_send_adl(ic);
254                        return msn_ns_finish_login(ic);
255                } else if (num_parts >= 3) {
256                        handler->msglen = atoi(cmd[2]);
257                }
258        } else if (strcmp(cmd[0], "CHL") == 0) {
259                char *resp;
260                int st;
261
262                if (num_parts < 3) {
263                        imcb_error(ic, "Syntax error");
264                        imc_logout(ic, TRUE);
265                        return(0);
266                }
267
268                resp = msn_p11_challenge(cmd[2]);
269
270                st =  msn_ns_write(ic, -1, "QRY %d %s %zd\r\n%s",
271                                   ++md->trId, MSNP11_PROD_ID,
272                                   strlen(resp), resp);
273                g_free(resp);
274                return st;
275        } else if (strcmp(cmd[0], "OUT") == 0) {
276                int allow_reconnect = TRUE;
277
278                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
279                        imcb_error(ic, "Someone else logged in with your account");
280                        allow_reconnect = FALSE;
281                } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) {
282                        imcb_error(ic, "Terminating session because of server shutdown");
283                } else {
284                        imcb_error(ic, "Session terminated by remote server (%s)",
285                                   cmd[1] ? cmd[1] : "reason unknown)");
286                }
287
288                imc_logout(ic, allow_reconnect);
289                return(0);
290        } else if (strcmp(cmd[0], "GCF") == 0) {
291                /* Coming up is cmd[2] bytes of stuff we're supposed to
292                   censore. Meh. */
293                handler->msglen = atoi(cmd[2]);
294        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
295                if (num_parts >= 3) {
296                        handler->msglen = atoi(cmd[2]);
297                }
298        } else if (strcmp(cmd[0], "QNG") == 0) {
299                ic->flags |= OPT_PONGED;
300        } else if (g_ascii_isdigit(cmd[0][0])) {
301                int num = atoi(cmd[0]);
302                const struct msn_status_code *err = msn_status_by_number(num);
303
304                imcb_error(ic, "Error reported by MSN server: %s", err->text);
305
306                if (err->flags & STATUS_FATAL) {
307                        imc_logout(ic, TRUE);
308                        return(0);
309                }
310
311                /* Oh yes, errors can have payloads too now. Discard them for now. */
312                if (num_parts >= 3) {
313                        handler->msglen = atoi(cmd[2]);
314                }
315        } else {
316                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
317        }
318
319        return(1);
320}
321
322int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
323{
324        struct im_connection *ic = handler->ic;
325        char *body;
326        int blen = 0;
327
328        if (!num_parts) {
329                return(1);
330        }
331
332        if ((body = strstr(msg, "\r\n\r\n"))) {
333                body += 4;
334                blen = msglen - (body - msg);
335        }
336
337        if (strcmp(cmd[0], "MSG") == 0) {
338                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
339                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
340
341                        if (!ct) {
342                                return(1);
343                        }
344
345                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
346                                char *mtype;
347                                char *arg1;
348
349                                if (!body) {
350                                        return(1);
351                                }
352
353                                mtype = get_rfc822_header(body, "Type:", blen);
354                                arg1 = get_rfc822_header(body, "Arg1:", blen);
355
356                                if (mtype && strcmp(mtype, "1") == 0) {
357                                        if (arg1) {
358                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
359                                                         arg1);
360                                        }
361                                }
362
363                                g_free(arg1);
364                                g_free(mtype);
365                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
366                                /* We don't care about this profile for now... */
367                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
368                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
369                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
370                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
371
372                                        if (inbox && folders) {
373                                                imcb_log(ic,
374                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
375                                                         folders);
376                                        }
377
378                                        g_free(inbox);
379                                        g_free(folders);
380                                }
381                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
382                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
383                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
384                                        char *fromname = get_rfc822_header(body, "From:", blen);
385
386                                        if (from && fromname) {
387                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
388                                                         from);
389                                        }
390
391                                        g_free(from);
392                                        g_free(fromname);
393                                }
394                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
395                                /* Notification that a message has been read... Ignore it */
396                        } else {
397                                debug("Can't handle %s packet from notification server", ct);
398                        }
399
400                        g_free(ct);
401                }
402        } else if (strcmp(cmd[0], "ADL") == 0) {
403                struct xt_node *adl, *d, *c;
404
405                if (!(adl = xt_from_string(msg, msglen))) {
406                        return 1;
407                }
408
409                for (d = adl->children; d; d = d->next) {
410                        char *dn;
411                        if (strcmp(d->name, "d") != 0 ||
412                            (dn = xt_find_attr(d, "n")) == NULL) {
413                                continue;
414                        }
415                        for (c = d->children; c; c = c->next) {
416                                bee_user_t *bu;
417                                struct msn_buddy_data *bd;
418                                char *cn, *handle, *f, *l;
419                                int flags;
420
421                                if (strcmp(c->name, "c") != 0 ||
422                                    (l = xt_find_attr(c, "l")) == NULL ||
423                                    (cn = xt_find_attr(c, "n")) == NULL) {
424                                        continue;
425                                }
426
427                                /* FIXME: Use "t" here, guess I should just add it
428                                   as a prefix like elsewhere in the protocol. */
429                                handle = g_strdup_printf("%s@%s", cn, dn);
430                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
431                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
432                                        g_free(handle);
433                                        continue;
434                                }
435                                g_free(handle);
436                                bd = bu->data;
437
438                                if ((f = xt_find_attr(c, "f"))) {
439                                        http_decode(f);
440                                        imcb_rename_buddy(ic, bu->handle, f);
441                                }
442
443                                flags = atoi(l) & 15;
444                                if (bd->flags != flags) {
445                                        bd->flags = flags;
446                                        msn_buddy_ask(bu);
447                                }
448                        }
449                }
450        } else if (strcmp(cmd[0], "SDG") == 0) {
451                char **parts = g_strsplit(msg, "\r\n\r\n", 4);
452                char *from = NULL;
453                char *mt = NULL;
454                char *who = NULL;
455                char *s = NULL;
456
457                if ((from = get_rfc822_header(parts[0], "From", 0)) &&
458                    (mt = get_rfc822_header(parts[2], "Message-Type", 0)) &&
459                    (s = strchr(from, ';'))) {
460
461                        who = g_strndup(from + 2, s - from - 2);
462
463                        if (strcmp(mt, "Control/Typing") == 0) {
464                                imcb_buddy_typing(ic, who, OPT_TYPING);
465                        } else if (strcmp(mt, "Text") == 0) {
466                                imcb_buddy_msg(ic, who, parts[3], 0, 0);
467                        }
468                }
469                g_free(from);
470                g_free(mt);
471                g_free(who);
472                return 1;
473        }
474
475        return 1;
476}
477
478void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
479{
480        struct msn_data *md;
481
482        /* Dead connection? */
483        if (g_slist_find(msn_connections, ic) == NULL) {
484                return;
485        }
486
487        md = ic->proto_data;
488
489        if (token) {
490                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
491        } else {
492                imcb_error(ic, "Error during Passport authentication: %s", error);
493                imc_logout(ic, TRUE);
494        }
495}
496
497void msn_auth_got_contact_list(struct im_connection *ic)
498{
499        /* Dead connection? */
500        if (g_slist_find(msn_connections, ic) == NULL) {
501                return;
502        }
503
504        msn_ns_send_adl_start(ic);
505        msn_ns_finish_login(ic);
506}
507
508static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
509{
510        struct xt_node *adl = data, *d, *c, *s;
511        struct bee_user *bu = value;
512        struct msn_buddy_data *bd = bu->data;
513        struct msn_data *md = bu->ic->proto_data;
514        char handle[strlen(bu->handle) + 1];
515        char *domain;
516        char l[4];
517
518        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
519                return FALSE;
520        }
521
522        strcpy(handle, bu->handle);
523        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
524                return FALSE;
525        }
526        *domain = '\0';
527        domain++;
528
529        if ((d = adl->children) == NULL ||
530            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
531                d = xt_new_node("d", NULL, NULL);
532                xt_add_attr(d, "n", domain);
533                xt_insert_child(adl, d);
534        }
535
536        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
537        c = xt_new_node("c", NULL, NULL);
538        xt_add_attr(c, "n", handle);
539        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
540        s = xt_new_node("s", NULL, NULL);
541        xt_add_attr(s, "n", "IM");
542        xt_add_attr(s, "l", l);
543        xt_insert_child(c, s);
544        xt_insert_child(d, c);
545
546        /* Do this in batches of 100. */
547        bd->flags |= MSN_BUDDY_ADL_SYNCED;
548        return (--md->adl_todo % 140) == 0;
549}
550
551static void msn_ns_send_adl(struct im_connection *ic)
552{
553        struct xt_node *adl;
554        struct msn_data *md = ic->proto_data;
555        char *adls;
556
557        adl = xt_new_node("ml", NULL, NULL);
558        xt_add_attr(adl, "l", "1");
559        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
560        if (adl->children == NULL) {
561                /* This tells the caller that we're done now. */
562                md->adl_todo = -1;
563                xt_free_node(adl);
564                return;
565        }
566
567        adls = xt_to_string(adl);
568        xt_free_node(adl);
569        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
570        g_free(adls);
571}
572
573static void msn_ns_send_adl_start(struct im_connection *ic)
574{
575        struct msn_data *md;
576        GSList *l;
577
578        /* Dead connection? */
579        if (g_slist_find(msn_connections, ic) == NULL) {
580                return;
581        }
582
583        md = ic->proto_data;
584        md->adl_todo = 0;
585        for (l = ic->bee->users; l; l = l->next) {
586                bee_user_t *bu = l->data;
587                struct msn_buddy_data *bd = bu->data;
588
589                if (bu->ic != ic || (bd->flags & 7) == 0) {
590                        continue;
591                }
592
593                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
594                md->adl_todo++;
595        }
596
597        msn_ns_send_adl(ic);
598}
599
600int msn_ns_finish_login(struct im_connection *ic)
601{
602        struct msn_data *md = ic->proto_data;
603
604        if (ic->flags & OPT_LOGGED_IN) {
605                return 1;
606        }
607
608        if (md->adl_todo < 0) {
609                md->flags |= MSN_DONE_ADL;
610        }
611
612        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
613                imcb_connected(ic);
614        }
615
616        return 1;
617}
618
619// TODO: typing notifications, nudges lol, etc
620int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
621{
622        struct msn_data *md = ic->proto_data;
623        int retval = 0;
624        char *buf;
625
626        if (strncmp(text, "\r\r\r", 3) == 0) {
627                /* Err. Shouldn't happen but I guess it can. Don't send others
628                   any of the "SHAKE THAT THING" messages. :-D */
629                return 1;
630        }
631
632        buf = g_strdup_printf(MSN_MESSAGE_HEADERS, bu->handle, ic->acc->user, md->uuid, strlen(text), text);
633        retval = msn_ns_write(ic, -1, "SDG %d %zd\r\n%s", ++md->trId, strlen(buf), buf);
634        g_free(buf);
635        return retval;
636}
Note: See TracBrowser for help on using the repository browser.