source: protocols/msn/msn_util.c @ c998255

Last change on this file since c998255 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

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