source: protocols/msn/msn.c @ 1febf5c

Last change on this file since 1febf5c was 1febf5c, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-01-05T21:15:32Z

Added "mail_notifications" setting. Who needs those notifications anyway?
Closes: #338.

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