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