source: protocols/msn/ns.c @ a4be2f6

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

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

  • Property mode set to 100644
File size: 16.2 KB
RevLine 
[5ebff60]1/********************************************************************\
[b7d3cc34]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[f9258ae]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[b7d3cc34]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;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]24*/
25
26#include <ctype.h>
[f9258ae]27#include <sys/utsname.h>
[b7d3cc34]28#include "nogaim.h"
29#include "msn.h"
30#include "md5.h"
[f9258ae]31#include "sha1.h"
[7db65b7]32#include "soap.h"
[ca7de3a]33#include "xmltree.h"
[b7d3cc34]34
[5ebff60]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);
[b7d3cc34]37
[5ebff60]38static void msn_ns_send_adl_start(struct im_connection *ic);
39static void msn_ns_send_adl(struct im_connection *ic);
[b7d3cc34]40
[5ebff60]41int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...)
[64768d4]42{
43        struct msn_data *md = ic->proto_data;
44        va_list params;
45        char *out;
46        size_t len;
47        int st;
[5ebff60]48
49        va_start(params, fmt);
50        out = g_strdup_vprintf(fmt, params);
51        va_end(params);
52
53        if (fd < 0) {
[11e42dc]54                fd = md->fd;
[5ebff60]55        }
56
57        if (getenv("BITLBEE_DEBUG")) {
[693aca0]58                fprintf(stderr, "\x1b[91m>>>[NS%d] %s\n\x1b[97m", fd, out);
[5ebff60]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);
[64768d4]67                return 0;
68        }
[5ebff60]69
[64768d4]70        return 1;
71}
72
[11e42dc]73gboolean msn_ns_connect(struct im_connection *ic, const char *host, int port)
[b7d3cc34]74{
[11e42dc]75        struct msn_data *handler = ic->proto_data;
76
[5ebff60]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);
[ba9edaa]85                return FALSE;
[b7d3cc34]86        }
[5ebff60]87
[bae0617]88        return TRUE;
89}
90
[5ebff60]91static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond)
[bae0617]92{
[11e42dc]93        struct msn_data *md = data;
94        struct msn_data *handler = md;
95        struct im_connection *ic = md->ic;
[5ebff60]96
97        if (source == -1) {
98                imcb_error(ic, "Could not connect to server");
99                imc_logout(ic, TRUE);
[bae0617]100                return FALSE;
[b7d3cc34]101        }
[5ebff60]102
103        g_free(handler->rxq);
[bae0617]104        handler->rxlen = 0;
[5ebff60]105        handler->rxq = g_new0(char, 1);
106
107        if (md->uuid == NULL) {
[f9258ae]108                struct utsname name;
109                sha1_state_t sha[1];
[5ebff60]110
[f9258ae]111                /* UUID == SHA1("BitlBee" + my hostname + MSN username) */
[5ebff60]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));
[f9258ae]116                }
[5ebff60]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 */
[f9258ae]120        }
[5ebff60]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");
[b7d3cc34]125        }
[5ebff60]126
[ba9edaa]127        return FALSE;
[b7d3cc34]128}
129
[11e42dc]130void msn_ns_close(struct msn_data *handler)
[bae0617]131{
[5ebff60]132        if (handler->fd >= 0) {
133                closesocket(handler->fd);
134                b_event_remove(handler->inpa);
[bae0617]135        }
[5ebff60]136
[bae0617]137        handler->fd = handler->inpa = -1;
[5ebff60]138        g_free(handler->rxq);
139        g_free(handler->cmd_text);
140
[bae0617]141        handler->rxlen = 0;
142        handler->rxq = NULL;
143        handler->cmd_text = NULL;
144}
145
[5ebff60]146static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond)
[b7d3cc34]147{
[11e42dc]148        struct msn_data *handler = data;
149        struct im_connection *ic = handler->ic;
[a4be2f6]150        char bytes[1024];
151        int st;
[5ebff60]152
[a4be2f6]153        st = read(handler->fd, bytes, 1024);
154        if (st <= 0) {
[5ebff60]155                imcb_error(ic, "Error while reading from server");
156                imc_logout(ic, TRUE);
[ba9edaa]157                return FALSE;
[5ebff60]158        }
[a4be2f6]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       
[b7d3cc34]167}
168
[11e42dc]169int msn_ns_command(struct msn_data *handler, char **cmd, int num_parts)
[b7d3cc34]170{
[11e42dc]171        struct im_connection *ic = handler->ic;
172        struct msn_data *md = handler;
[5ebff60]173
174        if (num_parts == 0) {
[b7d3cc34]175                /* Hrrm... Empty command...? Ignore? */
[5ebff60]176                return(1);
[b7d3cc34]177        }
[5ebff60]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);
[b7d3cc34]184                }
[5ebff60]185
[254a4da]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",
[5ebff60]187                                    ++md->trId, ic->acc->user));
188        } else if (strcmp(cmd[0], "CVR") == 0) {
[b7d3cc34]189                /* We don't give a damn about the information we just received */
[5ebff60]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) {
[b7d3cc34]192                char *server;
193                int port;
[5ebff60]194
195                if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) {
196                        b_event_remove(handler->inpa);
[bae0617]197                        handler->inpa = -1;
[5ebff60]198
199                        server = strchr(cmd[3], ':');
200                        if (!server) {
201                                imcb_error(ic, "Syntax error");
202                                imc_logout(ic, TRUE);
203                                return(0);
[b7d3cc34]204                        }
205                        *server = 0;
[5ebff60]206                        port = atoi(server + 1);
[b7d3cc34]207                        server = cmd[3];
[5ebff60]208
209                        imcb_log(ic, "Transferring to other server");
[11e42dc]210                        return msn_ns_connect(ic, server, port);
[5ebff60]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) {
[ed0589c]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. */
[5ebff60]226                        if (cmd[4][0] == '0') {
[ed0589c]227                                md->flags |= MSN_EMAIL_UNVERIFIED;
[5ebff60]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);
[e5854a8]242                }
[5ebff60]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) {
[be7a180]259                char *resp;
[64768d4]260                int st;
[5ebff60]261
262                if (num_parts < 3) {
263                        imcb_error(ic, "Syntax error");
264                        imc_logout(ic, TRUE);
265                        return(0);
[b7d3cc34]266                }
[5ebff60]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);
[64768d4]274                return st;
[5ebff60]275        } else if (strcmp(cmd[0], "OUT") == 0) {
[c2fb3809]276                int allow_reconnect = TRUE;
[5ebff60]277
278                if (cmd[1] && strcmp(cmd[1], "OTH") == 0) {
279                        imcb_error(ic, "Someone else logged in with your account");
[c2fb3809]280                        allow_reconnect = FALSE;
[5ebff60]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)");
[b7d3cc34]286                }
[5ebff60]287
288                imc_logout(ic, allow_reconnect);
289                return(0);
[d550358]290        } else if (strcmp(cmd[0], "GCF") == 0) {
[5fecede]291                /* Coming up is cmd[2] bytes of stuff we're supposed to
292                   censore. Meh. */
[5ebff60]293                handler->msglen = atoi(cmd[2]);
[11e42dc]294        } else if ((strcmp(cmd[0], "NFY") == 0) || (strcmp(cmd[0], "SDG") == 0)) {
[254a4da]295                if (num_parts >= 3) {
296                        handler->msglen = atoi(cmd[2]);
297                }
[5ebff60]298        } else if (strcmp(cmd[0], "QNG") == 0) {
[e132b60]299                ic->flags |= OPT_PONGED;
[5ebff60]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);
[b7d3cc34]309                }
[5ebff60]310
[02bb9db]311                /* Oh yes, errors can have payloads too now. Discard them for now. */
[5ebff60]312                if (num_parts >= 3) {
313                        handler->msglen = atoi(cmd[2]);
314                }
315        } else {
[d550358]316                imcb_error(ic, "Received unknown command from main server: %s", cmd[0]);
[b7d3cc34]317        }
[5ebff60]318
319        return(1);
[b7d3cc34]320}
321
[11e42dc]322int msn_ns_message(struct msn_data *handler, char *msg, int msglen, char **cmd, int num_parts)
[b7d3cc34]323{
[11e42dc]324        struct im_connection *ic = handler->ic;
[b7d3cc34]325        char *body;
326        int blen = 0;
[5ebff60]327
328        if (!num_parts) {
329                return(1);
330        }
331
332        if ((body = strstr(msg, "\r\n\r\n"))) {
[b7d3cc34]333                body += 4;
[5ebff60]334                blen = msglen - (body - msg);
[b7d3cc34]335        }
[5ebff60]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) {
[b7d3cc34]346                                char *mtype;
347                                char *arg1;
[5ebff60]348
349                                if (!body) {
350                                        return(1);
[b7d3cc34]351                                }
[5ebff60]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) {
[b7d3cc34]366                                /* We don't care about this profile for now... */
[5ebff60]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);
[b7d3cc34]380                                }
[5ebff60]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);
[b7d3cc34]393                                }
[5ebff60]394                        } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) {
[d550358]395                                /* Notification that a message has been read... Ignore it */
[5ebff60]396                        } else {
397                                debug("Can't handle %s packet from notification server", ct);
[b7d3cc34]398                        }
[5ebff60]399
400                        g_free(ct);
[b7d3cc34]401                }
[5ebff60]402        } else if (strcmp(cmd[0], "ADL") == 0) {
[e5854a8]403                struct xt_node *adl, *d, *c;
[5ebff60]404
405                if (!(adl = xt_from_string(msg, msglen))) {
[e5854a8]406                        return 1;
[5ebff60]407                }
408
409                for (d = adl->children; d; d = d->next) {
[e5854a8]410                        char *dn;
[5ebff60]411                        if (strcmp(d->name, "d") != 0 ||
412                            (dn = xt_find_attr(d, "n")) == NULL) {
[e5854a8]413                                continue;
[5ebff60]414                        }
415                        for (c = d->children; c; c = c->next) {
[e5854a8]416                                bee_user_t *bu;
417                                struct msn_buddy_data *bd;
418                                char *cn, *handle, *f, *l;
419                                int flags;
[5ebff60]420
421                                if (strcmp(c->name, "c") != 0 ||
422                                    (l = xt_find_attr(c, "l")) == NULL ||
423                                    (cn = xt_find_attr(c, "n")) == NULL) {
[e5854a8]424                                        continue;
[5ebff60]425                                }
426
[3901b5d]427                                /* FIXME: Use "t" here, guess I should just add it
428                                   as a prefix like elsewhere in the protocol. */
[5ebff60]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);
[e5854a8]433                                        continue;
434                                }
[5ebff60]435                                g_free(handle);
[e5854a8]436                                bd = bu->data;
[5ebff60]437
438                                if ((f = xt_find_attr(c, "f"))) {
439                                        http_decode(f);
440                                        imcb_rename_buddy(ic, bu->handle, f);
[e5854a8]441                                }
[5ebff60]442
443                                flags = atoi(l) & 15;
444                                if (bd->flags != flags) {
[e5854a8]445                                        bd->flags = flags;
[5ebff60]446                                        msn_buddy_ask(bu);
[e5854a8]447                                }
448                        }
449                }
[11e42dc]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;
[5ebff60]456
[11e42dc]457                if ((from = get_rfc822_header(parts[0], "From", 0)) &&
458                    (mt = get_rfc822_header(parts[2], "Message-Type", 0)) &&
459                    (s = strchr(from, ';'))) {
[5ebff60]460
[11e42dc]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;
[3901b5d]473        }
[5ebff60]474
[3901b5d]475        return 1;
[b7d3cc34]476}
477
[5ebff60]478void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error)
[b7d3cc34]479{
[d84e2a9]480        struct msn_data *md;
[5ebff60]481
[d84e2a9]482        /* Dead connection? */
[5ebff60]483        if (g_slist_find(msn_connections, ic) == NULL) {
[d84e2a9]484                return;
[b7d3cc34]485        }
[5ebff60]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);
[660cb00]494        }
[b7d3cc34]495}
[e3413cc]496
[5ebff60]497void msn_auth_got_contact_list(struct im_connection *ic)
[ca7de3a]498{
499        /* Dead connection? */
[5ebff60]500        if (g_slist_find(msn_connections, ic) == NULL) {
[ca7de3a]501                return;
[5ebff60]502        }
503
[254a4da]504        msn_ns_send_adl_start(ic);
505        msn_ns_finish_login(ic);
[ca7de3a]506}
507
[5ebff60]508static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data)
[ca7de3a]509{
[254a4da]510        struct xt_node *adl = data, *d, *c, *s;
[ca7de3a]511        struct bee_user *bu = value;
512        struct msn_buddy_data *bd = bu->data;
[5a7af1b]513        struct msn_data *md = bu->ic->proto_data;
[4f161e3]514        char handle[strlen(bu->handle) + 1];
[ca7de3a]515        char *domain;
516        char l[4];
[5ebff60]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 */
[e5854a8]524                return FALSE;
[5ebff60]525        }
[ca7de3a]526        *domain = '\0';
[5ebff60]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);
[ca7de3a]534        }
[5ebff60]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 */
[254a4da]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);
[5ebff60]544        xt_insert_child(d, c);
545
[5a7af1b]546        /* Do this in batches of 100. */
547        bd->flags |= MSN_BUDDY_ADL_SYNCED;
548        return (--md->adl_todo % 140) == 0;
[ca7de3a]549}
550
[5ebff60]551static void msn_ns_send_adl(struct im_connection *ic)
[ca7de3a]552{
553        struct xt_node *adl;
[5a7af1b]554        struct msn_data *md = ic->proto_data;
[64768d4]555        char *adls;
[5ebff60]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) {
[5a7af1b]561                /* This tells the caller that we're done now. */
562                md->adl_todo = -1;
[5ebff60]563                xt_free_node(adl);
[5a7af1b]564                return;
565        }
[5ebff60]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);
[5a7af1b]571}
572
[5ebff60]573static void msn_ns_send_adl_start(struct im_connection *ic)
[5a7af1b]574{
575        struct msn_data *md;
576        GSList *l;
[5ebff60]577
[5a7af1b]578        /* Dead connection? */
[5ebff60]579        if (g_slist_find(msn_connections, ic) == NULL) {
[5a7af1b]580                return;
[5ebff60]581        }
582
[5a7af1b]583        md = ic->proto_data;
584        md->adl_todo = 0;
[5ebff60]585        for (l = ic->bee->users; l; l = l->next) {
[5a7af1b]586                bee_user_t *bu = l->data;
587                struct msn_buddy_data *bd = bu->data;
[5ebff60]588
589                if (bu->ic != ic || (bd->flags & 7) == 0) {
[5a7af1b]590                        continue;
[5ebff60]591                }
592
[5a7af1b]593                bd->flags &= ~MSN_BUDDY_ADL_SYNCED;
594                md->adl_todo++;
595        }
[5ebff60]596
597        msn_ns_send_adl(ic);
[ca7de3a]598}
[80175a1]599
[5ebff60]600int msn_ns_finish_login(struct im_connection *ic)
[80175a1]601{
602        struct msn_data *md = ic->proto_data;
[5ebff60]603
604        if (ic->flags & OPT_LOGGED_IN) {
[80175a1]605                return 1;
[5ebff60]606        }
607
608        if (md->adl_todo < 0) {
[80175a1]609                md->flags |= MSN_DONE_ADL;
[ed0589c]610        }
[5ebff60]611
612        if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) {
[d550358]613                imcb_connected(ic);
[5ebff60]614        }
615
[ed0589c]616        return 1;
[80175a1]617}
[bc676ac]618
[11e42dc]619// TODO: typing notifications, nudges lol, etc
[5ebff60]620int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text)
[bc676ac]621{
622        struct msn_data *md = ic->proto_data;
[11e42dc]623        int retval = 0;
624        char *buf;
[5ebff60]625
626        if (strncmp(text, "\r\r\r", 3) == 0) {
[bc676ac]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;
[5ebff60]630        }
631
[11e42dc]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;
[bc676ac]636}
Note: See TracBrowser for help on using the repository browser.