Mercurial > pidgin.yaz
annotate src/protocols/zephyr/zephyr.c @ 2170:c24595d3c364
[gaim-migrate @ 2180]
buddy_menu
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Mon, 27 Aug 2001 07:11:50 +0000 |
parents | edf8c5a70e5b |
children | cff4fbe01c7b |
rev | line source |
---|---|
2086 | 1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ |
2 /* | |
3 * gaim | |
4 * | |
5 * Copyright (C) 1998-2001, Mark Spencer <markster@marko.net> | |
6 * Some code borrowed from GtkZephyr, by | |
7 * Jag/Sean Dilda <agrajag@linuxpower.org>/<smdilda@unity.ncsu.edu> | |
8 * http://gtkzephyr.linuxpower.org/ | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 * | |
24 */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 #include "config.h" | |
28 #endif | |
29 | |
30 #include <gtk/gtk.h> | |
31 #include <string.h> | |
32 #include <stdlib.h> | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
33 #include <errno.h> |
2086 | 34 #include "gaim.h" |
35 #include "prpl.h" | |
36 #include "zephyr/zephyr.h" | |
37 | |
38 extern Code_t ZGetLocations(ZLocations_t *, int *); | |
39 extern Code_t ZSetLocation(char *); | |
40 extern Code_t ZUnsetLocation(); | |
41 | |
42 typedef struct _zframe zframe; | |
43 typedef struct _zephyr_triple zephyr_triple; | |
44 | |
45 /* struct I need for zephyr_to_html */ | |
46 struct _zframe { | |
47 /* true for everything but @color, since inside the parens of that one is | |
48 * the color. */ | |
49 gboolean has_closer; | |
50 /* </i>, </font>, </b>, etc. */ | |
51 char *closing; | |
52 /* text including the opening html thingie. */ | |
53 GString *text; | |
54 struct _zframe *enclosing; | |
55 }; | |
56 | |
57 struct _zephyr_triple { | |
58 char *class; | |
59 char *instance; | |
60 char *recipient; | |
61 char *name; | |
62 gboolean open; | |
63 int id; | |
64 }; | |
65 | |
66 static char *zephyr_name() | |
67 { | |
68 return "Zephyr"; | |
69 } | |
70 | |
71 #define z_call(func) if (func != ZERR_NONE)\ | |
72 return; | |
73 #define z_call_r(func) if (func != ZERR_NONE)\ | |
74 return TRUE; | |
75 #define z_call_s(func, err) if (func != ZERR_NONE) {\ | |
76 hide_login_progress(zgc, err);\ | |
77 signoff(zgc);\ | |
78 return;\ | |
79 } | |
80 | |
81 static char *zephyr_normalize(const char *); | |
82 | |
83 /* this is so bad, and if Zephyr weren't so fucked up to begin with I | |
84 * wouldn't do this. but it is so i will. */ | |
85 static guint32 nottimer = 0; | |
86 static guint32 loctimer = 0; | |
87 struct gaim_connection *zgc = NULL; | |
88 static GList *pending_zloc_names = NULL; | |
89 static GSList *subscrips = NULL; | |
90 static int last_id = 0; | |
91 static GtkWidget *class_entry; | |
92 static GtkWidget *inst_entry; | |
93 static GtkWidget *recip_entry; | |
94 | |
95 /* just for debugging | |
96 static void handle_unknown(ZNotice_t notice) | |
97 { | |
98 g_print("z_packet: %s\n", notice.z_packet); | |
99 g_print("z_version: %s\n", notice.z_version); | |
100 g_print("z_kind: %d\n", notice.z_kind); | |
101 g_print("z_class: %s\n", notice.z_class); | |
102 g_print("z_class_inst: %s\n", notice.z_class_inst); | |
103 g_print("z_opcode: %s\n", notice.z_opcode); | |
104 g_print("z_sender: %s\n", notice.z_sender); | |
105 g_print("z_recipient: %s\n", notice.z_recipient); | |
106 g_print("z_message: %s\n", notice.z_message); | |
107 g_print("z_message_len: %d\n", notice.z_message_len); | |
108 g_print("\n"); | |
109 } | |
110 */ | |
111 | |
112 static zephyr_triple *new_triple(const char *c, const char *i, const char *r) | |
113 { | |
114 zephyr_triple *zt; | |
115 zt = g_new0(zephyr_triple, 1); | |
116 zt->class = g_strdup(c); | |
117 zt->instance = g_strdup(i); | |
118 zt->recipient = g_strdup(r); | |
119 zt->name = g_strdup_printf("%s,%s,%s", c, i, r); | |
120 zt->id = ++last_id; | |
121 zt->open = FALSE; | |
122 return zt; | |
123 } | |
124 | |
125 static void free_triple(zephyr_triple *zt) | |
126 { | |
127 g_free(zt->class); | |
128 g_free(zt->instance); | |
129 g_free(zt->recipient); | |
130 g_free(zt->name); | |
131 g_free(zt); | |
132 } | |
133 | |
134 /* returns true if zt1 is a subset of zt2, i.e. zt2 has the same thing or | |
135 * wildcards in each field of zt1. */ | |
136 static gboolean triple_subset(zephyr_triple *zt1, zephyr_triple *zt2) | |
137 { | |
138 if (g_strcasecmp(zt2->class, zt1->class) && | |
139 g_strcasecmp(zt2->class, "*")) { | |
140 return FALSE; | |
141 } | |
142 if (g_strcasecmp(zt2->instance, zt1->instance) && | |
143 g_strcasecmp(zt2->instance, "*")) { | |
144 return FALSE; | |
145 } | |
146 if (g_strcasecmp(zt2->recipient, zt1->recipient) && | |
147 g_strcasecmp(zt2->recipient, "*")) { | |
148 return FALSE; | |
149 } | |
150 return TRUE; | |
151 } | |
152 | |
153 static zephyr_triple *find_sub_by_triple(zephyr_triple *zt) | |
154 { | |
155 zephyr_triple *curr_t; | |
156 GSList *curr = subscrips; | |
157 while (curr) { | |
158 curr_t = curr->data; | |
159 if (triple_subset(zt, curr_t)) | |
160 return curr_t; | |
161 curr = curr->next; | |
162 } | |
163 return NULL; | |
164 } | |
165 | |
166 static zephyr_triple *find_sub_by_id(int id) | |
167 { | |
168 zephyr_triple *zt; | |
169 GSList *curr = subscrips; | |
170 while (curr) { | |
171 zt = curr->data; | |
172 if (zt->id == id) | |
173 return zt; | |
174 curr = curr->next; | |
175 } | |
176 return NULL; | |
177 } | |
178 | |
179 /* utility macros that are useful for zephyr_to_html */ | |
180 | |
181 #define IS_OPENER(c) ((c == '{') || (c == '[') || (c == '(') || (c == '<')) | |
182 #define IS_CLOSER(c) ((c == '}') || (c == ']') || (c == ')') || (c == '>')) | |
183 | |
184 /* this parses zephyr formatting and converts it to html. For example, if | |
185 * you pass in "@{@color(blue)@i(hello)}" you should get out | |
186 * "<font color=blue><i>hello</i></font>". */ | |
187 static char *zephyr_to_html(char *message) | |
188 { | |
189 int len, cnt; | |
190 zframe *frames, *curr; | |
191 char *ret; | |
192 | |
193 frames = g_new(zframe, 1); | |
194 frames->text = g_string_new(""); | |
195 frames->enclosing = NULL; | |
196 frames->closing = ""; | |
197 frames->has_closer = FALSE; | |
198 | |
199 len = strlen(message); | |
200 cnt = 0; | |
201 while (cnt <= len) { | |
202 if (message[cnt] == '@') { | |
203 zframe *new_f; | |
204 char *buf; | |
205 int end; | |
206 for (end=1; (cnt+end) <= len && | |
207 !IS_OPENER(message[cnt+end]); end++); | |
208 buf = g_new0(char, end); | |
209 if (end) { | |
210 g_snprintf(buf, end, "%s", message+cnt+1); | |
211 } | |
212 if (!g_strcasecmp(buf, "italic") || | |
213 !g_strcasecmp(buf, "i")) { | |
214 new_f = g_new(zframe, 1); | |
215 new_f->enclosing = frames; | |
216 new_f->text = g_string_new("<i>"); | |
217 new_f->closing = "</i>"; | |
218 new_f->has_closer = TRUE; | |
219 frames = new_f; | |
220 cnt += end+1; /* cnt points to char after opener */ | |
221 } else if (!g_strcasecmp(buf, "bold") | |
222 || !g_strcasecmp(buf, "b")) { | |
223 new_f = g_new(zframe, 1); | |
224 new_f->enclosing = frames; | |
225 new_f->text = g_string_new("<b>"); | |
226 new_f->closing = "</b>"; | |
227 new_f->has_closer = TRUE; | |
228 frames = new_f; | |
229 cnt += end+1; | |
230 } else if (!g_strcasecmp(buf, "color")) { | |
231 cnt += end+1; | |
232 new_f = g_new(zframe, 1); | |
233 new_f->enclosing = frames; | |
234 new_f->text = g_string_new("<font color="); | |
235 for (; (cnt <= len) && !IS_CLOSER(message[cnt]); cnt++) { | |
236 g_string_append_c(new_f->text, message[cnt]); | |
237 } | |
238 cnt++; /* point to char after closer */ | |
239 g_string_append_c(new_f->text, '>'); | |
240 new_f->closing = "</font>"; | |
241 new_f->has_closer = FALSE; | |
242 frames = new_f; | |
243 } else if (!g_strcasecmp(buf, "")) { | |
244 new_f = g_new(zframe, 1); | |
245 new_f->enclosing = frames; | |
246 new_f->text = g_string_new(""); | |
247 new_f->closing = ""; | |
248 new_f->has_closer = TRUE; | |
249 frames = new_f; | |
250 cnt += end+1; /* cnt points to char after opener */ | |
251 } else { | |
252 if ((cnt+end) > len) { | |
253 g_string_append_c(frames->text, '@'); | |
254 cnt++; | |
255 } else { | |
256 /* unrecognized thingie. act like it's not there, but we | |
257 * still need to take care of the corresponding closer, | |
258 * make a frame that does nothing. */ | |
259 new_f = g_new(zframe, 1); | |
260 new_f->enclosing = frames; | |
261 new_f->text = g_string_new(""); | |
262 new_f->closing = ""; | |
263 new_f->has_closer = TRUE; | |
264 frames = new_f; | |
265 cnt += end+1; /* cnt points to char after opener */ | |
266 } | |
267 } | |
268 } else if (IS_CLOSER(message[cnt])) { | |
269 zframe *popped; | |
270 gboolean last_had_closer; | |
271 if (frames->enclosing) { | |
272 do { | |
273 popped = frames; | |
274 frames = frames->enclosing; | |
275 g_string_append(frames->text, popped->text->str); | |
276 g_string_append(frames->text, popped->closing); | |
277 g_string_free(popped->text, TRUE); | |
278 last_had_closer = popped->has_closer; | |
279 g_free(popped); | |
280 } while (frames && frames->enclosing && !last_had_closer); | |
281 } else { | |
282 g_string_append_c(frames->text, message[cnt]); | |
283 } | |
284 cnt++; | |
285 } else if (message[cnt] == '\n') { | |
286 g_string_append(frames->text, "<br>"); | |
287 cnt++; | |
288 } else { | |
289 g_string_append_c(frames->text, message[cnt++]); | |
290 } | |
291 } | |
292 /* go through all the stuff that they didn't close */ | |
293 while (frames->enclosing) { | |
294 curr = frames; | |
295 g_string_append(frames->enclosing->text, frames->text->str); | |
296 g_string_append(frames->enclosing->text, frames->closing); | |
297 g_string_free(frames->text, TRUE); | |
298 frames = frames->enclosing; | |
299 g_free(curr); | |
300 } | |
301 ret = frames->text->str; | |
302 g_string_free(frames->text, FALSE); | |
303 g_free(frames); | |
304 return ret; | |
305 } | |
306 | |
307 static gboolean pending_zloc(char *who) | |
308 { | |
309 GList *curr; | |
310 for (curr = pending_zloc_names; curr != NULL; curr = curr->next) { | |
311 if (!g_strcasecmp(who, (char*)curr->data)) { | |
312 g_free((char*)curr->data); | |
313 pending_zloc_names = g_list_remove(pending_zloc_names, curr->data); | |
314 return TRUE; | |
315 } | |
316 } | |
317 return FALSE; | |
318 } | |
319 | |
320 static void handle_message(ZNotice_t notice, struct sockaddr_in from) | |
321 { | |
322 if (!g_strcasecmp(notice.z_class, LOGIN_CLASS)) { | |
323 /* well, we'll be updating in 2 seconds anyway, might as well ignore this. */ | |
324 } else if (!g_strcasecmp(notice.z_class, LOCATE_CLASS)) { | |
325 if (!g_strcasecmp(notice.z_opcode, LOCATE_LOCATE)) { | |
326 int nlocs; | |
327 char *user; | |
328 struct buddy *b; | |
329 | |
330 if (ZParseLocations(¬ice, NULL, &nlocs, &user) != ZERR_NONE) | |
331 return; | |
332 if ((b = find_buddy(zgc, user)) == NULL) { | |
333 char *e = strchr(user, '@'); | |
334 if (e) *e = '\0'; | |
335 b = find_buddy(zgc, user); | |
336 } | |
337 if (!b) { | |
338 free(user); | |
339 return; | |
340 } | |
341 if (pending_zloc(b->name)) { | |
342 ZLocations_t locs; | |
343 int one = 1; | |
344 GString *str = g_string_new(""); | |
345 g_string_sprintfa(str, "<b>User:</b> %s<br>" | |
346 "<b>Alias:</b> %s<br>", | |
347 b->name, b->show); | |
348 if (!nlocs) { | |
349 g_string_sprintfa(str, "<br>Hidden or not logged-in"); | |
350 } | |
351 for (; nlocs > 0; nlocs--) { | |
352 ZGetLocations(&locs, &one); | |
353 g_string_sprintfa(str, "<br>At %s since %s", locs.host, | |
354 locs.time); | |
355 } | |
2137
18722ae5b882
[gaim-migrate @ 2147]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2131
diff
changeset
|
356 g_show_info_text(str->str, NULL); |
2086 | 357 g_string_free(str, TRUE); |
358 } else | |
359 serv_got_update(zgc, b->name, nlocs, 0, 0, 0, 0, 0); | |
360 | |
361 free(user); | |
362 } | |
363 } else { | |
364 char *buf, *buf2; | |
365 char *ptr = notice.z_message + strlen(notice.z_message) + 1; | |
366 int len = notice.z_message_len - (ptr - notice.z_message); | |
367 int away; | |
368 if (len > 0) { | |
369 buf = g_malloc(len + 1); | |
370 g_snprintf(buf, len + 1, "%s", ptr); | |
371 g_strchomp(buf); | |
372 buf2 = zephyr_to_html(buf); | |
373 g_free(buf); | |
374 if (!g_strcasecmp(notice.z_class, "MESSAGE") && | |
375 !g_strcasecmp(notice.z_class_inst, "PERSONAL")) { | |
376 if (!g_strcasecmp(notice.z_message, "Automated reply:")) | |
377 away = TRUE; | |
378 else | |
379 away = FALSE; | |
380 len = MAX(BUF_LONG, strlen(buf2)); | |
381 buf = g_malloc(len + 1); | |
382 g_snprintf(buf, len + 1, "%s", buf2); | |
383 serv_got_im(zgc, notice.z_sender, buf, 0, time((time_t)NULL)); | |
384 g_free(buf); | |
385 } else { | |
386 zephyr_triple *zt1, *zt2; | |
387 zt1 = new_triple(notice.z_class, notice.z_class_inst, | |
388 notice.z_recipient); | |
389 zt2 = find_sub_by_triple(zt1); | |
390 if (!zt2) { | |
391 /* we shouldn't be subscribed to this message. ignore. */ | |
392 } else { | |
393 len = MAX(BUF_LONG, strlen(buf2)); | |
394 buf = g_malloc(len + 1); | |
395 g_snprintf(buf, len + 1, "%s", buf2); | |
396 if (!zt2->open) { | |
397 zt2->open = TRUE; | |
398 serv_got_joined_chat(zgc, zt2->id, zt2->name); | |
399 } | |
400 serv_got_chat_in(zgc, zt2->id, notice.z_sender, FALSE, | |
401 buf, time((time_t)NULL)); | |
402 g_free(buf); | |
403 } | |
404 free_triple(zt1); | |
405 } | |
406 g_free(buf2); | |
407 } | |
408 } | |
409 } | |
410 | |
411 static gint check_notify(gpointer data) | |
412 { | |
413 while (ZPending()) { | |
414 ZNotice_t notice; | |
415 struct sockaddr_in from; | |
416 z_call_r(ZReceiveNotice(¬ice, &from)); | |
417 | |
418 switch (notice.z_kind) { | |
419 case UNSAFE: | |
420 case UNACKED: | |
421 case ACKED: | |
422 handle_message(notice, from); | |
423 break; | |
424 default: | |
425 /* we'll just ignore things for now */ | |
426 debug_printf("ZEPHYR: Unhandled Notice\n"); | |
427 break; | |
428 } | |
429 | |
430 ZFreeNotice(¬ice); | |
431 } | |
432 | |
433 return TRUE; | |
434 } | |
435 | |
436 static gint check_loc(gpointer data) | |
437 { | |
438 GSList *gr, *m; | |
439 ZAsyncLocateData_t ald; | |
440 | |
441 ald.user = NULL; | |
442 memset(&(ald.uid), 0, sizeof(ZUnique_Id_t)); | |
443 ald.version = NULL; | |
444 | |
445 gr = zgc->groups; | |
446 while (gr) { | |
447 struct group *g = gr->data; | |
448 m = g->members; | |
449 while (m) { | |
450 struct buddy *b = m->data; | |
451 char *chk; | |
452 chk = zephyr_normalize(b->name); | |
453 /* doesn't matter if this fails or not; we'll just move on to the next one */ | |
454 ZRequestLocations(chk, &ald, UNACKED, ZAUTH); | |
455 free(ald.user); | |
456 free(ald.version); | |
457 m = m->next; | |
458 } | |
459 gr = gr->next; | |
460 } | |
461 | |
462 return TRUE; | |
463 } | |
464 | |
465 static char *get_exposure_level() | |
466 { | |
467 char *exposure = ZGetVariable("exposure"); | |
468 | |
469 if (!exposure) | |
470 return EXPOSE_REALMVIS; | |
471 if (!g_strcasecmp(exposure, EXPOSE_NONE)) | |
472 return EXPOSE_NONE; | |
473 if (!g_strcasecmp(exposure, EXPOSE_OPSTAFF)) | |
474 return EXPOSE_OPSTAFF; | |
475 if (!g_strcasecmp(exposure, EXPOSE_REALMANN)) | |
476 return EXPOSE_REALMANN; | |
477 if (!g_strcasecmp(exposure, EXPOSE_NETVIS)) | |
478 return EXPOSE_NETVIS; | |
479 if (!g_strcasecmp(exposure, EXPOSE_NETANN)) | |
480 return EXPOSE_NETANN; | |
481 return EXPOSE_REALMVIS; | |
482 } | |
483 | |
484 static void strip_comments(char *str) | |
485 { | |
486 char *tmp = strchr(str, '#'); | |
487 if (tmp) | |
488 *tmp = '\0'; | |
489 g_strchug(str); | |
490 g_strchomp(str); | |
491 } | |
492 | |
493 static void process_zsubs() | |
494 { | |
495 FILE *f; | |
496 gchar *fname; | |
497 gchar buff[BUFSIZ]; | |
498 | |
499 fname = g_strdup_printf("%s/.zephyr.subs", g_getenv("HOME")); | |
500 f = fopen(fname, "r"); | |
501 if (f) { | |
502 char **triple; | |
503 ZSubscription_t sub; | |
504 char *recip; | |
505 while (fgets(buff, BUFSIZ, f)) { | |
506 strip_comments(buff); | |
507 if (buff[0]) { | |
508 triple = g_strsplit(buff, ",", 3); | |
509 if (triple[0] && triple[1] && triple[2]) { | |
510 sub.zsub_class = triple[0]; | |
511 sub.zsub_classinst = triple[1]; | |
512 if (!g_strcasecmp(triple[2], "%me%")) { | |
513 recip = g_strdup_printf("%s@%s", g_getenv("USER"), | |
514 ZGetRealm()); | |
515 } else if (!g_strcasecmp(triple[2], "*")) { | |
516 /* wildcard */ | |
517 recip = g_strdup_printf("@%s", ZGetRealm()); | |
518 } else { | |
519 recip = g_strdup(triple[2]); | |
520 } | |
521 sub.zsub_recipient = recip; | |
522 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) { | |
523 debug_printf("Zephyr: Couldn't subscribe to %s, %s, " | |
524 "%s\n", | |
525 sub.zsub_class, | |
526 sub.zsub_classinst, | |
527 sub.zsub_recipient); | |
528 } | |
529 subscrips = g_slist_append(subscrips, | |
530 new_triple(triple[0], triple[1], recip)); | |
531 g_free(recip); | |
532 } | |
533 g_strfreev(triple); | |
534 } | |
535 } | |
536 } | |
537 } | |
538 | |
539 static void process_anyone() | |
540 { | |
541 FILE *fd; | |
542 gchar buff[BUFSIZ], *filename; | |
543 | |
544 filename = g_strconcat(g_get_home_dir(), "/.anyone", NULL); | |
545 if ((fd = fopen(filename, "r")) != NULL) { | |
546 while (fgets(buff, BUFSIZ, fd)) { | |
547 strip_comments(buff); | |
548 if (buff[0]) | |
549 add_buddy(zgc, "Anyone", buff, buff); | |
550 } | |
551 fclose(fd); | |
552 } | |
553 g_free(filename); | |
554 } | |
555 | |
556 static void zephyr_login(struct aim_user *user) | |
557 { | |
558 ZSubscription_t sub; | |
559 | |
560 if (zgc) { | |
561 do_error_dialog("Already logged in with Zephyr", "Zephyr"); | |
562 return; | |
563 } | |
564 | |
565 zgc = new_gaim_conn(user); | |
566 | |
567 z_call_s(ZInitialize(), "Couldn't initialize zephyr"); | |
568 z_call_s(ZOpenPort(NULL), "Couldn't open port"); | |
569 z_call_s(ZSetLocation(get_exposure_level()), "Couldn't set location"); | |
570 | |
571 sub.zsub_class = "MESSAGE"; | |
572 sub.zsub_classinst = "PERSONAL"; | |
573 sub.zsub_recipient = ZGetSender(); | |
574 | |
575 /* we don't care if this fails. i'm lying right now. */ | |
576 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) { | |
577 debug_printf("Zephyr: Couldn't subscribe to messages!\n"); | |
578 } | |
579 | |
580 account_online(zgc); | |
581 serv_finish_login(zgc); | |
582 | |
583 if (bud_list_cache_exists(zgc)) | |
584 do_import(NULL, zgc); | |
585 process_anyone(); | |
586 process_zsubs(); | |
587 | |
2131
acc11216ec5d
[gaim-migrate @ 2141]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2123
diff
changeset
|
588 nottimer = g_timeout_add(100, check_notify, NULL); |
acc11216ec5d
[gaim-migrate @ 2141]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2123
diff
changeset
|
589 loctimer = g_timeout_add(2000, check_loc, NULL); |
2086 | 590 } |
591 | |
592 static void write_zsubs() | |
593 { | |
594 GSList *s = subscrips; | |
595 zephyr_triple *zt; | |
596 FILE *fd; | |
597 char *fname; | |
598 | |
599 fname = g_strdup_printf("%s/.zephyr.subs", g_get_home_dir()); | |
600 fd = fopen(fname, "w"); | |
601 | |
602 if (!fd) { | |
603 g_free(fname); | |
604 return; | |
605 } | |
606 | |
607 while (s) { | |
608 zt = s->data; | |
609 fprintf(fd, "%s\n", zt->name); | |
610 s = s->next; | |
611 } | |
612 g_free(fname); | |
613 fclose(fd); | |
614 } | |
615 | |
616 static void write_anyone() | |
617 { | |
618 GSList *gr, *m; | |
619 struct group *g; | |
620 struct buddy *b; | |
621 char *ptr, *fname; | |
622 FILE *fd; | |
623 | |
624 fname = g_strdup_printf("%s/.anyone", g_get_home_dir()); | |
625 fd = fopen(fname, "w"); | |
626 if (!fd) { | |
627 g_free(fname); | |
628 return; | |
629 } | |
630 | |
631 gr = zgc->groups; | |
632 while (gr) { | |
633 g = gr->data; | |
634 m = g->members; | |
635 while (m) { | |
636 b = m->data; | |
637 if ((ptr = strchr(b->name, '@')) != NULL) | |
638 *ptr = '\0'; | |
639 fprintf(fd, "%s\n", b->name); | |
640 if (ptr) | |
641 *ptr = '@'; | |
642 m = m->next; | |
643 } | |
644 gr = gr->next; | |
645 } | |
646 | |
647 fclose(fd); | |
648 g_free(fname); | |
649 } | |
650 | |
651 static void zephyr_close(struct gaim_connection *gc) | |
652 { | |
653 GList *l; | |
654 GSList *s; | |
655 l = pending_zloc_names; | |
656 while (l) { | |
657 g_free((char*)l->data); | |
658 l = l->next; | |
659 } | |
660 g_list_free(pending_zloc_names); | |
661 | |
662 write_anyone(); | |
663 write_zsubs(); | |
664 | |
665 s = subscrips; | |
666 while (s) { | |
667 free_triple((zephyr_triple*)s->data); | |
668 s = s->next; | |
669 } | |
670 g_slist_free(subscrips); | |
671 | |
672 if (nottimer) | |
2131
acc11216ec5d
[gaim-migrate @ 2141]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2123
diff
changeset
|
673 g_source_remove(nottimer); |
2086 | 674 nottimer = 0; |
675 if (loctimer) | |
2131
acc11216ec5d
[gaim-migrate @ 2141]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2123
diff
changeset
|
676 g_source_remove(loctimer); |
2086 | 677 loctimer = 0; |
678 zgc = NULL; | |
679 z_call(ZCancelSubscriptions(0)); | |
680 z_call(ZUnsetLocation()); | |
681 z_call(ZClosePort()); | |
682 } | |
683 | |
684 static void zephyr_add_buddy(struct gaim_connection *gc, char *buddy) { } | |
685 static void zephyr_remove_buddy(struct gaim_connection *gc, char *buddy) { } | |
686 | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
687 static int zephyr_chat_send(struct gaim_connection *gc, int id, char *im) |
2086 | 688 { |
689 ZNotice_t notice; | |
690 zephyr_triple *zt; | |
691 char *buf; | |
692 const char *sig; | |
693 | |
694 zt = find_sub_by_id(id); | |
695 if (!zt) | |
696 /* this should never happen. */ | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
697 return -EINVAL; |
2086 | 698 |
699 sig = ZGetVariable("zwrite-signature"); | |
700 if (!sig) { | |
701 sig = g_get_real_name(); | |
702 } | |
703 buf = g_strdup_printf("%s%c%s", sig, '\0', im); | |
704 | |
705 bzero((char *)¬ice, sizeof(notice)); | |
706 notice.z_kind = ACKED; | |
707 notice.z_port = 0; | |
708 notice.z_opcode = ""; | |
709 notice.z_class = zt->class; | |
710 notice.z_class_inst = zt->instance; | |
711 if (!g_strcasecmp(zt->recipient, "*")) | |
712 notice.z_recipient = zephyr_normalize(""); | |
713 else | |
714 notice.z_recipient = zephyr_normalize(zt->recipient); | |
715 notice.z_sender = 0; | |
716 notice.z_default_format = | |
717 "Class $class, Instance $instance:\n" | |
718 "To: @bold($recipient) at $time $date\n" | |
719 "From: @bold($1) <$sender>\n\n$2"; | |
720 notice.z_message_len = strlen(im) + strlen(sig) + 4; | |
721 notice.z_message = buf; | |
722 ZSendNotice(¬ice, ZAUTH); | |
723 g_free(buf); | |
2167
edf8c5a70e5b
[gaim-migrate @ 2177]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2162
diff
changeset
|
724 return 0; |
2086 | 725 } |
726 | |
2123
56c4382f2909
[gaim-migrate @ 2133]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2100
diff
changeset
|
727 static int zephyr_send_im(struct gaim_connection *gc, char *who, char *im, int away) { |
2086 | 728 ZNotice_t notice; |
729 char *buf; | |
730 const char *sig; | |
731 | |
732 if (away) | |
733 sig = "Automated reply:"; | |
734 else { | |
735 sig = ZGetVariable("zwrite-signature"); | |
736 if (!sig) { | |
737 sig = g_get_real_name(); | |
738 } | |
739 } | |
740 buf = g_strdup_printf("%s%c%s", sig, '\0', im); | |
741 | |
742 bzero((char *)¬ice, sizeof(notice)); | |
743 notice.z_kind = ACKED; | |
744 notice.z_port = 0; | |
745 notice.z_opcode = ""; | |
746 notice.z_class = "MESSAGE"; | |
747 notice.z_class_inst = "PERSONAL"; | |
748 notice.z_sender = 0; | |
749 notice.z_recipient = who; | |
750 notice.z_default_format = | |
751 "Class $class, Instance $instance:\n" | |
752 "To: @bold($recipient) at $time $date\n" | |
753 "From: @bold($1) <$sender>\n\n$2"; | |
754 notice.z_message_len = strlen(im) + strlen(sig) + 4; | |
755 notice.z_message = buf; | |
756 ZSendNotice(¬ice, ZAUTH); | |
757 g_free(buf); | |
2123
56c4382f2909
[gaim-migrate @ 2133]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2100
diff
changeset
|
758 return 0; |
2086 | 759 } |
760 | |
761 static char *zephyr_normalize(const char *orig) | |
762 { | |
763 static char buf[80]; | |
764 if (strchr(orig, '@')) { | |
765 g_snprintf(buf, 80, "%s", orig); | |
766 } else { | |
767 g_snprintf(buf, 80, "%s@%s", orig, ZGetRealm()); | |
768 } | |
769 return buf; | |
770 } | |
771 | |
772 static void zephyr_zloc(struct gaim_connection *gc, char *who) | |
773 { | |
774 ZAsyncLocateData_t ald; | |
775 | |
776 if (ZRequestLocations(zephyr_normalize(who), &ald, UNACKED, ZAUTH) | |
777 != ZERR_NONE) { | |
778 return; | |
779 } | |
780 pending_zloc_names = g_list_append(pending_zloc_names, | |
781 g_strdup(zephyr_normalize(who))); | |
782 } | |
783 | |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
784 static GList *zephyr_buddy_menu(struct gaim_connection *gc, char *who) |
2086 | 785 { |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
786 GList *m = NULL; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
787 struct proto_buddy_menu *pbm; |
2086 | 788 |
2170
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
789 pbm = g_new0(struct proto_buddy_menu, 1); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
790 pbm->label = _("ZLocate"); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
791 pbm->callback = zephyr_zloc; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
792 pbm->gc = gc; |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
793 m = g_list_append(m, pbm); |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
794 |
c24595d3c364
[gaim-migrate @ 2180]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2167
diff
changeset
|
795 return m; |
2086 | 796 } |
797 | |
798 static void zephyr_set_away(struct gaim_connection *gc, char *state, char *msg) | |
799 { | |
800 if (gc->away) | |
801 g_free(gc->away); | |
802 gc->away = NULL; | |
803 if (!g_strcasecmp(state, "Hidden")) | |
804 ZSetLocation(EXPOSE_OPSTAFF); | |
805 else if (!g_strcasecmp(state, "Online")) | |
806 ZSetLocation(get_exposure_level()); | |
807 else /* state is GAIM_AWAY_CUSTOM */ if (msg) | |
808 gc->away = g_strdup(msg); | |
809 } | |
810 | |
811 static GList *zephyr_away_states() | |
812 { | |
813 GList *m = NULL; | |
814 | |
815 m = g_list_append(m, "Online"); | |
816 m = g_list_append(m, GAIM_AWAY_CUSTOM); | |
817 m = g_list_append(m, "Hidden"); | |
818 | |
819 return m; | |
820 } | |
821 | |
822 static void zephyr_draw_jc(struct gaim_connection *gc, GtkWidget *vbox) { | |
823 GtkWidget *label; | |
824 GtkWidget *rowbox; | |
825 | |
826 rowbox = gtk_hbox_new(FALSE, 5); | |
827 gtk_box_pack_start(GTK_BOX(vbox), rowbox, FALSE, FALSE, 0); | |
828 | |
829 label = gtk_label_new(_("Class:")); | |
830 gtk_box_pack_start(GTK_BOX(rowbox), label, FALSE, FALSE, 5); | |
831 gtk_widget_show(label); | |
832 | |
833 class_entry = gtk_entry_new(); | |
834 gtk_box_pack_end(GTK_BOX(rowbox), class_entry, FALSE, FALSE, 5); | |
835 gtk_widget_show(class_entry); | |
836 | |
837 gtk_widget_show(rowbox); | |
838 | |
839 rowbox = gtk_hbox_new(FALSE, 5); | |
840 gtk_box_pack_start(GTK_BOX(vbox), rowbox, FALSE, FALSE, 0); | |
841 | |
842 label = gtk_label_new(_("Instance:")); | |
843 gtk_box_pack_start(GTK_BOX(rowbox), label, FALSE, FALSE, 5); | |
844 gtk_widget_show(label); | |
845 | |
846 inst_entry = gtk_entry_new(); | |
847 gtk_box_pack_end(GTK_BOX(rowbox), inst_entry, FALSE, FALSE, 5); | |
848 gtk_widget_show(inst_entry); | |
849 | |
850 gtk_widget_show(rowbox); | |
851 | |
852 rowbox = gtk_hbox_new(FALSE, 5); | |
853 gtk_box_pack_start(GTK_BOX(vbox), rowbox, FALSE, FALSE, 0); | |
854 | |
855 label = gtk_label_new(_("Recipient:")); | |
856 gtk_box_pack_start(GTK_BOX(rowbox), label, FALSE, FALSE, 5); | |
857 gtk_widget_show(label); | |
858 | |
859 recip_entry = gtk_entry_new(); | |
860 gtk_box_pack_end(GTK_BOX(rowbox), recip_entry, FALSE, FALSE, 5); | |
861 gtk_widget_show(recip_entry); | |
862 | |
863 gtk_widget_show(rowbox); | |
864 } | |
865 | |
866 static void zephyr_join_chat(struct gaim_connection *gc, int id, char *nm) | |
867 { | |
868 ZSubscription_t sub; | |
869 zephyr_triple *zt1, *zt2; | |
870 const char *classname; | |
871 const char *instname; | |
872 const char *recip; | |
873 char **splitted; | |
874 | |
875 if (!nm) { | |
876 splitted = NULL; | |
877 classname = gtk_entry_get_text(GTK_ENTRY(class_entry)); | |
878 instname = gtk_entry_get_text(GTK_ENTRY(inst_entry)); | |
879 recip = gtk_entry_get_text(GTK_ENTRY(recip_entry)); | |
880 if (!g_strcasecmp(recip, "%me%")) | |
881 recip = g_getenv("USER"); | |
882 } else { | |
883 splitted = g_strsplit(nm, ",", 3); | |
884 if (!splitted[0] || !splitted[1] || !splitted[2]) { | |
885 g_strfreev(splitted); | |
886 return; | |
887 } | |
888 classname = g_strstrip(splitted[0]); | |
889 instname = g_strstrip(splitted[1]); | |
890 recip = g_strstrip(splitted[2]); | |
891 } | |
892 | |
893 zt1 = new_triple(classname, instname, recip); | |
894 if (splitted) | |
895 g_strfreev(splitted); | |
896 zt2 = find_sub_by_triple(zt1); | |
897 if (zt2) { | |
898 free_triple(zt1); | |
899 if (!zt2->open) | |
900 serv_got_joined_chat(gc, zt2->id, zt2->name); | |
901 return; | |
902 } | |
903 | |
904 sub.zsub_class = zt1->class; | |
905 sub.zsub_classinst = zt1->instance; | |
906 sub.zsub_recipient = zt1->recipient; | |
907 | |
908 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) { | |
909 free_triple(zt1); | |
910 return; | |
911 } | |
912 | |
913 subscrips = g_slist_append(subscrips, zt1); | |
914 zt1->open = TRUE; | |
915 serv_got_joined_chat(gc, zt1->id, zt1->name); | |
916 } | |
917 | |
918 static void zephyr_chat_leave(struct gaim_connection *gc, int id) | |
919 { | |
920 zephyr_triple *zt; | |
921 zt = find_sub_by_id(id); | |
922 if (zt) { | |
923 zt->open = FALSE; | |
924 zt->id = ++last_id; | |
925 } | |
926 } | |
927 | |
928 static struct prpl *my_protocol = NULL; | |
929 | |
930 void zephyr_init(struct prpl *ret) | |
931 { | |
932 ret->protocol = PROTO_ZEPHYR; | |
2100
a93aeb6f813d
[gaim-migrate @ 2110]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
933 ret->options = OPT_PROTO_NO_PASSWORD; |
2086 | 934 ret->name = zephyr_name; |
935 ret->login = zephyr_login; | |
936 ret->close = zephyr_close; | |
937 ret->add_buddy = zephyr_add_buddy; | |
938 ret->remove_buddy = zephyr_remove_buddy; | |
939 ret->send_im = zephyr_send_im; | |
940 ret->get_info = zephyr_zloc; | |
941 ret->normalize = zephyr_normalize; | |
942 ret->buddy_menu = zephyr_buddy_menu; | |
943 ret->away_states = zephyr_away_states; | |
944 ret->set_away = zephyr_set_away; | |
945 ret->draw_join_chat = zephyr_draw_jc; | |
946 ret->join_chat = zephyr_join_chat; | |
947 ret->chat_send = zephyr_chat_send; | |
948 ret->chat_leave = zephyr_chat_leave; | |
949 | |
950 my_protocol = ret; | |
951 } | |
952 | |
953 #ifndef STATIC | |
954 | |
955 char *gaim_plugin_init(GModule *handle) | |
956 { | |
957 load_protocol(zephyr_init, sizeof(struct prpl)); | |
958 return NULL; | |
959 } | |
960 | |
961 void gaim_plugin_remove() | |
962 { | |
963 struct prpl *p = find_prpl(PROTO_ZEPHYR); | |
964 if (p == my_protocol) | |
965 unload_protocol(p); | |
966 } | |
967 | |
968 char *name() | |
969 { | |
970 return "Zephyr"; | |
971 } | |
972 | |
973 char *description() | |
974 { | |
2162
a464da684307
[gaim-migrate @ 2172]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2137
diff
changeset
|
975 return PRPL_DESC("Zephyr"); |
2086 | 976 } |
977 | |
978 #endif |