[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 - Main file; functions to be called from BitlBee */ |
---|
| 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 | |
---|
[911f2eb] | 29 | static char *msn_set_display_name( set_t *set, char *value ); |
---|
| 30 | |
---|
[0da65d5] | 31 | static void msn_init( account_t *acc ) |
---|
[911f2eb] | 32 | { |
---|
| 33 | set_t *s; |
---|
| 34 | |
---|
| 35 | s = set_add( &acc->set, "display_name", NULL, msn_set_display_name, acc ); |
---|
| 36 | s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY; |
---|
[1febf5c] | 37 | |
---|
| 38 | s = set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); |
---|
[911f2eb] | 39 | } |
---|
| 40 | |
---|
[0a3c243] | 41 | static void msn_login( account_t *acc ) |
---|
[b7d3cc34] | 42 | { |
---|
[84b045d] | 43 | struct im_connection *ic = imcb_new( acc ); |
---|
[b7d3cc34] | 44 | struct msn_data *md = g_new0( struct msn_data, 1 ); |
---|
| 45 | |
---|
[0da65d5] | 46 | ic->proto_data = md; |
---|
[b7d3cc34] | 47 | md->fd = -1; |
---|
| 48 | |
---|
[0a3c243] | 49 | if( strchr( acc->user, '@' ) == NULL ) |
---|
[b7d3cc34] | 50 | { |
---|
[84b045d] | 51 | imcb_error( ic, "Invalid account name" ); |
---|
[c2fb3809] | 52 | imc_logout( ic, FALSE ); |
---|
[b7d3cc34] | 53 | return; |
---|
| 54 | } |
---|
| 55 | |
---|
[84b045d] | 56 | imcb_log( ic, "Connecting" ); |
---|
[2a29eac] | 57 | |
---|
[0da65d5] | 58 | md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic ); |
---|
[b7d3cc34] | 59 | if( md->fd < 0 ) |
---|
| 60 | { |
---|
[84b045d] | 61 | imcb_error( ic, "Could not connect to server" ); |
---|
[c2fb3809] | 62 | imc_logout( ic, TRUE ); |
---|
[2a29eac] | 63 | return; |
---|
[b7d3cc34] | 64 | } |
---|
[2a29eac] | 65 | |
---|
[0da65d5] | 66 | md->ic = ic; |
---|
[2a29eac] | 67 | md->away_state = msn_away_state_list; |
---|
| 68 | |
---|
[0da65d5] | 69 | msn_connections = g_slist_append( msn_connections, ic ); |
---|
[b7d3cc34] | 70 | } |
---|
| 71 | |
---|
[0da65d5] | 72 | static void msn_logout( struct im_connection *ic ) |
---|
[b7d3cc34] | 73 | { |
---|
[0da65d5] | 74 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 75 | GSList *l; |
---|
| 76 | |
---|
[5a5c926] | 77 | if( md ) |
---|
[b7d3cc34] | 78 | { |
---|
[5a5c926] | 79 | if( md->fd >= 0 ) |
---|
| 80 | closesocket( md->fd ); |
---|
[b7d3cc34] | 81 | |
---|
[5a5c926] | 82 | if( md->handler ) |
---|
[b7d3cc34] | 83 | { |
---|
[5a5c926] | 84 | if( md->handler->rxq ) g_free( md->handler->rxq ); |
---|
| 85 | if( md->handler->cmd_text ) g_free( md->handler->cmd_text ); |
---|
| 86 | g_free( md->handler ); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | while( md->switchboards ) |
---|
| 90 | msn_sb_destroy( md->switchboards->data ); |
---|
[1053836] | 91 | |
---|
[46dca11] | 92 | msn_msgq_purge( ic, &md->msgq ); |
---|
[5a5c926] | 93 | |
---|
[bf25fa3] | 94 | while( md->groupcount > 0 ) |
---|
| 95 | g_free( md->grouplist[--md->groupcount] ); |
---|
[59f5c42a] | 96 | g_free( md->grouplist ); |
---|
| 97 | |
---|
[5a5c926] | 98 | g_free( md ); |
---|
[b7d3cc34] | 99 | } |
---|
| 100 | |
---|
[0da65d5] | 101 | for( l = ic->permit; l; l = l->next ) |
---|
[b7d3cc34] | 102 | g_free( l->data ); |
---|
[0da65d5] | 103 | g_slist_free( ic->permit ); |
---|
[b7d3cc34] | 104 | |
---|
[0da65d5] | 105 | for( l = ic->deny; l; l = l->next ) |
---|
[b7d3cc34] | 106 | g_free( l->data ); |
---|
[0da65d5] | 107 | g_slist_free( ic->deny ); |
---|
[b7d3cc34] | 108 | |
---|
[0da65d5] | 109 | msn_connections = g_slist_remove( msn_connections, ic ); |
---|
[b7d3cc34] | 110 | } |
---|
| 111 | |
---|
[f6c963b] | 112 | static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
[b7d3cc34] | 113 | { |
---|
| 114 | struct msn_switchboard *sb; |
---|
[0da65d5] | 115 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 116 | |
---|
[0da65d5] | 117 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
[b7d3cc34] | 118 | { |
---|
| 119 | return( msn_sb_sendmessage( sb, message ) ); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | struct msn_message *m; |
---|
| 124 | char buf[1024]; |
---|
| 125 | |
---|
| 126 | /* Create a message. We have to arrange a usable switchboard, and send the message later. */ |
---|
| 127 | m = g_new0( struct msn_message, 1 ); |
---|
| 128 | m->who = g_strdup( who ); |
---|
| 129 | m->text = g_strdup( message ); |
---|
| 130 | |
---|
| 131 | /* FIXME: *CHECK* the reliability of using spare sb's! */ |
---|
[0da65d5] | 132 | if( ( sb = msn_sb_spare( ic ) ) ) |
---|
[b7d3cc34] | 133 | { |
---|
| 134 | debug( "Trying to use a spare switchboard to message %s", who ); |
---|
| 135 | |
---|
| 136 | sb->who = g_strdup( who ); |
---|
| 137 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 138 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
| 139 | { |
---|
| 140 | /* He/She should join the switchboard soon, let's queue the message. */ |
---|
| 141 | sb->msgq = g_slist_append( sb->msgq, m ); |
---|
| 142 | return( 1 ); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | debug( "Creating a new switchboard to message %s", who ); |
---|
| 147 | |
---|
| 148 | /* If we reach this line, there was no spare switchboard, so let's make one. */ |
---|
| 149 | g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); |
---|
[0da65d5] | 150 | if( !msn_write( ic, buf, strlen( buf ) ) ) |
---|
[b7d3cc34] | 151 | { |
---|
| 152 | g_free( m->who ); |
---|
| 153 | g_free( m->text ); |
---|
| 154 | g_free( m ); |
---|
| 155 | |
---|
| 156 | return( 0 ); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /* And queue the message to md. We'll pick it up when the switchboard comes up. */ |
---|
| 160 | md->msgq = g_slist_append( md->msgq, m ); |
---|
| 161 | |
---|
| 162 | /* FIXME: If the switchboard creation fails, the message will not be sent. */ |
---|
| 163 | |
---|
| 164 | return( 1 ); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | return( 0 ); |
---|
| 168 | } |
---|
| 169 | |
---|
[0da65d5] | 170 | static GList *msn_away_states( struct im_connection *ic ) |
---|
[b7d3cc34] | 171 | { |
---|
[5e202b0] | 172 | static GList *l = NULL; |
---|
[b7d3cc34] | 173 | int i; |
---|
| 174 | |
---|
[5e202b0] | 175 | if( l == NULL ) |
---|
| 176 | for( i = 0; msn_away_state_list[i].number > -1; i ++ ) |
---|
| 177 | l = g_list_append( l, (void*) msn_away_state_list[i].name ); |
---|
[b7d3cc34] | 178 | |
---|
[5e202b0] | 179 | return l; |
---|
[b7d3cc34] | 180 | } |
---|
| 181 | |
---|
[0da65d5] | 182 | static void msn_set_away( struct im_connection *ic, char *state, char *message ) |
---|
[b7d3cc34] | 183 | { |
---|
| 184 | char buf[1024]; |
---|
[0da65d5] | 185 | struct msn_data *md = ic->proto_data; |
---|
[08995b0] | 186 | const struct msn_away_state *st; |
---|
[b7d3cc34] | 187 | |
---|
| 188 | if( strcmp( state, GAIM_AWAY_CUSTOM ) == 0 ) |
---|
| 189 | st = msn_away_state_by_name( "Away" ); |
---|
| 190 | else |
---|
| 191 | st = msn_away_state_by_name( state ); |
---|
| 192 | |
---|
| 193 | if( !st ) st = msn_away_state_list; |
---|
| 194 | md->away_state = st; |
---|
| 195 | |
---|
| 196 | g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, st->code ); |
---|
[0da65d5] | 197 | msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 198 | } |
---|
| 199 | |
---|
[0da65d5] | 200 | static void msn_set_my_name( struct im_connection *ic, char *info ) |
---|
[b7d3cc34] | 201 | { |
---|
[0da65d5] | 202 | msn_set_display_name( set_find( &ic->acc->set, "display_name" ), info ); |
---|
[b7d3cc34] | 203 | } |
---|
| 204 | |
---|
[0da65d5] | 205 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 206 | { |
---|
| 207 | /* Just make an URL and let the user fetch the info */ |
---|
[84b045d] | 208 | imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who ); |
---|
[b7d3cc34] | 209 | } |
---|
| 210 | |
---|
[0da65d5] | 211 | static void msn_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[b7d3cc34] | 212 | { |
---|
[0da65d5] | 213 | msn_buddy_list_add( ic, "FL", who, who ); |
---|
[b7d3cc34] | 214 | } |
---|
| 215 | |
---|
[0da65d5] | 216 | static void msn_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[b7d3cc34] | 217 | { |
---|
[0da65d5] | 218 | msn_buddy_list_remove( ic, "FL", who ); |
---|
[b7d3cc34] | 219 | } |
---|
| 220 | |
---|
[f6c963b] | 221 | static void msn_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
[b7d3cc34] | 222 | { |
---|
[fa29d093] | 223 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
[b7d3cc34] | 224 | |
---|
| 225 | if( sb ) |
---|
[0da65d5] | 226 | msn_sb_sendmessage( sb, message ); |
---|
| 227 | /* FIXME: Error handling (although this can't happen unless something's |
---|
| 228 | already severely broken) disappeared here! */ |
---|
[b7d3cc34] | 229 | } |
---|
| 230 | |
---|
[c058ff9] | 231 | static void msn_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
[b7d3cc34] | 232 | { |
---|
[fa29d093] | 233 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
[b7d3cc34] | 234 | char buf[1024]; |
---|
| 235 | |
---|
| 236 | if( sb ) |
---|
| 237 | { |
---|
| 238 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 239 | msn_sb_write( sb, buf, strlen( buf ) ); |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | |
---|
[0da65d5] | 243 | static void msn_chat_leave( struct groupchat *c ) |
---|
[b7d3cc34] | 244 | { |
---|
[fa29d093] | 245 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
[b7d3cc34] | 246 | |
---|
| 247 | if( sb ) |
---|
| 248 | msn_sb_write( sb, "OUT\r\n", 5 ); |
---|
| 249 | } |
---|
| 250 | |
---|
[0da65d5] | 251 | static struct groupchat *msn_chat_with( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 252 | { |
---|
| 253 | struct msn_switchboard *sb; |
---|
[0da65d5] | 254 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 255 | char buf[1024]; |
---|
| 256 | |
---|
[0da65d5] | 257 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
[b7d3cc34] | 258 | { |
---|
| 259 | debug( "Converting existing switchboard to %s to a groupchat", who ); |
---|
[fa29d093] | 260 | return msn_sb_to_chat( sb ); |
---|
[b7d3cc34] | 261 | } |
---|
| 262 | else |
---|
| 263 | { |
---|
| 264 | struct msn_message *m; |
---|
| 265 | |
---|
[0da65d5] | 266 | if( ( sb = msn_sb_spare( ic ) ) ) |
---|
[b7d3cc34] | 267 | { |
---|
| 268 | debug( "Trying to reuse an existing switchboard as a groupchat with %s", who ); |
---|
| 269 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 270 | if( msn_sb_write( sb, buf, strlen( buf ) ) ) |
---|
[fa29d093] | 271 | return msn_sb_to_chat( sb ); |
---|
[b7d3cc34] | 272 | } |
---|
| 273 | |
---|
| 274 | /* If the stuff above failed for some reason: */ |
---|
| 275 | debug( "Creating a new switchboard to groupchat with %s", who ); |
---|
| 276 | |
---|
| 277 | /* Request a new switchboard. */ |
---|
| 278 | g_snprintf( buf, sizeof( buf ), "XFR %d SB\r\n", ++md->trId ); |
---|
[0da65d5] | 279 | if( !msn_write( ic, buf, strlen( buf ) ) ) |
---|
[b7d3cc34] | 280 | return( 0 ); |
---|
| 281 | |
---|
| 282 | /* Create a magic message. This is quite hackish, but who cares? :-P */ |
---|
| 283 | m = g_new0( struct msn_message, 1 ); |
---|
| 284 | m->who = g_strdup( who ); |
---|
| 285 | m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE ); |
---|
| 286 | |
---|
| 287 | /* Queue the magic message and cross your fingers. */ |
---|
| 288 | md->msgq = g_slist_append( md->msgq, m ); |
---|
| 289 | |
---|
[fa29d093] | 290 | /* FIXME: Can I try to return something here already? */ |
---|
| 291 | return NULL; |
---|
[b7d3cc34] | 292 | } |
---|
| 293 | |
---|
[fa29d093] | 294 | return NULL; |
---|
[b7d3cc34] | 295 | } |
---|
| 296 | |
---|
[0da65d5] | 297 | static void msn_keepalive( struct im_connection *ic ) |
---|
[b7d3cc34] | 298 | { |
---|
[0da65d5] | 299 | msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) ); |
---|
[b7d3cc34] | 300 | } |
---|
| 301 | |
---|
[0da65d5] | 302 | static void msn_add_permit( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 303 | { |
---|
[0da65d5] | 304 | msn_buddy_list_add( ic, "AL", who, who ); |
---|
[b7d3cc34] | 305 | } |
---|
| 306 | |
---|
[0da65d5] | 307 | static void msn_rem_permit( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 308 | { |
---|
[0da65d5] | 309 | msn_buddy_list_remove( ic, "AL", who ); |
---|
[b7d3cc34] | 310 | } |
---|
| 311 | |
---|
[0da65d5] | 312 | static void msn_add_deny( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 313 | { |
---|
| 314 | struct msn_switchboard *sb; |
---|
| 315 | |
---|
[0da65d5] | 316 | msn_buddy_list_add( ic, "BL", who, who ); |
---|
[b7d3cc34] | 317 | |
---|
| 318 | /* If there's still a conversation with this person, close it. */ |
---|
[0da65d5] | 319 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
[b7d3cc34] | 320 | { |
---|
| 321 | msn_sb_destroy( sb ); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
[0da65d5] | 325 | static void msn_rem_deny( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 326 | { |
---|
[0da65d5] | 327 | msn_buddy_list_remove( ic, "BL", who ); |
---|
[b7d3cc34] | 328 | } |
---|
| 329 | |
---|
[0da65d5] | 330 | static int msn_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
[b7d3cc34] | 331 | { |
---|
[df1fb67] | 332 | if( typing & OPT_TYPING ) |
---|
[f6c963b] | 333 | return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) ); |
---|
[b7d3cc34] | 334 | else |
---|
| 335 | return( 1 ); |
---|
| 336 | } |
---|
| 337 | |
---|
[911f2eb] | 338 | static char *msn_set_display_name( set_t *set, char *value ) |
---|
| 339 | { |
---|
| 340 | account_t *acc = set->data; |
---|
[0da65d5] | 341 | struct im_connection *ic = acc->ic; |
---|
[911f2eb] | 342 | struct msn_data *md; |
---|
[e97827b] | 343 | char buf[1024], *fn; |
---|
[911f2eb] | 344 | |
---|
| 345 | /* Double-check. */ |
---|
[0da65d5] | 346 | if( ic == NULL ) |
---|
[911f2eb] | 347 | return NULL; |
---|
| 348 | |
---|
[0da65d5] | 349 | md = ic->proto_data; |
---|
[911f2eb] | 350 | |
---|
| 351 | if( strlen( value ) > 129 ) |
---|
| 352 | { |
---|
[84b045d] | 353 | imcb_log( ic, "Maximum name length exceeded" ); |
---|
[911f2eb] | 354 | return NULL; |
---|
| 355 | } |
---|
| 356 | |
---|
[e97827b] | 357 | fn = msn_http_encode( value ); |
---|
[911f2eb] | 358 | |
---|
[c2fb3809] | 359 | g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, ic->acc->user, fn ); |
---|
[0da65d5] | 360 | msn_write( ic, buf, strlen( buf ) ); |
---|
[911f2eb] | 361 | g_free( fn ); |
---|
| 362 | |
---|
| 363 | /* Returning NULL would be better, because the server still has to |
---|
| 364 | confirm the name change. However, it looks a bit confusing to the |
---|
| 365 | user. */ |
---|
| 366 | return value; |
---|
| 367 | } |
---|
| 368 | |
---|
[0da65d5] | 369 | void msn_initmodule() |
---|
[b7d3cc34] | 370 | { |
---|
[7b23afd] | 371 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[911f2eb] | 372 | |
---|
[7b23afd] | 373 | ret->name = "msn"; |
---|
[5a71d9c] | 374 | ret->mms = 1409; /* this guess taken from libotr UPGRADING file */ |
---|
[b7d3cc34] | 375 | ret->login = msn_login; |
---|
[0da65d5] | 376 | ret->init = msn_init; |
---|
| 377 | ret->logout = msn_logout; |
---|
[f6c963b] | 378 | ret->buddy_msg = msn_buddy_msg; |
---|
[b7d3cc34] | 379 | ret->away_states = msn_away_states; |
---|
| 380 | ret->set_away = msn_set_away; |
---|
| 381 | ret->get_info = msn_get_info; |
---|
[0da65d5] | 382 | ret->set_my_name = msn_set_my_name; |
---|
[b7d3cc34] | 383 | ret->add_buddy = msn_add_buddy; |
---|
| 384 | ret->remove_buddy = msn_remove_buddy; |
---|
[f6c963b] | 385 | ret->chat_msg = msn_chat_msg; |
---|
[b7d3cc34] | 386 | ret->chat_invite = msn_chat_invite; |
---|
| 387 | ret->chat_leave = msn_chat_leave; |
---|
[0da65d5] | 388 | ret->chat_with = msn_chat_with; |
---|
[b7d3cc34] | 389 | ret->keepalive = msn_keepalive; |
---|
| 390 | ret->add_permit = msn_add_permit; |
---|
| 391 | ret->rem_permit = msn_rem_permit; |
---|
| 392 | ret->add_deny = msn_add_deny; |
---|
| 393 | ret->rem_deny = msn_rem_deny; |
---|
| 394 | ret->send_typing = msn_send_typing; |
---|
[5b52a48] | 395 | ret->handle_cmp = g_strcasecmp; |
---|
[b7d3cc34] | 396 | |
---|
[7b23afd] | 397 | register_protocol(ret); |
---|
[b7d3cc34] | 398 | } |
---|