[b7d3cc34] | 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 - Miscellaneous utilities */ |
---|
| 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 "nogaim.h" |
---|
| 27 | #include "msn.h" |
---|
| 28 | #include <ctype.h> |
---|
| 29 | |
---|
[0da65d5] | 30 | int msn_write( struct im_connection *ic, char *s, int len ) |
---|
[b7d3cc34] | 31 | { |
---|
[0da65d5] | 32 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 33 | int st; |
---|
| 34 | |
---|
| 35 | st = write( md->fd, s, len ); |
---|
| 36 | if( st != len ) |
---|
| 37 | { |
---|
[84b045d] | 38 | imcb_error( ic, "Short write() to main server" ); |
---|
[c2fb3809] | 39 | imc_logout( ic, TRUE ); |
---|
[e3413cc] | 40 | return 0; |
---|
[b7d3cc34] | 41 | } |
---|
| 42 | |
---|
[e3413cc] | 43 | return 1; |
---|
[b7d3cc34] | 44 | } |
---|
| 45 | |
---|
[0da65d5] | 46 | int msn_logged_in( struct im_connection *ic ) |
---|
[b7d3cc34] | 47 | { |
---|
[84b045d] | 48 | imcb_connected( ic ); |
---|
[b7d3cc34] | 49 | |
---|
| 50 | return( 0 ); |
---|
| 51 | } |
---|
| 52 | |
---|
[6acc033] | 53 | int msn_buddy_list_add( struct im_connection *ic, const char *list, const char *who, const char *realname_, const char *group ) |
---|
[b7d3cc34] | 54 | { |
---|
[0da65d5] | 55 | struct msn_data *md = ic->proto_data; |
---|
[6acc033] | 56 | char buf[1024], *realname, groupid[8]; |
---|
[b7d3cc34] | 57 | |
---|
[6acc033] | 58 | *groupid = '\0'; |
---|
| 59 | if( group ) |
---|
| 60 | { |
---|
| 61 | int i; |
---|
| 62 | for( i = 0; i < md->groupcount; i ++ ) |
---|
| 63 | if( g_strcasecmp( md->grouplist[i], group ) == 0 ) |
---|
| 64 | { |
---|
| 65 | g_snprintf( groupid, sizeof( groupid ), " %d", i ); |
---|
| 66 | break; |
---|
| 67 | } |
---|
[b7d3cc34] | 68 | |
---|
[70ac477] | 69 | if( *groupid == '\0' ) |
---|
| 70 | { |
---|
| 71 | /* Have to create this group, it doesn't exist yet. */ |
---|
| 72 | struct msn_groupadd *ga; |
---|
| 73 | GSList *l; |
---|
| 74 | |
---|
| 75 | for( l = md->grpq; l; l = l->next ) |
---|
| 76 | { |
---|
| 77 | ga = l->data; |
---|
| 78 | if( g_strcasecmp( ga->group, group ) == 0 ) |
---|
| 79 | break; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | ga = g_new0( struct msn_groupadd, 1 ); |
---|
| 83 | ga->who = g_strdup( who ); |
---|
| 84 | ga->group = g_strdup( group ); |
---|
| 85 | md->grpq = g_slist_prepend( md->grpq, ga ); |
---|
| 86 | |
---|
| 87 | if( l == NULL ) |
---|
| 88 | { |
---|
| 89 | char *groupname = msn_http_encode( group ); |
---|
| 90 | g_snprintf( buf, sizeof( buf ), "ADG %d %s %d\r\n", ++md->trId, groupname, 0 ); |
---|
| 91 | g_free( groupname ); |
---|
| 92 | return msn_write( ic, buf, strlen( buf ) ); |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | /* This can happen if the user's doing lots of adds to a |
---|
| 97 | new group at once; we're still waiting for the server |
---|
| 98 | to confirm group creation. */ |
---|
| 99 | return 1; |
---|
| 100 | } |
---|
| 101 | } |
---|
[b7d3cc34] | 102 | } |
---|
| 103 | |
---|
[70ac477] | 104 | realname = msn_http_encode( realname_ ); |
---|
| 105 | g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s%s\r\n", ++md->trId, list, who, realname, groupid ); |
---|
[b7d3cc34] | 106 | g_free( realname ); |
---|
| 107 | |
---|
[70ac477] | 108 | return msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 109 | } |
---|
| 110 | |
---|
[8b01217] | 111 | int msn_buddy_list_remove( struct im_connection *ic, char *list, const char *who, const char *group ) |
---|
[b7d3cc34] | 112 | { |
---|
[0da65d5] | 113 | struct msn_data *md = ic->proto_data; |
---|
[8b01217] | 114 | char buf[1024], groupid[8]; |
---|
[b7d3cc34] | 115 | |
---|
[8b01217] | 116 | *groupid = '\0'; |
---|
| 117 | if( group ) |
---|
| 118 | { |
---|
| 119 | int i; |
---|
| 120 | for( i = 0; i < md->groupcount; i ++ ) |
---|
| 121 | if( g_strcasecmp( md->grouplist[i], group ) == 0 ) |
---|
| 122 | { |
---|
| 123 | g_snprintf( groupid, sizeof( groupid ), " %d", i ); |
---|
| 124 | break; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | g_snprintf( buf, sizeof( buf ), "REM %d %s %s%s\r\n", ++md->trId, list, who, groupid ); |
---|
[0da65d5] | 129 | if( msn_write( ic, buf, strlen( buf ) ) ) |
---|
[b7d3cc34] | 130 | return( 1 ); |
---|
| 131 | |
---|
| 132 | return( 0 ); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | struct msn_buddy_ask_data |
---|
| 136 | { |
---|
[0da65d5] | 137 | struct im_connection *ic; |
---|
[b7d3cc34] | 138 | char *handle; |
---|
| 139 | char *realname; |
---|
| 140 | }; |
---|
| 141 | |
---|
[9143aeb] | 142 | static void msn_buddy_ask_yes( void *data ) |
---|
[b7d3cc34] | 143 | { |
---|
[9143aeb] | 144 | struct msn_buddy_ask_data *bla = data; |
---|
| 145 | |
---|
[6acc033] | 146 | msn_buddy_list_add( bla->ic, "AL", bla->handle, bla->realname, NULL ); |
---|
[b7d3cc34] | 147 | |
---|
[17a6ee9] | 148 | imcb_ask_add( bla->ic, bla->handle, NULL ); |
---|
[e6d6047] | 149 | |
---|
[b7d3cc34] | 150 | g_free( bla->handle ); |
---|
| 151 | g_free( bla->realname ); |
---|
| 152 | g_free( bla ); |
---|
| 153 | } |
---|
| 154 | |
---|
[9143aeb] | 155 | static void msn_buddy_ask_no( void *data ) |
---|
[b7d3cc34] | 156 | { |
---|
[9143aeb] | 157 | struct msn_buddy_ask_data *bla = data; |
---|
| 158 | |
---|
[6acc033] | 159 | msn_buddy_list_add( bla->ic, "BL", bla->handle, bla->realname, NULL ); |
---|
[b7d3cc34] | 160 | |
---|
| 161 | g_free( bla->handle ); |
---|
| 162 | g_free( bla->realname ); |
---|
| 163 | g_free( bla ); |
---|
| 164 | } |
---|
| 165 | |
---|
[0da65d5] | 166 | void msn_buddy_ask( struct im_connection *ic, char *handle, char *realname ) |
---|
[b7d3cc34] | 167 | { |
---|
| 168 | struct msn_buddy_ask_data *bla = g_new0( struct msn_buddy_ask_data, 1 ); |
---|
| 169 | char buf[1024]; |
---|
| 170 | |
---|
[0da65d5] | 171 | bla->ic = ic; |
---|
[b7d3cc34] | 172 | bla->handle = g_strdup( handle ); |
---|
| 173 | bla->realname = g_strdup( realname ); |
---|
| 174 | |
---|
| 175 | g_snprintf( buf, sizeof( buf ), |
---|
[5c09a59] | 176 | "The user %s (%s) wants to add you to his/her buddy list.", |
---|
[b7d3cc34] | 177 | handle, realname ); |
---|
[84b045d] | 178 | imcb_ask( ic, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no ); |
---|
[b7d3cc34] | 179 | } |
---|
| 180 | |
---|
| 181 | char *msn_findheader( char *text, char *header, int len ) |
---|
| 182 | { |
---|
| 183 | int hlen = strlen( header ), i; |
---|
| 184 | char *ret; |
---|
| 185 | |
---|
| 186 | if( len == 0 ) |
---|
| 187 | len = strlen( text ); |
---|
| 188 | |
---|
| 189 | i = 0; |
---|
| 190 | while( ( i + hlen ) < len ) |
---|
| 191 | { |
---|
| 192 | /* Maybe this is a bit over-commented, but I just hate this part... */ |
---|
| 193 | if( g_strncasecmp( text + i, header, hlen ) == 0 ) |
---|
| 194 | { |
---|
| 195 | /* Skip to the (probable) end of the header */ |
---|
| 196 | i += hlen; |
---|
| 197 | |
---|
| 198 | /* Find the first non-[: \t] character */ |
---|
| 199 | while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++; |
---|
| 200 | |
---|
| 201 | /* Make sure we're still inside the string */ |
---|
| 202 | if( i >= len ) return( NULL ); |
---|
| 203 | |
---|
| 204 | /* Save the position */ |
---|
| 205 | ret = text + i; |
---|
| 206 | |
---|
| 207 | /* Search for the end of this line */ |
---|
| 208 | while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++; |
---|
| 209 | |
---|
| 210 | /* Make sure we're still inside the string */ |
---|
| 211 | if( i >= len ) return( NULL ); |
---|
| 212 | |
---|
| 213 | /* Copy the found data */ |
---|
| 214 | return( g_strndup( ret, text + i - ret ) ); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /* This wasn't the header we were looking for, skip to the next line. */ |
---|
| 218 | while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++; |
---|
| 219 | while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++; |
---|
| 220 | |
---|
| 221 | /* End of headers? */ |
---|
[75ec2c8] | 222 | if( ( i >= 4 && strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 ) || |
---|
| 223 | ( i >= 2 && ( strncmp( text + i - 2, "\n\n", 2 ) == 0 || |
---|
| 224 | strncmp( text + i - 2, "\r\r", 2 ) == 0 ) ) ) |
---|
[b7d3cc34] | 225 | { |
---|
| 226 | break; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | return( NULL ); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /* *NOT* thread-safe, but that's not a problem for now... */ |
---|
| 234 | char **msn_linesplit( char *line ) |
---|
| 235 | { |
---|
| 236 | static char **ret = NULL; |
---|
| 237 | static int size = 3; |
---|
| 238 | int i, n = 0; |
---|
| 239 | |
---|
| 240 | if( ret == NULL ) |
---|
| 241 | ret = g_new0( char*, size ); |
---|
| 242 | |
---|
| 243 | for( i = 0; line[i] && line[i] == ' '; i ++ ); |
---|
| 244 | if( line[i] ) |
---|
| 245 | { |
---|
| 246 | ret[n++] = line + i; |
---|
| 247 | for( i ++; line[i]; i ++ ) |
---|
| 248 | { |
---|
| 249 | if( line[i] == ' ' ) |
---|
| 250 | line[i] = 0; |
---|
| 251 | else if( line[i] != ' ' && !line[i-1] ) |
---|
| 252 | ret[n++] = line + i; |
---|
| 253 | |
---|
| 254 | if( n >= size ) |
---|
| 255 | ret = g_renew( char*, ret, size += 2 ); |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | ret[n] = NULL; |
---|
| 259 | |
---|
| 260 | return( ret ); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give |
---|
| 264 | commands, but sometimes they give additional data (payload). This function tries to handle |
---|
| 265 | this all in a nice way and send all data to the right places. */ |
---|
| 266 | |
---|
| 267 | /* Return values: -1: Read error, abort connection. |
---|
| 268 | 0: Command reported error; Abort *immediately*. (The connection does not exist anymore) |
---|
| 269 | 1: OK */ |
---|
| 270 | |
---|
| 271 | int msn_handler( struct msn_handler_data *h ) |
---|
| 272 | { |
---|
| 273 | int st; |
---|
| 274 | |
---|
| 275 | h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 ); |
---|
| 276 | st = read( h->fd, h->rxq + h->rxlen, 1024 ); |
---|
| 277 | h->rxlen += st; |
---|
| 278 | |
---|
| 279 | if( st <= 0 ) |
---|
| 280 | return( -1 ); |
---|
| 281 | |
---|
| 282 | while( st ) |
---|
| 283 | { |
---|
| 284 | int i; |
---|
| 285 | |
---|
| 286 | if( h->msglen == 0 ) |
---|
| 287 | { |
---|
| 288 | for( i = 0; i < h->rxlen; i ++ ) |
---|
| 289 | { |
---|
| 290 | if( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) |
---|
| 291 | { |
---|
| 292 | char *cmd_text, **cmd; |
---|
| 293 | int count; |
---|
| 294 | |
---|
| 295 | cmd_text = g_strndup( h->rxq, i ); |
---|
| 296 | cmd = msn_linesplit( cmd_text ); |
---|
| 297 | for( count = 0; cmd[count]; count ++ ); |
---|
| 298 | st = h->exec_command( h->data, cmd, count ); |
---|
| 299 | g_free( cmd_text ); |
---|
| 300 | |
---|
| 301 | /* If the connection broke, don't continue. We don't even exist anymore. */ |
---|
| 302 | if( !st ) |
---|
| 303 | return( 0 ); |
---|
| 304 | |
---|
| 305 | if( h->msglen ) |
---|
| 306 | h->cmd_text = g_strndup( h->rxq, i ); |
---|
| 307 | |
---|
| 308 | /* Skip to the next non-emptyline */ |
---|
| 309 | while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++; |
---|
| 310 | |
---|
| 311 | break; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | /* If we reached the end of the buffer, there's still an incomplete command there. |
---|
| 316 | Return and wait for more data. */ |
---|
| 317 | if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' ) |
---|
| 318 | break; |
---|
| 319 | } |
---|
| 320 | else |
---|
| 321 | { |
---|
| 322 | char *msg, **cmd; |
---|
| 323 | int count; |
---|
| 324 | |
---|
| 325 | /* Do we have the complete message already? */ |
---|
| 326 | if( h->msglen > h->rxlen ) |
---|
| 327 | break; |
---|
| 328 | |
---|
| 329 | msg = g_strndup( h->rxq, h->msglen ); |
---|
| 330 | cmd = msn_linesplit( h->cmd_text ); |
---|
| 331 | for( count = 0; cmd[count]; count ++ ); |
---|
| 332 | |
---|
| 333 | st = h->exec_message( h->data, msg, h->msglen, cmd, count ); |
---|
| 334 | g_free( msg ); |
---|
| 335 | g_free( h->cmd_text ); |
---|
| 336 | h->cmd_text = NULL; |
---|
| 337 | |
---|
| 338 | if( !st ) |
---|
| 339 | return( 0 ); |
---|
| 340 | |
---|
| 341 | i = h->msglen; |
---|
| 342 | h->msglen = 0; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /* More data after this block? */ |
---|
| 346 | if( i < h->rxlen ) |
---|
| 347 | { |
---|
| 348 | char *tmp; |
---|
| 349 | |
---|
| 350 | tmp = g_memdup( h->rxq + i, h->rxlen - i ); |
---|
| 351 | g_free( h->rxq ); |
---|
| 352 | h->rxq = tmp; |
---|
| 353 | h->rxlen -= i; |
---|
| 354 | i = 0; |
---|
| 355 | } |
---|
| 356 | else |
---|
| 357 | /* If not, reset the rx queue and get lost. */ |
---|
| 358 | { |
---|
| 359 | g_free( h->rxq ); |
---|
| 360 | h->rxq = g_new0( char, 1 ); |
---|
| 361 | h->rxlen = 0; |
---|
| 362 | return( 1 ); |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | return( 1 ); |
---|
| 367 | } |
---|
[54794b8] | 368 | |
---|
| 369 | /* The difference between this function and the normal http_encode() function |
---|
| 370 | is that this one escapes every 7-bit ASCII character because this is said |
---|
| 371 | to avoid some lame server-side checks when setting a real-name. Also, |
---|
| 372 | non-ASCII characters are not escaped because MSN servers don't seem to |
---|
| 373 | appreciate that! */ |
---|
| 374 | char *msn_http_encode( const char *input ) |
---|
| 375 | { |
---|
| 376 | char *ret, *s; |
---|
| 377 | int i; |
---|
| 378 | |
---|
| 379 | ret = s = g_new0( char, strlen( input ) * 3 + 1 ); |
---|
| 380 | for( i = 0; input[i]; i ++ ) |
---|
| 381 | if( input[i] & 128 ) |
---|
| 382 | { |
---|
| 383 | *s = input[i]; |
---|
| 384 | s ++; |
---|
| 385 | } |
---|
| 386 | else |
---|
| 387 | { |
---|
| 388 | g_snprintf( s, 4, "%%%02X", input[i] ); |
---|
| 389 | s += 3; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | return ret; |
---|
| 393 | } |
---|
[46dca11] | 394 | |
---|
| 395 | void msn_msgq_purge( struct im_connection *ic, GSList **list ) |
---|
| 396 | { |
---|
| 397 | struct msn_message *m; |
---|
| 398 | GString *ret; |
---|
| 399 | GSList *l; |
---|
[4255320] | 400 | int n = 0; |
---|
[46dca11] | 401 | |
---|
| 402 | l = *list; |
---|
| 403 | if( l == NULL ) |
---|
| 404 | return; |
---|
| 405 | |
---|
| 406 | m = l->data; |
---|
| 407 | ret = g_string_sized_new( 1024 ); |
---|
[43462708] | 408 | g_string_printf( ret, "Warning: Cleaning up MSN (switchboard) connection with unsent " |
---|
[46dca11] | 409 | "messages to %s:", m->who ? m->who : "unknown recipient" ); |
---|
| 410 | |
---|
| 411 | while( l ) |
---|
| 412 | { |
---|
| 413 | m = l->data; |
---|
| 414 | |
---|
[4255320] | 415 | if( strncmp( m->text, "\r\r\r", 3 ) != 0 ) |
---|
| 416 | { |
---|
| 417 | g_string_append_printf( ret, "\n%s", m->text ); |
---|
| 418 | n ++; |
---|
| 419 | } |
---|
[46dca11] | 420 | |
---|
| 421 | g_free( m->who ); |
---|
| 422 | g_free( m->text ); |
---|
| 423 | g_free( m ); |
---|
| 424 | |
---|
| 425 | l = l->next; |
---|
| 426 | } |
---|
| 427 | g_slist_free( *list ); |
---|
| 428 | *list = NULL; |
---|
| 429 | |
---|
[4255320] | 430 | if( n > 0 ) |
---|
| 431 | imcb_log( ic, "%s", ret->str ); |
---|
[46dca11] | 432 | g_string_free( ret, TRUE ); |
---|
| 433 | } |
---|
[e3413cc] | 434 | |
---|
| 435 | gboolean msn_set_display_name( struct im_connection *ic, const char *rawname ) |
---|
| 436 | { |
---|
| 437 | char *fn = msn_http_encode( rawname ); |
---|
| 438 | struct msn_data *md = ic->proto_data; |
---|
| 439 | char buf[1024]; |
---|
| 440 | |
---|
| 441 | g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, ic->acc->user, fn ); |
---|
| 442 | g_free( fn ); |
---|
| 443 | |
---|
| 444 | return msn_write( ic, buf, strlen( buf ) ) != 0; |
---|
| 445 | } |
---|