Mercurial > pidgin
annotate plugins/yay/rxhandlers.c @ 1562:12f16a852540
[gaim-migrate @ 1572]
bounce
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Sat, 10 Mar 2001 23:59:53 +0000 |
parents | 9fe7b059d2a5 |
children | 393b23c8f494 |
rev | line source |
---|---|
1546 | 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 int 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 (!strncmp(*it, "ERROR", strlen("ERROR"))) { | |
35 yahoo_close(session, conn); | |
36 CALLBACK(session, YAHOO_HANDLE_BADPASSWORD); | |
37 return 1; | |
38 } else if (!strncmp(*it, "Set-Cookie: ", strlen("Set-Cookie: "))) { | |
39 char **sa; | |
40 char **m; | |
41 | |
42 char *end = strchr(*it, ';'); | |
43 if (session->cookie) | |
44 g_free(session->cookie); | |
45 session->cookie = g_strndup(*it + strlen("Set-Cookie: "), | |
46 end - *it - strlen("Set-Cookie: ")); | |
47 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, session->cookie); | |
48 if (!session->cookie) { | |
49 yahoo_close(session, conn); | |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
50 YAHOO_PRINT(session, YAHOO_LOG_ERROR, "did not get cookie"); |
1546 | 51 CALLBACK(session, YAHOO_HANDLE_DISCONNECT); |
52 return 1; | |
53 } | |
54 | |
55 sa = g_strsplit(session->cookie, "&", 8); | |
56 for (m = sa; *m; m++) { | |
57 if (!strncmp(*m, "n=", 2)) { | |
58 if (session->login_cookie) | |
59 g_free(session->login_cookie); | |
60 session->login_cookie = g_strdup(*m + 2); | |
61 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, session->login_cookie); | |
62 } | |
63 } | |
64 g_strfreev(sa); | |
65 } else if (!strncmp(*it, "BEGIN BUDDYLIST", strlen("BEGIN BUDDYLIST"))) { | |
66 state = 1; | |
67 } else if (!strncmp(*it, "END BUDDYLIST", strlen("END BUDDYLIST"))) { | |
68 state = 0; | |
69 } else if (!strncmp(*it, "BEGIN IGNORELIST", strlen("BEGIN IGNORELIST"))) { | |
70 state = 2; | |
71 } else if (!strncmp(*it, "END IGNORELIST", strlen("END IGNORELIST"))) { | |
72 state = 0; | |
73 } else if (!strncmp(*it, "BEGIN IDENTITIES", strlen("BEGIN IDENTITIES"))) { | |
74 state = 3; | |
75 } else if (!strncmp(*it, "END IDENTITIES", strlen("END IDENTITIES"))) { | |
76 state = 0; | |
77 } else if (!strncmp(*it, "Mail=", strlen("Mail="))) { | |
78 session->mail = atoi(*it + strlen("Mail=")); | |
79 } else if (!strncmp(*it, "Login=", strlen("Login="))) { | |
80 if (session->login) | |
81 g_free(session->login); | |
82 session->login = g_strdup(*it + strlen("Login=")); | |
83 } else { | |
84 if (state == 1) { | |
85 struct yahoo_group *grp = g_new0(struct yahoo_group, 1); | |
86 char *end = strchr(*it, ':'); | |
87 grp->name = g_strndup(*it, end - *it); | |
88 end++; | |
89 grp->buddies = g_strsplit(end, ",", 1024); | |
90 session->groups = g_list_append(session->groups, grp); | |
91 } else if (state == 2) { | |
92 session->ignored = g_list_append(session->ignored, g_strdup(*it)); | |
93 } else if (state == 3) { | |
94 session->identities = g_strsplit(*it, ",", 6); | |
95 } | |
96 } | |
97 } | |
98 | |
99 g_strfreev(str_array); | |
100 yahoo_close(session, conn); | |
101 CALLBACK(session, YAHOO_HANDLE_LOGINCOOKIE); | |
102 return 0; | |
103 } | |
104 | |
105 static void yahoo_parse_status(struct yahoo_session *sess, struct yahoo_packet *pkt) | |
106 { | |
107 char *tmp = pkt->content; | |
108 int count = 0; | |
109 char **strs; | |
110 int i; | |
111 | |
112 YAHOO_PRINT(sess, YAHOO_LOG_DEBUG, pkt->content); | |
113 | |
114 if (strstr(pkt->content, "was not AWAY")) | |
115 return; | |
116 | |
117 while (*tmp && isdigit((int)*tmp)) | |
118 count = count * 10 + *tmp++ - '0'; | |
119 if (*tmp == ',') | |
120 tmp++; | |
121 count = count ? count : 1; | |
122 | |
123 if (count > 1) | |
124 strs = g_strsplit(tmp, "),", count); | |
125 else | |
126 strs = g_strsplit(tmp, ")", count); | |
127 | |
128 for (i = 0; i < count && strs[i]; i++) { | |
129 char **vals; | |
130 char *end, *who; | |
131 char **it; | |
132 int c, j; | |
133 | |
134 who = strs[i]; | |
135 end = strchr(who, '('); | |
136 *end++ = '\0'; | |
137 | |
138 vals = g_strsplit(end, ",", 1024); | |
139 | |
140 for (it = vals, c = 0; *it; it++, c++); | |
141 if (c > 6) | |
142 end = g_strdup(vals[1]); | |
143 for (j = 2; j < c - 5; j++) { | |
144 char *x = end; | |
145 end = g_strconcat(end, ",", vals[j], NULL); | |
146 g_free(x); | |
147 } | |
148 | |
149 CALLBACK(sess, YAHOO_HANDLE_STATUS, who, atoi(vals[0]), end, | |
150 atoi(vals[c - 3]), atoi(vals[c - 2]), atoi(vals[c - 1])); | |
151 | |
152 if (c > 6) | |
153 g_free(end); | |
154 g_strfreev(vals); | |
155 } | |
156 | |
157 g_strfreev(strs); | |
158 } | |
159 | |
160 static void yahoo_parse_message(struct yahoo_session *sess, struct yahoo_packet *pkt) | |
161 { | |
162 char buf[256]; | |
163 int type = yahoo_makeint(pkt->msgtype); | |
164 char **str_array; | |
165 switch(type) { | |
166 case YAHOO_MESSAGE_NORMAL: | |
167 str_array = g_strsplit(pkt->content, ",,", 2); | |
168 CALLBACK(sess, YAHOO_HANDLE_MESSAGE, pkt->nick2, str_array[0], str_array[1]); | |
169 g_strfreev(str_array); | |
170 break; | |
1562
12f16a852540
[gaim-migrate @ 1572]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1561
diff
changeset
|
171 case YAHOO_MESSAGE_BOUNCE: |
12f16a852540
[gaim-migrate @ 1572]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1561
diff
changeset
|
172 CALLBACK(sess, YAHOO_HANDLE_BOUNCE); |
12f16a852540
[gaim-migrate @ 1572]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1561
diff
changeset
|
173 break; |
1546 | 174 default: |
175 g_snprintf(buf, sizeof(buf), "unhandled message type %d: %s", type, pkt->content); | |
176 YAHOO_PRINT(sess, YAHOO_LOG_WARNING, buf); | |
177 break; | |
178 } | |
179 } | |
180 | |
181 static void yahoo_parse_packet(struct yahoo_session *sess, | |
182 struct yahoo_conn *conn, struct yahoo_packet *pkt) | |
183 { | |
184 char buf[256]; | |
185 int service = yahoo_makeint(pkt->service); | |
186 conn->magic_id = yahoo_makeint(pkt->magic_id); | |
187 g_snprintf(buf, sizeof(buf), "Service %d (msgtype %d)", service, yahoo_makeint(pkt->msgtype)); | |
188 YAHOO_PRINT(sess, YAHOO_LOG_DEBUG, buf); | |
189 switch(service) { | |
190 case YAHOO_SERVICE_LOGON: | |
191 if (yahoo_makeint(pkt->msgtype) == 0) | |
192 CALLBACK(sess, YAHOO_HANDLE_ONLINE); | |
193 case YAHOO_SERVICE_LOGOFF: | |
194 case YAHOO_SERVICE_ISAWAY: | |
195 case YAHOO_SERVICE_ISBACK: | |
1561
9fe7b059d2a5
[gaim-migrate @ 1571]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1557
diff
changeset
|
196 yahoo_parse_status(sess, pkt); |
9fe7b059d2a5
[gaim-migrate @ 1571]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1557
diff
changeset
|
197 break; |
1557
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
198 case YAHOO_SERVICE_NEWCONTACT: |
1561
9fe7b059d2a5
[gaim-migrate @ 1571]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1557
diff
changeset
|
199 if (yahoo_makeint(pkt->msgtype) == 3) |
9fe7b059d2a5
[gaim-migrate @ 1571]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1557
diff
changeset
|
200 yahoo_parse_message(sess, pkt); |
9fe7b059d2a5
[gaim-migrate @ 1571]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1557
diff
changeset
|
201 else |
9fe7b059d2a5
[gaim-migrate @ 1571]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1557
diff
changeset
|
202 yahoo_parse_status(sess, pkt); |
1546 | 203 break; |
204 case YAHOO_SERVICE_IDACT: | |
205 CALLBACK(sess, YAHOO_HANDLE_ACTIVATE, pkt->content); | |
206 break; | |
207 case YAHOO_SERVICE_MESSAGE: | |
208 yahoo_parse_message(sess, pkt); | |
209 break; | |
210 case YAHOO_SERVICE_NEWMAIL: | |
211 CALLBACK(sess, YAHOO_HANDLE_NEWMAIL, strlen(pkt->content) ? atoi(pkt->content) : 0); | |
212 break; | |
213 default: | |
214 g_snprintf(buf, sizeof(buf), "unhandled service type %d: %s", service, pkt->content); | |
215 YAHOO_PRINT(sess, YAHOO_LOG_WARNING, buf); | |
216 break; | |
217 } | |
218 } | |
219 | |
220 void yahoo_socket_handler(struct yahoo_session *session, int socket, int type) | |
221 { | |
222 int pos = 0; | |
223 struct yahoo_conn *conn; | |
224 | |
225 if (!session) | |
226 return; | |
227 | |
228 if (!(conn = yahoo_find_conn(session, socket))) | |
229 return; | |
230 | |
231 if (type == YAHOO_SOCKET_WRITE) { | |
232 int error = ETIMEDOUT, len = sizeof(error); | |
233 | |
234 if (getsockopt(socket, SOL_SOCKET, SO_ERROR, &error, &len) < 0) | |
235 error = errno; | |
236 if (error) { | |
237 yahoo_close(session, conn); | |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
238 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "unable to connect"); |
1546 | 239 CALLBACK(session, YAHOO_HANDLE_DISCONNECT); |
240 return; | |
241 } | |
242 | |
243 fcntl(socket, F_SETFL, 0); | |
244 | |
245 YAHOO_PRINT(session, YAHOO_LOG_NOTICE, "connected"); | |
246 | |
247 if (yahoo_socket_notify) | |
248 (*yahoo_socket_notify)(session, socket, YAHOO_SOCKET_WRITE, FALSE); | |
249 if (yahoo_socket_notify) | |
250 (*yahoo_socket_notify)(session, socket, YAHOO_SOCKET_READ, TRUE); | |
251 | |
252 if (conn->type == YAHOO_CONN_TYPE_AUTH) { | |
253 CALLBACK(session, YAHOO_HANDLE_AUTHCONNECT); | |
254 } else if (conn->type == YAHOO_CONN_TYPE_MAIN) { | |
255 CALLBACK(session, YAHOO_HANDLE_MAINCONNECT); | |
256 } else if (conn->type == YAHOO_CONN_TYPE_DUMB) { | |
257 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, "sending to buddy list host"); | |
258 yahoo_write(session, conn, conn->txqueue, strlen(conn->txqueue)); | |
259 g_free(conn->txqueue); | |
260 conn->txqueue = NULL; | |
261 } | |
262 | |
263 return; | |
264 } | |
265 | |
266 if (conn->type == YAHOO_CONN_TYPE_AUTH) { | |
267 char *buf = g_malloc0(5000); | |
268 while (read(socket, &buf[pos++], 1) == 1); | |
269 if (pos == 1) { | |
270 g_free(buf); | |
271 yahoo_close(session, conn); | |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
272 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "could not read auth response"); |
1546 | 273 CALLBACK(session, YAHOO_HANDLE_DISCONNECT); |
274 return; | |
275 } | |
276 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, buf); | |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
277 if (yahoo_parse_config(session, conn, buf)) { |
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
278 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "could not parse auth response"); |
1546 | 279 CALLBACK(session, YAHOO_HANDLE_DISCONNECT); |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
280 } |
1546 | 281 g_free(buf); |
282 } else if (conn->type == YAHOO_CONN_TYPE_MAIN) { | |
283 struct yahoo_packet pkt; | |
284 int len; | |
285 | |
286 if ((read(socket, &pkt, 8) != 8) || strcmp(pkt.version, "YHOO1.0")) { | |
287 yahoo_close(session, conn); | |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
288 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "invalid version type"); |
1546 | 289 CALLBACK(session, YAHOO_HANDLE_DISCONNECT); |
290 return; | |
291 } | |
292 | |
293 if (read(socket, &pkt.len, 4) != 4) { | |
294 yahoo_close(session, conn); | |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
295 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "could not read length"); |
1546 | 296 CALLBACK(session, YAHOO_HANDLE_DISCONNECT); |
297 return; | |
298 } | |
299 len = yahoo_makeint(pkt.len); | |
300 | |
301 if (read(socket, &pkt.service, len - 12) != len - 12) { | |
302 yahoo_close(session, conn); | |
1554
c3afde736a8a
[gaim-migrate @ 1564]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1553
diff
changeset
|
303 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "could not read data"); |
1546 | 304 CALLBACK(session, YAHOO_HANDLE_DISCONNECT); |
305 return; | |
306 } | |
307 yahoo_parse_packet(session, conn, &pkt); | |
308 } else if (conn->type == YAHOO_CONN_TYPE_DUMB) { | |
1557
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
309 char *buf = g_malloc0(5000); |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
310 while (read(socket, &buf[pos++], 1) == 1); |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
311 if (pos == 1) { |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
312 g_free(buf); |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
313 YAHOO_PRINT(session, YAHOO_LOG_WARNING, "error reading from listserv"); |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
314 return; |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
315 } |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
316 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, buf); |
1ca5f5fcd483
[gaim-migrate @ 1567]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1554
diff
changeset
|
317 YAHOO_PRINT(session, YAHOO_LOG_NOTICE, "closing buddy list host connnection"); |
1546 | 318 yahoo_close(session, conn); |
319 } | |
320 } | |
321 | |
322 void yahoo_add_handler(struct yahoo_session *session, int type, yahoo_callback function) | |
323 { | |
324 if (!session) | |
325 return; | |
326 | |
327 session->callbacks[type].function = function; | |
328 } |