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