Mercurial > pidgin
annotate src/protocols/gg/gg.c @ 2791:8f6365332a05
[gaim-migrate @ 2804]
i hate this shit. i should just stop hacking gaim.
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Mon, 26 Nov 2001 20:39:54 +0000 |
parents | a0fd8f91e294 |
children | 9123abd0db92 |
rev | line source |
---|---|
2393 | 1 /* |
2 * gaim - Gadu-Gadu Protocol Plugin | |
2791
8f6365332a05
[gaim-migrate @ 2804]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2773
diff
changeset
|
3 * $Id: gg.c 2804 2001-11-26 20:39:54Z 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 { | |
2563 | 95 #ifdef HAVE_ICONV |
2542
221e1c8e2410
[gaim-migrate @ 2555]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
96 gchar *result = NULL; |
221e1c8e2410
[gaim-migrate @ 2555]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
97 if (iconv_string(encdst, encsrc, locstr, locstr+strlen(locstr)+1, &result, NULL) >= 0) |
221e1c8e2410
[gaim-migrate @ 2555]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2541
diff
changeset
|
98 return result; |
2541
8229710b343b
[gaim-migrate @ 2554]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2518
diff
changeset
|
99 #endif |
2393 | 100 return g_strdup(locstr); |
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 | |
2706
e841b14b5b89
[gaim-migrate @ 2719]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2681
diff
changeset
|
537 gc->checkbox = _("Send as message"); |
e841b14b5b89
[gaim-migrate @ 2719]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2681
diff
changeset
|
538 |
2393 | 539 gd->sess = g_new0(struct gg_session, 1); |
540 | |
541 if (user->proto_opt[USEROPT_NICK][0]) | |
542 g_snprintf(gc->displayname, sizeof(gc->displayname), "%s", | |
543 user->proto_opt[USEROPT_NICK]); | |
544 | |
545 set_login_progress(gc, 1, _("Looking up GG server")); | |
546 | |
547 if (invalid_uin(user->username)) { | |
548 hide_login_progress(gc, _("Invalid Gadu-Gadu UIN specified")); | |
549 signoff(gc); | |
550 return; | |
551 } | |
552 | |
553 gc->inpa = 0; | |
554 | |
555 /* | |
556 if (gg_login(gd->sess, strtol(user->username, (char **)NULL, 10), user->password, 1) < 0) { | |
557 debug_printf("uin=%u, pass=%s", strtol(user->username, (char **)NULL, 10), user->password); | |
558 hide_login_progress(gc, "Unable to connect."); | |
559 signoff(gc); | |
560 return; | |
561 } | |
562 | |
563 gg_login() sucks for me, so I'm using proxy_connect() | |
564 */ | |
565 | |
566 gd->sess->uin = (uin_t) strtol(user->username, (char **)NULL, 10); | |
567 gd->sess->password = g_strdup(user->password); | |
568 gd->sess->state = GG_STATE_CONNECTING_HTTP; | |
569 gd->sess->check = GG_CHECK_WRITE; | |
570 gd->sess->async = 1; | |
571 gd->sess->recv_left = 0; | |
572 gd->sess->fd = proxy_connect(GG_APPMSG_HOST, GG_APPMSG_PORT, login_callback, gc); | |
573 | |
574 if (gd->sess->fd < 0) { | |
575 g_snprintf(buf, sizeof(buf), _("Connect to %s failed"), GG_APPMSG_HOST); | |
576 hide_login_progress(gc, buf); | |
577 signoff(gc); | |
578 return; | |
579 } | |
580 } | |
581 | |
582 static void agg_close(struct gaim_connection *gc) | |
583 { | |
584 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
585 if (gc->inpa) | |
586 gaim_input_remove(gc->inpa); | |
587 gg_logoff(gd->sess); | |
588 gg_free_session(gd->sess); | |
589 g_free(gc->proto_data); | |
590 } | |
591 | |
592 static int agg_send_im(struct gaim_connection *gc, char *who, char *msg, int flags) | |
593 { | |
594 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
595 gchar *imsg; | |
596 | |
597 if (invalid_uin(who)) { | |
598 do_error_dialog(_("You are trying to send message to invalid Gadu-Gadu UIN!"), | |
599 _("Gadu-Gadu Error")); | |
600 return -1; | |
601 } | |
602 | |
603 if (strlen(msg) > 0) { | |
604 imsg = charset_convert(msg, find_local_charset(), "CP1250"); | |
605 if (gg_send_message(gd->sess, (flags & IM_FLAG_CHECKBOX) ? GG_CLASS_MSG : GG_CLASS_CHAT, | |
606 strtol(who, (char **)NULL, 10), imsg) < 0) | |
607 return -1; | |
608 g_free(imsg); | |
609 } | |
610 return 1; | |
611 } | |
612 | |
613 static void agg_add_buddy(struct gaim_connection *gc, char *who) | |
614 { | |
615 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
616 if (invalid_uin(who)) | |
617 return; | |
618 gg_add_notify(gd->sess, strtol(who, (char **)NULL, 10)); | |
619 } | |
620 | |
2681
37d80035e77f
[gaim-migrate @ 2694]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2563
diff
changeset
|
621 static void agg_rem_buddy(struct gaim_connection *gc, char *who, char *group) |
2393 | 622 { |
623 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
624 if (invalid_uin(who)) | |
625 return; | |
626 gg_remove_notify(gd->sess, strtol(who, (char **)NULL, 10)); | |
627 } | |
628 | |
629 static void agg_add_buddies(struct gaim_connection *gc, GList *whos) | |
630 { | |
631 struct agg_data *gd = (struct agg_data *)gc->proto_data; | |
632 uin_t *userlist = NULL; | |
633 int userlist_size = 0; | |
634 | |
635 while (whos) { | |
636 if (!invalid_uin(whos->data)) { | |
637 userlist_size++; | |
638 userlist = g_renew(uin_t, userlist, userlist_size); | |
639 userlist[userlist_size - 1] = | |
640 (uin_t) strtol((char *)whos->data, (char **)NULL, 10); | |
641 } | |
642 whos = g_list_next(whos); | |
643 } | |
644 | |
645 if (userlist) { | |
646 gg_notify(gd->sess, userlist, userlist_size); | |
647 g_free(userlist); | |
648 } | |
649 } | |
650 | |
651 static void search_results(gpointer data, gint source, GaimInputCondition cond) | |
652 { | |
653 struct agg_search *srch = data; | |
654 struct gaim_connection *gc = srch->gc; | |
655 gchar *buf; | |
656 char *ptr; | |
657 char *webdata; | |
658 int len; | |
659 char read_data; | |
660 gchar **webdata_tbl; | |
661 int i, j; | |
662 | |
663 if (!g_slist_find(connections, gc)) { | |
664 debug_printf("search_callback: g_slist_find error\n"); | |
665 gaim_input_remove(srch->inpa); | |
666 g_free(srch); | |
667 close(source); | |
668 return; | |
669 } | |
670 | |
671 webdata = NULL; | |
672 len = 0; | |
673 | |
674 while (read(source, &read_data, 1) > 0 || errno == EWOULDBLOCK) { | |
675 if (errno == EWOULDBLOCK) { | |
676 errno = 0; | |
677 continue; | |
678 } | |
679 | |
680 if (!read_data) | |
681 continue; | |
682 | |
683 len++; | |
684 webdata = g_realloc(webdata, len); | |
685 webdata[len - 1] = read_data; | |
686 } | |
687 | |
688 webdata = g_realloc(webdata, len + 1); | |
689 webdata[len] = 0; | |
690 | |
691 gaim_input_remove(srch->inpa); | |
692 g_free(srch); | |
693 close(source); | |
694 | |
695 if ((ptr = strstr(webdata, "query_results:")) == NULL || (ptr = strchr(ptr, '\n')) == NULL) { | |
696 debug_printf("search_callback: pubdir result [%s]\n", webdata); | |
697 g_free(webdata); | |
698 do_error_dialog(_("Couldn't get search results"), _("Gadu-Gadu Error")); | |
699 return; | |
700 } | |
701 ptr++; | |
702 | |
703 buf = g_strconcat("<B>", _("Gadu-Gadu Search Engine"), "</B><BR>\n", NULL); | |
704 | |
705 webdata_tbl = g_strsplit(ptr, "\n", AGG_PUBDIR_MAX_ENTRIES); | |
706 | |
707 g_free(webdata); | |
708 | |
709 j = 0; | |
710 | |
711 /* Parse array */ | |
712 for (i = 0; webdata_tbl[i] != NULL; i++) { | |
713 gchar *p, *oldibuf; | |
714 static gchar *ibuf; | |
715 | |
716 g_strdelimit(webdata_tbl[i], "\t\n", ' '); | |
717 | |
718 /* GG_PUBDIR_HOST service returns 7 lines of data per directory entry */ | |
719 if (i % 8 == 0) | |
720 j = 0; | |
721 | |
722 p = charset_convert(g_strstrip(webdata_tbl[i]), "CP1250", find_local_charset()); | |
723 | |
724 oldibuf = ibuf; | |
725 | |
726 switch (j) { | |
727 case 0: | |
728 ibuf = g_strconcat("---------------------------------<BR>\n", NULL); | |
729 oldibuf = ibuf; | |
730 ibuf = g_strconcat(oldibuf, "<B>", _("Active"), ":</B> ", | |
731 (atoi(p) == 2) ? _("yes") : _("no"), "<BR>\n", NULL); | |
732 g_free(oldibuf); | |
733 break; | |
734 case 1: | |
735 ibuf = g_strconcat(oldibuf, "<B>", _("UIN"), ":</B> ", p, "<BR>\n", NULL); | |
736 g_free(oldibuf); | |
737 break; | |
738 case 2: | |
739 ibuf = g_strconcat(oldibuf, "<B>", _("First name"), ":</B> ", p, "<BR>\n", NULL); | |
740 g_free(oldibuf); | |
741 break; | |
742 case 3: | |
743 ibuf = | |
744 g_strconcat(oldibuf, "<B>", _("Second Name"), ":</B> ", p, "<BR>\n", NULL); | |
745 g_free(oldibuf); | |
746 break; | |
747 case 4: | |
748 ibuf = g_strconcat(oldibuf, "<B>", _("Nick"), ":</B> ", p, "<BR>\n", NULL); | |
749 g_free(oldibuf); | |
750 break; | |
751 case 5: | |
752 /* Hack, invalid_uin does what we really want here but may change in future */ | |
753 if (invalid_uin(p)) | |
754 ibuf = | |
755 g_strconcat(oldibuf, "<B>", _("Birth year"), ":</B> <BR>\n", NULL); | |
756 else | |
757 ibuf = | |
758 g_strconcat(oldibuf, "<B>", _("Birth year"), ":</B> ", p, "<BR>\n", | |
759 NULL); | |
760 g_free(oldibuf); | |
761 break; | |
762 case 6: | |
763 if (atoi(p) == GG_GENDER_FEMALE) | |
764 ibuf = g_strconcat(oldibuf, "<B>", _("Sex"), ":</B> woman<BR>\n", NULL); | |
765 else if (atoi(p) == GG_GENDER_MALE) | |
766 ibuf = g_strconcat(oldibuf, "<B>", _("Sex"), ":</B> man<BR>\n", NULL); | |
767 else | |
768 ibuf = g_strconcat(oldibuf, "<B>", _("Sex"), ":</B> <BR>\n", NULL); | |
769 g_free(oldibuf); | |
770 break; | |
771 case 7: | |
772 ibuf = g_strconcat(oldibuf, "<B>", _("City"), ":</B> ", p, "<BR>\n", NULL); | |
773 g_free(oldibuf); | |
774 | |
775 /* We have all lines, so add them to buffer */ | |
776 { | |
777 gchar *oldbuf = buf; | |
778 buf = g_strconcat(oldbuf, ibuf, NULL); | |
779 g_free(oldbuf); | |
780 } | |
781 | |
782 g_free(ibuf); | |
783 break; | |
784 } | |
785 | |
786 g_free(p); | |
787 | |
788 j++; | |
789 } | |
790 | |
791 g_strfreev(webdata_tbl); | |
792 | |
2791
8f6365332a05
[gaim-migrate @ 2804]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2773
diff
changeset
|
793 g_show_info_text(gc, NULL, 2, buf, NULL); |
2393 | 794 |
795 g_free(buf); | |
796 } | |
797 | |
798 static void search_callback(gpointer data, gint source, GaimInputCondition cond) | |
799 { | |
800 struct agg_search *srch = data; | |
801 struct gaim_connection *gc = srch->gc; | |
802 gchar *search_data = srch->search_data; | |
803 gchar *buf; | |
804 char *ptr; | |
805 | |
806 debug_printf("search_callback enter: begin\n"); | |
807 | |
808 if (!g_slist_find(connections, gc)) { | |
809 debug_printf("search_callback: g_slist_find error\n"); | |
810 g_free(search_data); | |
811 g_free(srch); | |
812 close(source); | |
813 return; | |
814 } | |
815 | |
816 if (source == -1) { | |
817 g_free(search_data); | |
818 g_free(srch); | |
819 return; | |
820 } | |
821 | |
822 ptr = encode_postdata(search_data); | |
823 g_free(search_data); | |
824 | |
825 debug_printf("search_callback: pubdir request [%s]\n", ptr); | |
826 | |
827 buf = g_strdup_printf("POST " AGG_PUBDIR_FORM " HTTP/1.0\r\n" | |
828 "Host: " GG_PUBDIR_HOST "\r\n" | |
829 "Content-Type: application/x-www-form-urlencoded\r\n" | |
830 "User-Agent: Mozilla/4.7 [en] (Win98; I)\r\n" | |
831 "Content-Length: %d\r\n" | |
832 "Pragma: no-cache\r\n" "\r\n" "%s\r\n", strlen(ptr), ptr); | |
833 | |
834 g_free(ptr); | |
835 | |
836 if (write(source, buf, strlen(buf)) < strlen(buf)) { | |
837 g_free(buf); | |
838 g_free(srch); | |
839 close(source); | |
840 do_error_dialog(_("Couldn't send search request"), _("Gadu-Gadu Error")); | |
841 return; | |
842 } | |
843 | |
844 g_free(buf); | |
845 | |
846 srch->inpa = gaim_input_add(source, GAIM_INPUT_READ, search_results, srch); | |
847 } | |
848 | |
849 static void agg_dir_search(struct gaim_connection *gc, char *first, char *middle, | |
850 char *last, char *maiden, char *city, char *state, char *country, char *email) | |
851 { | |
852 struct agg_search *srch = g_new0(struct agg_search, 1); | |
853 static char msg[AGG_BUF_LEN]; | |
854 | |
855 srch->gc = gc; | |
856 | |
857 if (email && strlen(email)) { | |
858 srch->search_data = g_strdup_printf("Mode=1&Email=%s", email); | |
859 } else { | |
860 gchar *new_first = charset_convert(first, find_local_charset(), "CP1250"); | |
861 gchar *new_last = charset_convert(last, find_local_charset(), "CP1250"); | |
862 gchar *new_city = charset_convert(city, find_local_charset(), "CP1250"); | |
863 | |
864 /* For active only add &ActiveOnly= */ | |
865 srch->search_data = g_strdup_printf("Mode=0&FirstName=%s&LastName=%s&Gender=%d" | |
866 "&NickName=%s&City=%s&MinBirth=%d&MaxBirth=%d", | |
867 new_first, new_last, AGG_GENDER_NONE, | |
868 "", new_city, 0, 0); | |
869 | |
870 g_free(new_first); | |
871 g_free(new_last); | |
872 g_free(new_city); | |
873 } | |
874 | |
875 if (proxy_connect(GG_PUBDIR_HOST, GG_PUBDIR_PORT, search_callback, srch) < 0) { | |
876 g_snprintf(msg, sizeof(msg), _("Connect to search service failed (%s)"), GG_PUBDIR_HOST); | |
877 do_error_dialog(msg, _("Gadu-Gadu Error")); | |
878 g_free(srch->search_data); | |
879 g_free(srch); | |
880 return; | |
881 } | |
882 } | |
883 | |
884 static void agg_do_action(struct gaim_connection *gc, char *action) | |
885 { | |
886 if (!strcmp(action, _("Directory Search"))) { | |
887 show_find_info(gc); | |
888 } | |
889 } | |
890 | |
891 static GList *agg_actions() | |
892 { | |
893 GList *m = NULL; | |
894 | |
895 m = g_list_append(m, _("Directory Search")); | |
896 | |
897 return m; | |
898 } | |
899 | |
900 static void agg_get_info(struct gaim_connection *gc, char *who) | |
901 { | |
902 struct agg_search *srch = g_new0(struct agg_search, 1); | |
903 static char msg[AGG_BUF_LEN]; | |
904 | |
905 srch->gc = gc; | |
906 | |
907 /* If it's invalid uin then maybe it's nickname? */ | |
908 if (invalid_uin(who)) { | |
909 gchar *new_who = charset_convert(who, find_local_charset(), "CP1250"); | |
910 | |
911 srch->search_data = g_strdup_printf("Mode=0&FirstName=%s&LastName=%s&Gender=%d" | |
912 "&NickName=%s&City=%s&MinBirth=%d&MaxBirth=%d", | |
913 "", "", AGG_GENDER_NONE, new_who, "", 0, 0); | |
914 | |
915 g_free(new_who); | |
916 } else | |
917 srch->search_data = g_strdup_printf("Mode=3&UserId=%s", who); | |
918 | |
919 if (proxy_connect(GG_PUBDIR_HOST, GG_PUBDIR_PORT, search_callback, srch) < 0) { | |
920 g_snprintf(msg, sizeof(msg), _("Connect to search service failed (%s)"), GG_PUBDIR_HOST); | |
921 do_error_dialog(msg, _("Gadu-Gadu Error")); | |
922 g_free(srch->search_data); | |
923 g_free(srch); | |
924 return; | |
925 } | |
926 } | |
927 | |
928 static char **agg_list_icon(int uc) | |
929 { | |
930 guint status; | |
931 if (uc == UC_UNAVAILABLE) | |
932 return (char **)gg_sunred_xpm; | |
933 status = uc >> 5; | |
934 /* Drop all masks */ | |
935 status &= ~(GG_STATUS_FRIENDS_MASK); | |
936 if (status == GG_STATUS_AVAIL) | |
937 return (char **)gg_sunyellow_xpm; | |
938 if (status == GG_STATUS_BUSY) | |
939 return (char **)gg_suncloud_xpm; | |
940 if (status == GG_STATUS_INVISIBLE) | |
941 return (char **)gg_sunwhitered_xpm; | |
942 return (char **)gg_sunyellow_xpm; | |
943 } | |
944 | |
945 static void agg_set_permit_deny_dummy(struct gaim_connection *gc) | |
946 { | |
947 /* It's implemented on client side because GG server doesn't support this */ | |
948 } | |
949 | |
950 static void agg_permit_deny_dummy(struct gaim_connection *gc, char *who) | |
951 { | |
952 /* It's implemented on client side because GG server doesn't support this */ | |
953 } | |
954 | |
955 static struct prpl *my_protocol = NULL; | |
956 | |
2423
ce09589b7681
[gaim-migrate @ 2436]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
957 void gg_init(struct prpl *ret) |
2393 | 958 { |
959 ret->protocol = PROTO_GADUGADU; | |
960 ret->options = 0; | |
961 ret->name = agg_name; | |
962 ret->list_icon = agg_list_icon; | |
963 ret->away_states = agg_away_states; | |
964 ret->actions = agg_actions; | |
965 ret->do_action = agg_do_action; | |
966 ret->user_opts = agg_user_opts; | |
967 ret->buddy_menu = agg_buddy_menu; | |
968 ret->chat_info = NULL; | |
969 ret->login = agg_login; | |
970 ret->close = agg_close; | |
971 ret->send_im = agg_send_im; | |
972 ret->set_info = NULL; | |
973 ret->get_info = agg_get_info; | |
974 ret->set_away = agg_set_away; | |
975 ret->set_dir = NULL; | |
976 ret->get_dir = agg_get_info; | |
977 ret->dir_search = agg_dir_search; | |
978 ret->set_idle = NULL; | |
979 ret->change_passwd = NULL; | |
980 ret->add_buddy = agg_add_buddy; | |
981 ret->add_buddies = agg_add_buddies; | |
982 ret->remove_buddy = agg_rem_buddy; | |
983 ret->add_permit = agg_permit_deny_dummy; | |
984 ret->add_deny = agg_permit_deny_dummy; | |
985 ret->rem_permit = agg_permit_deny_dummy; | |
986 ret->rem_deny = agg_permit_deny_dummy; | |
987 ret->set_permit_deny = agg_set_permit_deny_dummy; | |
988 ret->warn = NULL; | |
989 ret->join_chat = NULL; | |
990 ret->chat_invite = NULL; | |
991 ret->chat_leave = NULL; | |
992 ret->chat_whisper = NULL; | |
993 ret->chat_send = NULL; | |
994 ret->keepalive = agg_keepalive; | |
995 ret->normalize = NULL; | |
996 my_protocol = ret; | |
997 } | |
998 | |
999 #ifndef STATIC | |
1000 | |
1001 char *gaim_plugin_init(GModule *handle) | |
1002 { | |
2423
ce09589b7681
[gaim-migrate @ 2436]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2393
diff
changeset
|
1003 load_protocol(gg_init, sizeof(struct prpl)); |
2393 | 1004 return NULL; |
1005 } | |
1006 | |
1007 void gaim_plugin_remove() | |
1008 { | |
1009 struct prpl *p = find_prpl(PROTO_GADUGADU); | |
1010 if (p == my_protocol) | |
1011 unload_protocol(p); | |
1012 } | |
1013 | |
1014 char *name() | |
1015 { | |
1016 return "Gadu-Gadu"; | |
1017 } | |
1018 | |
1019 char *description() | |
1020 { | |
1021 return PRPL_DESC("Gadu-Gadu"); | |
1022 } | |
1023 | |
1024 #endif |