[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 | |
---|
| 30 | int msn_write( struct gaim_connection *gc, char *s, int len ) |
---|
| 31 | { |
---|
| 32 | struct msn_data *md = gc->proto_data; |
---|
| 33 | int st; |
---|
| 34 | |
---|
| 35 | st = write( md->fd, s, len ); |
---|
| 36 | if( st != len ) |
---|
| 37 | { |
---|
| 38 | hide_login_progress_error( gc, "Short write() to main server" ); |
---|
| 39 | signoff( gc ); |
---|
| 40 | return( 0 ); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | return( 1 ); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | int msn_logged_in( struct gaim_connection *gc ) |
---|
| 47 | { |
---|
| 48 | account_online( gc ); |
---|
| 49 | |
---|
| 50 | return( 0 ); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | int msn_buddy_list_add( struct gaim_connection *gc, char *list, char *who, char *realname_ ) |
---|
| 54 | { |
---|
| 55 | struct msn_data *md = gc->proto_data; |
---|
| 56 | GSList *l, **lp = NULL; |
---|
| 57 | char buf[1024], *realname; |
---|
| 58 | |
---|
| 59 | if( strcmp( list, "AL" ) == 0 ) |
---|
| 60 | lp = &gc->permit; |
---|
| 61 | else if( strcmp( list, "BL" ) == 0 ) |
---|
| 62 | lp = &gc->deny; |
---|
| 63 | |
---|
| 64 | if( lp ) |
---|
| 65 | for( l = *lp; l; l = l->next ) |
---|
| 66 | if( g_strcasecmp( l->data, who ) == 0 ) |
---|
| 67 | return( 1 ); |
---|
| 68 | |
---|
| 69 | realname = g_new0( char, strlen( realname_ ) * 3 + 1 ); |
---|
| 70 | strcpy( realname, realname_ ); |
---|
| 71 | http_encode( realname ); |
---|
| 72 | |
---|
| 73 | g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s\r\n", ++md->trId, list, who, realname ); |
---|
| 74 | if( msn_write( gc, buf, strlen( buf ) ) ) |
---|
| 75 | { |
---|
| 76 | g_free( realname ); |
---|
| 77 | |
---|
| 78 | if( lp ) |
---|
| 79 | *lp = g_slist_append( *lp, g_strdup( who ) ); |
---|
| 80 | |
---|
| 81 | return( 1 ); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | g_free( realname ); |
---|
| 85 | |
---|
| 86 | return( 0 ); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | int msn_buddy_list_remove( struct gaim_connection *gc, char *list, char *who ) |
---|
| 90 | { |
---|
| 91 | struct msn_data *md = gc->proto_data; |
---|
| 92 | GSList *l = NULL, **lp = NULL; |
---|
| 93 | char buf[1024]; |
---|
| 94 | |
---|
| 95 | if( strcmp( list, "AL" ) == 0 ) |
---|
| 96 | lp = &gc->permit; |
---|
| 97 | else if( strcmp( list, "BL" ) == 0 ) |
---|
| 98 | lp = &gc->deny; |
---|
| 99 | |
---|
| 100 | if( lp ) |
---|
| 101 | { |
---|
| 102 | for( l = *lp; l; l = l->next ) |
---|
| 103 | if( g_strcasecmp( l->data, who ) == 0 ) |
---|
| 104 | break; |
---|
| 105 | |
---|
| 106 | if( !l ) |
---|
| 107 | return( 1 ); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | g_snprintf( buf, sizeof( buf ), "REM %d %s %s\r\n", ++md->trId, list, who ); |
---|
| 111 | if( msn_write( gc, buf, strlen( buf ) ) ) |
---|
| 112 | { |
---|
| 113 | if( lp ) |
---|
| 114 | *lp = g_slist_remove( *lp, l->data ); |
---|
| 115 | |
---|
| 116 | return( 1 ); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | return( 0 ); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | struct msn_buddy_ask_data |
---|
| 123 | { |
---|
| 124 | struct gaim_connection *gc; |
---|
| 125 | char *handle; |
---|
| 126 | char *realname; |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | static void msn_buddy_ask_yes( gpointer w, struct msn_buddy_ask_data *bla ) |
---|
| 130 | { |
---|
| 131 | msn_buddy_list_add( bla->gc, "AL", bla->handle, bla->realname ); |
---|
| 132 | |
---|
[e6d6047] | 133 | if( find_buddy( bla->gc, bla->handle ) == NULL ) |
---|
| 134 | show_got_added( bla->gc, bla->handle, NULL ); |
---|
| 135 | |
---|
[b7d3cc34] | 136 | g_free( bla->handle ); |
---|
| 137 | g_free( bla->realname ); |
---|
| 138 | g_free( bla ); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | static void msn_buddy_ask_no( gpointer w, struct msn_buddy_ask_data *bla ) |
---|
| 142 | { |
---|
| 143 | msn_buddy_list_add( bla->gc, "BL", bla->handle, bla->realname ); |
---|
| 144 | |
---|
| 145 | g_free( bla->handle ); |
---|
| 146 | g_free( bla->realname ); |
---|
| 147 | g_free( bla ); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | void msn_buddy_ask( struct gaim_connection *gc, char *handle, char *realname ) |
---|
| 151 | { |
---|
| 152 | struct msn_buddy_ask_data *bla = g_new0( struct msn_buddy_ask_data, 1 ); |
---|
| 153 | char buf[1024]; |
---|
| 154 | |
---|
| 155 | bla->gc = gc; |
---|
| 156 | bla->handle = g_strdup( handle ); |
---|
| 157 | bla->realname = g_strdup( realname ); |
---|
| 158 | |
---|
| 159 | g_snprintf( buf, sizeof( buf ), |
---|
[5c09a59] | 160 | "The user %s (%s) wants to add you to his/her buddy list.", |
---|
[b7d3cc34] | 161 | handle, realname ); |
---|
| 162 | do_ask_dialog( gc, buf, bla, msn_buddy_ask_yes, msn_buddy_ask_no ); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | char *msn_findheader( char *text, char *header, int len ) |
---|
| 166 | { |
---|
| 167 | int hlen = strlen( header ), i; |
---|
| 168 | char *ret; |
---|
| 169 | |
---|
| 170 | if( len == 0 ) |
---|
| 171 | len = strlen( text ); |
---|
| 172 | |
---|
| 173 | i = 0; |
---|
| 174 | while( ( i + hlen ) < len ) |
---|
| 175 | { |
---|
| 176 | /* Maybe this is a bit over-commented, but I just hate this part... */ |
---|
| 177 | if( g_strncasecmp( text + i, header, hlen ) == 0 ) |
---|
| 178 | { |
---|
| 179 | /* Skip to the (probable) end of the header */ |
---|
| 180 | i += hlen; |
---|
| 181 | |
---|
| 182 | /* Find the first non-[: \t] character */ |
---|
| 183 | while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++; |
---|
| 184 | |
---|
| 185 | /* Make sure we're still inside the string */ |
---|
| 186 | if( i >= len ) return( NULL ); |
---|
| 187 | |
---|
| 188 | /* Save the position */ |
---|
| 189 | ret = text + i; |
---|
| 190 | |
---|
| 191 | /* Search for the end of this line */ |
---|
| 192 | while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++; |
---|
| 193 | |
---|
| 194 | /* Make sure we're still inside the string */ |
---|
| 195 | if( i >= len ) return( NULL ); |
---|
| 196 | |
---|
| 197 | /* Copy the found data */ |
---|
| 198 | return( g_strndup( ret, text + i - ret ) ); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /* This wasn't the header we were looking for, skip to the next line. */ |
---|
| 202 | while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++; |
---|
| 203 | while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++; |
---|
| 204 | |
---|
| 205 | /* End of headers? */ |
---|
| 206 | if( strncmp( text + i - 2, "\n\n", 2 ) == 0 || |
---|
| 207 | strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 || |
---|
| 208 | strncmp( text + i - 2, "\r\r", 2 ) == 0 ) |
---|
| 209 | { |
---|
| 210 | break; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | return( NULL ); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | /* *NOT* thread-safe, but that's not a problem for now... */ |
---|
| 218 | char **msn_linesplit( char *line ) |
---|
| 219 | { |
---|
| 220 | static char **ret = NULL; |
---|
| 221 | static int size = 3; |
---|
| 222 | int i, n = 0; |
---|
| 223 | |
---|
| 224 | if( ret == NULL ) |
---|
| 225 | ret = g_new0( char*, size ); |
---|
| 226 | |
---|
| 227 | for( i = 0; line[i] && line[i] == ' '; i ++ ); |
---|
| 228 | if( line[i] ) |
---|
| 229 | { |
---|
| 230 | ret[n++] = line + i; |
---|
| 231 | for( i ++; line[i]; i ++ ) |
---|
| 232 | { |
---|
| 233 | if( line[i] == ' ' ) |
---|
| 234 | line[i] = 0; |
---|
| 235 | else if( line[i] != ' ' && !line[i-1] ) |
---|
| 236 | ret[n++] = line + i; |
---|
| 237 | |
---|
| 238 | if( n >= size ) |
---|
| 239 | ret = g_renew( char*, ret, size += 2 ); |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | ret[n] = NULL; |
---|
| 243 | |
---|
| 244 | return( ret ); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /* This one handles input from a MSN Messenger server. Both the NS and SB servers usually give |
---|
| 248 | commands, but sometimes they give additional data (payload). This function tries to handle |
---|
| 249 | this all in a nice way and send all data to the right places. */ |
---|
| 250 | |
---|
| 251 | /* Return values: -1: Read error, abort connection. |
---|
| 252 | 0: Command reported error; Abort *immediately*. (The connection does not exist anymore) |
---|
| 253 | 1: OK */ |
---|
| 254 | |
---|
| 255 | int msn_handler( struct msn_handler_data *h ) |
---|
| 256 | { |
---|
| 257 | int st; |
---|
| 258 | |
---|
| 259 | h->rxq = g_renew( char, h->rxq, h->rxlen + 1024 ); |
---|
| 260 | st = read( h->fd, h->rxq + h->rxlen, 1024 ); |
---|
| 261 | h->rxlen += st; |
---|
| 262 | |
---|
| 263 | if( st <= 0 ) |
---|
| 264 | return( -1 ); |
---|
| 265 | |
---|
| 266 | while( st ) |
---|
| 267 | { |
---|
| 268 | int i; |
---|
| 269 | |
---|
| 270 | if( h->msglen == 0 ) |
---|
| 271 | { |
---|
| 272 | for( i = 0; i < h->rxlen; i ++ ) |
---|
| 273 | { |
---|
| 274 | if( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) |
---|
| 275 | { |
---|
| 276 | char *cmd_text, **cmd; |
---|
| 277 | int count; |
---|
| 278 | |
---|
| 279 | cmd_text = g_strndup( h->rxq, i ); |
---|
| 280 | cmd = msn_linesplit( cmd_text ); |
---|
| 281 | for( count = 0; cmd[count]; count ++ ); |
---|
| 282 | st = h->exec_command( h->data, cmd, count ); |
---|
| 283 | g_free( cmd_text ); |
---|
| 284 | |
---|
| 285 | /* If the connection broke, don't continue. We don't even exist anymore. */ |
---|
| 286 | if( !st ) |
---|
| 287 | return( 0 ); |
---|
| 288 | |
---|
| 289 | if( h->msglen ) |
---|
| 290 | h->cmd_text = g_strndup( h->rxq, i ); |
---|
| 291 | |
---|
| 292 | /* Skip to the next non-emptyline */ |
---|
| 293 | while( i < h->rxlen && ( h->rxq[i] == '\r' || h->rxq[i] == '\n' ) ) i ++; |
---|
| 294 | |
---|
| 295 | break; |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /* If we reached the end of the buffer, there's still an incomplete command there. |
---|
| 300 | Return and wait for more data. */ |
---|
| 301 | if( i == h->rxlen && h->rxq[i-1] != '\r' && h->rxq[i-1] != '\n' ) |
---|
| 302 | break; |
---|
| 303 | } |
---|
| 304 | else |
---|
| 305 | { |
---|
| 306 | char *msg, **cmd; |
---|
| 307 | int count; |
---|
| 308 | |
---|
| 309 | /* Do we have the complete message already? */ |
---|
| 310 | if( h->msglen > h->rxlen ) |
---|
| 311 | break; |
---|
| 312 | |
---|
| 313 | msg = g_strndup( h->rxq, h->msglen ); |
---|
| 314 | cmd = msn_linesplit( h->cmd_text ); |
---|
| 315 | for( count = 0; cmd[count]; count ++ ); |
---|
| 316 | |
---|
| 317 | st = h->exec_message( h->data, msg, h->msglen, cmd, count ); |
---|
| 318 | g_free( msg ); |
---|
| 319 | g_free( h->cmd_text ); |
---|
| 320 | h->cmd_text = NULL; |
---|
| 321 | |
---|
| 322 | if( !st ) |
---|
| 323 | return( 0 ); |
---|
| 324 | |
---|
| 325 | i = h->msglen; |
---|
| 326 | h->msglen = 0; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | /* More data after this block? */ |
---|
| 330 | if( i < h->rxlen ) |
---|
| 331 | { |
---|
| 332 | char *tmp; |
---|
| 333 | |
---|
| 334 | tmp = g_memdup( h->rxq + i, h->rxlen - i ); |
---|
| 335 | g_free( h->rxq ); |
---|
| 336 | h->rxq = tmp; |
---|
| 337 | h->rxlen -= i; |
---|
| 338 | i = 0; |
---|
| 339 | } |
---|
| 340 | else |
---|
| 341 | /* If not, reset the rx queue and get lost. */ |
---|
| 342 | { |
---|
| 343 | g_free( h->rxq ); |
---|
| 344 | h->rxq = g_new0( char, 1 ); |
---|
| 345 | h->rxlen = 0; |
---|
| 346 | return( 1 ); |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | return( 1 ); |
---|
| 351 | } |
---|