[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 | |
---|
[c6ca3ee] | 29 | int msn_chat_id; |
---|
| 30 | GSList *msn_connections; |
---|
| 31 | GSList *msn_switchboards; |
---|
| 32 | |
---|
[e3413cc] | 33 | static char *set_eval_display_name( set_t *set, char *value ); |
---|
[911f2eb] | 34 | |
---|
[0da65d5] | 35 | static void msn_init( account_t *acc ) |
---|
[911f2eb] | 36 | { |
---|
[e3413cc] | 37 | set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc ); |
---|
| 38 | set_add( &acc->set, "local_display_name", "false", set_eval_bool, acc ); |
---|
| 39 | set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); |
---|
[911f2eb] | 40 | } |
---|
| 41 | |
---|
[0a3c243] | 42 | static void msn_login( account_t *acc ) |
---|
[b7d3cc34] | 43 | { |
---|
[84b045d] | 44 | struct im_connection *ic = imcb_new( acc ); |
---|
[b7d3cc34] | 45 | struct msn_data *md = g_new0( struct msn_data, 1 ); |
---|
| 46 | |
---|
[0da65d5] | 47 | ic->proto_data = md; |
---|
[b7d3cc34] | 48 | md->fd = -1; |
---|
| 49 | |
---|
[0a3c243] | 50 | if( strchr( acc->user, '@' ) == NULL ) |
---|
[b7d3cc34] | 51 | { |
---|
[84b045d] | 52 | imcb_error( ic, "Invalid account name" ); |
---|
[c2fb3809] | 53 | imc_logout( ic, FALSE ); |
---|
[b7d3cc34] | 54 | return; |
---|
| 55 | } |
---|
| 56 | |
---|
[84b045d] | 57 | imcb_log( ic, "Connecting" ); |
---|
[2a29eac] | 58 | |
---|
[0da65d5] | 59 | md->fd = proxy_connect( "messenger.hotmail.com", 1863, msn_ns_connected, ic ); |
---|
[b7d3cc34] | 60 | if( md->fd < 0 ) |
---|
| 61 | { |
---|
[84b045d] | 62 | imcb_error( ic, "Could not connect to server" ); |
---|
[c2fb3809] | 63 | imc_logout( ic, TRUE ); |
---|
[2a29eac] | 64 | return; |
---|
[b7d3cc34] | 65 | } |
---|
[2a29eac] | 66 | |
---|
[0da65d5] | 67 | md->ic = ic; |
---|
[2a29eac] | 68 | md->away_state = msn_away_state_list; |
---|
| 69 | |
---|
[0da65d5] | 70 | msn_connections = g_slist_append( msn_connections, ic ); |
---|
[b7d3cc34] | 71 | } |
---|
| 72 | |
---|
[0da65d5] | 73 | static void msn_logout( struct im_connection *ic ) |
---|
[b7d3cc34] | 74 | { |
---|
[0da65d5] | 75 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 76 | GSList *l; |
---|
| 77 | |
---|
[5a5c926] | 78 | if( md ) |
---|
[b7d3cc34] | 79 | { |
---|
[5a5c926] | 80 | if( md->fd >= 0 ) |
---|
| 81 | closesocket( md->fd ); |
---|
[b7d3cc34] | 82 | |
---|
[5a5c926] | 83 | if( md->handler ) |
---|
[b7d3cc34] | 84 | { |
---|
[5a5c926] | 85 | if( md->handler->rxq ) g_free( md->handler->rxq ); |
---|
| 86 | if( md->handler->cmd_text ) g_free( md->handler->cmd_text ); |
---|
| 87 | g_free( md->handler ); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | while( md->switchboards ) |
---|
| 91 | msn_sb_destroy( md->switchboards->data ); |
---|
[1053836] | 92 | |
---|
[46dca11] | 93 | msn_msgq_purge( ic, &md->msgq ); |
---|
[5a5c926] | 94 | |
---|
[bf25fa3] | 95 | while( md->groupcount > 0 ) |
---|
| 96 | g_free( md->grouplist[--md->groupcount] ); |
---|
[59f5c42a] | 97 | g_free( md->grouplist ); |
---|
| 98 | |
---|
[5a5c926] | 99 | g_free( md ); |
---|
[b7d3cc34] | 100 | } |
---|
| 101 | |
---|
[0da65d5] | 102 | for( l = ic->permit; l; l = l->next ) |
---|
[b7d3cc34] | 103 | g_free( l->data ); |
---|
[0da65d5] | 104 | g_slist_free( ic->permit ); |
---|
[b7d3cc34] | 105 | |
---|
[0da65d5] | 106 | for( l = ic->deny; l; l = l->next ) |
---|
[b7d3cc34] | 107 | g_free( l->data ); |
---|
[0da65d5] | 108 | g_slist_free( ic->deny ); |
---|
[b7d3cc34] | 109 | |
---|
[0da65d5] | 110 | msn_connections = g_slist_remove( msn_connections, ic ); |
---|
[b7d3cc34] | 111 | } |
---|
| 112 | |
---|
[f6c963b] | 113 | static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
[b7d3cc34] | 114 | { |
---|
| 115 | struct msn_switchboard *sb; |
---|
| 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 | |
---|
| 125 | /* Create a message. We have to arrange a usable switchboard, and send the message later. */ |
---|
| 126 | m = g_new0( struct msn_message, 1 ); |
---|
| 127 | m->who = g_strdup( who ); |
---|
| 128 | m->text = g_strdup( message ); |
---|
| 129 | |
---|
[a830512] | 130 | return msn_sb_write_msg( ic, m ); |
---|
[b7d3cc34] | 131 | } |
---|
| 132 | |
---|
| 133 | return( 0 ); |
---|
| 134 | } |
---|
| 135 | |
---|
[0da65d5] | 136 | static GList *msn_away_states( struct im_connection *ic ) |
---|
[b7d3cc34] | 137 | { |
---|
[5e202b0] | 138 | static GList *l = NULL; |
---|
[b7d3cc34] | 139 | int i; |
---|
| 140 | |
---|
[5e202b0] | 141 | if( l == NULL ) |
---|
[b051d39] | 142 | for( i = 0; *msn_away_state_list[i].code; i ++ ) |
---|
| 143 | if( *msn_away_state_list[i].name ) |
---|
| 144 | l = g_list_append( l, (void*) msn_away_state_list[i].name ); |
---|
[b7d3cc34] | 145 | |
---|
[5e202b0] | 146 | return l; |
---|
[b7d3cc34] | 147 | } |
---|
| 148 | |
---|
[0da65d5] | 149 | static void msn_set_away( struct im_connection *ic, char *state, char *message ) |
---|
[b7d3cc34] | 150 | { |
---|
| 151 | char buf[1024]; |
---|
[0da65d5] | 152 | struct msn_data *md = ic->proto_data; |
---|
[b7d3cc34] | 153 | |
---|
[b051d39] | 154 | if( state ) |
---|
| 155 | md->away_state = msn_away_state_by_name( state ) ? : |
---|
| 156 | msn_away_state_list + 1; |
---|
[b7d3cc34] | 157 | else |
---|
[b051d39] | 158 | md->away_state = msn_away_state_list; |
---|
[b7d3cc34] | 159 | |
---|
[b051d39] | 160 | g_snprintf( buf, sizeof( buf ), "CHG %d %s\r\n", ++md->trId, md->away_state->code ); |
---|
[0da65d5] | 161 | msn_write( ic, buf, strlen( buf ) ); |
---|
[b7d3cc34] | 162 | } |
---|
| 163 | |
---|
[0da65d5] | 164 | static void msn_set_my_name( struct im_connection *ic, char *info ) |
---|
[b7d3cc34] | 165 | { |
---|
[e3413cc] | 166 | msn_set_display_name( ic, info ); |
---|
[b7d3cc34] | 167 | } |
---|
| 168 | |
---|
[0da65d5] | 169 | static void msn_get_info(struct im_connection *ic, char *who) |
---|
[b7d3cc34] | 170 | { |
---|
| 171 | /* Just make an URL and let the user fetch the info */ |
---|
[84b045d] | 172 | imcb_log( ic, "%s\n%s: %s%s", _("User Info"), _("For now, fetch yourself"), PROFILE_URL, who ); |
---|
[b7d3cc34] | 173 | } |
---|
| 174 | |
---|
[0da65d5] | 175 | static void msn_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[b7d3cc34] | 176 | { |
---|
[0da65d5] | 177 | msn_buddy_list_add( ic, "FL", who, who ); |
---|
[b7d3cc34] | 178 | } |
---|
| 179 | |
---|
[0da65d5] | 180 | static void msn_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
[b7d3cc34] | 181 | { |
---|
[0da65d5] | 182 | msn_buddy_list_remove( ic, "FL", who ); |
---|
[b7d3cc34] | 183 | } |
---|
| 184 | |
---|
[f6c963b] | 185 | static void msn_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
[b7d3cc34] | 186 | { |
---|
[fa29d093] | 187 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
[b7d3cc34] | 188 | |
---|
| 189 | if( sb ) |
---|
[0da65d5] | 190 | msn_sb_sendmessage( sb, message ); |
---|
| 191 | /* FIXME: Error handling (although this can't happen unless something's |
---|
| 192 | already severely broken) disappeared here! */ |
---|
[b7d3cc34] | 193 | } |
---|
| 194 | |
---|
[c058ff9] | 195 | static void msn_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
[b7d3cc34] | 196 | { |
---|
[fa29d093] | 197 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
[b7d3cc34] | 198 | char buf[1024]; |
---|
| 199 | |
---|
| 200 | if( sb ) |
---|
| 201 | { |
---|
| 202 | g_snprintf( buf, sizeof( buf ), "CAL %d %s\r\n", ++sb->trId, who ); |
---|
| 203 | msn_sb_write( sb, buf, strlen( buf ) ); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
[0da65d5] | 207 | static void msn_chat_leave( struct groupchat *c ) |
---|
[b7d3cc34] | 208 | { |
---|
[fa29d093] | 209 | struct msn_switchboard *sb = msn_sb_by_chat( c ); |
---|
[b7d3cc34] | 210 | |
---|
| 211 | if( sb ) |
---|
| 212 | msn_sb_write( sb, "OUT\r\n", 5 ); |
---|
| 213 | } |
---|
| 214 | |
---|
[0da65d5] | 215 | static struct groupchat *msn_chat_with( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 216 | { |
---|
| 217 | struct msn_switchboard *sb; |
---|
| 218 | |
---|
[0da65d5] | 219 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
[b7d3cc34] | 220 | { |
---|
| 221 | debug( "Converting existing switchboard to %s to a groupchat", who ); |
---|
[fa29d093] | 222 | return msn_sb_to_chat( sb ); |
---|
[b7d3cc34] | 223 | } |
---|
| 224 | else |
---|
| 225 | { |
---|
| 226 | struct msn_message *m; |
---|
| 227 | |
---|
| 228 | /* Create a magic message. This is quite hackish, but who cares? :-P */ |
---|
| 229 | m = g_new0( struct msn_message, 1 ); |
---|
| 230 | m->who = g_strdup( who ); |
---|
| 231 | m->text = g_strdup( GROUPCHAT_SWITCHBOARD_MESSAGE ); |
---|
| 232 | |
---|
[a830512] | 233 | msn_sb_write_msg( ic, m ); |
---|
| 234 | |
---|
[fa29d093] | 235 | return NULL; |
---|
[b7d3cc34] | 236 | } |
---|
| 237 | |
---|
[fa29d093] | 238 | return NULL; |
---|
[b7d3cc34] | 239 | } |
---|
| 240 | |
---|
[0da65d5] | 241 | static void msn_keepalive( struct im_connection *ic ) |
---|
[b7d3cc34] | 242 | { |
---|
[0da65d5] | 243 | msn_write( ic, "PNG\r\n", strlen( "PNG\r\n" ) ); |
---|
[b7d3cc34] | 244 | } |
---|
| 245 | |
---|
[0da65d5] | 246 | static void msn_add_permit( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 247 | { |
---|
[0da65d5] | 248 | msn_buddy_list_add( ic, "AL", who, who ); |
---|
[b7d3cc34] | 249 | } |
---|
| 250 | |
---|
[0da65d5] | 251 | static void msn_rem_permit( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 252 | { |
---|
[0da65d5] | 253 | msn_buddy_list_remove( ic, "AL", who ); |
---|
[b7d3cc34] | 254 | } |
---|
| 255 | |
---|
[0da65d5] | 256 | static void msn_add_deny( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 257 | { |
---|
| 258 | struct msn_switchboard *sb; |
---|
| 259 | |
---|
[0da65d5] | 260 | msn_buddy_list_add( ic, "BL", who, who ); |
---|
[b7d3cc34] | 261 | |
---|
| 262 | /* If there's still a conversation with this person, close it. */ |
---|
[0da65d5] | 263 | if( ( sb = msn_sb_by_handle( ic, who ) ) ) |
---|
[b7d3cc34] | 264 | { |
---|
| 265 | msn_sb_destroy( sb ); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[0da65d5] | 269 | static void msn_rem_deny( struct im_connection *ic, char *who ) |
---|
[b7d3cc34] | 270 | { |
---|
[0da65d5] | 271 | msn_buddy_list_remove( ic, "BL", who ); |
---|
[b7d3cc34] | 272 | } |
---|
| 273 | |
---|
[0da65d5] | 274 | static int msn_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
[b7d3cc34] | 275 | { |
---|
[df1fb67] | 276 | if( typing & OPT_TYPING ) |
---|
[f6c963b] | 277 | return( msn_buddy_msg( ic, who, TYPING_NOTIFICATION_MESSAGE, 0 ) ); |
---|
[b7d3cc34] | 278 | else |
---|
| 279 | return( 1 ); |
---|
| 280 | } |
---|
| 281 | |
---|
[e3413cc] | 282 | static char *set_eval_display_name( set_t *set, char *value ) |
---|
[911f2eb] | 283 | { |
---|
| 284 | account_t *acc = set->data; |
---|
[0da65d5] | 285 | struct im_connection *ic = acc->ic; |
---|
[911f2eb] | 286 | |
---|
[e3413cc] | 287 | /* Allow any name if we're offline. */ |
---|
[0da65d5] | 288 | if( ic == NULL ) |
---|
[e3413cc] | 289 | return value; |
---|
[911f2eb] | 290 | |
---|
| 291 | if( strlen( value ) > 129 ) |
---|
| 292 | { |
---|
[84b045d] | 293 | imcb_log( ic, "Maximum name length exceeded" ); |
---|
[911f2eb] | 294 | return NULL; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /* Returning NULL would be better, because the server still has to |
---|
| 298 | confirm the name change. However, it looks a bit confusing to the |
---|
| 299 | user. */ |
---|
[e3413cc] | 300 | return msn_set_display_name( ic, value ) ? value : NULL; |
---|
[911f2eb] | 301 | } |
---|
| 302 | |
---|
[0da65d5] | 303 | void msn_initmodule() |
---|
[b7d3cc34] | 304 | { |
---|
[7b23afd] | 305 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
[911f2eb] | 306 | |
---|
[7b23afd] | 307 | ret->name = "msn"; |
---|
[b7d3cc34] | 308 | ret->login = msn_login; |
---|
[0da65d5] | 309 | ret->init = msn_init; |
---|
| 310 | ret->logout = msn_logout; |
---|
[f6c963b] | 311 | ret->buddy_msg = msn_buddy_msg; |
---|
[b7d3cc34] | 312 | ret->away_states = msn_away_states; |
---|
| 313 | ret->set_away = msn_set_away; |
---|
| 314 | ret->get_info = msn_get_info; |
---|
[0da65d5] | 315 | ret->set_my_name = msn_set_my_name; |
---|
[b7d3cc34] | 316 | ret->add_buddy = msn_add_buddy; |
---|
| 317 | ret->remove_buddy = msn_remove_buddy; |
---|
[f6c963b] | 318 | ret->chat_msg = msn_chat_msg; |
---|
[b7d3cc34] | 319 | ret->chat_invite = msn_chat_invite; |
---|
| 320 | ret->chat_leave = msn_chat_leave; |
---|
[0da65d5] | 321 | ret->chat_with = msn_chat_with; |
---|
[b7d3cc34] | 322 | ret->keepalive = msn_keepalive; |
---|
| 323 | ret->add_permit = msn_add_permit; |
---|
| 324 | ret->rem_permit = msn_rem_permit; |
---|
| 325 | ret->add_deny = msn_add_deny; |
---|
| 326 | ret->rem_deny = msn_rem_deny; |
---|
| 327 | ret->send_typing = msn_send_typing; |
---|
[5b52a48] | 328 | ret->handle_cmp = g_strcasecmp; |
---|
[b7d3cc34] | 329 | |
---|
[7b23afd] | 330 | register_protocol(ret); |
---|
[b7d3cc34] | 331 | } |
---|