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