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