source: protocols/msn/ns.c @ 3752019

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

msn: start update to msnp21

  • bump protocol version
  • update CVR parameters
  • don't send BLP, send ADL right after getting contact list
  • NFY payload size parsing
  • MSNP21 style ADL
  • Property mode set to 100644
File size: 24.9 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);
37static int msn_ns_command(struct msn_handler_data *handler, char **cmd, int num_parts);
38static int msn_ns_message(struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts);
39
40static void msn_ns_send_adl_start(struct im_connection *ic);
41static void msn_ns_send_adl(struct im_connection *ic);
42
43int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...)
44{
45        struct msn_data *md = ic->proto_data;
46        va_list params;
47        char *out;
48        size_t len;
49        int st;
50
51        va_start(params, fmt);
52        out = g_strdup_vprintf(fmt, params);
53        va_end(params);
54
55        if (fd < 0) {
56                fd = md->ns->fd;
57        }
58
59        if (getenv("BITLBEE_DEBUG")) {
60                fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, out);
61        }
62
63        len = strlen(out);
64        st = write(fd, out, len);
65        g_free(out);
66        if (st != len) {
67                imcb_error(ic, "Short write() to main server");
68                imc_logout(ic, TRUE);
69                return 0;
70        }
71
72        return 1;
73}
74
75gboolean msn_ns_connect(struct im_connection *ic, struct msn_handler_data *handler, const char *host, int port)
76{
77        if (handler->fd >= 0) {
78                closesocket(handler->fd);
79        }
80
81        handler->exec_command = msn_ns_command;
82        handler->exec_message = msn_ns_message;
83        handler->data = ic;
84        handler->fd = proxy_connect(host, port, msn_ns_connected, handler);
85        if (handler->fd < 0) {
86                imcb_error(ic, "Could not connect to server");
87                imc_logout(ic, TRUE);
88                return FALSE;
89        }
90
91        return TRUE;
92}
93
94static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
95{
96        struct msn_handler_data *handler = data;
97        struct im_connection *ic = handler->data;
98        struct msn_data *md;
99
100        if (!g_slist_find(msn_connections, ic)) {
101                return FALSE;
102        }
103
104        md = ic->proto_data;
105
106        if (source == -1) {
107                imcb_error(ic, "Could not connect to server");
108                imc_logout(ic, TRUE);
109                return FALSE;
110        }
111
112        g_free(handler->rxq);
113        handler->rxlen = 0;
114        handler->rxq = g_new0(char, 1);
115
116        if (md->uuid == NULL) {
117                struct utsname name;
118                sha1_state_t sha[1];
119
120                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
121                sha1_init(sha);
122                sha1_append(sha, (void *) "BitlBee", 7);
123                if (uname(&name) == 0) {
124                        sha1_append(sha, (void *) name.nodename, strlen(name.nodename));
125                }
126                sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user));
127                md->uuid = sha1_random_uuid(sha);
128                memcpy(md->uuid, "b171be3e", 8);   /* :-P */
129        }
130
131        if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) {
132                handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler);
133                imcb_log(ic, "Connected to server, waiting for reply");
134        }
135
136        return FALSE;
137}
138
139void msn_ns_close(struct msn_handler_data *handler)
140{
141        if (handler->fd >= 0) {
142                closesocket(handler->fd);
143                b_event_remove(handler->inpa);
144        }
145
146        handler->fd = handler->inpa = -1;
147        g_free(handler->rxq);
148        g_free(handler->cmd_text);
149
150        handler->rxlen = 0;
151        handler->rxq = NULL;
152        handler->cmd_text = NULL;
153}
154
155static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
156{
157        struct msn_handler_data *handler = data;
158        struct im_connection *ic = handler->data;
159
160        if (msn_handler(handler) == -1) {  /* Don't do this on ret == 0, it's already done then. */
161                imcb_error(ic, "Error while reading from server");
162                imc_logout(ic, TRUE);
163
164                return FALSE;
165        } else {
166                return TRUE;
167        }
168}
169
170static int msn_ns_command(struct msn_handler_data *handler, char **cmd, int num_parts)
171{
172        struct im_connection *ic = handler->data;
173        struct msn_data *md = ic->proto_data;
174
175        if (num_parts == 0) {
176                /* Hrrm... Empty command...? Ignore? */
177                return(1);
178        }
179
180        if (strcmp(cmd[0], "VER") == 0) {
181                if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) {
182                        imcb_error(ic, "Unsupported protocol");
183                        imc_logout(ic, FALSE);
184                        return(0);
185                }
186
187                return(msn_ns_write(ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s VmVyc2lvbjogMQ0KWGZyQ291bnQ6IDINClhmclNlbnRVVENUaW1lOiA2MzU2MTQ3OTU5NzgzOTAwMDANCklzR2VvWGZyOiB0cnVlDQo=\r\n",
188                                    ++md->trId, ic->acc->user));
189        } else if (strcmp(cmd[0], "CVR") == 0) {
190                /* We don't give a damn about the information we just received */
191                return msn_ns_write(ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user);
192        } else if (strcmp(cmd[0], "XFR") == 0) {
193                char *server;
194                int port;
195
196                if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) {
197                        b_event_remove(handler->inpa);
198                        handler->inpa = -1;
199
200                        server = strchr(cmd[3], ':');
201                        if (!server) {
202                                imcb_error(ic, "Syntax error");
203                                imc_logout(ic, TRUE);
204                                return(0);
205                        }
206                        *server = 0;
207                        port = atoi(server + 1);
208                        server = cmd[3];
209
210                        imcb_log(ic, "Transferring to other server");
211                        return msn_ns_connect(ic, handler, server, port);
212                } else if (num_parts >= 6 && strcmp(cmd[2], "SB") == 0) {
213                        struct msn_switchboard *sb;
214
215                        server = strchr(cmd[3], ':');
216                        if (!server) {
217                                imcb_error(ic, "Syntax error");
218                                imc_logout(ic, TRUE);
219                                return(0);
220                        }
221                        *server = 0;
222                        port = atoi(server + 1);
223                        server = cmd[3];
224
225                        if (strcmp(cmd[4], "CKI") != 0) {
226                                imcb_error(ic, "Unknown authentication method for switchboard");
227                                imc_logout(ic, TRUE);
228                                return(0);
229                        }
230
231                        debug("Connecting to a new switchboard with key %s", cmd[5]);
232
233                        if ((sb = msn_sb_create(ic, server, port, cmd[5], MSN_SB_NEW)) == NULL) {
234                                /* Although this isn't strictly fatal for the NS connection, it's
235                                   definitely something serious (we ran out of file descriptors?). */
236                                imcb_error(ic, "Could not create new switchboard");
237                                imc_logout(ic, TRUE);
238                                return(0);
239                        }
240
241                        if (md->msgq) {
242                                struct msn_message *m = md->msgq->data;
243                                GSList *l;
244
245                                sb->who = g_strdup(m->who);
246
247                                /* Move all the messages to the first user in the message
248                                   queue to the switchboard message queue. */
249                                l = md->msgq;
250                                while (l) {
251                                        m = l->data;
252                                        l = l->next;
253                                        if (strcmp(m->who, sb->who) == 0) {
254                                                sb->msgq = g_slist_append(sb->msgq, m);
255                                                md->msgq = g_slist_remove(md->msgq, m);
256                                        }
257                                }
258                        }
259                } else {
260                        imcb_error(ic, "Syntax error");
261                        imc_logout(ic, TRUE);
262                        return(0);
263                }
264        } else if (strcmp(cmd[0], "USR") == 0) {
265                if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 &&
266                    strcmp(cmd[3], "S") == 0) {
267                        g_free(md->pp_policy);
268                        md->pp_policy = g_strdup(cmd[4]);
269                        msn_soap_passport_sso_request(ic, cmd[5]);
270                } else if (strcmp(cmd[2], "OK") == 0) {
271                        /* If the number after the handle is 0, the e-mail
272                           address is unverified, which means we can't change
273                           the display name. */
274                        if (cmd[4][0] == '0') {
275                                md->flags |= MSN_EMAIL_UNVERIFIED;
276                        }
277
278                        imcb_log(ic, "Authenticated, getting buddy list");
279                        msn_soap_memlist_request(ic);
280                } else {
281                        imcb_error(ic, "Unknown authentication type");
282                        imc_logout(ic, FALSE);
283                        return(0);
284                }
285        } else if (strcmp(cmd[0], "MSG") == 0) {
286                if (num_parts < 4) {
287                        imcb_error(ic, "Syntax error");
288                        imc_logout(ic, TRUE);
289                        return(0);
290                }
291
292                handler->msglen = atoi(cmd[3]);
293
294                if (handler->msglen <= 0) {
295                        imcb_error(ic, "Syntax error");
296                        imc_logout(ic, TRUE);
297                        return(0);
298                }
299        } else if (strcmp(cmd[0], "ADL") == 0) {
300                if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) {
301                        msn_ns_send_adl(ic);
302                        return msn_ns_finish_login(ic);
303                } else if (num_parts >= 3) {
304                        handler->msglen = atoi(cmd[2]);
305                }
306        } else if (strcmp(cmd[0], "PRP") == 0) {
307                imcb_connected(ic);
308        } else if (strcmp(cmd[0], "CHL") == 0) {
309                char *resp;
310                int st;
311
312                if (num_parts < 3) {
313                        imcb_error(ic, "Syntax error");
314                        imc_logout(ic, TRUE);
315                        return(0);
316                }
317
318                resp = msn_p11_challenge(cmd[2]);
319
320                st =  msn_ns_write(ic, -1, "QRY %d %s %zd\r\n%s",
321                                   ++md->trId, MSNP11_PROD_ID,
322                                   strlen(resp), resp);
323                g_free(resp);
324                return st;
325        } else if (strcmp(cmd[0], "ILN") == 0 || strcmp(cmd[0], "NLN") == 0) {
326                const struct msn_away_state *st;
327                const char *handle;
328                int cap = 0;
329
330                if (num_parts < 6) {
331                        imcb_error(ic, "Syntax error");
332                        imc_logout(ic, TRUE);
333                        return(0);
334                }
335                /* ILN and NLN are more or less the same, except ILN has a trId
336                   at the start, and NLN has a capability field at the end.
337                   Does ILN still exist BTW? */
338                if (cmd[0][1] == 'I') {
339                        cmd++;
340                } else {
341                        cap = atoi(cmd[4]);
342                }
343
344                handle = msn_normalize_handle(cmd[2]);
345                if (strcmp(handle, ic->acc->user) == 0) {
346                        return 1; /* That's me! */
347
348                }
349                http_decode(cmd[3]);
350                imcb_rename_buddy(ic, handle, cmd[3]);
351
352                st = msn_away_state_by_code(cmd[1]);
353                if (!st) {
354                        /* FIXME: Warn/Bomb about unknown away state? */
355                        st = msn_away_state_list + 1;
356                }
357
358                imcb_buddy_status(ic, handle, OPT_LOGGED_IN |
359                                  (st != msn_away_state_list ? OPT_AWAY : 0) |
360                                  (cap & 1 ? OPT_MOBILE : 0),
361                                  st->name, NULL);
362
363                msn_sb_stop_keepalives(msn_sb_by_handle(ic, handle));
364        } else if (strcmp(cmd[0], "FLN") == 0) {
365                const char *handle;
366
367                if (cmd[1] == NULL) {
368                        return 1;
369                }
370
371                handle = msn_normalize_handle(cmd[1]);
372                imcb_buddy_status(ic, handle, 0, NULL, NULL);
373                msn_sb_start_keepalives(msn_sb_by_handle(ic, handle), TRUE);
374        } else if (strcmp(cmd[0], "RNG") == 0) {
375                struct msn_switchboard *sb;
376                char *server;
377                int session, port;
378
379                if (num_parts < 7) {
380                        imcb_error(ic, "Syntax error");
381                        imc_logout(ic, TRUE);
382                        return(0);
383                }
384
385                session = atoi(cmd[1]);
386
387                server = strchr(cmd[2], ':');
388                if (!server) {
389                        imcb_error(ic, "Syntax error");
390                        imc_logout(ic, TRUE);
391                        return(0);
392                }
393                *server = 0;
394                port = atoi(server + 1);
395                server = cmd[2];
396
397                if (strcmp(cmd[3], "CKI") != 0) {
398                        imcb_error(ic, "Unknown authentication method for switchboard");
399                        imc_logout(ic, TRUE);
400                        return(0);
401                }
402
403                debug("Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4]);
404
405                if ((sb = msn_sb_create(ic, server, port, cmd[4], session)) == NULL) {
406                        /* Although this isn't strictly fatal for the NS connection, it's
407                           definitely something serious (we ran out of file descriptors?). */
408                        imcb_error(ic, "Could not create new switchboard");
409                        imc_logout(ic, TRUE);
410                        return(0);
411                } else {
412                        sb->who = g_strdup(msn_normalize_handle(cmd[5]));
413                }
414        } else if (strcmp(cmd[0], "OUT") == 0) {
415                int allow_reconnect = TRUE;
416
417                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
418                        imcb_error(ic, "Someone else logged in with your account");
419                        allow_reconnect = FALSE;
420                } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) {
421                        imcb_error(ic, "Terminating session because of server shutdown");
422                } else {
423                        imcb_error(ic, "Session terminated by remote server (%s)",
424                                   cmd[1] ? cmd[1] : "reason unknown)");
425                }
426
427                imc_logout(ic, allow_reconnect);
428                return(0);
429        } else if (strcmp(cmd[0], "IPG") == 0) {
430                imcb_error(ic, "Received IPG command, we don't handle them yet.");
431
432                handler->msglen = atoi(cmd[1]);
433
434                if (handler->msglen <= 0) {
435                        imcb_error(ic, "Syntax error");
436                        imc_logout(ic, TRUE);
437                        return(0);
438                }
439        }
440#if 0
441        else if (strcmp(cmd[0], "ADG") == 0) {
442                char *group = g_strdup(cmd[3]);
443                int groupnum, i;
444                GSList *l, *next;
445
446                http_decode(group);
447                if (sscanf(cmd[4], "%d", &groupnum) == 1) {
448                        if (groupnum >= md->groupcount) {
449                                md->grouplist = g_renew(char *, md->grouplist, groupnum + 1);
450                                for (i = md->groupcount; i <= groupnum; i++) {
451                                        md->grouplist[i] = NULL;
452                                }
453                                md->groupcount = groupnum + 1;
454                        }
455                        g_free(md->grouplist[groupnum]);
456                        md->grouplist[groupnum] = group;
457                } else {
458                        /* Shouldn't happen, but if it does, give up on the group. */
459                        g_free(group);
460                        imcb_error(ic, "Syntax error");
461                        imc_logout(ic, TRUE);
462                        return 0;
463                }
464
465                for (l = md->grpq; l; l = next) {
466                        struct msn_groupadd *ga = l->data;
467                        next = l->next;
468                        if (g_strcasecmp(ga->group, group) == 0) {
469                                if (!msn_buddy_list_add(ic, "FL", ga->who, ga->who, group)) {
470                                        return 0;
471                                }
472
473                                g_free(ga->group);
474                                g_free(ga->who);
475                                g_free(ga);
476                                md->grpq = g_slist_remove(md->grpq, ga);
477                        }
478                }
479        }
480#endif
481        else if (strcmp(cmd[0], "GCF") == 0) {
482                /* Coming up is cmd[2] bytes of stuff we're supposed to
483                   censore. Meh. */
484                handler->msglen = atoi(cmd[2]);
485        } else if (strcmp(cmd[0], "UBX") == 0) {
486                /* Status message. */
487                if (num_parts >= 3) {
488                        handler->msglen = atoi(cmd[2]);
489                }
490        } else if (strcmp(cmd[0], "NOT") == 0) {
491                /* Some kind of notification, poorly documented but
492                   apparently used to announce address book changes. */
493                if (num_parts >= 2) {
494                        handler->msglen = atoi(cmd[1]);
495                }
496        } else if (strcmp(cmd[0], "NFY") == 0) {
497                if (num_parts >= 3) {
498                        handler->msglen = atoi(cmd[2]);
499                }
500        } else if (strcmp(cmd[0], "UBM") == 0) {
501                if (num_parts >= 7) {
502                        handler->msglen = atoi(cmd[6]);
503                }
504        } else if (strcmp(cmd[0], "QNG") == 0) {
505                ic->flags |= OPT_PONGED;
506        } else if (g_ascii_isdigit(cmd[0][0])) {
507                int num = atoi(cmd[0]);
508                const struct msn_status_code *err = msn_status_by_number(num);
509
510                imcb_error(ic, "Error reported by MSN server: %s", err->text);
511
512                if (err->flags & STATUS_FATAL) {
513                        imc_logout(ic, TRUE);
514                        return(0);
515                }
516
517                /* Oh yes, errors can have payloads too now. Discard them for now. */
518                if (num_parts >= 3) {
519                        handler->msglen = atoi(cmd[2]);
520                }
521        } else {
522                /* debug( "Received unknown command from main server: %s", cmd[0] ); */
523        }
524
525        return(1);
526}
527
528static int msn_ns_message(struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts)
529{
530        struct im_connection *ic = handler->data;
531        char *body;
532        int blen = 0;
533
534        if (!num_parts) {
535                return(1);
536        }
537
538        if ((body = strstr(msg, "\r\n\r\n"))) {
539                body += 4;
540                blen = msglen - (body - msg);
541        }
542
543        if (strcmp(cmd[0], "MSG") == 0) {
544                if (g_strcasecmp(cmd[1], "Hotmail") == 0) {
545                        char *ct = get_rfc822_header(msg, "Content-Type:", msglen);
546
547                        if (!ct) {
548                                return(1);
549                        }
550
551                        if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) {
552                                char *mtype;
553                                char *arg1;
554
555                                if (!body) {
556                                        return(1);
557                                }
558
559                                mtype = get_rfc822_header(body, "Type:", blen);
560                                arg1 = get_rfc822_header(body, "Arg1:", blen);
561
562                                if (mtype && strcmp(mtype, "1") == 0) {
563                                        if (arg1) {
564                                                imcb_log(ic, "The server is going down for maintenance in %s minutes.",
565                                                         arg1);
566                                        }
567                                }
568
569                                g_free(arg1);
570                                g_free(mtype);
571                        } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) {
572                                /* We don't care about this profile for now... */
573                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) {
574                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
575                                        char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen);
576                                        char *folders = get_rfc822_header(body, "Folders-Unread:", blen);
577
578                                        if (inbox && folders) {
579                                                imcb_log(ic,
580                                                         "INBOX contains %s new messages, plus %s messages in other folders.", inbox,
581                                                         folders);
582                                        }
583
584                                        g_free(inbox);
585                                        g_free(folders);
586                                }
587                        } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) {
588                                if (set_getbool(&ic->acc->set, "mail_notifications")) {
589                                        char *from = get_rfc822_header(body, "From-Addr:", blen);
590                                        char *fromname = get_rfc822_header(body, "From:", blen);
591
592                                        if (from && fromname) {
593                                                imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname,
594                                                         from);
595                                        }
596
597                                        g_free(from);
598                                        g_free(fromname);
599                                }
600                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
601                        } else if (g_strncasecmp(ct, "text/x-msmsgsinitialmdatanotification", 37) == 0 ||
602                                   g_strncasecmp(ct, "text/x-msmsgsoimnotification", 28) == 0) {
603                                /* We received an offline message. Or at least notification
604                                   that there is one waiting for us. Fetching the message(s)
605                                   and purging them from the server is a lot of SOAPy work
606                                   not worth doing IMHO. Also I thought it was possible to
607                                   have the notification server send them directly, I was
608                                   pretty sure I saw Pidgin do it..
609
610                                   At least give a notification for now, seems like a
611                                   reasonable thing to do. Only problem is, they'll keep
612                                   coming back at login time until you read them using a
613                                   different client. :-( */
614
615                                char *xml = get_rfc822_header(body, "Mail-Data:", blen);
616                                struct xt_node *md, *m;
617
618                                if (!xml) {
619                                        return 1;
620                                }
621                                md = xt_from_string(xml, 0);
622                                if (!md) {
623                                        return 1;
624                                }
625
626                                for (m = md->children; (m = xt_find_node(m, "M")); m = m->next) {
627                                        struct xt_node *e = xt_find_node(m->children, "E");
628                                        struct xt_node *rt = xt_find_node(m->children, "RT");
629                                        struct tm tp;
630                                        time_t msgtime = 0;
631
632                                        if (!e || !e->text) {
633                                                continue;
634                                        }
635
636                                        memset(&tp, 0, sizeof(tp));
637                                        if (rt && rt->text &&
638                                            sscanf(rt->text, "%4d-%2d-%2dT%2d:%2d:%2d.",
639                                                   &tp.tm_year, &tp.tm_mon, &tp.tm_mday,
640                                                   &tp.tm_hour, &tp.tm_min, &tp.tm_sec) == 6) {
641                                                tp.tm_year -= 1900;
642                                                tp.tm_mon--;
643                                                msgtime = mktime_utc(&tp);
644
645                                        }
646                                        imcb_buddy_msg(ic, e->text,
647                                                       "<< \002BitlBee\002 - Received offline message. BitlBee can't show these. >>", 0,
648                                                       msgtime);
649                                }
650
651                                g_free(xml);
652                                xt_free_node(md);
653                        } else {
654                                debug("Can't handle %s packet from notification server", ct);
655                        }
656
657                        g_free(ct);
658                }
659        } else if (strcmp(cmd[0], "UBX") == 0) {
660                struct xt_node *ubx, *psm;
661                char *psm_text = NULL;
662
663                ubx = xt_from_string(msg, msglen);
664                if (ubx && strcmp(ubx->name, "Data") == 0 &&
665                    (psm = xt_find_node(ubx->children, "PSM"))) {
666                        psm_text = psm->text;
667                }
668
669                imcb_buddy_status_msg(ic, msn_normalize_handle(cmd[1]), psm_text);
670                xt_free_node(ubx);
671        } else if (strcmp(cmd[0], "ADL") == 0) {
672                struct xt_node *adl, *d, *c;
673
674                if (!(adl = xt_from_string(msg, msglen))) {
675                        return 1;
676                }
677
678                for (d = adl->children; d; d = d->next) {
679                        char *dn;
680                        if (strcmp(d->name, "d") != 0 ||
681                            (dn = xt_find_attr(d, "n")) == NULL) {
682                                continue;
683                        }
684                        for (c = d->children; c; c = c->next) {
685                                bee_user_t *bu;
686                                struct msn_buddy_data *bd;
687                                char *cn, *handle, *f, *l;
688                                int flags;
689
690                                if (strcmp(c->name, "c") != 0 ||
691                                    (l = xt_find_attr(c, "l")) == NULL ||
692                                    (cn = xt_find_attr(c, "n")) == NULL) {
693                                        continue;
694                                }
695
696                                /* FIXME: Use "t" here, guess I should just add it
697                                   as a prefix like elsewhere in the protocol. */
698                                handle = g_strdup_printf("%s@%s", cn, dn);
699                                if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) ||
700                                      (bu = bee_user_new(ic->bee, ic, handle, 0)))) {
701                                        g_free(handle);
702                                        continue;
703                                }
704                                g_free(handle);
705                                bd = bu->data;
706
707                                if ((f = xt_find_attr(c, "f"))) {
708                                        http_decode(f);
709                                        imcb_rename_buddy(ic, bu->handle, f);
710                                }
711
712                                flags = atoi(l) & 15;
713                                if (bd->flags != flags) {
714                                        bd->flags = flags;
715                                        msn_buddy_ask(bu);
716                                }
717                        }
718                }
719        } else if (strcmp(cmd[0], "UBM") == 0) {
720                /* This one will give us msgs from federated networks. Technically
721                   it should also get us offline messages, but I don't know how
722                   I can signal MSN servers to use it. */
723                char *ct, *handle;
724
725                if (strcmp(cmd[1], ic->acc->user) == 0) {
726                        /* With MPOP, you'll get copies of your own msgs from other
727                           sessions. Discard those at least for now. */
728                        return 1;
729                }
730
731                ct = get_rfc822_header(msg, "Content-Type", msglen);
732                if (strncmp(ct, "text/plain", 10) != 0) {
733                        /* Typing notification or something? */
734                        g_free(ct);
735                        return 1;
736                }
737                if (strcmp(cmd[2], "1") != 0) {
738                        handle = g_strdup_printf("%s:%s", cmd[2], cmd[1]);
739                } else {
740                        handle = g_strdup(cmd[1]);
741                }
742
743                imcb_buddy_msg(ic, handle, body, 0, 0);
744                g_free(handle);
745        }
746
747        return 1;
748}
749
750void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
751{
752        struct msn_data *md;
753
754        /* Dead connection? */
755        if (g_slist_find(msn_connections, ic) == NULL) {
756                return;
757        }
758
759        md = ic->proto_data;
760
761        if (token) {
762                msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid);
763        } else {
764                imcb_error(ic, "Error during Passport authentication: %s", error);
765                imc_logout(ic, TRUE);
766        }
767}
768
769void msn_auth_got_contact_list(struct im_connection *ic)
770{
771        /* Dead connection? */
772        if (g_slist_find(msn_connections, ic) == NULL) {
773                return;
774        }
775
776        msn_ns_send_adl_start(ic);
777        msn_ns_finish_login(ic);
778}
779
780static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
781{
782        struct xt_node *adl = data, *d, *c, *s;
783        struct bee_user *bu = value;
784        struct msn_buddy_data *bd = bu->data;
785        struct msn_data *md = bu->ic->proto_data;
786        char handle[strlen(bu->handle) + 1];
787        char *domain;
788        char l[4];
789
790        if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) {
791                return FALSE;
792        }
793
794        strcpy(handle, bu->handle);
795        if ((domain = strchr(handle, '@')) == NULL) {    /* WTF */
796                return FALSE;
797        }
798        *domain = '\0';
799        domain++;
800
801        if ((d = adl->children) == NULL ||
802            g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) {
803                d = xt_new_node("d", NULL, NULL);
804                xt_add_attr(d, "n", domain);
805                xt_insert_child(adl, d);
806        }
807
808        g_snprintf(l, sizeof(l), "%d", bd->flags & 7);
809        c = xt_new_node("c", NULL, NULL);
810        xt_add_attr(c, "n", handle);
811        xt_add_attr(c, "t", "1");   /* FIXME: Network type, i.e. 32 for Y!MSG */
812        s = xt_new_node("s", NULL, NULL);
813        xt_add_attr(s, "n", "IM");
814        xt_add_attr(s, "l", l);
815        xt_insert_child(c, s);
816        xt_insert_child(d, c);
817
818        /* Do this in batches of 100. */
819        bd->flags |= MSN_BUDDY_ADL_SYNCED;
820        return (--md->adl_todo % 140) == 0;
821}
822
823static void msn_ns_send_adl(struct im_connection *ic)
824{
825        struct xt_node *adl;
826        struct msn_data *md = ic->proto_data;
827        char *adls;
828
829        adl = xt_new_node("ml", NULL, NULL);
830        xt_add_attr(adl, "l", "1");
831        g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl);
832        if (adl->children == NULL) {
833                /* This tells the caller that we're done now. */
834                md->adl_todo = -1;
835                xt_free_node(adl);
836                return;
837        }
838
839        adls = xt_to_string(adl);
840        xt_free_node(adl);
841        msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls);
842        g_free(adls);
843}
844
845static void msn_ns_send_adl_start(struct im_connection *ic)
846{
847        struct msn_data *md;
848        GSList *l;
849
850        /* Dead connection? */
851        if (g_slist_find(msn_connections, ic) == NULL) {
852                return;
853        }
854
855        md = ic->proto_data;
856        md->adl_todo = 0;
857        for (l = ic->bee->users; l; l = l->next) {
858                bee_user_t *bu = l->data;
859                struct msn_buddy_data *bd = bu->data;
860
861                if (bu->ic != ic || (bd->flags & 7) == 0) {
862                        continue;
863                }
864
865                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
866                md->adl_todo++;
867        }
868
869        msn_ns_send_adl(ic);
870}
871
872int msn_ns_finish_login(struct im_connection *ic)
873{
874        struct msn_data *md = ic->proto_data;
875
876        if (ic->flags & OPT_LOGGED_IN) {
877                return 1;
878        }
879
880        if (md->adl_todo < 0) {
881                md->flags |= MSN_DONE_ADL;
882        }
883
884        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
885                if (md->flags & MSN_EMAIL_UNVERIFIED) {
886                        imcb_connected(ic);
887                } else {
888                        return msn_ns_set_display_name(ic, set_getstr(&ic->acc->set, "display_name"));
889                }
890        }
891
892        return 1;
893}
894
895int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
896{
897        struct msn_data *md = ic->proto_data;
898        int type = 0;
899        char *buf, *handle;
900
901        if (strncmp(text, "\r\r\r", 3) == 0) {
902                /* Err. Shouldn't happen but I guess it can. Don't send others
903                   any of the "SHAKE THAT THING" messages. :-D */
904                return 1;
905        }
906
907        /* This might be a federated contact. Get its network number,
908           prefixed to bu->handle with a colon. Default is 1. */
909        for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) {
910                type = type * 10 + *handle - '0';
911        }
912        if (*handle == ':') {
913                handle++;
914        } else {
915                type = 1;
916        }
917
918        buf = g_strdup_printf("%s%s", MSN_MESSAGE_HEADERS, text);
919
920        if (msn_ns_write(ic, -1, "UUM %d %s %d %d %zd\r\n%s",
921                         ++md->trId, handle, type,
922                         1,          /* type == IM (not nudge/typing) */
923                         strlen(buf), buf)) {
924                return 1;
925        } else {
926                return 0;
927        }
928}
929
930void msn_ns_oim_send_queue(struct im_connection *ic, GSList **msgq)
931{
932        GSList *l;
933
934        for (l = *msgq; l; l = l->next) {
935                struct msn_message *m = l->data;
936                bee_user_t *bu = bee_user_by_handle(ic->bee, ic, m->who);
937
938                if (bu) {
939                        if (!msn_ns_sendmessage(ic, bu, m->text)) {
940                                return;
941                        }
942                }
943        }
944
945        while (*msgq != NULL) {
946                struct msn_message *m = (*msgq)->data;
947
948                *msgq = g_slist_remove(*msgq, m);
949                g_free(m->who);
950                g_free(m->text);
951                g_free(m);
952        }
953}
Note: See TracBrowser for help on using the repository browser.