comparison src/protocols/gg/gg.c @ 2393:a7ecfd3f7714

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