source: protocols/msn/msn.c @ b051d39

Last change on this file since b051d39 was b051d39, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-06T15:44:46Z

MSN module updated to deal with the new API. Not many changes since it
doesn't currently support away messages anyway. This is more a bit of a
cleanup.

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