Mercurial > pidgin.yaz
annotate src/protocols/gg/gg.c @ 2518:e7478176e760
[gaim-migrate @ 2531]
don't delete buddy list
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Tue, 16 Oct 2001 23:24:35 +0000 |
parents | 227cc42ffa6e |
children | 8229710b343b |
rev | line source |
---|---|
2393 | 1 /* |
2 * gaim - Gadu-Gadu Protocol Plugin | |
2518
e7478176e760
[gaim-migrate @ 2531]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
3 * $Id: gg.c 2531 2001-10-16 23:24:35Z warmenhoven $ |
2393 | 4 * |
5 * Copyright (C) 2001, Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL> | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 * | |
21 */ | |
22 | |
23 #include <config.h> | |
24 | |
25 #include <netdb.h> | |
26 #include <unistd.h> | |
27 #include <errno.h> | |
28 #include <netinet/in.h> | |
29 #include <arpa/inet.h> | |
30 #include <fcntl.h> | |
31 #include <string.h> | |
32 #include <stdlib.h> | |
33 #include <stdio.h> | |
34 #include <time.h> | |
35 #include <sys/socket.h> | |
36 #include <sys/stat.h> | |
37 #include <ctype.h> | |
38 #ifdef HAVE_LANGINFO_CODESET | |
39 #include <langinfo.h> | |
40 #endif | |
41 #ifdef HAVE_ICONV | |
42 #include <iconv.h> | |
43 #endif | |
44 /* Library from EKG (Eksperymentalny Klient Gadu-Gadu) */ | |
45 #include "libgg.h" | |
46 #include "multi.h" | |
47 #include "prpl.h" | |
48 #include "gaim.h" | |
49 #include "proxy.h" | |
50 | |
51 #include "pixmaps/gg_suncloud.xpm" | |
52 #include "pixmaps/gg_sunred.xpm" | |
53 #include "pixmaps/gg_sunwhitered.xpm" | |
54 #include "pixmaps/gg_sunyellow.xpm" | |
55 | |
56 #define USEROPT_NICK 0 | |
57 | |
58 #define AGG_BUF_LEN 1024 | |
59 | |
60 #define AGG_GENDER_NONE -1 | |
61 | |
62 #define AGG_PUBDIR_FORM "/appsvc/fmpubquery2.asp" | |
63 #define AGG_PUBDIR_MAX_ENTRIES 200 | |
64 | |
65 #define AGG_STATUS_AVAIL _("Available") | |
66 #define AGG_STATUS_AVAIL_FRIENDS _("Available for friends only") | |
67 #define AGG_STATUS_BUSY _("Away") | |
68 #define AGG_STATUS_BUSY_FRIENDS _("Away for friends only") | |
69 #define AGG_STATUS_INVISIBLE _("Invisible") | |
70 #define AGG_STATUS_INVISIBLE_FRIENDS _("Invisible for friends only") | |
71 #define AGG_STATUS_NOT_AVAIL _("Unavailable") | |
72 | |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2423
diff
changeset
|
73 #define UC_NORMAL 2 |
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2423
diff
changeset
|
74 |
2393 | 75 struct agg_data { |
76 struct gg_session *sess; | |
77 }; | |
78 | |
79 struct agg_search { | |
80 struct gaim_connection *gc; | |
81 gchar *search_data; | |
82 int inpa; | |
83 }; | |
84 | |
85 static char *agg_name() | |
86 { | |
87 return "Gadu-Gadu"; | |
88 } | |
89 | |
90 static gchar *charset_convert(const gchar *locstr, char *encsrc, char *encdst) | |
91 { | |
92 #ifdef HAVE_ICONV | |
93 gchar *dststr; | |
94 size_t loclen, dstlen; | |
95 gchar *fsave, *tsave; | |
96 size_t count; | |
97 static iconv_t cd = (iconv_t)(-1); | |
98 | |
99 if (cd == (iconv_t)(-1)) { | |
100 cd = iconv_open(encdst, encsrc); | |
101 if (cd == (iconv_t)(-1)) { | |
102 return g_strdup(locstr); | |
103 } | |
104 } | |
105 | |
106 loclen = strlen(locstr); | |
107 /* we are ready for multibyte conversions */ | |
108 dstlen = MB_LEN_MAX * loclen; | |
109 dststr = g_new0(gchar, dstlen + 1); | |
110 fsave = (gchar *)locstr; | |
111 tsave = dststr; | |
112 count = iconv(cd, &fsave, &loclen, &tsave, &dstlen); | |
113 if (count == -1) { | |
114 g_free(dststr); | |
115 return g_strdup(locstr); | |
116 } | |
117 return dststr; | |
118 #else | |
119 return g_strdup(locstr); | |
120 #endif | |
121 } | |
122 | |
123 static gboolean invalid_uin(char *uin) | |
124 { | |
125 unsigned long int res = strtol(uin, (char **)NULL, 10); | |
126 if (res == LONG_MIN || res == LONG_MAX || res == 0) | |
127 return TRUE; | |
128 return FALSE; | |
129 } | |
130 | |
131 static gint args_compare(gconstpointer a, gconstpointer b) | |
132 { | |
133 gchar *arg_a = (gchar *)a; | |
134 gchar *arg_b = (gchar *)b; | |
135 | |
136 return g_strcasecmp(arg_a, arg_b); | |
137 } | |
138 | |
139 static gboolean allowed_uin(struct gaim_connection *gc, char *uin) | |
140 { | |
141 switch (gc->permdeny) { | |
142 case 1: | |
143 /* permit all, deny none */ | |
144 return TRUE; | |
145 break; | |
146 case 2: | |
147 /* deny all, permit none. */ | |
148 return FALSE; | |
149 break; | |
150 case 3: | |
151 /* permit some. */ | |
152 if (g_slist_find_custom(gc->permit, uin, args_compare)) | |
153 return TRUE; | |
154 return FALSE; | |
155 break; | |
156 case 4: | |
157 /* deny some. */ | |
158 if (g_slist_find_custom(gc->deny, uin, args_compare)) | |
159 return FALSE; | |
160 return TRUE; | |
161 break; | |
162 default: | |
163 return TRUE; | |
164 break; | |
165 } | |
166 } | |
167 | |
168 static gchar *find_local_charset(void) | |
169 { | |
170 gchar *gg_localenc = g_getenv("GG_CHARSET"); | |
171 | |
172 if (gg_localenc == NULL) { | |
173 #ifdef HAVE_LANGINFO_CODESET | |
174 gg_localenc = nl_langinfo(CODESET); | |
175 #else | |
176 gg_localenc = "US-ASCII"; | |
177 #endif | |
178 } | |
179 return gg_localenc; | |
180 } | |
181 | |
182 static char *handle_errcode(int errcode, gboolean show) | |
183 { | |
184 static char msg[AGG_BUF_LEN]; | |
185 | |
186 switch (errcode) { | |
187 case GG_FAILURE_RESOLVING: | |
188 g_snprintf(msg, sizeof(msg), _("Unable to resolve hostname.")); | |
189 break; | |
190 case GG_FAILURE_CONNECTING: | |
191 g_snprintf(msg, sizeof(msg), _("Unable to connect to server.")); | |
192 break; | |
193 case GG_FAILURE_INVALID: | |
194 g_snprintf(msg, sizeof(msg), _("Invalid response from server.")); | |
195 break; | |
196 case GG_FAILURE_READING: | |
197 g_snprintf(msg, sizeof(msg), _("Error while reading from socket.")); | |
198 break; | |
199 case GG_FAILURE_WRITING: | |
200 g_snprintf(msg, sizeof(msg), _("Error while writting to socket.")); | |
201 break; | |
202 case GG_FAILURE_PASSWORD: | |
203 g_snprintf(msg, sizeof(msg), _("Authentification failed.")); | |
204 break; | |
205 default: | |
206 g_snprintf(msg, sizeof(msg), _("Unknown Error Code.")); | |
207 break; | |
208 } | |
209 | |
210 if (show) | |
211 do_error_dialog(msg, _("Gadu-Gadu Error")); | |
212 | |
213 return msg; | |
214 } | |
215 | |
216 static gchar *encode_postdata(const gchar *data) | |
217 { | |
218 gchar *p = NULL; | |
219 int i, j = 0; | |
220 for (i = 0; i < strlen(data); i++) { | |
221 /* locale insensitive, doesn't reflect RFC (1738 section 2.2, 1866 section 8.2.1) */ | |
222 if ((data[i] >= 'a' && data[i] <= 'z') | |
223 || (data[i] >= 'A' && data[i] <= 'Z') | |
224 || (data[i] >= '0' && data[i] <= '9') | |
225 || data[i] == '=' || data[i] == '&' | |
226 || data[i] == '\n' || data[i] == '\r' || data[i] == '\t' || data[i] == '\014') { | |
227 p = g_realloc(p, j + 1); | |
228 p[j] = data[i]; | |
229 j++; | |
230 } else { | |
231 p = g_realloc(p, j + 4); /* remember, sprintf appends a '\0' */ | |
232 sprintf(p + j, "%%%02x", (unsigned char)data[i]); | |
233 j += 3; | |
234 } | |
235 } | |
236 p = g_realloc(p, j + 1); | |
237 p[j] = '\0'; | |
238 | |
239 if (p && strlen(p)) | |
240 return p; | |
241 else | |
242 return g_strdup(data); | |
243 } | |
244 | |
245 static void agg_set_away(struct gaim_connection *gc, char *state, char *msg) | |
246 { | |
247 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
248 | |
249 if (gc->away) | |
250 gc->away = NULL; | |
251 | |
252 if (!g_strcasecmp(state, AGG_STATUS_AVAIL)) | |
253 gg_change_status(gd->sess, GG_STATUS_AVAIL); | |
254 else if (!g_strcasecmp(state, AGG_STATUS_AVAIL_FRIENDS)) | |
255 gg_change_status(gd->sess, GG_STATUS_AVAIL | GG_STATUS_FRIENDS_MASK); | |
256 else if (!g_strcasecmp(state, AGG_STATUS_BUSY)) { | |
257 gg_change_status(gd->sess, GG_STATUS_BUSY); | |
258 gc->away = ""; | |
259 } else if (!g_strcasecmp(state, AGG_STATUS_BUSY_FRIENDS)) { | |
260 gg_change_status(gd->sess, GG_STATUS_BUSY | GG_STATUS_FRIENDS_MASK); | |
261 gc->away = ""; | |
262 } else if (!g_strcasecmp(state, AGG_STATUS_INVISIBLE)) { | |
263 gg_change_status(gd->sess, GG_STATUS_INVISIBLE); | |
264 gc->away = ""; | |
265 } else if (!g_strcasecmp(state, AGG_STATUS_INVISIBLE_FRIENDS)) { | |
266 gg_change_status(gd->sess, GG_STATUS_INVISIBLE | GG_STATUS_FRIENDS_MASK); | |
267 gc->away = ""; | |
268 } else if (!g_strcasecmp(state, AGG_STATUS_NOT_AVAIL)) { | |
269 gg_change_status(gd->sess, GG_STATUS_NOT_AVAIL); | |
270 gc->away = ""; | |
271 } else if (!g_strcasecmp(state, GAIM_AWAY_CUSTOM)) { | |
272 if (msg) { | |
273 gg_change_status(gd->sess, GG_STATUS_BUSY); | |
274 gc->away = ""; | |
275 } else | |
276 gg_change_status(gd->sess, GG_STATUS_AVAIL); | |
277 } | |
278 } | |
279 | |
280 static gchar *get_away_text(int uc) | |
281 { | |
282 if (uc == UC_UNAVAILABLE) | |
283 return AGG_STATUS_NOT_AVAIL; | |
284 uc = uc >> 5; | |
285 switch (uc) { | |
286 case GG_STATUS_AVAIL: | |
287 default: | |
288 return AGG_STATUS_AVAIL; | |
289 case GG_STATUS_AVAIL | GG_STATUS_FRIENDS_MASK: | |
290 return AGG_STATUS_AVAIL_FRIENDS; | |
291 case GG_STATUS_BUSY: | |
292 return AGG_STATUS_BUSY; | |
293 case GG_STATUS_BUSY | GG_STATUS_FRIENDS_MASK: | |
294 return AGG_STATUS_BUSY_FRIENDS; | |
295 case GG_STATUS_INVISIBLE: | |
296 return AGG_STATUS_INVISIBLE; | |
297 case GG_STATUS_INVISIBLE | GG_STATUS_FRIENDS_MASK: | |
298 return AGG_STATUS_INVISIBLE_FRIENDS; | |
299 case GG_STATUS_NOT_AVAIL: | |
300 return AGG_STATUS_NOT_AVAIL; | |
301 } | |
302 } | |
303 | |
2501
227cc42ffa6e
[gaim-migrate @ 2514]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2423
diff
changeset
|
304 static GList *agg_away_states(struct gaim_connection *gc) |
2393 | 305 { |
306 GList *m = NULL; | |
307 | |
308 m = g_list_append(m, AGG_STATUS_AVAIL); | |
309 m = g_list_append(m, AGG_STATUS_BUSY); | |
310 m = g_list_append(m, AGG_STATUS_INVISIBLE); | |
311 m = g_list_append(m, AGG_STATUS_AVAIL_FRIENDS); | |
312 m = g_list_append(m, AGG_STATUS_BUSY_FRIENDS); | |
313 m = g_list_append(m, AGG_STATUS_INVISIBLE_FRIENDS); | |
314 m = g_list_append(m, AGG_STATUS_NOT_AVAIL); | |
315 return m; | |
316 } | |
317 | |
318 /* Enhance these functions, more options and such stuff */ | |
319 static GList *agg_buddy_menu(struct gaim_connection *gc, char *who) | |
320 { | |
321 GList *m = NULL; | |
322 struct proto_buddy_menu *pbm; | |
323 struct buddy *b = find_buddy(gc, who); | |
324 static char buf[AGG_BUF_LEN]; | |
325 | |
326 if (!b) { | |
327 return m; | |
328 } | |
329 | |
330 pbm = g_new0(struct proto_buddy_menu, 1); | |
331 g_snprintf(buf, sizeof(buf), _("Status: %s"), get_away_text(b->uc)); | |
332 pbm->label = buf; | |
333 pbm->callback = NULL; | |
334 pbm->gc = gc; | |
335 m = g_list_append(m, pbm); | |
336 | |
337 return m; | |
338 } | |
339 | |
340 static GList *agg_user_opts() | |
341 { | |
342 GList *m = NULL; | |
343 struct proto_user_opt *puo; | |
344 | |
345 puo = g_new0(struct proto_user_opt, 1); | |
346 puo->label = _("Nick:"); | |
347 puo->def = _("Gadu-Gadu User"); | |
348 puo->pos = USEROPT_NICK; | |
349 m = g_list_append(m, puo); | |
350 | |
351 return m; | |
352 } | |
353 | |
354 static void main_callback(gpointer data, gint source, GaimInputCondition cond) | |
355 { | |
356 struct gaim_connection *gc = data; | |
357 struct agg_data *gd = gc->proto_data; | |
358 struct gg_event *e; | |
359 | |
360 debug_printf("main_callback enter: begin\n"); | |
361 | |
362 if (gd->sess->fd != source) | |
363 gd->sess->fd = source; | |
364 | |
365 if (source == -1) { | |
366 signoff(gc); | |
367 return; | |
368 } | |
369 | |
370 if (!(e = gg_watch_fd(gd->sess))) { | |
371 debug_printf("main_callback: gg_watch_fd failed - CRITICAL!\n"); | |
372 signoff(gc); | |
373 return; | |
374 } | |
375 | |
376 switch (e->type) { | |
377 case GG_EVENT_NONE: | |
378 /* do nothing */ | |
379 break; | |
380 case GG_EVENT_CONN_SUCCESS: | |
381 debug_printf("main_callback: CONNECTED AGAIN!?\n"); | |
382 break; | |
383 case GG_EVENT_CONN_FAILED: | |
384 if (gc->inpa) | |
385 gaim_input_remove(gc->inpa); | |
386 handle_errcode(e->event.failure, TRUE); | |
387 signoff(gc); | |
388 break; | |
389 case GG_EVENT_MSG: | |
390 { | |
391 gchar *imsg; | |
392 gchar user[20]; | |
393 | |
394 g_snprintf(user, sizeof(user), "%u", e->event.msg.sender); | |
395 if (!allowed_uin(gc, user)) | |
396 break; | |
397 imsg = charset_convert(e->event.msg.message, "CP1250", find_local_charset()); | |
398 serv_got_im(gc, user, imsg, 0, time((time_t) NULL)); | |
399 g_free(imsg); | |
400 } | |
401 break; | |
402 case GG_EVENT_NOTIFY: | |
403 { | |
404 gchar user[20]; | |
405 struct gg_notify_reply *n = e->event.notify; | |
406 guint status; | |
407 | |
408 while (n->uin) { | |
409 switch (n->status) { | |
410 case GG_STATUS_NOT_AVAIL: | |
411 status = UC_UNAVAILABLE; | |
412 break; | |
413 case GG_STATUS_AVAIL: | |
414 case GG_STATUS_BUSY: | |
415 case GG_STATUS_INVISIBLE: | |
416 case GG_STATUS_FRIENDS_MASK: | |
417 status = UC_NORMAL | (e->event.status.status << 5); | |
418 break; | |
419 default: | |
420 status = UC_NORMAL; | |
421 break; | |
422 } | |
423 | |
424 g_snprintf(user, sizeof(user), "%u", n->uin); | |
425 serv_got_update(gc, user, (status == UC_UNAVAILABLE) ? 0 : 1, 0, 0, 0, | |
426 status, 0); | |
427 n++; | |
428 } | |
429 } | |
430 break; | |
431 case GG_EVENT_STATUS: | |
432 { | |
433 gchar user[20]; | |
434 guint status; | |
435 | |
436 switch (e->event.status.status) { | |
437 case GG_STATUS_NOT_AVAIL: | |
438 status = UC_UNAVAILABLE; | |
439 break; | |
440 case GG_STATUS_AVAIL: | |
441 case GG_STATUS_BUSY: | |
442 case GG_STATUS_INVISIBLE: | |
443 case GG_STATUS_FRIENDS_MASK: | |
444 status = UC_NORMAL | (e->event.status.status << 5); | |
445 break; | |
446 default: | |
447 status = UC_NORMAL; | |
448 break; | |
449 } | |
450 | |
451 g_snprintf(user, sizeof(user), "%u", e->event.status.uin); | |
452 serv_got_update(gc, user, (status == UC_UNAVAILABLE) ? 0 : 1, 0, 0, 0, status, | |
453 0); | |
454 } | |
455 break; | |
456 case GG_EVENT_ACK: | |
457 debug_printf("main_callback: message %d to %u sent with status %d\n", | |
458 e->event.ack.seq, e->event.ack.recipient, e->event.ack.status); | |
459 break; | |
460 default: | |
461 debug_printf("main_callback: unsupported event %d\n", e->type); | |
462 break; | |
463 } | |
464 gg_free_event(e); | |
465 } | |
466 | |
467 static void login_callback(gpointer data, gint source, GaimInputCondition cond) | |
468 { | |
469 struct gaim_connection *gc = data; | |
470 struct agg_data *gd = gc->proto_data; | |
471 struct gg_event *e; | |
472 | |
473 if (!g_slist_find(connections, data)) { | |
474 close(source); | |
475 return; | |
476 } | |
477 | |
478 if (gd->sess->fd != source) | |
479 gd->sess->fd = source; | |
480 | |
481 if (source == -1) { | |
482 hide_login_progress(gc, _("Unable to connect.")); | |
483 signoff(gc); | |
484 return; | |
485 } | |
486 | |
487 if (gc->inpa == 0) | |
488 gc->inpa = gaim_input_add(gd->sess->fd, GAIM_INPUT_READ, login_callback, gc); | |
489 | |
490 switch (gd->sess->state) { | |
491 case GG_STATE_CONNECTING_HTTP: | |
492 case GG_STATE_WRITING_HTTP: | |
493 set_login_progress(gc, 2, _("Handshake")); | |
494 case GG_STATE_CONNECTING_GG: | |
495 set_login_progress(gc, 3, _("Connecting to GG server")); | |
496 break; | |
497 case GG_STATE_WAITING_FOR_KEY: | |
498 set_login_progress(gc, 4, _("Waiting for server key")); | |
499 case GG_STATE_SENDING_KEY: | |
500 set_login_progress(gc, 5, _("Sending key")); | |
501 break; | |
502 default: | |
503 break; | |
504 } | |
505 | |
506 if (!(e = gg_watch_fd(gd->sess))) { | |
507 debug_printf("login_callback: gg_watch_fd failed - CRITICAL!\n"); | |
508 signoff(gc); | |
509 return; | |
510 } | |
511 | |
512 switch (e->type) { | |
513 case GG_EVENT_NONE: | |
514 /* nothing */ | |
515 break; | |
516 case GG_EVENT_CONN_SUCCESS: | |
517 /* Setup new input handler */ | |
518 if (gc->inpa) | |
519 gaim_input_remove(gc->inpa); | |
520 gc->inpa = gaim_input_add(gd->sess->fd, GAIM_INPUT_READ, main_callback, gc); | |
521 | |
522 /* Our signon is complete */ | |
523 account_online(gc); | |
524 serv_finish_login(gc); | |
525 | |
2518
e7478176e760
[gaim-migrate @ 2531]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
526 if (bud_list_cache_exists(gc)) |
e7478176e760
[gaim-migrate @ 2531]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2501
diff
changeset
|
527 do_import(gc, NULL); |
2393 | 528 break; |
529 case GG_EVENT_CONN_FAILED: | |
530 gaim_input_remove(gc->inpa); | |
531 gc->inpa = 0; | |
532 handle_errcode(e->event.failure, TRUE); | |
533 signoff(gc); | |
534 break; | |
535 default: | |
536 break; | |
537 } | |
538 | |
539 gg_free_event(e); | |
540 } | |
541 | |
542 static void agg_keepalive(struct gaim_connection *gc) | |
543 { | |
544 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
545 if (gg_ping(gd->sess) < 0) { | |
546 signoff(gc); | |
547 return; | |
548 } | |
549 } | |
550 | |
551 static void agg_login(struct aim_user *user) | |
552 { | |
553 struct gaim_connection *gc = new_gaim_conn(user); | |
554 struct agg_data *gd = gc->proto_data = g_new0(struct agg_data, 1); | |
555 char buf[80]; | |
556 | |
557 gd->sess = g_new0(struct gg_session, 1); | |
558 | |
559 if (user->proto_opt[USEROPT_NICK][0]) | |
560 g_snprintf(gc->displayname, sizeof(gc->displayname), "%s", | |
561 user->proto_opt[USEROPT_NICK]); | |
562 | |
563 set_login_progress(gc, 1, _("Looking up GG server")); | |
564 | |
565 if (invalid_uin(user->username)) { | |
566 hide_login_progress(gc, _("Invalid Gadu-Gadu UIN specified")); | |
567 signoff(gc); | |
568 return; | |
569 } | |
570 | |
571 gc->inpa = 0; | |
572 | |
573 /* | |
574 if (gg_login(gd->sess, strtol(user->username, (char **)NULL, 10), user->password, 1) < 0) { | |
575 debug_printf("uin=%u, pass=%s", strtol(user->username, (char **)NULL, 10), user->password); | |
576 hide_login_progress(gc, "Unable to connect."); | |
577 signoff(gc); | |
578 return; | |
579 } | |
580 | |
581 gg_login() sucks for me, so I'm using proxy_connect() | |
582 */ | |
583 | |
584 gd->sess->uin = (uin_t) strtol(user->username, (char **)NULL, 10); | |
585 gd->sess->password = g_strdup(user->password); | |
586 gd->sess->state = GG_STATE_CONNECTING_HTTP; | |
587 gd->sess->check = GG_CHECK_WRITE; | |
588 gd->sess->async = 1; | |
589 gd->sess->recv_left = 0; | |
590 gd->sess->fd = proxy_connect(GG_APPMSG_HOST, GG_APPMSG_PORT, login_callback, gc); | |
591 | |
592 if (gd->sess->fd < 0) { | |
593 g_snprintf(buf, sizeof(buf), _("Connect to %s failed"), GG_APPMSG_HOST); | |
594 hide_login_progress(gc, buf); | |
595 signoff(gc); | |
596 return; | |
597 } | |
598 } | |
599 | |
600 static void agg_close(struct gaim_connection *gc) | |
601 { | |
602 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
603 if (gc->inpa) | |
604 gaim_input_remove(gc->inpa); | |
605 gg_logoff(gd->sess); | |
606 gg_free_session(gd->sess); | |
607 g_free(gc->proto_data); | |
608 } | |
609 | |
610 static int agg_send_im(struct gaim_connection *gc, char *who, char *msg, int flags) | |
611 { | |
612 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
613 gchar *imsg; | |
614 | |
615 if (invalid_uin(who)) { | |
616 do_error_dialog(_("You are trying to send message to invalid Gadu-Gadu UIN!"), | |
617 _("Gadu-Gadu Error")); | |
618 return -1; | |
619 } | |
620 | |
621 if (strlen(msg) > 0) { | |
622 imsg = charset_convert(msg, find_local_charset(), "CP1250"); | |
623 if (gg_send_message(gd->sess, (flags & IM_FLAG_CHECKBOX) ? GG_CLASS_MSG : GG_CLASS_CHAT, | |
624 strtol(who, (char **)NULL, 10), imsg) < 0) | |
625 return -1; | |
626 g_free(imsg); | |
627 } | |
628 return 1; | |
629 } | |
630 | |
631 static void agg_add_buddy(struct gaim_connection *gc, char *who) | |
632 { | |
633 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
634 if (invalid_uin(who)) | |
635 return; | |
636 gg_add_notify(gd->sess, strtol(who, (char **)NULL, 10)); | |
637 } | |
638 | |
639 static void agg_rem_buddy(struct gaim_connection *gc, char *who) | |
640 { | |
641 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
642 if (invalid_uin(who)) | |
643 return; | |
644 gg_remove_notify(gd->sess, strtol(who, (char **)NULL, 10)); | |
645 } | |
646 | |
647 static void agg_add_buddies(struct gaim_connection *gc, GList *whos) | |
648 { | |
649 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
650 uin_t *userlist = NULL; | |
651 int userlist_size = 0; | |
652 | |
653 while (whos) { | |
654 if (!invalid_uin(whos->data)) { | |
655 userlist_size++; | |
656 userlist = g_renew(uin_t, userlist, userlist_size); | |
657 userlist[userlist_size - 1] = | |
658 (uin_t) strtol((char *)whos->data, (char **)NULL, 10); | |
659 } | |
660 whos = g_list_next(whos); | |
661 } | |
662 | |
663 if (userlist) { | |
664 gg_notify(gd->sess, userlist, userlist_size); | |
665 g_free(userlist); | |
666 } | |
667 } | |
668 | |
669 static void search_results(gpointer data, gint source, GaimInputCondition cond) | |
670 { | |
671 struct agg_search *srch = data; | |
672 struct gaim_connection *gc = srch->gc; | |
673 gchar *buf; | |
674 char *ptr; | |
675 char *webdata; | |
676 int len; | |
677 char read_data; | |
678 gchar **webdata_tbl; | |
679 int i, j; | |
680 | |
681 if (!g_slist_find(connections, gc)) { | |
682 debug_printf("search_callback: g_slist_find error\n"); | |
683 gaim_input_remove(srch->inpa); | |
684 g_free(srch); | |
685 close(source); | |
686 return; | |
687 } | |
688 | |
689 webdata = NULL; | |
690 len = 0; | |
691 | |
692 while (read(source, &read_data, 1) > 0 || errno == EWOULDBLOCK) { | |
693 if (errno == EWOULDBLOCK) { | |
694 errno = 0; | |
695 continue; | |
696 } | |
697 | |
698 if (!read_data) | |
699 continue; | |
700 | |
701 len++; | |
702 webdata = g_realloc(webdata, len); | |
703 webdata[len - 1] = read_data; | |
704 } | |
705 | |
706 webdata = g_realloc(webdata, len + 1); | |
707 webdata[len] = 0; | |
708 | |
709 gaim_input_remove(srch->inpa); | |
710 g_free(srch); | |
711 close(source); | |
712 | |
713 if ((ptr = strstr(webdata, "query_results:")) == NULL || (ptr = strchr(ptr, '\n')) == NULL) { | |
714 debug_printf("search_callback: pubdir result [%s]\n", webdata); | |
715 g_free(webdata); | |
716 do_error_dialog(_("Couldn't get search results"), _("Gadu-Gadu Error")); | |
717 return; | |
718 } | |
719 ptr++; | |
720 | |
721 buf = g_strconcat("<B>", _("Gadu-Gadu Search Engine"), "</B><BR>\n", NULL); | |
722 | |
723 webdata_tbl = g_strsplit(ptr, "\n", AGG_PUBDIR_MAX_ENTRIES); | |
724 | |
725 g_free(webdata); | |
726 | |
727 j = 0; | |
728 | |
729 /* Parse array */ | |
730 for (i = 0; webdata_tbl[i] != NULL; i++) { | |
731 gchar *p, *oldibuf; | |
732 static gchar *ibuf; | |
733 | |
734 g_strdelimit(webdata_tbl[i], "\t\n", ' '); | |
735 | |
736 /* GG_PUBDIR_HOST service returns 7 lines of data per directory entry */ | |
737 if (i % 8 == 0) | |
738 j = 0; | |
739 | |
740 p = charset_convert(g_strstrip(webdata_tbl[i]), "CP1250", find_local_charset()); | |
741 | |
742 oldibuf = ibuf; | |
743 | |
744 switch (j) { | |
745 case 0: | |
746 ibuf = g_strconcat("---------------------------------<BR>\n", NULL); | |
747 oldibuf = ibuf; | |
748 ibuf = g_strconcat(oldibuf, "<B>", _("Active"), ":</B> ", | |
749 (atoi(p) == 2) ? _("yes") : _("no"), "<BR>\n", NULL); | |
750 g_free(oldibuf); | |
751 break; | |
752 case 1: | |
753 ibuf = g_strconcat(oldibuf, "<B>", _("UIN"), ":</B> ", p, "<BR>\n", NULL); | |
754 g_free(oldibuf); | |
755 break; | |
756 case 2: | |
757 ibuf = g_strconcat(oldibuf, "<B>", _("First name"), ":</B> ", p, "<BR>\n", NULL); | |
758 g_free(oldibuf); | |
759 break; | |
760 case 3: | |
761 ibuf = | |
762 g_strconcat(oldibuf, "<B>", _("Second Name"), ":</B> ", p, "<BR>\n", NULL); | |
763 g_free(oldibuf); | |
764 break; | |
765 case 4: | |
766 ibuf = g_strconcat(oldibuf, "<B>", _("Nick"), ":</B> ", p, "<BR>\n", NULL); | |
767 g_free(oldibuf); | |
768 break; | |
769 case 5: | |
770 /* Hack, invalid_uin does what we really want here but may change in future */ | |
771 if (invalid_uin(p)) | |
772 ibuf = | |
773 g_strconcat(oldibuf, "<B>", _("Birth year"), ":</B> <BR>\n", NULL); | |
774 else | |
775 ibuf = | |
776 g_strconcat(oldibuf, "<B>", _("Birth year"), ":</B> ", p, "<BR>\n", | |
777 NULL); | |
778 g_free(oldibuf); | |
779 break; | |
780 case 6: | |
781 if (atoi(p) == GG_GENDER_FEMALE) | |
782 ibuf = g_strconcat(oldibuf, "<B>", _("Sex"), ":</B> woman<BR>\n", NULL); | |
783 else if (atoi(p) == GG_GENDER_MALE) | |
784 ibuf = g_strconcat(oldibuf, "<B>", _("Sex"), ":</B> man<BR>\n", NULL); | |
785 else | |
786 ibuf = g_strconcat(oldibuf, "<B>", _("Sex"), ":</B> <BR>\n", NULL); | |
787 g_free(oldibuf); | |
788 break; | |
789 case 7: | |
790 ibuf = g_strconcat(oldibuf, "<B>", _("City"), ":</B> ", p, "<BR>\n", NULL); | |
791 g_free(oldibuf); | |
792 | |
793 /* We have all lines, so add them to buffer */ | |
794 { | |
795 gchar *oldbuf = buf; | |
796 buf = g_strconcat(oldbuf, ibuf, NULL); | |
797 g_free(oldbuf); | |
798 } | |
799 | |
800 g_free(ibuf); | |
801 break; | |
802 } | |
803 | |
804 g_free(p); | |
805 | |
806 j++; | |
807 } | |
808 | |
809 g_strfreev(webdata_tbl); | |
810 | |
811 g_show_info_text(buf, NULL); | |
812 | |
813 g_free(buf); | |
814 } | |
815 | |
816 static void search_callback(gpointer data, gint source, GaimInputCondition cond) | |
817 { | |
818 struct agg_search *srch = data; | |
819 struct gaim_connection *gc = srch->gc; | |
820 gchar *search_data = srch->search_data; | |
821 gchar *buf; | |
822 char *ptr; | |
823 | |
824 debug_printf("search_callback enter: begin\n"); | |
825 | |
826 if (!g_slist_find(connections, gc)) { | |
827 debug_printf("search_callback: g_slist_find error\n"); | |
828 g_free(search_data); | |
829 g_free(srch); | |
830 close(source); | |
831 return; | |
832 } | |
833 | |
834 if (source == -1) { | |
835 g_free(search_data); | |
836 g_free(srch); | |
837 return; | |
838 } | |
839 | |
840 ptr = encode_postdata(search_data); | |
841 g_free(search_data); | |
842 | |
843 debug_printf("search_callback: pubdir request [%s]\n", ptr); | |
844 | |
845 buf = g_strdup_printf("POST " AGG_PUBDIR_FORM " HTTP/1.0\r\n" | |
846 "Host: " GG_PUBDIR_HOST "\r\n" | |
847 "Content-Type: application/x-www-form-urlencoded\r\n" | |
848 "User-Agent: Mozilla/4.7 [en] (Win98; I)\r\n" | |
849 "Content-Length: %d\r\n" | |
850 "Pragma: no-cache\r\n" "\r\n" "%s\r\n", strlen(ptr), ptr); | |
851 | |
852 g_free(ptr); | |
853 | |
854 if (write(source, buf, strlen(buf)) < strlen(buf)) { | |
855 g_free(buf); | |
856 g_free(srch); | |
857 close(source); | |
858 do_error_dialog(_("Couldn't send search request"), _("Gadu-Gadu Error")); | |
859 return; | |
860 } | |
861 | |
862 g_free(buf); | |
863 | |
864 srch->inpa = gaim_input_add(source, GAIM_INPUT_READ, search_results, srch); | |
865 } | |
866 | |
867 static void agg_dir_search(struct gaim_connection *gc, char *first, char *middle, | |
868 char *last, char *maiden, char *city, char *state, char *country, char *email) | |
869 { | |
870 struct agg_search *srch = g_new0(struct agg_search, 1); | |
871 static char msg[AGG_BUF_LEN]; | |
872 | |
873 srch->gc = gc; | |
874 | |
875 if (email && strlen(email)) { | |
876 srch->search_data = g_strdup_printf("Mode=1&Email=%s", email); | |
877 } else { | |
878 gchar *new_first = charset_convert(first, find_local_charset(), "CP1250"); | |
879 gchar *new_last = charset_convert(last, find_local_charset(), "CP1250"); | |
880 gchar *new_city = charset_convert(city, find_local_charset(), "CP1250"); | |
881 | |
882 /* For active only add &ActiveOnly= */ | |
883 srch->search_data = g_strdup_printf("Mode=0&FirstName=%s&LastName=%s&Gender=%d" | |
884 "&NickName=%s&City=%s&MinBirth=%d&MaxBirth=%d", | |
885 new_first, new_last, AGG_GENDER_NONE, | |
886 "", new_city, 0, 0); | |
887 | |
888 g_free(new_first); | |
889 g_free(new_last); | |
890 g_free(new_city); | |
891 } | |
892 | |
893 if (proxy_connect(GG_PUBDIR_HOST, GG_PUBDIR_PORT, search_callback, srch) < 0) { | |
894 g_snprintf(msg, sizeof(msg), _("Connect to search service failed (%s)"), GG_PUBDIR_HOST); | |
895 do_error_dialog(msg, _("Gadu-Gadu Error")); | |
896 g_free(srch->search_data); | |
897 g_free(srch); | |
898 return; | |
899 } | |
900 } | |
901 | |
902 static void agg_do_action(struct gaim_connection *gc, char *action) | |
903 { | |
904 if (!strcmp(action, _("Directory Search"))) { | |
905 show_find_info(gc); | |
906 } | |
907 } | |
908 | |
909 static GList *agg_actions() | |
910 { | |
911 GList *m = NULL; | |
912 | |
913 m = g_list_append(m, _("Directory Search")); | |
914 | |
915 return m; | |
916 } | |
917 | |
918 static void agg_get_info(struct gaim_connection *gc, char *who) | |
919 { | |
920 struct agg_search *srch = g_new0(struct agg_search, 1); | |
921 static char msg[AGG_BUF_LEN]; | |
922 | |
923 srch->gc = gc; | |
924 | |
925 /* If it's invalid uin then maybe it's nickname? */ | |
926 if (invalid_uin(who)) { | |
927 gchar *new_who = charset_convert(who, find_local_charset(), "CP1250"); | |
928 | |
929 srch->search_data = g_strdup_printf("Mode=0&FirstName=%s&LastName=%s&Gender=%d" | |
930 "&NickName=%s&City=%s&MinBirth=%d&MaxBirth=%d", | |
931 "", "", AGG_GENDER_NONE, new_who, "", 0, 0); | |
932 | |
933 g_free(new_who); | |
934 } else | |
935 srch->search_data = g_strdup_printf("Mode=3&UserId=%s", who); | |
936 | |
937 if (proxy_connect(GG_PUBDIR_HOST, GG_PUBDIR_PORT, search_callback, srch) < 0) { | |
938 g_snprintf(msg, sizeof(msg), _("Connect to search service failed (%s)"), GG_PUBDIR_HOST); | |
939 do_error_dialog(msg, _("Gadu-Gadu Error")); | |
940 g_free(srch->search_data); | |
941 g_free(srch); | |
942 return; | |
943 } | |
944 } | |
945 | |
946 static char **agg_list_icon(int uc) | |
947 { | |
948 guint status; | |
949 if (uc == UC_UNAVAILABLE) | |
950 return (char **)gg_sunred_xpm; | |
951 status = uc >> 5; | |
952 /* Drop all masks */ | |
953 status &= ~(GG_STATUS_FRIENDS_MASK); | |
954 if (status == GG_STATUS_AVAIL) | |
955 return (char **)gg_sunyellow_xpm; | |
956 if (status == GG_STATUS_BUSY) | |
957 return (char **)gg_suncloud_xpm; | |
958 if (status == GG_STATUS_INVISIBLE) | |
959 return (char **)gg_sunwhitered_xpm; | |
960 return (char **)gg_sunyellow_xpm; | |
961 } | |
962 | |
963 static void agg_set_permit_deny_dummy(struct gaim_connection *gc) | |
964 { | |
965 /* It's implemented on client side because GG server doesn't support this */ | |
966 } | |
967 | |
968 static void agg_permit_deny_dummy(struct gaim_connection *gc, char *who) | |
969 { | |
970 /* It's implemented on client side because GG server doesn't support this */ | |
971 } | |
972 | |
973 static struct prpl *my_protocol = NULL; | |
974 | |
2423
ce09589b7681
[gaim-migrate @ 2436]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
975 void gg_init(struct prpl *ret) |
2393 | 976 { |
977 ret->protocol = PROTO_GADUGADU; | |
978 ret->options = 0; | |
979 ret->name = agg_name; | |
980 ret->checkbox = _("Send as message"); | |
981 ret->list_icon = agg_list_icon; | |
982 ret->away_states = agg_away_states; | |
983 ret->actions = agg_actions; | |
984 ret->do_action = agg_do_action; | |
985 ret->user_opts = agg_user_opts; | |
986 ret->buddy_menu = agg_buddy_menu; | |
987 ret->chat_info = NULL; | |
988 ret->login = agg_login; | |
989 ret->close = agg_close; | |
990 ret->send_im = agg_send_im; | |
991 ret->set_info = NULL; | |
992 ret->get_info = agg_get_info; | |
993 ret->set_away = agg_set_away; | |
994 ret->set_dir = NULL; | |
995 ret->get_dir = agg_get_info; | |
996 ret->dir_search = agg_dir_search; | |
997 ret->set_idle = NULL; | |
998 ret->change_passwd = NULL; | |
999 ret->add_buddy = agg_add_buddy; | |
1000 ret->add_buddies = agg_add_buddies; | |
1001 ret->remove_buddy = agg_rem_buddy; | |
1002 ret->add_permit = agg_permit_deny_dummy; | |
1003 ret->add_deny = agg_permit_deny_dummy; | |
1004 ret->rem_permit = agg_permit_deny_dummy; | |
1005 ret->rem_deny = agg_permit_deny_dummy; | |
1006 ret->set_permit_deny = agg_set_permit_deny_dummy; | |
1007 ret->warn = NULL; | |
1008 ret->join_chat = NULL; | |
1009 ret->chat_invite = NULL; | |
1010 ret->chat_leave = NULL; | |
1011 ret->chat_whisper = NULL; | |
1012 ret->chat_send = NULL; | |
1013 ret->keepalive = agg_keepalive; | |
1014 ret->normalize = NULL; | |
1015 my_protocol = ret; | |
1016 } | |
1017 | |
1018 #ifndef STATIC | |
1019 | |
1020 char *gaim_plugin_init(GModule *handle) | |
1021 { | |
2423
ce09589b7681
[gaim-migrate @ 2436]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
1022 load_protocol(gg_init, sizeof(struct prpl)); |
2393 | 1023 return NULL; |
1024 } | |
1025 | |
1026 void gaim_plugin_remove() | |
1027 { | |
1028 struct prpl *p = find_prpl(PROTO_GADUGADU); | |
1029 if (p == my_protocol) | |
1030 unload_protocol(p); | |
1031 } | |
1032 | |
1033 char *name() | |
1034 { | |
1035 return "Gadu-Gadu"; | |
1036 } | |
1037 | |
1038 char *description() | |
1039 { | |
1040 return PRPL_DESC("Gadu-Gadu"); | |
1041 } | |
1042 | |
1043 #endif |