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 | static int msn_ns_command(struct msn_handler_data *handler, char **cmd, int num_parts); |
---|
38 | static int msn_ns_message(struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts); |
---|
39 | |
---|
40 | static void msn_ns_send_adl_start(struct im_connection *ic); |
---|
41 | static void msn_ns_send_adl(struct im_connection *ic); |
---|
42 | |
---|
43 | int msn_ns_write(struct im_connection *ic, int fd, const char *fmt, ...) |
---|
44 | { |
---|
45 | struct msn_data *md = ic->proto_data; |
---|
46 | va_list params; |
---|
47 | char *out; |
---|
48 | size_t len; |
---|
49 | int st; |
---|
50 | |
---|
51 | va_start(params, fmt); |
---|
52 | out = g_strdup_vprintf(fmt, params); |
---|
53 | va_end(params); |
---|
54 | |
---|
55 | if (fd < 0) { |
---|
56 | fd = md->ns->fd; |
---|
57 | } |
---|
58 | |
---|
59 | if (getenv("BITLBEE_DEBUG")) { |
---|
60 | fprintf(stderr, "->NS%d:%s\n", fd, out); |
---|
61 | } |
---|
62 | |
---|
63 | len = strlen(out); |
---|
64 | st = write(fd, out, len); |
---|
65 | g_free(out); |
---|
66 | if (st != len) { |
---|
67 | imcb_error(ic, "Short write() to main server"); |
---|
68 | imc_logout(ic, TRUE); |
---|
69 | return 0; |
---|
70 | } |
---|
71 | |
---|
72 | return 1; |
---|
73 | } |
---|
74 | |
---|
75 | gboolean msn_ns_connect(struct im_connection *ic, struct msn_handler_data *handler, const char *host, int port) |
---|
76 | { |
---|
77 | if (handler->fd >= 0) { |
---|
78 | closesocket(handler->fd); |
---|
79 | } |
---|
80 | |
---|
81 | handler->exec_command = msn_ns_command; |
---|
82 | handler->exec_message = msn_ns_message; |
---|
83 | handler->data = ic; |
---|
84 | handler->fd = proxy_connect(host, port, msn_ns_connected, handler); |
---|
85 | if (handler->fd < 0) { |
---|
86 | imcb_error(ic, "Could not connect to server"); |
---|
87 | imc_logout(ic, TRUE); |
---|
88 | return FALSE; |
---|
89 | } |
---|
90 | |
---|
91 | return TRUE; |
---|
92 | } |
---|
93 | |
---|
94 | static gboolean msn_ns_connected(gpointer data, gint source, b_input_condition cond) |
---|
95 | { |
---|
96 | struct msn_handler_data *handler = data; |
---|
97 | struct im_connection *ic = handler->data; |
---|
98 | struct msn_data *md; |
---|
99 | |
---|
100 | if (!g_slist_find(msn_connections, ic)) { |
---|
101 | return FALSE; |
---|
102 | } |
---|
103 | |
---|
104 | md = ic->proto_data; |
---|
105 | |
---|
106 | if (source == -1) { |
---|
107 | imcb_error(ic, "Could not connect to server"); |
---|
108 | imc_logout(ic, TRUE); |
---|
109 | return FALSE; |
---|
110 | } |
---|
111 | |
---|
112 | g_free(handler->rxq); |
---|
113 | handler->rxlen = 0; |
---|
114 | handler->rxq = g_new0(char, 1); |
---|
115 | |
---|
116 | if (md->uuid == NULL) { |
---|
117 | struct utsname name; |
---|
118 | sha1_state_t sha[1]; |
---|
119 | |
---|
120 | /* UUID == SHA1("BitlBee" + my hostname + MSN username) */ |
---|
121 | sha1_init(sha); |
---|
122 | sha1_append(sha, (void *) "BitlBee", 7); |
---|
123 | if (uname(&name) == 0) { |
---|
124 | sha1_append(sha, (void *) name.nodename, strlen(name.nodename)); |
---|
125 | } |
---|
126 | sha1_append(sha, (void *) ic->acc->user, strlen(ic->acc->user)); |
---|
127 | md->uuid = sha1_random_uuid(sha); |
---|
128 | memcpy(md->uuid, "b171be3e", 8); /* :-P */ |
---|
129 | } |
---|
130 | |
---|
131 | if (msn_ns_write(ic, source, "VER %d %s CVR0\r\n", ++md->trId, MSNP_VER)) { |
---|
132 | handler->inpa = b_input_add(handler->fd, B_EV_IO_READ, msn_ns_callback, handler); |
---|
133 | imcb_log(ic, "Connected to server, waiting for reply"); |
---|
134 | } |
---|
135 | |
---|
136 | return FALSE; |
---|
137 | } |
---|
138 | |
---|
139 | void msn_ns_close(struct msn_handler_data *handler) |
---|
140 | { |
---|
141 | if (handler->fd >= 0) { |
---|
142 | closesocket(handler->fd); |
---|
143 | b_event_remove(handler->inpa); |
---|
144 | } |
---|
145 | |
---|
146 | handler->fd = handler->inpa = -1; |
---|
147 | g_free(handler->rxq); |
---|
148 | g_free(handler->cmd_text); |
---|
149 | |
---|
150 | handler->rxlen = 0; |
---|
151 | handler->rxq = NULL; |
---|
152 | handler->cmd_text = NULL; |
---|
153 | } |
---|
154 | |
---|
155 | static gboolean msn_ns_callback(gpointer data, gint source, b_input_condition cond) |
---|
156 | { |
---|
157 | struct msn_handler_data *handler = data; |
---|
158 | struct im_connection *ic = handler->data; |
---|
159 | |
---|
160 | if (msn_handler(handler) == -1) { /* Don't do this on ret == 0, it's already done then. */ |
---|
161 | imcb_error(ic, "Error while reading from server"); |
---|
162 | imc_logout(ic, TRUE); |
---|
163 | |
---|
164 | return FALSE; |
---|
165 | } else { |
---|
166 | return TRUE; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | static int msn_ns_command(struct msn_handler_data *handler, char **cmd, int num_parts) |
---|
171 | { |
---|
172 | struct im_connection *ic = handler->data; |
---|
173 | struct msn_data *md = ic->proto_data; |
---|
174 | |
---|
175 | if (num_parts == 0) { |
---|
176 | /* Hrrm... Empty command...? Ignore? */ |
---|
177 | return(1); |
---|
178 | } |
---|
179 | |
---|
180 | if (strcmp(cmd[0], "VER") == 0) { |
---|
181 | if (cmd[2] && strncmp(cmd[2], MSNP_VER, 5) != 0) { |
---|
182 | imcb_error(ic, "Unsupported protocol"); |
---|
183 | imc_logout(ic, FALSE); |
---|
184 | return(0); |
---|
185 | } |
---|
186 | |
---|
187 | return(msn_ns_write(ic, handler->fd, "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n", |
---|
188 | ++md->trId, ic->acc->user)); |
---|
189 | } else if (strcmp(cmd[0], "CVR") == 0) { |
---|
190 | /* We don't give a damn about the information we just received */ |
---|
191 | return msn_ns_write(ic, handler->fd, "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user); |
---|
192 | } else if (strcmp(cmd[0], "XFR") == 0) { |
---|
193 | char *server; |
---|
194 | int port; |
---|
195 | |
---|
196 | if (num_parts >= 6 && strcmp(cmd[2], "NS") == 0) { |
---|
197 | b_event_remove(handler->inpa); |
---|
198 | handler->inpa = -1; |
---|
199 | |
---|
200 | server = strchr(cmd[3], ':'); |
---|
201 | if (!server) { |
---|
202 | imcb_error(ic, "Syntax error"); |
---|
203 | imc_logout(ic, TRUE); |
---|
204 | return(0); |
---|
205 | } |
---|
206 | *server = 0; |
---|
207 | port = atoi(server + 1); |
---|
208 | server = cmd[3]; |
---|
209 | |
---|
210 | imcb_log(ic, "Transferring to other server"); |
---|
211 | return msn_ns_connect(ic, handler, server, port); |
---|
212 | } else if (num_parts >= 6 && strcmp(cmd[2], "SB") == 0) { |
---|
213 | struct msn_switchboard *sb; |
---|
214 | |
---|
215 | server = strchr(cmd[3], ':'); |
---|
216 | if (!server) { |
---|
217 | imcb_error(ic, "Syntax error"); |
---|
218 | imc_logout(ic, TRUE); |
---|
219 | return(0); |
---|
220 | } |
---|
221 | *server = 0; |
---|
222 | port = atoi(server + 1); |
---|
223 | server = cmd[3]; |
---|
224 | |
---|
225 | if (strcmp(cmd[4], "CKI") != 0) { |
---|
226 | imcb_error(ic, "Unknown authentication method for switchboard"); |
---|
227 | imc_logout(ic, TRUE); |
---|
228 | return(0); |
---|
229 | } |
---|
230 | |
---|
231 | debug("Connecting to a new switchboard with key %s", cmd[5]); |
---|
232 | |
---|
233 | if ((sb = msn_sb_create(ic, server, port, cmd[5], MSN_SB_NEW)) == NULL) { |
---|
234 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
235 | definitely something serious (we ran out of file descriptors?). */ |
---|
236 | imcb_error(ic, "Could not create new switchboard"); |
---|
237 | imc_logout(ic, TRUE); |
---|
238 | return(0); |
---|
239 | } |
---|
240 | |
---|
241 | if (md->msgq) { |
---|
242 | struct msn_message *m = md->msgq->data; |
---|
243 | GSList *l; |
---|
244 | |
---|
245 | sb->who = g_strdup(m->who); |
---|
246 | |
---|
247 | /* Move all the messages to the first user in the message |
---|
248 | queue to the switchboard message queue. */ |
---|
249 | l = md->msgq; |
---|
250 | while (l) { |
---|
251 | m = l->data; |
---|
252 | l = l->next; |
---|
253 | if (strcmp(m->who, sb->who) == 0) { |
---|
254 | sb->msgq = g_slist_append(sb->msgq, m); |
---|
255 | md->msgq = g_slist_remove(md->msgq, m); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | } else { |
---|
260 | imcb_error(ic, "Syntax error"); |
---|
261 | imc_logout(ic, TRUE); |
---|
262 | return(0); |
---|
263 | } |
---|
264 | } else if (strcmp(cmd[0], "USR") == 0) { |
---|
265 | if (num_parts >= 6 && strcmp(cmd[2], "SSO") == 0 && |
---|
266 | strcmp(cmd[3], "S") == 0) { |
---|
267 | g_free(md->pp_policy); |
---|
268 | md->pp_policy = g_strdup(cmd[4]); |
---|
269 | msn_soap_passport_sso_request(ic, cmd[5]); |
---|
270 | } else if (strcmp(cmd[2], "OK") == 0) { |
---|
271 | /* If the number after the handle is 0, the e-mail |
---|
272 | address is unverified, which means we can't change |
---|
273 | the display name. */ |
---|
274 | if (cmd[4][0] == '0') { |
---|
275 | md->flags |= MSN_EMAIL_UNVERIFIED; |
---|
276 | } |
---|
277 | |
---|
278 | imcb_log(ic, "Authenticated, getting buddy list"); |
---|
279 | msn_soap_memlist_request(ic); |
---|
280 | } else { |
---|
281 | imcb_error(ic, "Unknown authentication type"); |
---|
282 | imc_logout(ic, FALSE); |
---|
283 | return(0); |
---|
284 | } |
---|
285 | } else if (strcmp(cmd[0], "MSG") == 0) { |
---|
286 | if (num_parts < 4) { |
---|
287 | imcb_error(ic, "Syntax error"); |
---|
288 | imc_logout(ic, TRUE); |
---|
289 | return(0); |
---|
290 | } |
---|
291 | |
---|
292 | handler->msglen = atoi(cmd[3]); |
---|
293 | |
---|
294 | if (handler->msglen <= 0) { |
---|
295 | imcb_error(ic, "Syntax error"); |
---|
296 | imc_logout(ic, TRUE); |
---|
297 | return(0); |
---|
298 | } |
---|
299 | } else if (strcmp(cmd[0], "BLP") == 0) { |
---|
300 | msn_ns_send_adl_start(ic); |
---|
301 | return msn_ns_finish_login(ic); |
---|
302 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
303 | if (num_parts >= 3 && strcmp(cmd[2], "OK") == 0) { |
---|
304 | msn_ns_send_adl(ic); |
---|
305 | return msn_ns_finish_login(ic); |
---|
306 | } else if (num_parts >= 3) { |
---|
307 | handler->msglen = atoi(cmd[2]); |
---|
308 | } |
---|
309 | } else if (strcmp(cmd[0], "PRP") == 0) { |
---|
310 | imcb_connected(ic); |
---|
311 | } else if (strcmp(cmd[0], "CHL") == 0) { |
---|
312 | char *resp; |
---|
313 | int st; |
---|
314 | |
---|
315 | if (num_parts < 3) { |
---|
316 | imcb_error(ic, "Syntax error"); |
---|
317 | imc_logout(ic, TRUE); |
---|
318 | return(0); |
---|
319 | } |
---|
320 | |
---|
321 | resp = msn_p11_challenge(cmd[2]); |
---|
322 | |
---|
323 | st = msn_ns_write(ic, -1, "QRY %d %s %zd\r\n%s", |
---|
324 | ++md->trId, MSNP11_PROD_ID, |
---|
325 | strlen(resp), resp); |
---|
326 | g_free(resp); |
---|
327 | return st; |
---|
328 | } else if (strcmp(cmd[0], "ILN") == 0 || strcmp(cmd[0], "NLN") == 0) { |
---|
329 | const struct msn_away_state *st; |
---|
330 | const char *handle; |
---|
331 | int cap = 0; |
---|
332 | |
---|
333 | if (num_parts < 6) { |
---|
334 | imcb_error(ic, "Syntax error"); |
---|
335 | imc_logout(ic, TRUE); |
---|
336 | return(0); |
---|
337 | } |
---|
338 | /* ILN and NLN are more or less the same, except ILN has a trId |
---|
339 | at the start, and NLN has a capability field at the end. |
---|
340 | Does ILN still exist BTW? */ |
---|
341 | if (cmd[0][1] == 'I') { |
---|
342 | cmd++; |
---|
343 | } else { |
---|
344 | cap = atoi(cmd[4]); |
---|
345 | } |
---|
346 | |
---|
347 | handle = msn_normalize_handle(cmd[2]); |
---|
348 | if (strcmp(handle, ic->acc->user) == 0) { |
---|
349 | return 1; /* That's me! */ |
---|
350 | |
---|
351 | } |
---|
352 | http_decode(cmd[3]); |
---|
353 | imcb_rename_buddy(ic, handle, cmd[3]); |
---|
354 | |
---|
355 | st = msn_away_state_by_code(cmd[1]); |
---|
356 | if (!st) { |
---|
357 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
358 | st = msn_away_state_list + 1; |
---|
359 | } |
---|
360 | |
---|
361 | imcb_buddy_status(ic, handle, OPT_LOGGED_IN | |
---|
362 | (st != msn_away_state_list ? OPT_AWAY : 0) | |
---|
363 | (cap & 1 ? OPT_MOBILE : 0), |
---|
364 | st->name, NULL); |
---|
365 | |
---|
366 | msn_sb_stop_keepalives(msn_sb_by_handle(ic, handle)); |
---|
367 | } else if (strcmp(cmd[0], "FLN") == 0) { |
---|
368 | const char *handle; |
---|
369 | |
---|
370 | if (cmd[1] == NULL) { |
---|
371 | return 1; |
---|
372 | } |
---|
373 | |
---|
374 | handle = msn_normalize_handle(cmd[1]); |
---|
375 | imcb_buddy_status(ic, handle, 0, NULL, NULL); |
---|
376 | msn_sb_start_keepalives(msn_sb_by_handle(ic, handle), TRUE); |
---|
377 | } else if (strcmp(cmd[0], "RNG") == 0) { |
---|
378 | struct msn_switchboard *sb; |
---|
379 | char *server; |
---|
380 | int session, port; |
---|
381 | |
---|
382 | if (num_parts < 7) { |
---|
383 | imcb_error(ic, "Syntax error"); |
---|
384 | imc_logout(ic, TRUE); |
---|
385 | return(0); |
---|
386 | } |
---|
387 | |
---|
388 | session = atoi(cmd[1]); |
---|
389 | |
---|
390 | server = strchr(cmd[2], ':'); |
---|
391 | if (!server) { |
---|
392 | imcb_error(ic, "Syntax error"); |
---|
393 | imc_logout(ic, TRUE); |
---|
394 | return(0); |
---|
395 | } |
---|
396 | *server = 0; |
---|
397 | port = atoi(server + 1); |
---|
398 | server = cmd[2]; |
---|
399 | |
---|
400 | if (strcmp(cmd[3], "CKI") != 0) { |
---|
401 | imcb_error(ic, "Unknown authentication method for switchboard"); |
---|
402 | imc_logout(ic, TRUE); |
---|
403 | return(0); |
---|
404 | } |
---|
405 | |
---|
406 | debug("Got a call from %s (session %d). Key = %s", cmd[5], session, cmd[4]); |
---|
407 | |
---|
408 | if ((sb = msn_sb_create(ic, server, port, cmd[4], session)) == NULL) { |
---|
409 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
410 | definitely something serious (we ran out of file descriptors?). */ |
---|
411 | imcb_error(ic, "Could not create new switchboard"); |
---|
412 | imc_logout(ic, TRUE); |
---|
413 | return(0); |
---|
414 | } else { |
---|
415 | sb->who = g_strdup(msn_normalize_handle(cmd[5])); |
---|
416 | } |
---|
417 | } else if (strcmp(cmd[0], "OUT") == 0) { |
---|
418 | int allow_reconnect = TRUE; |
---|
419 | |
---|
420 | if (cmd[1] && strcmp(cmd[1], "OTH") == 0) { |
---|
421 | imcb_error(ic, "Someone else logged in with your account"); |
---|
422 | allow_reconnect = FALSE; |
---|
423 | } else if (cmd[1] && strcmp(cmd[1], "SSD") == 0) { |
---|
424 | imcb_error(ic, "Terminating session because of server shutdown"); |
---|
425 | } else { |
---|
426 | imcb_error(ic, "Session terminated by remote server (%s)", |
---|
427 | cmd[1] ? cmd[1] : "reason unknown)"); |
---|
428 | } |
---|
429 | |
---|
430 | imc_logout(ic, allow_reconnect); |
---|
431 | return(0); |
---|
432 | } else if (strcmp(cmd[0], "IPG") == 0) { |
---|
433 | imcb_error(ic, "Received IPG command, we don't handle them yet."); |
---|
434 | |
---|
435 | handler->msglen = atoi(cmd[1]); |
---|
436 | |
---|
437 | if (handler->msglen <= 0) { |
---|
438 | imcb_error(ic, "Syntax error"); |
---|
439 | imc_logout(ic, TRUE); |
---|
440 | return(0); |
---|
441 | } |
---|
442 | } |
---|
443 | #if 0 |
---|
444 | else if (strcmp(cmd[0], "ADG") == 0) { |
---|
445 | char *group = g_strdup(cmd[3]); |
---|
446 | int groupnum, i; |
---|
447 | GSList *l, *next; |
---|
448 | |
---|
449 | http_decode(group); |
---|
450 | if (sscanf(cmd[4], "%d", &groupnum) == 1) { |
---|
451 | if (groupnum >= md->groupcount) { |
---|
452 | md->grouplist = g_renew(char *, md->grouplist, groupnum + 1); |
---|
453 | for (i = md->groupcount; i <= groupnum; i++) { |
---|
454 | md->grouplist[i] = NULL; |
---|
455 | } |
---|
456 | md->groupcount = groupnum + 1; |
---|
457 | } |
---|
458 | g_free(md->grouplist[groupnum]); |
---|
459 | md->grouplist[groupnum] = group; |
---|
460 | } else { |
---|
461 | /* Shouldn't happen, but if it does, give up on the group. */ |
---|
462 | g_free(group); |
---|
463 | imcb_error(ic, "Syntax error"); |
---|
464 | imc_logout(ic, TRUE); |
---|
465 | return 0; |
---|
466 | } |
---|
467 | |
---|
468 | for (l = md->grpq; l; l = next) { |
---|
469 | struct msn_groupadd *ga = l->data; |
---|
470 | next = l->next; |
---|
471 | if (g_strcasecmp(ga->group, group) == 0) { |
---|
472 | if (!msn_buddy_list_add(ic, "FL", ga->who, ga->who, group)) { |
---|
473 | return 0; |
---|
474 | } |
---|
475 | |
---|
476 | g_free(ga->group); |
---|
477 | g_free(ga->who); |
---|
478 | g_free(ga); |
---|
479 | md->grpq = g_slist_remove(md->grpq, ga); |
---|
480 | } |
---|
481 | } |
---|
482 | } |
---|
483 | #endif |
---|
484 | else if (strcmp(cmd[0], "GCF") == 0) { |
---|
485 | /* Coming up is cmd[2] bytes of stuff we're supposed to |
---|
486 | censore. Meh. */ |
---|
487 | handler->msglen = atoi(cmd[2]); |
---|
488 | } else if (strcmp(cmd[0], "UBX") == 0) { |
---|
489 | /* Status message. */ |
---|
490 | if (num_parts >= 3) { |
---|
491 | handler->msglen = atoi(cmd[2]); |
---|
492 | } |
---|
493 | } else if (strcmp(cmd[0], "NOT") == 0) { |
---|
494 | /* Some kind of notification, poorly documented but |
---|
495 | apparently used to announce address book changes. */ |
---|
496 | if (num_parts >= 2) { |
---|
497 | handler->msglen = atoi(cmd[1]); |
---|
498 | } |
---|
499 | } else if (strcmp(cmd[0], "UBM") == 0) { |
---|
500 | if (num_parts >= 7) { |
---|
501 | handler->msglen = atoi(cmd[6]); |
---|
502 | } |
---|
503 | } else if (strcmp(cmd[0], "QNG") == 0) { |
---|
504 | ic->flags |= OPT_PONGED; |
---|
505 | } else if (g_ascii_isdigit(cmd[0][0])) { |
---|
506 | int num = atoi(cmd[0]); |
---|
507 | const struct msn_status_code *err = msn_status_by_number(num); |
---|
508 | |
---|
509 | imcb_error(ic, "Error reported by MSN server: %s", err->text); |
---|
510 | |
---|
511 | if (err->flags & STATUS_FATAL) { |
---|
512 | imc_logout(ic, TRUE); |
---|
513 | return(0); |
---|
514 | } |
---|
515 | |
---|
516 | /* Oh yes, errors can have payloads too now. Discard them for now. */ |
---|
517 | if (num_parts >= 3) { |
---|
518 | handler->msglen = atoi(cmd[2]); |
---|
519 | } |
---|
520 | } else { |
---|
521 | /* debug( "Received unknown command from main server: %s", cmd[0] ); */ |
---|
522 | } |
---|
523 | |
---|
524 | return(1); |
---|
525 | } |
---|
526 | |
---|
527 | static int msn_ns_message(struct msn_handler_data *handler, char *msg, int msglen, char **cmd, int num_parts) |
---|
528 | { |
---|
529 | struct im_connection *ic = handler->data; |
---|
530 | char *body; |
---|
531 | int blen = 0; |
---|
532 | |
---|
533 | if (!num_parts) { |
---|
534 | return(1); |
---|
535 | } |
---|
536 | |
---|
537 | if ((body = strstr(msg, "\r\n\r\n"))) { |
---|
538 | body += 4; |
---|
539 | blen = msglen - (body - msg); |
---|
540 | } |
---|
541 | |
---|
542 | if (strcmp(cmd[0], "MSG") == 0) { |
---|
543 | if (g_strcasecmp(cmd[1], "Hotmail") == 0) { |
---|
544 | char *ct = get_rfc822_header(msg, "Content-Type:", msglen); |
---|
545 | |
---|
546 | if (!ct) { |
---|
547 | return(1); |
---|
548 | } |
---|
549 | |
---|
550 | if (g_strncasecmp(ct, "application/x-msmsgssystemmessage", 33) == 0) { |
---|
551 | char *mtype; |
---|
552 | char *arg1; |
---|
553 | |
---|
554 | if (!body) { |
---|
555 | return(1); |
---|
556 | } |
---|
557 | |
---|
558 | mtype = get_rfc822_header(body, "Type:", blen); |
---|
559 | arg1 = get_rfc822_header(body, "Arg1:", blen); |
---|
560 | |
---|
561 | if (mtype && strcmp(mtype, "1") == 0) { |
---|
562 | if (arg1) { |
---|
563 | imcb_log(ic, "The server is going down for maintenance in %s minutes.", |
---|
564 | arg1); |
---|
565 | } |
---|
566 | } |
---|
567 | |
---|
568 | g_free(arg1); |
---|
569 | g_free(mtype); |
---|
570 | } else if (g_strncasecmp(ct, "text/x-msmsgsprofile", 20) == 0) { |
---|
571 | /* We don't care about this profile for now... */ |
---|
572 | } else if (g_strncasecmp(ct, "text/x-msmsgsinitialemailnotification", 37) == 0) { |
---|
573 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
574 | char *inbox = get_rfc822_header(body, "Inbox-Unread:", blen); |
---|
575 | char *folders = get_rfc822_header(body, "Folders-Unread:", blen); |
---|
576 | |
---|
577 | if (inbox && folders) { |
---|
578 | imcb_log(ic, |
---|
579 | "INBOX contains %s new messages, plus %s messages in other folders.", inbox, |
---|
580 | folders); |
---|
581 | } |
---|
582 | |
---|
583 | g_free(inbox); |
---|
584 | g_free(folders); |
---|
585 | } |
---|
586 | } else if (g_strncasecmp(ct, "text/x-msmsgsemailnotification", 30) == 0) { |
---|
587 | if (set_getbool(&ic->acc->set, "mail_notifications")) { |
---|
588 | char *from = get_rfc822_header(body, "From-Addr:", blen); |
---|
589 | char *fromname = get_rfc822_header(body, "From:", blen); |
---|
590 | |
---|
591 | if (from && fromname) { |
---|
592 | imcb_log(ic, "Received an e-mail message from %s <%s>.", fromname, |
---|
593 | from); |
---|
594 | } |
---|
595 | |
---|
596 | g_free(from); |
---|
597 | g_free(fromname); |
---|
598 | } |
---|
599 | } else if (g_strncasecmp(ct, "text/x-msmsgsactivemailnotification", 35) == 0) { |
---|
600 | } else if (g_strncasecmp(ct, "text/x-msmsgsinitialmdatanotification", 37) == 0 || |
---|
601 | g_strncasecmp(ct, "text/x-msmsgsoimnotification", 28) == 0) { |
---|
602 | /* We received an offline message. Or at least notification |
---|
603 | that there is one waiting for us. Fetching the message(s) |
---|
604 | and purging them from the server is a lot of SOAPy work |
---|
605 | not worth doing IMHO. Also I thought it was possible to |
---|
606 | have the notification server send them directly, I was |
---|
607 | pretty sure I saw Pidgin do it.. |
---|
608 | |
---|
609 | At least give a notification for now, seems like a |
---|
610 | reasonable thing to do. Only problem is, they'll keep |
---|
611 | coming back at login time until you read them using a |
---|
612 | different client. :-( */ |
---|
613 | |
---|
614 | char *xml = get_rfc822_header(body, "Mail-Data:", blen); |
---|
615 | struct xt_node *md, *m; |
---|
616 | |
---|
617 | if (!xml) { |
---|
618 | return 1; |
---|
619 | } |
---|
620 | md = xt_from_string(xml, 0); |
---|
621 | if (!md) { |
---|
622 | return 1; |
---|
623 | } |
---|
624 | |
---|
625 | for (m = md->children; (m = xt_find_node(m, "M")); m = m->next) { |
---|
626 | struct xt_node *e = xt_find_node(m->children, "E"); |
---|
627 | struct xt_node *rt = xt_find_node(m->children, "RT"); |
---|
628 | struct tm tp; |
---|
629 | time_t msgtime = 0; |
---|
630 | |
---|
631 | if (!e || !e->text) { |
---|
632 | continue; |
---|
633 | } |
---|
634 | |
---|
635 | memset(&tp, 0, sizeof(tp)); |
---|
636 | if (rt && rt->text && |
---|
637 | sscanf(rt->text, "%4d-%2d-%2dT%2d:%2d:%2d.", |
---|
638 | &tp.tm_year, &tp.tm_mon, &tp.tm_mday, |
---|
639 | &tp.tm_hour, &tp.tm_min, &tp.tm_sec) == 6) { |
---|
640 | tp.tm_year -= 1900; |
---|
641 | tp.tm_mon--; |
---|
642 | msgtime = mktime_utc(&tp); |
---|
643 | |
---|
644 | } |
---|
645 | imcb_buddy_msg(ic, e->text, |
---|
646 | "<< \002BitlBee\002 - Received offline message. BitlBee can't show these. >>", 0, |
---|
647 | msgtime); |
---|
648 | } |
---|
649 | |
---|
650 | g_free(xml); |
---|
651 | xt_free_node(md); |
---|
652 | } else { |
---|
653 | debug("Can't handle %s packet from notification server", ct); |
---|
654 | } |
---|
655 | |
---|
656 | g_free(ct); |
---|
657 | } |
---|
658 | } else if (strcmp(cmd[0], "UBX") == 0) { |
---|
659 | struct xt_node *ubx, *psm; |
---|
660 | char *psm_text = NULL; |
---|
661 | |
---|
662 | ubx = xt_from_string(msg, msglen); |
---|
663 | if (ubx && strcmp(ubx->name, "Data") == 0 && |
---|
664 | (psm = xt_find_node(ubx->children, "PSM"))) { |
---|
665 | psm_text = psm->text; |
---|
666 | } |
---|
667 | |
---|
668 | imcb_buddy_status_msg(ic, msn_normalize_handle(cmd[1]), psm_text); |
---|
669 | xt_free_node(ubx); |
---|
670 | } else if (strcmp(cmd[0], "ADL") == 0) { |
---|
671 | struct xt_node *adl, *d, *c; |
---|
672 | |
---|
673 | if (!(adl = xt_from_string(msg, msglen))) { |
---|
674 | return 1; |
---|
675 | } |
---|
676 | |
---|
677 | for (d = adl->children; d; d = d->next) { |
---|
678 | char *dn; |
---|
679 | if (strcmp(d->name, "d") != 0 || |
---|
680 | (dn = xt_find_attr(d, "n")) == NULL) { |
---|
681 | continue; |
---|
682 | } |
---|
683 | for (c = d->children; c; c = c->next) { |
---|
684 | bee_user_t *bu; |
---|
685 | struct msn_buddy_data *bd; |
---|
686 | char *cn, *handle, *f, *l; |
---|
687 | int flags; |
---|
688 | |
---|
689 | if (strcmp(c->name, "c") != 0 || |
---|
690 | (l = xt_find_attr(c, "l")) == NULL || |
---|
691 | (cn = xt_find_attr(c, "n")) == NULL) { |
---|
692 | continue; |
---|
693 | } |
---|
694 | |
---|
695 | /* FIXME: Use "t" here, guess I should just add it |
---|
696 | as a prefix like elsewhere in the protocol. */ |
---|
697 | handle = g_strdup_printf("%s@%s", cn, dn); |
---|
698 | if (!((bu = bee_user_by_handle(ic->bee, ic, handle)) || |
---|
699 | (bu = bee_user_new(ic->bee, ic, handle, 0)))) { |
---|
700 | g_free(handle); |
---|
701 | continue; |
---|
702 | } |
---|
703 | g_free(handle); |
---|
704 | bd = bu->data; |
---|
705 | |
---|
706 | if ((f = xt_find_attr(c, "f"))) { |
---|
707 | http_decode(f); |
---|
708 | imcb_rename_buddy(ic, bu->handle, f); |
---|
709 | } |
---|
710 | |
---|
711 | flags = atoi(l) & 15; |
---|
712 | if (bd->flags != flags) { |
---|
713 | bd->flags = flags; |
---|
714 | msn_buddy_ask(bu); |
---|
715 | } |
---|
716 | } |
---|
717 | } |
---|
718 | } else if (strcmp(cmd[0], "UBM") == 0) { |
---|
719 | /* This one will give us msgs from federated networks. Technically |
---|
720 | it should also get us offline messages, but I don't know how |
---|
721 | I can signal MSN servers to use it. */ |
---|
722 | char *ct, *handle; |
---|
723 | |
---|
724 | if (strcmp(cmd[1], ic->acc->user) == 0) { |
---|
725 | /* With MPOP, you'll get copies of your own msgs from other |
---|
726 | sessions. Discard those at least for now. */ |
---|
727 | return 1; |
---|
728 | } |
---|
729 | |
---|
730 | ct = get_rfc822_header(msg, "Content-Type", msglen); |
---|
731 | if (strncmp(ct, "text/plain", 10) != 0) { |
---|
732 | /* Typing notification or something? */ |
---|
733 | g_free(ct); |
---|
734 | return 1; |
---|
735 | } |
---|
736 | if (strcmp(cmd[2], "1") != 0) { |
---|
737 | handle = g_strdup_printf("%s:%s", cmd[2], cmd[1]); |
---|
738 | } else { |
---|
739 | handle = g_strdup(cmd[1]); |
---|
740 | } |
---|
741 | |
---|
742 | imcb_buddy_msg(ic, handle, body, 0, 0); |
---|
743 | g_free(handle); |
---|
744 | } |
---|
745 | |
---|
746 | return 1; |
---|
747 | } |
---|
748 | |
---|
749 | void msn_auth_got_passport_token(struct im_connection *ic, const char *token, const char *error) |
---|
750 | { |
---|
751 | struct msn_data *md; |
---|
752 | |
---|
753 | /* Dead connection? */ |
---|
754 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
755 | return; |
---|
756 | } |
---|
757 | |
---|
758 | md = ic->proto_data; |
---|
759 | |
---|
760 | if (token) { |
---|
761 | msn_ns_write(ic, -1, "USR %d SSO S %s %s {%s}\r\n", ++md->trId, md->tokens[0], token, md->uuid); |
---|
762 | } else { |
---|
763 | imcb_error(ic, "Error during Passport authentication: %s", error); |
---|
764 | imc_logout(ic, TRUE); |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | void msn_auth_got_contact_list(struct im_connection *ic) |
---|
769 | { |
---|
770 | struct msn_data *md; |
---|
771 | |
---|
772 | /* Dead connection? */ |
---|
773 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
774 | return; |
---|
775 | } |
---|
776 | |
---|
777 | md = ic->proto_data; |
---|
778 | msn_ns_write(ic, -1, "BLP %d %s\r\n", ++md->trId, "BL"); |
---|
779 | } |
---|
780 | |
---|
781 | static gboolean msn_ns_send_adl_1(gpointer key, gpointer value, gpointer data) |
---|
782 | { |
---|
783 | struct xt_node *adl = data, *d, *c; |
---|
784 | struct bee_user *bu = value; |
---|
785 | struct msn_buddy_data *bd = bu->data; |
---|
786 | struct msn_data *md = bu->ic->proto_data; |
---|
787 | char handle[strlen(bu->handle) + 1]; |
---|
788 | char *domain; |
---|
789 | char l[4]; |
---|
790 | |
---|
791 | if ((bd->flags & 7) == 0 || (bd->flags & MSN_BUDDY_ADL_SYNCED)) { |
---|
792 | return FALSE; |
---|
793 | } |
---|
794 | |
---|
795 | strcpy(handle, bu->handle); |
---|
796 | if ((domain = strchr(handle, '@')) == NULL) { /* WTF */ |
---|
797 | return FALSE; |
---|
798 | } |
---|
799 | *domain = '\0'; |
---|
800 | domain++; |
---|
801 | |
---|
802 | if ((d = adl->children) == NULL || |
---|
803 | g_strcasecmp(xt_find_attr(d, "n"), domain) != 0) { |
---|
804 | d = xt_new_node("d", NULL, NULL); |
---|
805 | xt_add_attr(d, "n", domain); |
---|
806 | xt_insert_child(adl, d); |
---|
807 | } |
---|
808 | |
---|
809 | g_snprintf(l, sizeof(l), "%d", bd->flags & 7); |
---|
810 | c = xt_new_node("c", NULL, NULL); |
---|
811 | xt_add_attr(c, "n", handle); |
---|
812 | xt_add_attr(c, "l", l); |
---|
813 | xt_add_attr(c, "t", "1"); /* FIXME: Network type, i.e. 32 for Y!MSG */ |
---|
814 | xt_insert_child(d, c); |
---|
815 | |
---|
816 | /* Do this in batches of 100. */ |
---|
817 | bd->flags |= MSN_BUDDY_ADL_SYNCED; |
---|
818 | return (--md->adl_todo % 140) == 0; |
---|
819 | } |
---|
820 | |
---|
821 | static void msn_ns_send_adl(struct im_connection *ic) |
---|
822 | { |
---|
823 | struct xt_node *adl; |
---|
824 | struct msn_data *md = ic->proto_data; |
---|
825 | char *adls; |
---|
826 | |
---|
827 | adl = xt_new_node("ml", NULL, NULL); |
---|
828 | xt_add_attr(adl, "l", "1"); |
---|
829 | g_tree_foreach(md->domaintree, msn_ns_send_adl_1, adl); |
---|
830 | if (adl->children == NULL) { |
---|
831 | /* This tells the caller that we're done now. */ |
---|
832 | md->adl_todo = -1; |
---|
833 | xt_free_node(adl); |
---|
834 | return; |
---|
835 | } |
---|
836 | |
---|
837 | adls = xt_to_string(adl); |
---|
838 | xt_free_node(adl); |
---|
839 | msn_ns_write(ic, -1, "ADL %d %zd\r\n%s", ++md->trId, strlen(adls), adls); |
---|
840 | g_free(adls); |
---|
841 | } |
---|
842 | |
---|
843 | static void msn_ns_send_adl_start(struct im_connection *ic) |
---|
844 | { |
---|
845 | struct msn_data *md; |
---|
846 | GSList *l; |
---|
847 | |
---|
848 | /* Dead connection? */ |
---|
849 | if (g_slist_find(msn_connections, ic) == NULL) { |
---|
850 | return; |
---|
851 | } |
---|
852 | |
---|
853 | md = ic->proto_data; |
---|
854 | md->adl_todo = 0; |
---|
855 | for (l = ic->bee->users; l; l = l->next) { |
---|
856 | bee_user_t *bu = l->data; |
---|
857 | struct msn_buddy_data *bd = bu->data; |
---|
858 | |
---|
859 | if (bu->ic != ic || (bd->flags & 7) == 0) { |
---|
860 | continue; |
---|
861 | } |
---|
862 | |
---|
863 | bd->flags &= ~MSN_BUDDY_ADL_SYNCED; |
---|
864 | md->adl_todo++; |
---|
865 | } |
---|
866 | |
---|
867 | msn_ns_send_adl(ic); |
---|
868 | } |
---|
869 | |
---|
870 | int msn_ns_finish_login(struct im_connection *ic) |
---|
871 | { |
---|
872 | struct msn_data *md = ic->proto_data; |
---|
873 | |
---|
874 | if (ic->flags & OPT_LOGGED_IN) { |
---|
875 | return 1; |
---|
876 | } |
---|
877 | |
---|
878 | if (md->adl_todo < 0) { |
---|
879 | md->flags |= MSN_DONE_ADL; |
---|
880 | } |
---|
881 | |
---|
882 | if ((md->flags & MSN_DONE_ADL) && (md->flags & MSN_GOT_PROFILE)) { |
---|
883 | if (md->flags & MSN_EMAIL_UNVERIFIED) { |
---|
884 | imcb_connected(ic); |
---|
885 | } else { |
---|
886 | return msn_ns_set_display_name(ic, set_getstr(&ic->acc->set, "display_name")); |
---|
887 | } |
---|
888 | } |
---|
889 | |
---|
890 | return 1; |
---|
891 | } |
---|
892 | |
---|
893 | int msn_ns_sendmessage(struct im_connection *ic, bee_user_t *bu, const char *text) |
---|
894 | { |
---|
895 | struct msn_data *md = ic->proto_data; |
---|
896 | int type = 0; |
---|
897 | char *buf, *handle; |
---|
898 | |
---|
899 | if (strncmp(text, "\r\r\r", 3) == 0) { |
---|
900 | /* Err. Shouldn't happen but I guess it can. Don't send others |
---|
901 | any of the "SHAKE THAT THING" messages. :-D */ |
---|
902 | return 1; |
---|
903 | } |
---|
904 | |
---|
905 | /* This might be a federated contact. Get its network number, |
---|
906 | prefixed to bu->handle with a colon. Default is 1. */ |
---|
907 | for (handle = bu->handle; g_ascii_isdigit(*handle); handle++) { |
---|
908 | type = type * 10 + *handle - '0'; |
---|
909 | } |
---|
910 | if (*handle == ':') { |
---|
911 | handle++; |
---|
912 | } else { |
---|
913 | type = 1; |
---|
914 | } |
---|
915 | |
---|
916 | buf = g_strdup_printf("%s%s", MSN_MESSAGE_HEADERS, text); |
---|
917 | |
---|
918 | if (msn_ns_write(ic, -1, "UUM %d %s %d %d %zd\r\n%s", |
---|
919 | ++md->trId, handle, type, |
---|
920 | 1, /* type == IM (not nudge/typing) */ |
---|
921 | strlen(buf), buf)) { |
---|
922 | return 1; |
---|
923 | } else { |
---|
924 | return 0; |
---|
925 | } |
---|
926 | } |
---|
927 | |
---|
928 | void msn_ns_oim_send_queue(struct im_connection *ic, GSList **msgq) |
---|
929 | { |
---|
930 | GSList *l; |
---|
931 | |
---|
932 | for (l = *msgq; l; l = l->next) { |
---|
933 | struct msn_message *m = l->data; |
---|
934 | bee_user_t *bu = bee_user_by_handle(ic->bee, ic, m->who); |
---|
935 | |
---|
936 | if (bu) { |
---|
937 | if (!msn_ns_sendmessage(ic, bu, m->text)) { |
---|
938 | return; |
---|
939 | } |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | while (*msgq != NULL) { |
---|
944 | struct msn_message *m = (*msgq)->data; |
---|
945 | |
---|
946 | *msgq = g_slist_remove(*msgq, m); |
---|
947 | g_free(m->who); |
---|
948 | g_free(m->text); |
---|
949 | g_free(m); |
---|
950 | } |
---|
951 | } |
---|