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