source: protocols/msn/ns.c @ 951aefd

Last change on this file since 951aefd was 951aefd, checked in by dequis <dx@…>, at 2015-04-12T15:27:31Z

msn/gw.c: ensure that the im_connection still exists in callbacks

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