comparison src/protocols/zephyr/zephyr.c @ 2086:424a40f12a6c

[gaim-migrate @ 2096] moving protocols from plugins/ to src/protocols. making it so that you can select which protocols are compiled statically. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Tue, 31 Jul 2001 01:00:39 +0000
parents
children a93aeb6f813d
comparison
equal deleted inserted replaced
2085:7ebb4322f89b 2086:424a40f12a6c
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>
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 static GtkWidget *class_entry;
91 static GtkWidget *inst_entry;
92 static GtkWidget *recip_entry;
93
94 /* just for debugging
95 static void handle_unknown(ZNotice_t notice)
96 {
97 g_print("z_packet: %s\n", notice.z_packet);
98 g_print("z_version: %s\n", notice.z_version);
99 g_print("z_kind: %d\n", notice.z_kind);
100 g_print("z_class: %s\n", notice.z_class);
101 g_print("z_class_inst: %s\n", notice.z_class_inst);
102 g_print("z_opcode: %s\n", notice.z_opcode);
103 g_print("z_sender: %s\n", notice.z_sender);
104 g_print("z_recipient: %s\n", notice.z_recipient);
105 g_print("z_message: %s\n", notice.z_message);
106 g_print("z_message_len: %d\n", notice.z_message_len);
107 g_print("\n");
108 }
109 */
110
111 static zephyr_triple *new_triple(const char *c, const char *i, const char *r)
112 {
113 zephyr_triple *zt;
114 zt = g_new0(zephyr_triple, 1);
115 zt->class = g_strdup(c);
116 zt->instance = g_strdup(i);
117 zt->recipient = g_strdup(r);
118 zt->name = g_strdup_printf("%s,%s,%s", c, i, r);
119 zt->id = ++last_id;
120 zt->open = FALSE;
121 return zt;
122 }
123
124 static void free_triple(zephyr_triple *zt)
125 {
126 g_free(zt->class);
127 g_free(zt->instance);
128 g_free(zt->recipient);
129 g_free(zt->name);
130 g_free(zt);
131 }
132
133 /* returns true if zt1 is a subset of zt2, i.e. zt2 has the same thing or
134 * wildcards in each field of zt1. */
135 static gboolean triple_subset(zephyr_triple *zt1, zephyr_triple *zt2)
136 {
137 if (g_strcasecmp(zt2->class, zt1->class) &&
138 g_strcasecmp(zt2->class, "*")) {
139 return FALSE;
140 }
141 if (g_strcasecmp(zt2->instance, zt1->instance) &&
142 g_strcasecmp(zt2->instance, "*")) {
143 return FALSE;
144 }
145 if (g_strcasecmp(zt2->recipient, zt1->recipient) &&
146 g_strcasecmp(zt2->recipient, "*")) {
147 return FALSE;
148 }
149 return TRUE;
150 }
151
152 static zephyr_triple *find_sub_by_triple(zephyr_triple *zt)
153 {
154 zephyr_triple *curr_t;
155 GSList *curr = subscrips;
156 while (curr) {
157 curr_t = curr->data;
158 if (triple_subset(zt, curr_t))
159 return curr_t;
160 curr = curr->next;
161 }
162 return NULL;
163 }
164
165 static zephyr_triple *find_sub_by_id(int id)
166 {
167 zephyr_triple *zt;
168 GSList *curr = subscrips;
169 while (curr) {
170 zt = curr->data;
171 if (zt->id == id)
172 return zt;
173 curr = curr->next;
174 }
175 return NULL;
176 }
177
178 /* utility macros that are useful for zephyr_to_html */
179
180 #define IS_OPENER(c) ((c == '{') || (c == '[') || (c == '(') || (c == '<'))
181 #define IS_CLOSER(c) ((c == '}') || (c == ']') || (c == ')') || (c == '>'))
182
183 /* this parses zephyr formatting and converts it to html. For example, if
184 * you pass in "@{@color(blue)@i(hello)}" you should get out
185 * "<font color=blue><i>hello</i></font>". */
186 static char *zephyr_to_html(char *message)
187 {
188 int len, cnt;
189 zframe *frames, *curr;
190 char *ret;
191
192 frames = g_new(zframe, 1);
193 frames->text = g_string_new("");
194 frames->enclosing = NULL;
195 frames->closing = "";
196 frames->has_closer = FALSE;
197
198 len = strlen(message);
199 cnt = 0;
200 while (cnt <= len) {
201 if (message[cnt] == '@') {
202 zframe *new_f;
203 char *buf;
204 int end;
205 for (end=1; (cnt+end) <= len &&
206 !IS_OPENER(message[cnt+end]); end++);
207 buf = g_new0(char, end);
208 if (end) {
209 g_snprintf(buf, end, "%s", message+cnt+1);
210 }
211 if (!g_strcasecmp(buf, "italic") ||
212 !g_strcasecmp(buf, "i")) {
213 new_f = g_new(zframe, 1);
214 new_f->enclosing = frames;
215 new_f->text = g_string_new("<i>");
216 new_f->closing = "</i>";
217 new_f->has_closer = TRUE;
218 frames = new_f;
219 cnt += end+1; /* cnt points to char after opener */
220 } else if (!g_strcasecmp(buf, "bold")
221 || !g_strcasecmp(buf, "b")) {
222 new_f = g_new(zframe, 1);
223 new_f->enclosing = frames;
224 new_f->text = g_string_new("<b>");
225 new_f->closing = "</b>";
226 new_f->has_closer = TRUE;
227 frames = new_f;
228 cnt += end+1;
229 } else if (!g_strcasecmp(buf, "color")) {
230 cnt += end+1;
231 new_f = g_new(zframe, 1);
232 new_f->enclosing = frames;
233 new_f->text = g_string_new("<font color=");
234 for (; (cnt <= len) && !IS_CLOSER(message[cnt]); cnt++) {
235 g_string_append_c(new_f->text, message[cnt]);
236 }
237 cnt++; /* point to char after closer */
238 g_string_append_c(new_f->text, '>');
239 new_f->closing = "</font>";
240 new_f->has_closer = FALSE;
241 frames = new_f;
242 } else if (!g_strcasecmp(buf, "")) {
243 new_f = g_new(zframe, 1);
244 new_f->enclosing = frames;
245 new_f->text = g_string_new("");
246 new_f->closing = "";
247 new_f->has_closer = TRUE;
248 frames = new_f;
249 cnt += end+1; /* cnt points to char after opener */
250 } else {
251 if ((cnt+end) > len) {
252 g_string_append_c(frames->text, '@');
253 cnt++;
254 } else {
255 /* unrecognized thingie. act like it's not there, but we
256 * still need to take care of the corresponding closer,
257 * make a frame that does nothing. */
258 new_f = g_new(zframe, 1);
259 new_f->enclosing = frames;
260 new_f->text = g_string_new("");
261 new_f->closing = "";
262 new_f->has_closer = TRUE;
263 frames = new_f;
264 cnt += end+1; /* cnt points to char after opener */
265 }
266 }
267 } else if (IS_CLOSER(message[cnt])) {
268 zframe *popped;
269 gboolean last_had_closer;
270 if (frames->enclosing) {
271 do {
272 popped = frames;
273 frames = frames->enclosing;
274 g_string_append(frames->text, popped->text->str);
275 g_string_append(frames->text, popped->closing);
276 g_string_free(popped->text, TRUE);
277 last_had_closer = popped->has_closer;
278 g_free(popped);
279 } while (frames && frames->enclosing && !last_had_closer);
280 } else {
281 g_string_append_c(frames->text, message[cnt]);
282 }
283 cnt++;
284 } else if (message[cnt] == '\n') {
285 g_string_append(frames->text, "<br>");
286 cnt++;
287 } else {
288 g_string_append_c(frames->text, message[cnt++]);
289 }
290 }
291 /* go through all the stuff that they didn't close */
292 while (frames->enclosing) {
293 curr = frames;
294 g_string_append(frames->enclosing->text, frames->text->str);
295 g_string_append(frames->enclosing->text, frames->closing);
296 g_string_free(frames->text, TRUE);
297 frames = frames->enclosing;
298 g_free(curr);
299 }
300 ret = frames->text->str;
301 g_string_free(frames->text, FALSE);
302 g_free(frames);
303 return ret;
304 }
305
306 static gboolean pending_zloc(char *who)
307 {
308 GList *curr;
309 for (curr = pending_zloc_names; curr != NULL; curr = curr->next) {
310 if (!g_strcasecmp(who, (char*)curr->data)) {
311 g_free((char*)curr->data);
312 pending_zloc_names = g_list_remove(pending_zloc_names, curr->data);
313 return TRUE;
314 }
315 }
316 return FALSE;
317 }
318
319 static void handle_message(ZNotice_t notice, struct sockaddr_in from)
320 {
321 if (!g_strcasecmp(notice.z_class, LOGIN_CLASS)) {
322 /* well, we'll be updating in 2 seconds anyway, might as well ignore this. */
323 } else if (!g_strcasecmp(notice.z_class, LOCATE_CLASS)) {
324 if (!g_strcasecmp(notice.z_opcode, LOCATE_LOCATE)) {
325 int nlocs;
326 char *user;
327 struct buddy *b;
328
329 if (ZParseLocations(&notice, NULL, &nlocs, &user) != ZERR_NONE)
330 return;
331 if ((b = find_buddy(zgc, user)) == NULL) {
332 char *e = strchr(user, '@');
333 if (e) *e = '\0';
334 b = find_buddy(zgc, user);
335 }
336 if (!b) {
337 free(user);
338 return;
339 }
340 if (pending_zloc(b->name)) {
341 ZLocations_t locs;
342 int one = 1;
343 GString *str = g_string_new("");
344 g_string_sprintfa(str, "<b>User:</b> %s<br>"
345 "<b>Alias:</b> %s<br>",
346 b->name, b->show);
347 if (!nlocs) {
348 g_string_sprintfa(str, "<br>Hidden or not logged-in");
349 }
350 for (; nlocs > 0; nlocs--) {
351 ZGetLocations(&locs, &one);
352 g_string_sprintfa(str, "<br>At %s since %s", locs.host,
353 locs.time);
354 }
355 g_show_info_text(str->str);
356 g_string_free(str, TRUE);
357 } else
358 serv_got_update(zgc, b->name, nlocs, 0, 0, 0, 0, 0);
359
360 free(user);
361 }
362 } else {
363 char *buf, *buf2;
364 char *ptr = notice.z_message + strlen(notice.z_message) + 1;
365 int len = notice.z_message_len - (ptr - notice.z_message);
366 int away;
367 if (len > 0) {
368 buf = g_malloc(len + 1);
369 g_snprintf(buf, len + 1, "%s", ptr);
370 g_strchomp(buf);
371 buf2 = zephyr_to_html(buf);
372 g_free(buf);
373 if (!g_strcasecmp(notice.z_class, "MESSAGE") &&
374 !g_strcasecmp(notice.z_class_inst, "PERSONAL")) {
375 if (!g_strcasecmp(notice.z_message, "Automated reply:"))
376 away = TRUE;
377 else
378 away = FALSE;
379 len = MAX(BUF_LONG, strlen(buf2));
380 buf = g_malloc(len + 1);
381 g_snprintf(buf, len + 1, "%s", buf2);
382 serv_got_im(zgc, notice.z_sender, buf, 0, time((time_t)NULL));
383 g_free(buf);
384 } else {
385 zephyr_triple *zt1, *zt2;
386 zt1 = new_triple(notice.z_class, notice.z_class_inst,
387 notice.z_recipient);
388 zt2 = find_sub_by_triple(zt1);
389 if (!zt2) {
390 /* we shouldn't be subscribed to this message. ignore. */
391 } else {
392 len = MAX(BUF_LONG, strlen(buf2));
393 buf = g_malloc(len + 1);
394 g_snprintf(buf, len + 1, "%s", buf2);
395 if (!zt2->open) {
396 zt2->open = TRUE;
397 serv_got_joined_chat(zgc, zt2->id, zt2->name);
398 }
399 serv_got_chat_in(zgc, zt2->id, notice.z_sender, FALSE,
400 buf, time((time_t)NULL));
401 g_free(buf);
402 }
403 free_triple(zt1);
404 }
405 g_free(buf2);
406 }
407 }
408 }
409
410 static gint check_notify(gpointer data)
411 {
412 while (ZPending()) {
413 ZNotice_t notice;
414 struct sockaddr_in from;
415 z_call_r(ZReceiveNotice(&notice, &from));
416
417 switch (notice.z_kind) {
418 case UNSAFE:
419 case UNACKED:
420 case ACKED:
421 handle_message(notice, from);
422 break;
423 default:
424 /* we'll just ignore things for now */
425 debug_printf("ZEPHYR: Unhandled Notice\n");
426 break;
427 }
428
429 ZFreeNotice(&notice);
430 }
431
432 return TRUE;
433 }
434
435 static gint check_loc(gpointer data)
436 {
437 GSList *gr, *m;
438 ZAsyncLocateData_t ald;
439
440 ald.user = NULL;
441 memset(&(ald.uid), 0, sizeof(ZUnique_Id_t));
442 ald.version = NULL;
443
444 gr = zgc->groups;
445 while (gr) {
446 struct group *g = gr->data;
447 m = g->members;
448 while (m) {
449 struct buddy *b = m->data;
450 char *chk;
451 chk = zephyr_normalize(b->name);
452 /* doesn't matter if this fails or not; we'll just move on to the next one */
453 ZRequestLocations(chk, &ald, UNACKED, ZAUTH);
454 free(ald.user);
455 free(ald.version);
456 m = m->next;
457 }
458 gr = gr->next;
459 }
460
461 return TRUE;
462 }
463
464 static char *get_exposure_level()
465 {
466 char *exposure = ZGetVariable("exposure");
467
468 if (!exposure)
469 return EXPOSE_REALMVIS;
470 if (!g_strcasecmp(exposure, EXPOSE_NONE))
471 return EXPOSE_NONE;
472 if (!g_strcasecmp(exposure, EXPOSE_OPSTAFF))
473 return EXPOSE_OPSTAFF;
474 if (!g_strcasecmp(exposure, EXPOSE_REALMANN))
475 return EXPOSE_REALMANN;
476 if (!g_strcasecmp(exposure, EXPOSE_NETVIS))
477 return EXPOSE_NETVIS;
478 if (!g_strcasecmp(exposure, EXPOSE_NETANN))
479 return EXPOSE_NETANN;
480 return EXPOSE_REALMVIS;
481 }
482
483 static void strip_comments(char *str)
484 {
485 char *tmp = strchr(str, '#');
486 if (tmp)
487 *tmp = '\0';
488 g_strchug(str);
489 g_strchomp(str);
490 }
491
492 static void process_zsubs()
493 {
494 FILE *f;
495 gchar *fname;
496 gchar buff[BUFSIZ];
497
498 fname = g_strdup_printf("%s/.zephyr.subs", g_getenv("HOME"));
499 f = fopen(fname, "r");
500 if (f) {
501 char **triple;
502 ZSubscription_t sub;
503 char *recip;
504 while (fgets(buff, BUFSIZ, f)) {
505 strip_comments(buff);
506 if (buff[0]) {
507 triple = g_strsplit(buff, ",", 3);
508 if (triple[0] && triple[1] && triple[2]) {
509 sub.zsub_class = triple[0];
510 sub.zsub_classinst = triple[1];
511 if (!g_strcasecmp(triple[2], "%me%")) {
512 recip = g_strdup_printf("%s@%s", g_getenv("USER"),
513 ZGetRealm());
514 } else if (!g_strcasecmp(triple[2], "*")) {
515 /* wildcard */
516 recip = g_strdup_printf("@%s", ZGetRealm());
517 } else {
518 recip = g_strdup(triple[2]);
519 }
520 sub.zsub_recipient = recip;
521 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) {
522 debug_printf("Zephyr: Couldn't subscribe to %s, %s, "
523 "%s\n",
524 sub.zsub_class,
525 sub.zsub_classinst,
526 sub.zsub_recipient);
527 }
528 subscrips = g_slist_append(subscrips,
529 new_triple(triple[0], triple[1], recip));
530 g_free(recip);
531 }
532 g_strfreev(triple);
533 }
534 }
535 }
536 }
537
538 static void process_anyone()
539 {
540 FILE *fd;
541 gchar buff[BUFSIZ], *filename;
542
543 filename = g_strconcat(g_get_home_dir(), "/.anyone", NULL);
544 if ((fd = fopen(filename, "r")) != NULL) {
545 while (fgets(buff, BUFSIZ, fd)) {
546 strip_comments(buff);
547 if (buff[0])
548 add_buddy(zgc, "Anyone", buff, buff);
549 }
550 fclose(fd);
551 }
552 g_free(filename);
553 }
554
555 static void zephyr_login(struct aim_user *user)
556 {
557 ZSubscription_t sub;
558
559 if (zgc) {
560 do_error_dialog("Already logged in with Zephyr", "Zephyr");
561 return;
562 }
563
564 zgc = new_gaim_conn(user);
565
566 z_call_s(ZInitialize(), "Couldn't initialize zephyr");
567 z_call_s(ZOpenPort(NULL), "Couldn't open port");
568 z_call_s(ZSetLocation(get_exposure_level()), "Couldn't set location");
569
570 sub.zsub_class = "MESSAGE";
571 sub.zsub_classinst = "PERSONAL";
572 sub.zsub_recipient = ZGetSender();
573
574 /* we don't care if this fails. i'm lying right now. */
575 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) {
576 debug_printf("Zephyr: Couldn't subscribe to messages!\n");
577 }
578
579 account_online(zgc);
580 serv_finish_login(zgc);
581
582 if (bud_list_cache_exists(zgc))
583 do_import(NULL, zgc);
584 process_anyone();
585 process_zsubs();
586
587 nottimer = gtk_timeout_add(100, check_notify, NULL);
588 loctimer = gtk_timeout_add(2000, check_loc, NULL);
589 }
590
591 static void write_zsubs()
592 {
593 GSList *s = subscrips;
594 zephyr_triple *zt;
595 FILE *fd;
596 char *fname;
597
598 fname = g_strdup_printf("%s/.zephyr.subs", g_get_home_dir());
599 fd = fopen(fname, "w");
600
601 if (!fd) {
602 g_free(fname);
603 return;
604 }
605
606 while (s) {
607 zt = s->data;
608 fprintf(fd, "%s\n", zt->name);
609 s = s->next;
610 }
611 g_free(fname);
612 fclose(fd);
613 }
614
615 static void write_anyone()
616 {
617 GSList *gr, *m;
618 struct group *g;
619 struct buddy *b;
620 char *ptr, *fname;
621 FILE *fd;
622
623 fname = g_strdup_printf("%s/.anyone", g_get_home_dir());
624 fd = fopen(fname, "w");
625 if (!fd) {
626 g_free(fname);
627 return;
628 }
629
630 gr = zgc->groups;
631 while (gr) {
632 g = gr->data;
633 m = g->members;
634 while (m) {
635 b = m->data;
636 if ((ptr = strchr(b->name, '@')) != NULL)
637 *ptr = '\0';
638 fprintf(fd, "%s\n", b->name);
639 if (ptr)
640 *ptr = '@';
641 m = m->next;
642 }
643 gr = gr->next;
644 }
645
646 fclose(fd);
647 g_free(fname);
648 }
649
650 static void zephyr_close(struct gaim_connection *gc)
651 {
652 GList *l;
653 GSList *s;
654 l = pending_zloc_names;
655 while (l) {
656 g_free((char*)l->data);
657 l = l->next;
658 }
659 g_list_free(pending_zloc_names);
660
661 write_anyone();
662 write_zsubs();
663
664 s = subscrips;
665 while (s) {
666 free_triple((zephyr_triple*)s->data);
667 s = s->next;
668 }
669 g_slist_free(subscrips);
670
671 if (nottimer)
672 gtk_timeout_remove(nottimer);
673 nottimer = 0;
674 if (loctimer)
675 gtk_timeout_remove(loctimer);
676 loctimer = 0;
677 zgc = NULL;
678 z_call(ZCancelSubscriptions(0));
679 z_call(ZUnsetLocation());
680 z_call(ZClosePort());
681 }
682
683 static void zephyr_add_buddy(struct gaim_connection *gc, char *buddy) { }
684 static void zephyr_remove_buddy(struct gaim_connection *gc, char *buddy) { }
685
686 static void zephyr_chat_send(struct gaim_connection *gc, int id, char *im)
687 {
688 ZNotice_t notice;
689 zephyr_triple *zt;
690 char *buf;
691 const char *sig;
692
693 zt = find_sub_by_id(id);
694 if (!zt)
695 /* this should never happen. */
696 return;
697
698 sig = ZGetVariable("zwrite-signature");
699 if (!sig) {
700 sig = g_get_real_name();
701 }
702 buf = g_strdup_printf("%s%c%s", sig, '\0', im);
703
704 bzero((char *)&notice, sizeof(notice));
705 notice.z_kind = ACKED;
706 notice.z_port = 0;
707 notice.z_opcode = "";
708 notice.z_class = zt->class;
709 notice.z_class_inst = zt->instance;
710 if (!g_strcasecmp(zt->recipient, "*"))
711 notice.z_recipient = zephyr_normalize("");
712 else
713 notice.z_recipient = zephyr_normalize(zt->recipient);
714 notice.z_sender = 0;
715 notice.z_default_format =
716 "Class $class, Instance $instance:\n"
717 "To: @bold($recipient) at $time $date\n"
718 "From: @bold($1) <$sender>\n\n$2";
719 notice.z_message_len = strlen(im) + strlen(sig) + 4;
720 notice.z_message = buf;
721 ZSendNotice(&notice, ZAUTH);
722 g_free(buf);
723 }
724
725 static void zephyr_send_im(struct gaim_connection *gc, char *who, char *im, int away) {
726 ZNotice_t notice;
727 char *buf;
728 const char *sig;
729
730 if (away)
731 sig = "Automated reply:";
732 else {
733 sig = ZGetVariable("zwrite-signature");
734 if (!sig) {
735 sig = g_get_real_name();
736 }
737 }
738 buf = g_strdup_printf("%s%c%s", sig, '\0', im);
739
740 bzero((char *)&notice, sizeof(notice));
741 notice.z_kind = ACKED;
742 notice.z_port = 0;
743 notice.z_opcode = "";
744 notice.z_class = "MESSAGE";
745 notice.z_class_inst = "PERSONAL";
746 notice.z_sender = 0;
747 notice.z_recipient = who;
748 notice.z_default_format =
749 "Class $class, Instance $instance:\n"
750 "To: @bold($recipient) at $time $date\n"
751 "From: @bold($1) <$sender>\n\n$2";
752 notice.z_message_len = strlen(im) + strlen(sig) + 4;
753 notice.z_message = buf;
754 ZSendNotice(&notice, ZAUTH);
755 g_free(buf);
756 }
757
758 static char *zephyr_normalize(const char *orig)
759 {
760 static char buf[80];
761 if (strchr(orig, '@')) {
762 g_snprintf(buf, 80, "%s", orig);
763 } else {
764 g_snprintf(buf, 80, "%s@%s", orig, ZGetRealm());
765 }
766 return buf;
767 }
768
769 static void zephyr_zloc(struct gaim_connection *gc, char *who)
770 {
771 ZAsyncLocateData_t ald;
772
773 if (ZRequestLocations(zephyr_normalize(who), &ald, UNACKED, ZAUTH)
774 != ZERR_NONE) {
775 return;
776 }
777 pending_zloc_names = g_list_append(pending_zloc_names,
778 g_strdup(zephyr_normalize(who)));
779 }
780
781 static void info_callback(GtkObject *obj, char *who)
782 {
783 zephyr_zloc(gtk_object_get_user_data(obj), who);
784 }
785
786 static void zephyr_buddy_menu(GtkWidget *menu, struct gaim_connection *gc, char *who)
787 {
788 GtkWidget *button;
789
790 button = gtk_menu_item_new_with_label(_("ZLocate"));
791 gtk_signal_connect(GTK_OBJECT(button), "activate",
792 GTK_SIGNAL_FUNC(info_callback), who);
793 gtk_object_set_user_data(GTK_OBJECT(button), gc);
794 gtk_menu_append(GTK_MENU(menu), button);
795 gtk_widget_show(button);
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;
933 ret->name = zephyr_name;
934 ret->login = zephyr_login;
935 ret->close = zephyr_close;
936 ret->add_buddy = zephyr_add_buddy;
937 ret->remove_buddy = zephyr_remove_buddy;
938 ret->send_im = zephyr_send_im;
939 ret->get_info = zephyr_zloc;
940 ret->normalize = zephyr_normalize;
941 ret->buddy_menu = zephyr_buddy_menu;
942 ret->away_states = zephyr_away_states;
943 ret->set_away = zephyr_set_away;
944 ret->draw_join_chat = zephyr_draw_jc;
945 ret->join_chat = zephyr_join_chat;
946 ret->chat_send = zephyr_chat_send;
947 ret->chat_leave = zephyr_chat_leave;
948
949 my_protocol = ret;
950 }
951
952 #ifndef STATIC
953
954 char *gaim_plugin_init(GModule *handle)
955 {
956 load_protocol(zephyr_init, sizeof(struct prpl));
957 return NULL;
958 }
959
960 void gaim_plugin_remove()
961 {
962 struct prpl *p = find_prpl(PROTO_ZEPHYR);
963 if (p == my_protocol)
964 unload_protocol(p);
965 }
966
967 char *name()
968 {
969 return "Zephyr";
970 }
971
972 char *description()
973 {
974 return "Allows gaim to use the Zephyr protocol";
975 }
976
977 #endif