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