source: protocols/msn/ns.c @ 5fbf815

Last change on this file since 5fbf815 was 5fbf815, checked in by dequis <dx@…>, at 2015-04-11T19:36:13Z

msn: rename all old 'handler' variables to 'md' for consistency

I avoided doing this before to keep the diff small. But meh.

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