Mercurial > pidgin.yaz
annotate src/protocols/yahoo/rxhandlers.c @ 2388:5a7234d5e052
[gaim-migrate @ 2401]
default
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Fri, 28 Sep 2001 23:54:36 +0000 |
parents | dfaf5d9d433e |
children | 9d537e5012c9 |
rev | line source |
---|---|
2086 | 1 /* |
2 * libyay | |
3 * | |
4 * Copyright (C) 2001 Eric Warmenhoven <warmenhoven@yahoo.com> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 #include "internal.h" | |
23 #include <string.h> | |
24 #include <stdlib.h> | |
25 #include <ctype.h> | |
26 | |
27 static void yahoo_parse_config(struct yahoo_session *session, struct yahoo_conn *conn, char *buf) | |
28 { | |
29 char **str_array = g_strsplit(buf, "\n", 1024); | |
30 char **it; | |
31 int state = 0; | |
32 | |
33 for (it = str_array; *it; it++) { | |
34 if (!**it) continue; | |
35 if (!strncmp(*it, "ERROR", strlen("ERROR"))) { | |
36 yahoo_close(session, conn); | |
37 if (session->callbacks[YAHOO_HANDLE_BADPASSWORD].function) | |
38 (*session->callbacks[YAHOO_HANDLE_BADPASSWORD].function)(session); | |
39 return; | |
2135
1779daad8cc3
[gaim-migrate @ 2145]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
40 } else if (!strncmp(*it, "Set-Cookie: Y=", strlen("Set-Cookie: Y="))) { |
2086 | 41 char **sa; |
42 char **m; | |
43 | |
44 char *end = strchr(*it, ';'); | |
45 if (session->cookie) | |
46 g_free(session->cookie); | |
47 session->cookie = g_strndup(*it + strlen("Set-Cookie: "), | |
48 end - *it - strlen("Set-Cookie: ")); | |
49 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, session->cookie); | |
50 if (!session->cookie) { | |
51 yahoo_close(session, conn); | |
52 YAHOO_PRINT(session, YAHOO_LOG_ERROR, "did not get cookie"); | |
53 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
54 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
55 return; | |
56 } | |
57 | |
58 sa = g_strsplit(session->cookie, "&", 8); | |
59 for (m = sa; *m; m++) { | |
60 if (!strncmp(*m, "n=", 2)) { | |
61 if (session->login_cookie) | |
62 g_free(session->login_cookie); | |
63 session->login_cookie = g_strdup(*m + 2); | |
64 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, session->login_cookie); | |
65 } | |
66 } | |
67 g_strfreev(sa); | |
68 } else if (!strncmp(*it, "BEGIN BUDDYLIST", strlen("BEGIN BUDDYLIST"))) { | |
69 state = 1; | |
70 } else if (!strncmp(*it, "END BUDDYLIST", strlen("END BUDDYLIST"))) { | |
71 state = 0; | |
72 } else if (!strncmp(*it, "BEGIN IGNORELIST", strlen("BEGIN IGNORELIST"))) { | |
73 state = 2; | |
74 } else if (!strncmp(*it, "END IGNORELIST", strlen("END IGNORELIST"))) { | |
75 state = 0; | |
76 } else if (!strncmp(*it, "BEGIN IDENTITIES", strlen("BEGIN IDENTITIES"))) { | |
77 state = 3; | |
78 } else if (!strncmp(*it, "END IDENTITIES", strlen("END IDENTITIES"))) { | |
79 state = 0; | |
80 } else if (!strncmp(*it, "Mail=", strlen("Mail="))) { | |
81 session->mail = atoi(*it + strlen("Mail=")); | |
82 } else if (!strncmp(*it, "Login=", strlen("Login="))) { | |
83 if (session->login) | |
84 g_free(session->login); | |
85 session->login = g_strdup(*it + strlen("Login=")); | |
86 } else { | |
87 if (state == 1) { | |
88 struct yahoo_group *grp = g_new0(struct yahoo_group, 1); | |
89 char *end = strchr(*it, ':'); | |
90 if (end) { | |
91 grp->name = g_strndup(*it, end - *it); | |
92 end++; | |
93 grp->buddies = g_strsplit(end, ",", 1024); | |
94 session->groups = g_list_append(session->groups, grp); | |
95 } | |
96 } else if (state == 2) { | |
97 session->ignored = g_list_append(session->ignored, g_strdup(*it)); | |
98 } else if (state == 3) { | |
99 session->identities = g_strsplit(*it, ",", 6); | |
100 } | |
101 } | |
102 } | |
103 | |
104 g_strfreev(str_array); | |
105 yahoo_close(session, conn); | |
106 if (session->callbacks[YAHOO_HANDLE_LOGINCOOKIE].function) | |
107 (*session->callbacks[YAHOO_HANDLE_LOGINCOOKIE].function)(session); | |
108 } | |
109 | |
110 static void yahoo_parse_status(struct yahoo_session *sess, struct yahoo_packet *pkt) | |
111 { | |
112 /* OK, I'm going to comment it this time. We either get: | |
2193
aa9181460af6
[gaim-migrate @ 2203]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2135
diff
changeset
|
113 * warmenhoven(99,(test)\001,6634CD3,0,1,0,0) |
2086 | 114 * or |
2193
aa9181460af6
[gaim-migrate @ 2203]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2135
diff
changeset
|
115 * 2,smarterchild(0,6634CD3,0,1,0,0),warmenhoven(0,6C8C0C48,0,1,0,0) |
2086 | 116 * |
117 * in the first case, we only get one person, and we get a bunch of fields. | |
118 * in the second case, the number is how many people we got, and then the | |
119 * names followed by a bunch of fields, separated by commas. | |
120 * | |
121 * the fields are: (status, [optional: custom message \001,] session id, ?, pager, chat, game) | |
122 * | |
123 * the custom message may contain any characters (none are escaped) so we can't split on | |
124 * anything in particular. this is what we fucked up with the first time | |
125 */ | |
126 char *tmp = pkt->content; | |
127 int count = 0; | |
128 int i; | |
129 | |
130 if (strstr(pkt->content, "was not AWAY")) | |
131 return; | |
132 | |
133 /* count is the first number, if we got it. */ | |
134 while (*tmp && isdigit((int)*tmp)) | |
135 count = count * 10 + *tmp++ - '0'; | |
136 count = count ? count : 1; | |
137 | |
138 for (i = 0; i < count && *tmp; i++) { | |
139 int status, in_pager, in_chat, in_game; | |
140 char *p, *who, *msg = NULL; | |
141 | |
142 if (*tmp == ',') | |
143 tmp++; | |
144 | |
145 /* who is the person we're getting an update for. there will always be a paren | |
146 * after this so we should be OK. if we don't get a paren, we'll just break. */ | |
147 who = tmp; | |
148 if ((tmp = strchr(who, '(')) == NULL) | |
149 break; | |
150 *tmp++ = '\0'; | |
151 | |
152 /* tmp now points to the start of the fields. we'll get the status first. it | |
153 * should always be followed by a comma, and if it isn't, we'll just break. */ | |
154 if ((p = strchr(tmp, ',')) == NULL) | |
155 break; | |
156 *p++ = '\0'; | |
157 status = atoi(tmp); | |
158 tmp = p; | |
159 | |
160 if (status == YAHOO_STATUS_CUSTOM) { | |
161 /* tmp now points to the away message. it should end with "\001,". */ | |
162 msg = tmp; | |
163 if ((tmp = strstr(msg, "\001,")) == NULL) | |
164 break; | |
165 *tmp = '\0'; | |
166 tmp += 2; | |
167 } | |
168 | |
169 /* tmp now points to the session id. we don't need it. */ | |
170 if ((tmp = strchr(tmp, ',')) == NULL) | |
171 break; | |
172 tmp++; | |
173 | |
174 /* tmp is at the unknown value. we don't need it. */ | |
175 if ((tmp = strchr(tmp, ',')) == NULL) | |
176 break; | |
177 tmp++; | |
178 | |
179 /* tmp is at the in_pager value */ | |
180 if ((p = strchr(tmp, ',')) == NULL) | |
181 break; | |
182 *p++ = '\0'; | |
183 in_pager = atoi(tmp); | |
184 tmp = p; | |
185 | |
186 /* tmp is at the in_chat value */ | |
187 if ((p = strchr(tmp, ',')) == NULL) | |
188 break; | |
189 *p++ = '\0'; | |
190 in_chat = atoi(tmp); | |
191 tmp = p; | |
192 | |
193 /* tmp is at the in_game value. this is the last value, so it should end with | |
194 * a parenthesis. */ | |
195 if ((p = strchr(tmp, ')')) == NULL) | |
196 break; | |
197 *p++ = '\0'; | |
198 in_game = atoi(tmp); | |
199 tmp = p; | |
200 | |
201 if (sess->callbacks[YAHOO_HANDLE_STATUS].function) | |
202 (*sess->callbacks[YAHOO_HANDLE_STATUS].function)(sess, who, status, msg, | |
203 in_pager, in_chat, in_game); | |
204 } | |
205 } | |
206 | |
207 static void yahoo_parse_message(struct yahoo_session *sess, struct yahoo_packet *pkt) | |
208 { | |
209 char buf[256]; | |
210 int type = yahoo_makeint(pkt->msgtype); | |
211 char *str_array[4]; | |
212 char *tmp; | |
213 | |
214 switch(type) { | |
215 case YAHOO_MESSAGE_NORMAL: | |
216 str_array[0] = pkt->content; | |
217 if ((tmp = strchr(str_array[0], ',')) == NULL) | |
218 break; | |
219 *tmp++ = '\0'; | |
220 str_array[1] = tmp; | |
221 if ((tmp = strchr(str_array[1], ',')) == NULL) | |
222 break; | |
223 *tmp++ = '\0'; | |
224 str_array[2] = tmp; | |
2378
dfaf5d9d433e
[gaim-migrate @ 2391]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2193
diff
changeset
|
225 str_array[2][strlen(str_array[2]) - 1] = 0; |
2086 | 226 if (sess->callbacks[YAHOO_HANDLE_MESSAGE].function) |
227 (*sess->callbacks[YAHOO_HANDLE_MESSAGE].function)(sess, pkt->nick2, | |
228 str_array[0], | |
229 atol(str_array[1]), | |
230 str_array[2]); | |
231 break; | |
232 case YAHOO_MESSAGE_BOUNCE: | |
233 if (sess->callbacks[YAHOO_HANDLE_BOUNCE].function) | |
234 (*sess->callbacks[YAHOO_HANDLE_BOUNCE].function)(sess); | |
235 break; | |
236 case YAHOO_MESSAGE_OFFLINE: | |
237 tmp = pkt->content; | |
238 if ((tmp = strchr(tmp, ',')) == NULL) | |
239 break; | |
240 ++tmp; | |
241 if ((tmp = strchr(tmp, ',')) == NULL) | |
242 break; | |
243 str_array[0] = ++tmp; | |
244 if ((tmp = strchr(tmp, ',')) == NULL) | |
245 break; | |
246 *tmp++ = '\0'; | |
247 str_array[1] = tmp; | |
248 if ((tmp = strchr(tmp, ',')) == NULL) | |
249 break; | |
250 *tmp++ = '\0'; | |
251 str_array[2] = tmp; | |
252 if ((tmp = strchr(tmp, ',')) == NULL) | |
253 break; | |
254 *tmp++ = '\0'; | |
255 str_array[3] = tmp; | |
256 if (sess->callbacks[YAHOO_HANDLE_MESSAGE].function) | |
257 (*sess->callbacks[YAHOO_HANDLE_MESSAGE].function)(sess, str_array[0], | |
258 str_array[1], | |
259 atol(str_array[2]), | |
260 str_array[3]); | |
261 break; | |
262 default: | |
263 g_snprintf(buf, sizeof(buf), "unhandled message type %d", type); | |
264 YAHOO_PRINT(sess, YAHOO_LOG_WARNING, buf); | |
265 break; | |
266 } | |
267 } | |
268 | |
269 static void yahoo_parse_packet(struct yahoo_session *sess, | |
270 struct yahoo_conn *conn, struct yahoo_packet *pkt) | |
271 { | |
272 char buf[256]; | |
273 int service = yahoo_makeint(pkt->service); | |
274 conn->magic_id = yahoo_makeint(pkt->magic_id); | |
275 g_snprintf(buf, sizeof(buf), "Service %d (msgtype %d): %s", service, | |
276 yahoo_makeint(pkt->msgtype), pkt->content); | |
277 YAHOO_PRINT(sess, YAHOO_LOG_DEBUG, buf); | |
278 switch(service) { | |
279 case YAHOO_SERVICE_LOGON: | |
280 if (yahoo_makeint(pkt->msgtype) == 0) | |
281 if (sess->callbacks[YAHOO_HANDLE_ONLINE].function) | |
282 (*sess->callbacks[YAHOO_HANDLE_ONLINE].function)(sess); | |
283 case YAHOO_SERVICE_LOGOFF: | |
284 case YAHOO_SERVICE_ISAWAY: | |
285 case YAHOO_SERVICE_ISBACK: | |
286 yahoo_parse_status(sess, pkt); | |
287 break; | |
288 case YAHOO_SERVICE_NEWCONTACT: | |
289 if (yahoo_makeint(pkt->msgtype) == 3) { | |
290 char **str_array = g_strsplit(pkt->content, ",,", 2); | |
291 if (sess->callbacks[YAHOO_HANDLE_BUDDYADDED].function) | |
292 (*sess->callbacks[YAHOO_HANDLE_BUDDYADDED].function)(sess, | |
293 pkt->nick2, | |
294 str_array[0], | |
295 str_array[1]); | |
296 g_strfreev(str_array); | |
297 } else | |
298 yahoo_parse_status(sess, pkt); | |
299 break; | |
300 case YAHOO_SERVICE_IDACT: | |
301 if (sess->callbacks[YAHOO_HANDLE_ACTIVATE].function) | |
302 (*sess->callbacks[YAHOO_HANDLE_ACTIVATE].function)(sess); | |
303 break; | |
304 case YAHOO_SERVICE_MESSAGE: | |
305 yahoo_parse_message(sess, pkt); | |
306 break; | |
307 case YAHOO_SERVICE_NEWMAIL: | |
308 if (sess->callbacks[YAHOO_HANDLE_NEWMAIL].function) | |
309 (*sess->callbacks[YAHOO_HANDLE_NEWMAIL].function)(sess, strlen(pkt->content) ? | |
310 atoi(pkt->content) : 0); | |
311 break; | |
312 default: | |
313 g_snprintf(buf, sizeof(buf), "unhandled service type %d", service); | |
314 YAHOO_PRINT(sess, YAHOO_LOG_WARNING, buf); | |
315 break; | |
316 } | |
317 } | |
318 | |
319 void yahoo_socket_handler(struct yahoo_session *session, int socket, int type) | |
320 { | |
321 int pos = 0; | |
322 struct yahoo_conn *conn; | |
323 | |
324 if (!session) | |
325 return; | |
326 | |
327 if (!(conn = yahoo_find_conn(session, socket))) | |
328 return; | |
329 | |
330 if (type == YAHOO_SOCKET_WRITE) { | |
331 int error = ETIMEDOUT, len = sizeof(error); | |
332 | |
333 if (getsockopt(socket, SOL_SOCKET, SO_ERROR, &error, &len) < 0) | |
334 error = errno; | |
335 if (error) { | |
336 yahoo_close(session, conn); | |
337 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "unable to connect"); | |
338 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
339 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
340 return; | |
341 } | |
342 | |
343 fcntl(socket, F_SETFL, 0); | |
344 | |
345 YAHOO_PRINT(session, YAHOO_LOG_NOTICE, "connected"); | |
346 | |
347 conn->connected = TRUE; | |
348 if (yahoo_socket_notify) | |
349 (*yahoo_socket_notify)(session, socket, YAHOO_SOCKET_WRITE, FALSE); | |
350 if (yahoo_socket_notify) | |
351 (*yahoo_socket_notify)(session, socket, YAHOO_SOCKET_READ, TRUE); | |
352 | |
353 if (conn->type == YAHOO_CONN_TYPE_AUTH) { | |
354 if (session->callbacks[YAHOO_HANDLE_AUTHCONNECT].function) | |
355 (*session->callbacks[YAHOO_HANDLE_AUTHCONNECT].function)(session); | |
356 } else if (conn->type == YAHOO_CONN_TYPE_MAIN) { | |
357 if (session->callbacks[YAHOO_HANDLE_MAINCONNECT].function) | |
358 (*session->callbacks[YAHOO_HANDLE_MAINCONNECT].function)(session); | |
359 } else if (conn->type == YAHOO_CONN_TYPE_DUMB) { | |
360 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, "sending to buddy list host"); | |
361 yahoo_write(session, conn, conn->txqueue, strlen(conn->txqueue)); | |
362 g_free(conn->txqueue); | |
363 conn->txqueue = NULL; | |
364 } else if (conn->type == YAHOO_CONN_TYPE_PROXY) { | |
365 char buf[1024]; | |
366 g_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n\r\n", | |
367 session->pager_host, session->pager_port); | |
368 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, buf); | |
369 yahoo_write(session, conn, buf, strlen(buf)); | |
370 } | |
371 | |
372 return; | |
373 } | |
374 | |
375 if (conn->type == YAHOO_CONN_TYPE_AUTH) { | |
376 char *buf = g_malloc0(5000); | |
377 while (read(socket, &buf[pos++], 1) == 1); | |
378 if (pos == 1) { | |
379 g_free(buf); | |
380 yahoo_close(session, conn); | |
381 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "could not read auth response"); | |
382 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
383 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
384 return; | |
385 } | |
386 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, buf); | |
387 yahoo_parse_config(session, conn, buf); | |
388 g_free(buf); | |
389 } else if (conn->type == YAHOO_CONN_TYPE_MAIN) { | |
390 struct yahoo_packet pkt; | |
391 int len; | |
392 | |
393 if ((read(socket, &pkt, 8) != 8) || strcmp(pkt.version, "YHOO1.0")) { | |
394 yahoo_close(session, conn); | |
395 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "invalid version type"); | |
396 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
397 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
398 return; | |
399 } | |
400 | |
401 if (read(socket, &pkt.len, 4) != 4) { | |
402 yahoo_close(session, conn); | |
403 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "could not read length"); | |
404 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
405 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
406 return; | |
407 } | |
408 len = yahoo_makeint(pkt.len); | |
409 | |
410 if (read(socket, &pkt.service, len - 12) != len - 12) { | |
411 yahoo_close(session, conn); | |
412 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "could not read data"); | |
413 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
414 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
415 return; | |
416 } | |
417 yahoo_parse_packet(session, conn, &pkt); | |
418 } else if (conn->type == YAHOO_CONN_TYPE_DUMB) { | |
419 char *buf = g_malloc0(5000); | |
420 while (read(socket, &buf[pos++], 1) == 1); | |
421 if (pos == 1) { | |
422 g_free(buf); | |
423 yahoo_close(session, conn); | |
424 YAHOO_PRINT(session, YAHOO_LOG_WARNING, "error reading from listserv"); | |
425 return; | |
426 } | |
427 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, buf); | |
428 YAHOO_PRINT(session, YAHOO_LOG_NOTICE, "closing buddy list host connnection"); | |
429 yahoo_close(session, conn); | |
430 g_free(buf); | |
431 } else if (conn->type == YAHOO_CONN_TYPE_PROXY) { | |
432 char *buf = g_malloc0(5000); | |
433 int nlc = 0; | |
434 while ((nlc != 2) && (read(socket, &buf[pos++], 1) == 1)) { | |
435 if (buf[pos-1] == '\n') | |
436 nlc++; | |
437 else if (buf[pos-1] != '\r') | |
438 nlc = 0; | |
439 } | |
440 if (pos == 1) { | |
441 g_free(buf); | |
442 yahoo_close(session, conn); | |
443 YAHOO_PRINT(session, YAHOO_LOG_ERROR, "error reading from proxy"); | |
444 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
445 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
446 return; | |
447 } | |
448 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, buf); | |
449 if (!strncasecmp(buf, HTTP_GOODSTRING1, strlen(HTTP_GOODSTRING1)) || | |
450 !strncasecmp(buf, HTTP_GOODSTRING2, strlen(HTTP_GOODSTRING2))) { | |
451 conn->type = YAHOO_CONN_TYPE_MAIN; | |
452 YAHOO_PRINT(session, YAHOO_LOG_NOTICE, "proxy connected successfully"); | |
453 if (session->callbacks[YAHOO_HANDLE_MAINCONNECT].function) | |
454 (*session->callbacks[YAHOO_HANDLE_MAINCONNECT].function)(session); | |
455 } else { | |
456 yahoo_close(session, conn); | |
457 YAHOO_PRINT(session, YAHOO_LOG_ERROR, "proxy could not connect"); | |
458 if (session->callbacks[YAHOO_HANDLE_DISCONNECT].function) | |
459 (*session->callbacks[YAHOO_HANDLE_DISCONNECT].function)(session); | |
460 } | |
461 g_free(buf); | |
462 } | |
463 } | |
464 | |
465 void yahoo_add_handler(struct yahoo_session *session, int type, yahoo_callback function) | |
466 { | |
467 if (!session) | |
468 return; | |
469 | |
470 session->callbacks[type].function = function; | |
471 } |