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