1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 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 "nogaim.h" |
---|
28 | #include "msn.h" |
---|
29 | #include "md5.h" |
---|
30 | #include "soap.h" |
---|
31 | #include "xmltree.h" |
---|
32 | |
---|
33 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ); |
---|
34 | static int msn_ns_command( gpointer data, char **cmd, int num_parts ); |
---|
35 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ); |
---|
36 | |
---|
37 | static gboolean msn_ns_got_display_name( struct im_connection *ic, char *name ); |
---|
38 | static void msn_ns_send_adl( struct im_connection *ic ); |
---|
39 | |
---|
40 | gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond ) |
---|
41 | { |
---|
42 | struct im_connection *ic = data; |
---|
43 | struct msn_data *md; |
---|
44 | char s[1024]; |
---|
45 | |
---|
46 | if( !g_slist_find( msn_connections, ic ) ) |
---|
47 | return FALSE; |
---|
48 | |
---|
49 | if( source == -1 ) |
---|
50 | { |
---|
51 | imcb_error( ic, "Could not connect to server" ); |
---|
52 | imc_logout( ic, TRUE ); |
---|
53 | return FALSE; |
---|
54 | } |
---|
55 | |
---|
56 | md = ic->proto_data; |
---|
57 | |
---|
58 | if( !md->handler ) |
---|
59 | { |
---|
60 | md->handler = g_new0( struct msn_handler_data, 1 ); |
---|
61 | md->handler->data = ic; |
---|
62 | md->handler->exec_command = msn_ns_command; |
---|
63 | md->handler->exec_message = msn_ns_message; |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | if( md->handler->rxq ) |
---|
68 | g_free( md->handler->rxq ); |
---|
69 | |
---|
70 | md->handler->rxlen = 0; |
---|
71 | } |
---|
72 | |
---|
73 | md->handler->fd = md->fd; |
---|
74 | md->handler->rxq = g_new0( char, 1 ); |
---|
75 | |
---|
76 | g_snprintf( s, sizeof( s ), "VER %d MSNP15 CVR0\r\n", ++md->trId ); |
---|
77 | if( msn_write( ic, s, strlen( s ) ) ) |
---|
78 | { |
---|
79 | ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic ); |
---|
80 | imcb_log( ic, "Connected to server, waiting for reply" ); |
---|
81 | } |
---|
82 | |
---|
83 | return FALSE; |
---|
84 | } |
---|
85 | |
---|
86 | static gboolean msn_ns_callback( gpointer data, gint source, b_input_condition cond ) |
---|
87 | { |
---|
88 | struct im_connection *ic = data; |
---|
89 | struct msn_data *md = ic->proto_data; |
---|
90 | |
---|
91 | if( msn_handler( md->handler ) == -1 ) /* Don't do this on ret == 0, it's already done then. */ |
---|
92 | { |
---|
93 | imcb_error( ic, "Error while reading from server" ); |
---|
94 | imc_logout( ic, TRUE ); |
---|
95 | |
---|
96 | return FALSE; |
---|
97 | } |
---|
98 | else |
---|
99 | return TRUE; |
---|
100 | } |
---|
101 | |
---|
102 | static int msn_ns_command( gpointer data, char **cmd, int num_parts ) |
---|
103 | { |
---|
104 | struct im_connection *ic = data; |
---|
105 | struct msn_data *md = ic->proto_data; |
---|
106 | char buf[1024]; |
---|
107 | |
---|
108 | if( num_parts == 0 ) |
---|
109 | { |
---|
110 | /* Hrrm... Empty command...? Ignore? */ |
---|
111 | return( 1 ); |
---|
112 | } |
---|
113 | |
---|
114 | if( strcmp( cmd[0], "VER" ) == 0 ) |
---|
115 | { |
---|
116 | if( cmd[2] && strncmp( cmd[2], "MSNP15", 5 ) != 0 ) |
---|
117 | { |
---|
118 | imcb_error( ic, "Unsupported protocol" ); |
---|
119 | imc_logout( ic, FALSE ); |
---|
120 | return( 0 ); |
---|
121 | } |
---|
122 | |
---|
123 | g_snprintf( buf, sizeof( buf ), "CVR %d 0x0409 mac 10.2.0 ppc macmsgs 3.5.1 macmsgs %s\r\n", |
---|
124 | ++md->trId, ic->acc->user ); |
---|
125 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
126 | } |
---|
127 | else if( strcmp( cmd[0], "CVR" ) == 0 ) |
---|
128 | { |
---|
129 | /* We don't give a damn about the information we just received */ |
---|
130 | g_snprintf( buf, sizeof( buf ), "USR %d SSO I %s\r\n", ++md->trId, ic->acc->user ); |
---|
131 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
132 | } |
---|
133 | else if( strcmp( cmd[0], "XFR" ) == 0 ) |
---|
134 | { |
---|
135 | char *server; |
---|
136 | int port; |
---|
137 | |
---|
138 | if( num_parts >= 6 && strcmp( cmd[2], "NS" ) == 0 ) |
---|
139 | { |
---|
140 | b_event_remove( ic->inpa ); |
---|
141 | ic->inpa = 0; |
---|
142 | closesocket( md->fd ); |
---|
143 | |
---|
144 | server = strchr( cmd[3], ':' ); |
---|
145 | if( !server ) |
---|
146 | { |
---|
147 | imcb_error( ic, "Syntax error" ); |
---|
148 | imc_logout( ic, TRUE ); |
---|
149 | return( 0 ); |
---|
150 | } |
---|
151 | *server = 0; |
---|
152 | port = atoi( server + 1 ); |
---|
153 | server = cmd[3]; |
---|
154 | |
---|
155 | imcb_log( ic, "Transferring to other server" ); |
---|
156 | |
---|
157 | md->fd = proxy_connect( server, port, msn_ns_connected, ic ); |
---|
158 | } |
---|
159 | else if( num_parts >= 6 && strcmp( cmd[2], "SB" ) == 0 ) |
---|
160 | { |
---|
161 | struct msn_switchboard *sb; |
---|
162 | |
---|
163 | server = strchr( cmd[3], ':' ); |
---|
164 | if( !server ) |
---|
165 | { |
---|
166 | imcb_error( ic, "Syntax error" ); |
---|
167 | imc_logout( ic, TRUE ); |
---|
168 | return( 0 ); |
---|
169 | } |
---|
170 | *server = 0; |
---|
171 | port = atoi( server + 1 ); |
---|
172 | server = cmd[3]; |
---|
173 | |
---|
174 | if( strcmp( cmd[4], "CKI" ) != 0 ) |
---|
175 | { |
---|
176 | imcb_error( ic, "Unknown authentication method for switchboard" ); |
---|
177 | imc_logout( ic, TRUE ); |
---|
178 | return( 0 ); |
---|
179 | } |
---|
180 | |
---|
181 | debug( "Connecting to a new switchboard with key %s", cmd[5] ); |
---|
182 | |
---|
183 | if( ( sb = msn_sb_create( ic, server, port, cmd[5], MSN_SB_NEW ) ) == NULL ) |
---|
184 | { |
---|
185 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
186 | definitely something serious (we ran out of file descriptors?). */ |
---|
187 | imcb_error( ic, "Could not create new switchboard" ); |
---|
188 | imc_logout( ic, TRUE ); |
---|
189 | return( 0 ); |
---|
190 | } |
---|
191 | |
---|
192 | if( md->msgq ) |
---|
193 | { |
---|
194 | struct msn_message *m = md->msgq->data; |
---|
195 | GSList *l; |
---|
196 | |
---|
197 | sb->who = g_strdup( m->who ); |
---|
198 | |
---|
199 | /* Move all the messages to the first user in the message |
---|
200 | queue to the switchboard message queue. */ |
---|
201 | l = md->msgq; |
---|
202 | while( l ) |
---|
203 | { |
---|
204 | m = l->data; |
---|
205 | l = l->next; |
---|
206 | if( strcmp( m->who, sb->who ) == 0 ) |
---|
207 | { |
---|
208 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
209 | md->msgq = g_slist_remove( md->msgq, m ); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | imcb_error( ic, "Syntax error" ); |
---|
217 | imc_logout( ic, TRUE ); |
---|
218 | return( 0 ); |
---|
219 | } |
---|
220 | } |
---|
221 | else if( strcmp( cmd[0], "USR" ) == 0 ) |
---|
222 | { |
---|
223 | if( num_parts >= 6 && strcmp( cmd[2], "SSO" ) == 0 && |
---|
224 | strcmp( cmd[3], "S" ) == 0 ) |
---|
225 | { |
---|
226 | msn_soap_passport_sso_request( ic, cmd[4], cmd[5] ); |
---|
227 | } |
---|
228 | else if( strcmp( cmd[2], "OK" ) == 0 ) |
---|
229 | { |
---|
230 | if( num_parts == 7 ) |
---|
231 | msn_ns_got_display_name( ic, cmd[4] ); |
---|
232 | else |
---|
233 | imcb_log( ic, "Warning: Friendly name in server response was corrupted" ); |
---|
234 | |
---|
235 | imcb_log( ic, "Authenticated, getting buddy list" ); |
---|
236 | |
---|
237 | msn_soap_memlist_request( ic ); |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | imcb_error( ic, "Unknown authentication type" ); |
---|
242 | imc_logout( ic, FALSE ); |
---|
243 | return( 0 ); |
---|
244 | } |
---|
245 | } |
---|
246 | else if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
247 | { |
---|
248 | if( num_parts != 4 ) |
---|
249 | { |
---|
250 | imcb_error( ic, "Syntax error" ); |
---|
251 | imc_logout( ic, TRUE ); |
---|
252 | return( 0 ); |
---|
253 | } |
---|
254 | |
---|
255 | md->handler->msglen = atoi( cmd[3] ); |
---|
256 | |
---|
257 | if( md->handler->msglen <= 0 ) |
---|
258 | { |
---|
259 | imcb_error( ic, "Syntax error" ); |
---|
260 | imc_logout( ic, TRUE ); |
---|
261 | return( 0 ); |
---|
262 | } |
---|
263 | } |
---|
264 | else if( strcmp( cmd[0], "BLP" ) == 0 ) |
---|
265 | { |
---|
266 | msn_ns_send_adl( ic ); |
---|
267 | } |
---|
268 | else if( strcmp( cmd[0], "ADL" ) == 0 ) |
---|
269 | { |
---|
270 | if( num_parts >= 3 && strcmp( cmd[2], "OK" ) == 0 ) |
---|
271 | { |
---|
272 | char buf[1024]; |
---|
273 | char *fn_raw = set_getstr( &ic->acc->set, "display_name" ); |
---|
274 | char *fn; |
---|
275 | |
---|
276 | if( fn_raw == NULL ) |
---|
277 | fn_raw = ic->acc->user; |
---|
278 | fn = g_malloc( strlen( fn_raw ) * 3 + 1 ); |
---|
279 | strcpy( fn, fn_raw ); |
---|
280 | http_encode( fn ); |
---|
281 | |
---|
282 | g_snprintf( buf, sizeof( buf ), "PRP %d MFN %s\r\n", |
---|
283 | ++md->trId, fn ); |
---|
284 | g_free( fn ); |
---|
285 | |
---|
286 | msn_write( ic, buf, strlen( buf ) ); |
---|
287 | } |
---|
288 | } |
---|
289 | else if( strcmp( cmd[0], "PRP" ) == 0 ) |
---|
290 | { |
---|
291 | imcb_connected( ic ); |
---|
292 | } |
---|
293 | else if( strcmp( cmd[0], "CHL" ) == 0 ) |
---|
294 | { |
---|
295 | char *resp; |
---|
296 | |
---|
297 | if( num_parts < 3 ) |
---|
298 | { |
---|
299 | imcb_error( ic, "Syntax error" ); |
---|
300 | imc_logout( ic, TRUE ); |
---|
301 | return( 0 ); |
---|
302 | } |
---|
303 | |
---|
304 | resp = msn_p11_challenge( cmd[2] ); |
---|
305 | g_snprintf( buf, sizeof( buf ), "QRY %d %s %zd\r\n%s", |
---|
306 | ++md->trId, MSNP11_PROD_ID, |
---|
307 | strlen( resp ), resp ); |
---|
308 | g_free( resp ); |
---|
309 | |
---|
310 | return( msn_write( ic, buf, strlen( buf ) ) ); |
---|
311 | } |
---|
312 | else if( strcmp( cmd[0], "ILN" ) == 0 ) |
---|
313 | { |
---|
314 | const struct msn_away_state *st; |
---|
315 | |
---|
316 | if( num_parts < 6 ) |
---|
317 | { |
---|
318 | imcb_error( ic, "Syntax error" ); |
---|
319 | imc_logout( ic, TRUE ); |
---|
320 | return( 0 ); |
---|
321 | } |
---|
322 | |
---|
323 | http_decode( cmd[5] ); |
---|
324 | imcb_rename_buddy( ic, cmd[3], cmd[5] ); |
---|
325 | |
---|
326 | st = msn_away_state_by_code( cmd[2] ); |
---|
327 | if( !st ) |
---|
328 | { |
---|
329 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
330 | st = msn_away_state_list + 1; |
---|
331 | } |
---|
332 | |
---|
333 | imcb_buddy_status( ic, cmd[3], OPT_LOGGED_IN | |
---|
334 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
335 | st->name, NULL ); |
---|
336 | } |
---|
337 | else if( strcmp( cmd[0], "FLN" ) == 0 ) |
---|
338 | { |
---|
339 | if( cmd[1] == NULL ) |
---|
340 | return 1; |
---|
341 | |
---|
342 | imcb_buddy_status( ic, cmd[1], 0, NULL, NULL ); |
---|
343 | |
---|
344 | msn_sb_start_keepalives( msn_sb_by_handle( ic, cmd[1] ), TRUE ); |
---|
345 | } |
---|
346 | else if( strcmp( cmd[0], "NLN" ) == 0 ) |
---|
347 | { |
---|
348 | const struct msn_away_state *st; |
---|
349 | |
---|
350 | if( num_parts != 5 ) |
---|
351 | { |
---|
352 | imcb_error( ic, "Syntax error" ); |
---|
353 | imc_logout( ic, TRUE ); |
---|
354 | return( 0 ); |
---|
355 | } |
---|
356 | |
---|
357 | http_decode( cmd[3] ); |
---|
358 | imcb_rename_buddy( ic, cmd[2], cmd[3] ); |
---|
359 | |
---|
360 | st = msn_away_state_by_code( cmd[1] ); |
---|
361 | if( !st ) |
---|
362 | { |
---|
363 | /* FIXME: Warn/Bomb about unknown away state? */ |
---|
364 | st = msn_away_state_list + 1; |
---|
365 | } |
---|
366 | |
---|
367 | imcb_buddy_status( ic, cmd[2], OPT_LOGGED_IN | |
---|
368 | ( st != msn_away_state_list ? OPT_AWAY : 0 ), |
---|
369 | st->name, NULL ); |
---|
370 | |
---|
371 | msn_sb_stop_keepalives( msn_sb_by_handle( ic, cmd[2] ) ); |
---|
372 | } |
---|
373 | else if( strcmp( cmd[0], "RNG" ) == 0 ) |
---|
374 | { |
---|
375 | struct msn_switchboard *sb; |
---|
376 | char *server; |
---|
377 | int session, port; |
---|
378 | |
---|
379 | if( num_parts != 7 ) |
---|
380 | { |
---|
381 | imcb_error( ic, "Syntax error" ); |
---|
382 | imc_logout( ic, TRUE ); |
---|
383 | return( 0 ); |
---|
384 | } |
---|
385 | |
---|
386 | session = atoi( cmd[1] ); |
---|
387 | |
---|
388 | server = strchr( cmd[2], ':' ); |
---|
389 | if( !server ) |
---|
390 | { |
---|
391 | imcb_error( ic, "Syntax error" ); |
---|
392 | imc_logout( ic, TRUE ); |
---|
393 | return( 0 ); |
---|
394 | } |
---|
395 | *server = 0; |
---|
396 | port = atoi( server + 1 ); |
---|
397 | server = cmd[2]; |
---|
398 | |
---|
399 | if( strcmp( cmd[3], "CKI" ) != 0 ) |
---|
400 | { |
---|
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 | { |
---|
410 | /* Although this isn't strictly fatal for the NS connection, it's |
---|
411 | definitely something serious (we ran out of file descriptors?). */ |
---|
412 | imcb_error( ic, "Could not create new switchboard" ); |
---|
413 | imc_logout( ic, TRUE ); |
---|
414 | return( 0 ); |
---|
415 | } |
---|
416 | else |
---|
417 | { |
---|
418 | sb->who = g_strdup( cmd[5] ); |
---|
419 | } |
---|
420 | } |
---|
421 | else if( strcmp( cmd[0], "ADD" ) == 0 ) |
---|
422 | { |
---|
423 | if( num_parts == 6 && strcmp( cmd[2], "RL" ) == 0 ) |
---|
424 | { |
---|
425 | GSList *l; |
---|
426 | |
---|
427 | http_decode( cmd[5] ); |
---|
428 | |
---|
429 | if( strchr( cmd[4], '@' ) == NULL ) |
---|
430 | { |
---|
431 | imcb_error( ic, "Syntax error" ); |
---|
432 | imc_logout( ic, TRUE ); |
---|
433 | return 0; |
---|
434 | } |
---|
435 | |
---|
436 | /* We got added by someone. If we don't have this |
---|
437 | person in permit/deny yet, inform the user. */ |
---|
438 | for( l = ic->permit; l; l = l->next ) |
---|
439 | if( g_strcasecmp( l->data, cmd[4] ) == 0 ) |
---|
440 | return 1; |
---|
441 | |
---|
442 | for( l = ic->deny; l; l = l->next ) |
---|
443 | if( g_strcasecmp( l->data, cmd[4] ) == 0 ) |
---|
444 | return 1; |
---|
445 | |
---|
446 | msn_buddy_ask( ic, cmd[4], cmd[5] ); |
---|
447 | } |
---|
448 | else if( num_parts >= 6 && strcmp( cmd[2], "FL" ) == 0 ) |
---|
449 | { |
---|
450 | const char *group = NULL; |
---|
451 | int num; |
---|
452 | |
---|
453 | if( cmd[6] != NULL && sscanf( cmd[6], "%d", &num ) == 1 && num < md->groupcount ) |
---|
454 | group = md->grouplist[num]; |
---|
455 | |
---|
456 | http_decode( cmd[5] ); |
---|
457 | imcb_add_buddy( ic, cmd[4], group ); |
---|
458 | imcb_rename_buddy( ic, cmd[4], cmd[5] ); |
---|
459 | } |
---|
460 | } |
---|
461 | else if( strcmp( cmd[0], "OUT" ) == 0 ) |
---|
462 | { |
---|
463 | int allow_reconnect = TRUE; |
---|
464 | |
---|
465 | if( cmd[1] && strcmp( cmd[1], "OTH" ) == 0 ) |
---|
466 | { |
---|
467 | imcb_error( ic, "Someone else logged in with your account" ); |
---|
468 | allow_reconnect = FALSE; |
---|
469 | } |
---|
470 | else if( cmd[1] && strcmp( cmd[1], "SSD" ) == 0 ) |
---|
471 | { |
---|
472 | imcb_error( ic, "Terminating session because of server shutdown" ); |
---|
473 | } |
---|
474 | else |
---|
475 | { |
---|
476 | imcb_error( ic, "Session terminated by remote server (reason unknown)" ); |
---|
477 | } |
---|
478 | |
---|
479 | imc_logout( ic, allow_reconnect ); |
---|
480 | return( 0 ); |
---|
481 | } |
---|
482 | #if 0 |
---|
483 | /* Discard this one completely for now since I don't care about the ack |
---|
484 | and since MSN servers can apparently screw up the formatting. */ |
---|
485 | else if( strcmp( cmd[0], "REA" ) == 0 ) |
---|
486 | { |
---|
487 | if( num_parts != 5 ) |
---|
488 | { |
---|
489 | imcb_error( ic, "Syntax error" ); |
---|
490 | imc_logout( ic, TRUE ); |
---|
491 | return( 0 ); |
---|
492 | } |
---|
493 | |
---|
494 | if( g_strcasecmp( cmd[3], ic->acc->user ) == 0 ) |
---|
495 | { |
---|
496 | set_t *s; |
---|
497 | |
---|
498 | http_decode( cmd[4] ); |
---|
499 | strncpy( ic->displayname, cmd[4], sizeof( ic->displayname ) ); |
---|
500 | ic->displayname[sizeof(ic->displayname)-1] = 0; |
---|
501 | |
---|
502 | if( ( s = set_find( &ic->acc->set, "display_name" ) ) ) |
---|
503 | { |
---|
504 | g_free( s->value ); |
---|
505 | s->value = g_strdup( cmd[4] ); |
---|
506 | } |
---|
507 | } |
---|
508 | else |
---|
509 | { |
---|
510 | /* This is not supposed to happen, but let's handle it anyway... */ |
---|
511 | http_decode( cmd[4] ); |
---|
512 | imcb_rename_buddy( ic, cmd[3], cmd[4] ); |
---|
513 | } |
---|
514 | } |
---|
515 | #endif |
---|
516 | else if( strcmp( cmd[0], "IPG" ) == 0 ) |
---|
517 | { |
---|
518 | imcb_error( ic, "Received IPG command, we don't handle them yet." ); |
---|
519 | |
---|
520 | md->handler->msglen = atoi( cmd[1] ); |
---|
521 | |
---|
522 | if( md->handler->msglen <= 0 ) |
---|
523 | { |
---|
524 | imcb_error( ic, "Syntax error" ); |
---|
525 | imc_logout( ic, TRUE ); |
---|
526 | return( 0 ); |
---|
527 | } |
---|
528 | } |
---|
529 | else if( strcmp( cmd[0], "ADG" ) == 0 ) |
---|
530 | { |
---|
531 | char *group = g_strdup( cmd[3] ); |
---|
532 | int groupnum, i; |
---|
533 | GSList *l, *next; |
---|
534 | |
---|
535 | http_decode( group ); |
---|
536 | if( sscanf( cmd[4], "%d", &groupnum ) == 1 ) |
---|
537 | { |
---|
538 | if( groupnum >= md->groupcount ) |
---|
539 | { |
---|
540 | md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 ); |
---|
541 | for( i = md->groupcount; i <= groupnum; i ++ ) |
---|
542 | md->grouplist[i] = NULL; |
---|
543 | md->groupcount = groupnum + 1; |
---|
544 | } |
---|
545 | g_free( md->grouplist[groupnum] ); |
---|
546 | md->grouplist[groupnum] = group; |
---|
547 | } |
---|
548 | else |
---|
549 | { |
---|
550 | /* Shouldn't happen, but if it does, give up on the group. */ |
---|
551 | g_free( group ); |
---|
552 | imcb_error( ic, "Syntax error" ); |
---|
553 | imc_logout( ic, TRUE ); |
---|
554 | return 0; |
---|
555 | } |
---|
556 | |
---|
557 | for( l = md->grpq; l; l = next ) |
---|
558 | { |
---|
559 | struct msn_groupadd *ga = l->data; |
---|
560 | next = l->next; |
---|
561 | if( g_strcasecmp( ga->group, group ) == 0 ) |
---|
562 | { |
---|
563 | if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) ) |
---|
564 | return 0; |
---|
565 | |
---|
566 | g_free( ga->group ); |
---|
567 | g_free( ga->who ); |
---|
568 | g_free( ga ); |
---|
569 | md->grpq = g_slist_remove( md->grpq, ga ); |
---|
570 | } |
---|
571 | } |
---|
572 | } |
---|
573 | else if( strcmp( cmd[0], "GCF" ) == 0 ) |
---|
574 | { |
---|
575 | /* Coming up is cmd[2] bytes of stuff we're supposed to |
---|
576 | censore. Meh. */ |
---|
577 | md->handler->msglen = atoi( cmd[2] ); |
---|
578 | } |
---|
579 | else if( strcmp( cmd[0], "UBX" ) == 0 ) |
---|
580 | { |
---|
581 | /* Status message. Parser coming soon. */ |
---|
582 | if( num_parts >= 4 ) |
---|
583 | md->handler->msglen = atoi( cmd[3] ); |
---|
584 | } |
---|
585 | else if( isdigit( cmd[0][0] ) ) |
---|
586 | { |
---|
587 | int num = atoi( cmd[0] ); |
---|
588 | const struct msn_status_code *err = msn_status_by_number( num ); |
---|
589 | |
---|
590 | imcb_error( ic, "Error reported by MSN server: %s", err->text ); |
---|
591 | |
---|
592 | if( err->flags & STATUS_FATAL ) |
---|
593 | { |
---|
594 | imc_logout( ic, TRUE ); |
---|
595 | return( 0 ); |
---|
596 | } |
---|
597 | } |
---|
598 | else |
---|
599 | { |
---|
600 | /* debug( "Received unknown command from main server: %s", cmd[0] ); */ |
---|
601 | } |
---|
602 | |
---|
603 | return( 1 ); |
---|
604 | } |
---|
605 | |
---|
606 | static int msn_ns_message( gpointer data, char *msg, int msglen, char **cmd, int num_parts ) |
---|
607 | { |
---|
608 | struct im_connection *ic = data; |
---|
609 | char *body; |
---|
610 | int blen = 0; |
---|
611 | |
---|
612 | if( !num_parts ) |
---|
613 | return( 1 ); |
---|
614 | |
---|
615 | if( ( body = strstr( msg, "\r\n\r\n" ) ) ) |
---|
616 | { |
---|
617 | body += 4; |
---|
618 | blen = msglen - ( body - msg ); |
---|
619 | } |
---|
620 | |
---|
621 | if( strcmp( cmd[0], "MSG" ) == 0 ) |
---|
622 | { |
---|
623 | if( g_strcasecmp( cmd[1], "Hotmail" ) == 0 ) |
---|
624 | { |
---|
625 | char *ct = msn_findheader( msg, "Content-Type:", msglen ); |
---|
626 | |
---|
627 | if( !ct ) |
---|
628 | return( 1 ); |
---|
629 | |
---|
630 | if( g_strncasecmp( ct, "application/x-msmsgssystemmessage", 33 ) == 0 ) |
---|
631 | { |
---|
632 | char *mtype; |
---|
633 | char *arg1; |
---|
634 | |
---|
635 | if( !body ) |
---|
636 | return( 1 ); |
---|
637 | |
---|
638 | mtype = msn_findheader( body, "Type:", blen ); |
---|
639 | arg1 = msn_findheader( body, "Arg1:", blen ); |
---|
640 | |
---|
641 | if( mtype && strcmp( mtype, "1" ) == 0 ) |
---|
642 | { |
---|
643 | if( arg1 ) |
---|
644 | imcb_log( ic, "The server is going down for maintenance in %s minutes.", arg1 ); |
---|
645 | } |
---|
646 | |
---|
647 | g_free( arg1 ); |
---|
648 | g_free( mtype ); |
---|
649 | } |
---|
650 | else if( g_strncasecmp( ct, "text/x-msmsgsprofile", 20 ) == 0 ) |
---|
651 | { |
---|
652 | /* We don't care about this profile for now... */ |
---|
653 | } |
---|
654 | else if( g_strncasecmp( ct, "text/x-msmsgsinitialemailnotification", 37 ) == 0 ) |
---|
655 | { |
---|
656 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
657 | { |
---|
658 | char *inbox = msn_findheader( body, "Inbox-Unread:", blen ); |
---|
659 | char *folders = msn_findheader( body, "Folders-Unread:", blen ); |
---|
660 | |
---|
661 | if( inbox && folders ) |
---|
662 | imcb_log( ic, "INBOX contains %s new messages, plus %s messages in other folders.", inbox, folders ); |
---|
663 | |
---|
664 | g_free( inbox ); |
---|
665 | g_free( folders ); |
---|
666 | } |
---|
667 | } |
---|
668 | else if( g_strncasecmp( ct, "text/x-msmsgsemailnotification", 30 ) == 0 ) |
---|
669 | { |
---|
670 | if( set_getbool( &ic->acc->set, "mail_notifications" ) ) |
---|
671 | { |
---|
672 | char *from = msn_findheader( body, "From-Addr:", blen ); |
---|
673 | char *fromname = msn_findheader( body, "From:", blen ); |
---|
674 | |
---|
675 | if( from && fromname ) |
---|
676 | imcb_log( ic, "Received an e-mail message from %s <%s>.", fromname, from ); |
---|
677 | |
---|
678 | g_free( from ); |
---|
679 | g_free( fromname ); |
---|
680 | } |
---|
681 | } |
---|
682 | else if( g_strncasecmp( ct, "text/x-msmsgsactivemailnotification", 35 ) == 0 ) |
---|
683 | { |
---|
684 | /* Sorry, but this one really is *USELESS* */ |
---|
685 | } |
---|
686 | else |
---|
687 | { |
---|
688 | debug( "Can't handle %s packet from notification server", ct ); |
---|
689 | } |
---|
690 | |
---|
691 | g_free( ct ); |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | return( 1 ); |
---|
696 | } |
---|
697 | |
---|
698 | void msn_auth_got_passport_token( struct im_connection *ic, char *token ) |
---|
699 | { |
---|
700 | struct msn_data *md; |
---|
701 | |
---|
702 | /* Dead connection? */ |
---|
703 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
704 | return; |
---|
705 | |
---|
706 | md = ic->proto_data; |
---|
707 | |
---|
708 | { |
---|
709 | char buf[1024]; |
---|
710 | |
---|
711 | g_snprintf( buf, sizeof( buf ), "USR %d SSO S %s %s\r\n", ++md->trId, md->tokens[0], token ); |
---|
712 | msn_write( ic, buf, strlen( buf ) ); |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | void msn_auth_got_contact_list( struct im_connection *ic ) |
---|
717 | { |
---|
718 | char buf[64]; |
---|
719 | struct msn_data *md; |
---|
720 | |
---|
721 | /* Dead connection? */ |
---|
722 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
723 | return; |
---|
724 | |
---|
725 | md = ic->proto_data; |
---|
726 | |
---|
727 | |
---|
728 | g_snprintf( buf, sizeof( buf ), "BLP %d %s\r\n", ++md->trId, "BL" ); |
---|
729 | msn_write( ic, buf, strlen( buf ) ); |
---|
730 | } |
---|
731 | |
---|
732 | static gboolean msn_ns_send_adl_1( gpointer key, gpointer value, gpointer data ) |
---|
733 | { |
---|
734 | struct xt_node *adl = data, *d, *c; |
---|
735 | struct bee_user *bu = value; |
---|
736 | struct msn_buddy_data *bd = bu->data; |
---|
737 | char handle[strlen(bu->handle)]; |
---|
738 | char *domain; |
---|
739 | char l[4]; |
---|
740 | |
---|
741 | strcpy( handle, bu->handle ); |
---|
742 | if( ( domain = strchr( handle, '@' ) ) == NULL ) /* WTF */ |
---|
743 | return FALSE; |
---|
744 | *domain = '\0'; |
---|
745 | domain ++; |
---|
746 | |
---|
747 | if( ( d = adl->children ) == NULL || |
---|
748 | g_strcasecmp( xt_find_attr( d, "n" ), domain ) != 0 ) |
---|
749 | { |
---|
750 | d = xt_new_node( "d", NULL, NULL ); |
---|
751 | xt_add_attr( d, "n", domain ); |
---|
752 | xt_insert_child( adl, d ); |
---|
753 | } |
---|
754 | |
---|
755 | g_snprintf( l, sizeof( l ), "%d", bd->flags & 7 ); |
---|
756 | c = xt_new_node( "c", NULL, NULL ); |
---|
757 | xt_add_attr( c, "n", handle ); |
---|
758 | xt_add_attr( c, "l", l ); |
---|
759 | xt_add_attr( c, "t", "1" ); /* 1 means normal, 4 means mobile? */ |
---|
760 | xt_insert_child( d, c ); |
---|
761 | |
---|
762 | return FALSE; |
---|
763 | } |
---|
764 | |
---|
765 | static void msn_ns_send_adl( struct im_connection *ic ) |
---|
766 | { |
---|
767 | struct xt_node *adl; |
---|
768 | struct msn_data *md; |
---|
769 | char *adls, buf[64]; |
---|
770 | |
---|
771 | /* Dead connection? */ |
---|
772 | if( g_slist_find( msn_connections, ic ) == NULL ) |
---|
773 | return; |
---|
774 | |
---|
775 | md = ic->proto_data; |
---|
776 | |
---|
777 | adl = xt_new_node( "ml", NULL, NULL ); |
---|
778 | xt_add_attr( adl, "l", "1" ); |
---|
779 | g_tree_foreach( md->domaintree, msn_ns_send_adl_1, adl ); |
---|
780 | adls = xt_to_string( adl ); |
---|
781 | |
---|
782 | g_snprintf( buf, sizeof( buf ), "ADL %d %zd\r\n", ++md->trId, strlen( adls ) ); |
---|
783 | if( msn_write( ic, buf, strlen( buf ) ) ) |
---|
784 | msn_write( ic, adls, strlen( adls ) ); |
---|
785 | |
---|
786 | g_free( adls ); |
---|
787 | xt_free_node( adl ); |
---|
788 | } |
---|
789 | |
---|
790 | static gboolean msn_ns_got_display_name( struct im_connection *ic, char *name ) |
---|
791 | { |
---|
792 | set_t *s; |
---|
793 | |
---|
794 | if( ( s = set_find( &ic->acc->set, "display_name" ) ) == NULL ) |
---|
795 | return FALSE; /* Shouldn't happen.. */ |
---|
796 | |
---|
797 | http_decode( name ); |
---|
798 | |
---|
799 | if( s->value && strcmp( s->value, name ) == 0 ) |
---|
800 | { |
---|
801 | return TRUE; |
---|
802 | /* The names match, nothing to worry about. */ |
---|
803 | } |
---|
804 | else if( s->value != NULL && |
---|
805 | ( strcmp( name, ic->acc->user ) == 0 || |
---|
806 | set_getbool( &ic->acc->set, "local_display_name" ) ) ) |
---|
807 | { |
---|
808 | /* The server thinks our display name is our e-mail address |
---|
809 | which is probably wrong, or the user *wants* us to do this: |
---|
810 | Always use the locally set display_name. */ |
---|
811 | return msn_set_display_name( ic, s->value ); |
---|
812 | } |
---|
813 | else |
---|
814 | { |
---|
815 | if( s->value && *s->value ) |
---|
816 | imcb_log( ic, "BitlBee thinks your display name is `%s' but " |
---|
817 | "the MSN server says it's `%s'. Using the MSN " |
---|
818 | "server's name. Set local_display_name to true " |
---|
819 | "to use the local name.", s->value, name ); |
---|
820 | |
---|
821 | if( g_utf8_validate( name, -1, NULL ) ) |
---|
822 | { |
---|
823 | g_free( s->value ); |
---|
824 | s->value = g_strdup( name ); |
---|
825 | } |
---|
826 | else |
---|
827 | { |
---|
828 | imcb_log( ic, "Warning: Friendly name in server response was corrupted" ); |
---|
829 | } |
---|
830 | |
---|
831 | return TRUE; |
---|
832 | } |
---|
833 | } |
---|