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