[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / zephyr / zephyr.c
blob04aaabc940034d7e9245d0f5495d8a1ad1790f6b
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 /*
3 * gaim
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/
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.
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.
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
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include "gaim.h"
34 #include "prpl.h"
35 #include "zephyr/zephyr.h"
37 extern Code_t ZGetLocations(ZLocations_t *, int *);
38 extern Code_t ZSetLocation(char *);
39 extern Code_t ZUnsetLocation();
41 typedef struct _zframe zframe;
42 typedef struct _zephyr_triple zephyr_triple;
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;
56 struct _zephyr_triple {
57 char *class;
58 char *instance;
59 char *recipient;
60 char *name;
61 gboolean open;
62 int id;
65 static char *zephyr_name()
67 return "Zephyr";
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;\
80 static char *zephyr_normalize(const char *);
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;
91 /* just for debugging
92 static void handle_unknown(ZNotice_t notice)
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");
108 static zephyr_triple *new_triple(const char *c, const char *i, const char *r)
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;
121 static void free_triple(zephyr_triple *zt)
123 g_free(zt->class);
124 g_free(zt->instance);
125 g_free(zt->recipient);
126 g_free(zt->name);
127 g_free(zt);
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)
134 if (g_strcasecmp(zt2->class, zt1->class) &&
135 g_strcasecmp(zt2->class, "*")) {
136 return FALSE;
138 if (g_strcasecmp(zt2->instance, zt1->instance) &&
139 g_strcasecmp(zt2->instance, "*")) {
140 return FALSE;
142 if (g_strcasecmp(zt2->recipient, zt1->recipient) &&
143 g_strcasecmp(zt2->recipient, "*")) {
144 return FALSE;
146 return TRUE;
149 static zephyr_triple *find_sub_by_triple(zephyr_triple *zt)
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;
159 return NULL;
162 static zephyr_triple *find_sub_by_id(int id)
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;
172 return NULL;
175 /* utility macros that are useful for zephyr_to_html */
177 #define IS_OPENER(c) ((c == '{') || (c == '[') || (c == '(') || (c == '<'))
178 #define IS_CLOSER(c) ((c == '}') || (c == ']') || (c == ')') || (c == '>'))
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)
185 int len, cnt;
186 zframe *frames, *curr;
187 char *ret;
189 frames = g_new(zframe, 1);
190 frames->text = g_string_new("");
191 frames->enclosing = NULL;
192 frames->closing = "";
193 frames->has_closer = FALSE;
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);
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]);
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 */
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]);
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++]);
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);
297 ret = frames->text->str;
298 g_string_free(frames->text, FALSE);
299 g_free(frames);
300 return ret;
303 static gboolean pending_zloc(char *who)
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;
313 return FALSE;
316 static void handle_message(ZNotice_t notice, struct sockaddr_in from)
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;
326 if (ZParseLocations(&notice, 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);
333 if (!b) {
334 free(user);
335 return;
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");
347 for (; nlocs > 0; nlocs--) {
348 ZGetLocations(&locs, &one);
349 g_string_sprintfa(str, "<br>At %s since %s", locs.host,
350 locs.time);
352 g_show_info_text(NULL, NULL, 2, str->str, NULL);
353 g_string_free(str, TRUE);
354 } else
355 serv_got_update(zgc, b->name, nlocs, 0, 0, 0, 0, 0);
357 free(user);
359 } else {
360 char *buf, *buf2;
361 char *send_inst;
362 char *ptr = notice.z_message + strlen(notice.z_message) + 1;
363 int len = notice.z_message_len - (ptr - notice.z_message);
364 int away;
365 if (len > 0) {
366 buf = g_malloc(len + 1);
367 g_snprintf(buf, len + 1, "%s", ptr);
368 g_strchomp(buf);
369 buf2 = zephyr_to_html(buf);
370 g_free(buf);
371 if (!g_strcasecmp(notice.z_class, "MESSAGE") &&
372 !g_strcasecmp(notice.z_class_inst, "PERSONAL")) {
373 if (!g_strcasecmp(notice.z_message, "Automated reply:"))
374 away = TRUE;
375 else
376 away = FALSE;
377 serv_got_im(zgc, notice.z_sender, buf2, 0, time(NULL), -1);
378 } else {
379 zephyr_triple *zt1, *zt2;
380 zt1 = new_triple(notice.z_class, notice.z_class_inst,
381 notice.z_recipient);
382 zt2 = find_sub_by_triple(zt1);
383 if (!zt2) {
384 /* we shouldn't be subscribed to this message. ignore. */
385 } else {
386 if (!zt2->open) {
387 zt2->open = TRUE;
388 serv_got_joined_chat(zgc, zt2->id, zt2->name);
390 send_inst = g_strdup_printf("%s %s", notice.z_sender,
391 notice.z_class_inst);
392 serv_got_chat_in(zgc, zt2->id, send_inst, FALSE,
393 buf2, time(NULL));
394 g_free(send_inst);
396 free_triple(zt1);
398 g_free(buf2);
403 static gint check_notify(gpointer data)
405 while (ZPending()) {
406 ZNotice_t notice;
407 struct sockaddr_in from;
408 z_call_r(ZReceiveNotice(&notice, &from));
410 switch (notice.z_kind) {
411 case UNSAFE:
412 case UNACKED:
413 case ACKED:
414 handle_message(notice, from);
415 break;
416 default:
417 /* we'll just ignore things for now */
418 debug_printf("ZEPHYR: Unhandled Notice\n");
419 break;
422 ZFreeNotice(&notice);
425 return TRUE;
428 static gint check_loc(gpointer data)
430 GSList *gr, *m;
431 ZAsyncLocateData_t ald;
433 ald.user = NULL;
434 memset(&(ald.uid), 0, sizeof(ZUnique_Id_t));
435 ald.version = NULL;
437 gr = zgc->groups;
438 while (gr) {
439 struct group *g = gr->data;
440 m = g->members;
441 while (m) {
442 struct buddy *b = m->data;
443 char *chk;
444 chk = zephyr_normalize(b->name);
445 /* doesn't matter if this fails or not; we'll just move on to the next one */
446 ZRequestLocations(chk, &ald, UNACKED, ZAUTH);
447 free(ald.user);
448 free(ald.version);
449 m = m->next;
451 gr = gr->next;
454 return TRUE;
457 static char *get_exposure_level()
459 char *exposure = ZGetVariable("exposure");
461 if (!exposure)
462 return EXPOSE_REALMVIS;
463 if (!g_strcasecmp(exposure, EXPOSE_NONE))
464 return EXPOSE_NONE;
465 if (!g_strcasecmp(exposure, EXPOSE_OPSTAFF))
466 return EXPOSE_OPSTAFF;
467 if (!g_strcasecmp(exposure, EXPOSE_REALMANN))
468 return EXPOSE_REALMANN;
469 if (!g_strcasecmp(exposure, EXPOSE_NETVIS))
470 return EXPOSE_NETVIS;
471 if (!g_strcasecmp(exposure, EXPOSE_NETANN))
472 return EXPOSE_NETANN;
473 return EXPOSE_REALMVIS;
476 static void strip_comments(char *str)
478 char *tmp = strchr(str, '#');
479 if (tmp)
480 *tmp = '\0';
481 g_strchug(str);
482 g_strchomp(str);
485 static void process_zsubs()
487 FILE *f;
488 gchar *fname;
489 gchar buff[BUFSIZ];
491 fname = g_strdup_printf("%s/.zephyr.subs", g_getenv("HOME"));
492 f = fopen(fname, "r");
493 if (f) {
494 char **triple;
495 ZSubscription_t sub;
496 char *recip;
497 while (fgets(buff, BUFSIZ, f)) {
498 strip_comments(buff);
499 if (buff[0]) {
500 triple = g_strsplit(buff, ",", 3);
501 if (triple[0] && triple[1] && triple[2]) {
502 char *tmp = g_strdup_printf("%s@%s", g_getenv("USER"),
503 ZGetRealm());
504 char *atptr;
505 sub.zsub_class = triple[0];
506 sub.zsub_classinst = triple[1];
507 if (!g_strcasecmp(triple[2], "%me%")) {
508 recip = g_strdup_printf("%s@%s", g_getenv("USER"),
509 ZGetRealm());
510 } else if (!g_strcasecmp(triple[2], "*")) {
511 /* wildcard
512 * form of class,instance,* */
513 recip = g_malloc0(1);
514 } else if (!g_strcasecmp(triple[2], tmp)) {
515 /* form of class,instance,aatharuv@ATHENA.MIT.EDU */
516 recip = g_strdup(triple[2]);
517 } else if ((atptr = strchr(triple[2], '@')) != NULL) {
518 /* form of class,instance,*@ANDREW.CMU.EDU
519 * class,instance,@ANDREW.CMU.EDU
520 * If realm is local realm, blank recipient, else
521 * @REALM-NAME
523 char *realmat = g_strdup_printf("@%s", ZGetRealm());
524 if (!g_strcasecmp(atptr, realmat))
525 recip = g_malloc0(1);
526 else
527 recip = g_strdup(atptr);
528 g_free(realmat);
529 } else {
530 recip = g_strdup(triple[2]);
532 g_free(tmp);
533 sub.zsub_recipient = recip;
534 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) {
535 debug_printf("Zephyr: Couldn't subscribe to %s, %s, "
536 "%s\n",
537 sub.zsub_class,
538 sub.zsub_classinst,
539 sub.zsub_recipient);
541 subscrips = g_slist_append(subscrips,
542 new_triple(triple[0], triple[1], recip));
543 g_free(recip);
545 g_strfreev(triple);
551 static void process_anyone()
553 FILE *fd;
554 gchar buff[BUFSIZ], *filename;
556 filename = g_strconcat(g_get_home_dir(), "/.anyone", NULL);
557 if ((fd = fopen(filename, "r")) != NULL) {
558 while (fgets(buff, BUFSIZ, fd)) {
559 strip_comments(buff);
560 if (buff[0])
561 add_buddy(zgc, "Anyone", buff, buff);
563 fclose(fd);
565 g_free(filename);
568 static void zephyr_login(struct aim_user *user)
570 ZSubscription_t sub;
572 if (zgc) {
573 do_error_dialog("Already logged in with Zephyr", "Zephyr");
574 return;
577 zgc = new_gaim_conn(user);
579 z_call_s(ZInitialize(), "Couldn't initialize zephyr");
580 z_call_s(ZOpenPort(NULL), "Couldn't open port");
581 z_call_s(ZSetLocation(get_exposure_level()), "Couldn't set location");
583 sub.zsub_class = "MESSAGE";
584 sub.zsub_classinst = "PERSONAL";
585 sub.zsub_recipient = ZGetSender();
587 /* we don't care if this fails. i'm lying right now. */
588 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) {
589 debug_printf("Zephyr: Couldn't subscribe to messages!\n");
592 account_online(zgc);
593 serv_finish_login(zgc);
595 if (bud_list_cache_exists(zgc))
596 do_import(zgc, NULL);
597 process_anyone();
598 process_zsubs();
600 nottimer = g_timeout_add(100, check_notify, NULL);
601 loctimer = g_timeout_add(20000, check_loc, NULL);
604 static void write_zsubs()
606 GSList *s = subscrips;
607 zephyr_triple *zt;
608 FILE *fd;
609 char *fname;
611 fname = g_strdup_printf("%s/.zephyr.subs", g_get_home_dir());
612 fd = fopen(fname, "w");
614 if (!fd) {
615 g_free(fname);
616 return;
619 while (s) {
620 zt = s->data;
621 fprintf(fd, "%s\n", zt->name);
622 s = s->next;
624 g_free(fname);
625 fclose(fd);
628 static void write_anyone()
630 GSList *gr, *m;
631 struct group *g;
632 struct buddy *b;
633 char *ptr, *fname;
634 FILE *fd;
636 fname = g_strdup_printf("%s/.anyone", g_get_home_dir());
637 fd = fopen(fname, "w");
638 if (!fd) {
639 g_free(fname);
640 return;
643 gr = zgc->groups;
644 while (gr) {
645 g = gr->data;
646 m = g->members;
647 while (m) {
648 b = m->data;
649 if ((ptr = strchr(b->name, '@')) != NULL)
650 *ptr = '\0';
651 fprintf(fd, "%s\n", b->name);
652 if (ptr)
653 *ptr = '@';
654 m = m->next;
656 gr = gr->next;
659 fclose(fd);
660 g_free(fname);
663 static void zephyr_close(struct gaim_connection *gc)
665 GList *l;
666 GSList *s;
667 l = pending_zloc_names;
668 while (l) {
669 g_free((char*)l->data);
670 l = l->next;
672 g_list_free(pending_zloc_names);
674 write_anyone();
675 write_zsubs();
677 s = subscrips;
678 while (s) {
679 free_triple((zephyr_triple*)s->data);
680 s = s->next;
682 g_slist_free(subscrips);
684 if (nottimer)
685 g_source_remove(nottimer);
686 nottimer = 0;
687 if (loctimer)
688 g_source_remove(loctimer);
689 loctimer = 0;
690 zgc = NULL;
691 z_call(ZCancelSubscriptions(0));
692 z_call(ZUnsetLocation());
693 z_call(ZClosePort());
696 static void zephyr_add_buddy(struct gaim_connection *gc, char *buddy) { }
697 static void zephyr_remove_buddy(struct gaim_connection *gc, char *buddy, char *group) { }
699 static int zephyr_chat_send(struct gaim_connection *gc, int id, char *im)
701 ZNotice_t notice;
702 zephyr_triple *zt;
703 char *buf;
704 const char *sig;
706 zt = find_sub_by_id(id);
707 if (!zt)
708 /* this should never happen. */
709 return -EINVAL;
711 sig = ZGetVariable("zwrite-signature");
712 if (!sig) {
713 sig = g_get_real_name();
715 buf = g_strdup_printf("%s%c%s", sig, '\0', im);
717 bzero((char *)&notice, sizeof(notice));
718 notice.z_kind = ACKED;
719 notice.z_port = 0;
720 notice.z_opcode = "";
721 notice.z_class = zt->class;
722 notice.z_class_inst = zt->instance;
723 if (!g_strcasecmp(zt->recipient, "*"))
724 notice.z_recipient = zephyr_normalize("");
725 else
726 notice.z_recipient = zephyr_normalize(zt->recipient);
727 notice.z_sender = 0;
728 notice.z_default_format =
729 "Class $class, Instance $instance:\n"
730 "To: @bold($recipient) at $time $date\n"
731 "From: @bold($1) <$sender>\n\n$2";
732 notice.z_message_len = strlen(im) + strlen(sig) + 4;
733 notice.z_message = buf;
734 ZSendNotice(&notice, ZAUTH);
735 g_free(buf);
736 return 0;
739 static int zephyr_send_im(struct gaim_connection *gc, char *who, char *im, int len, int flags) {
740 ZNotice_t notice;
741 char *buf;
742 const char *sig;
744 if (flags & IM_FLAG_AWAY)
745 sig = "Automated reply:";
746 else {
747 sig = ZGetVariable("zwrite-signature");
748 if (!sig) {
749 sig = g_get_real_name();
752 buf = g_strdup_printf("%s%c%s", sig, '\0', im);
754 bzero((char *)&notice, sizeof(notice));
755 notice.z_kind = ACKED;
756 notice.z_port = 0;
757 notice.z_opcode = "";
758 notice.z_class = "MESSAGE";
759 notice.z_class_inst = "PERSONAL";
760 notice.z_sender = 0;
761 notice.z_recipient = who;
762 notice.z_default_format =
763 "Class $class, Instance $instance:\n"
764 "To: @bold($recipient) at $time $date\n"
765 "From: @bold($1) <$sender>\n\n$2";
766 notice.z_message_len = strlen(im) + strlen(sig) + 4;
767 notice.z_message = buf;
768 ZSendNotice(&notice, ZAUTH);
769 g_free(buf);
770 return 1;
773 static char *zephyr_normalize(const char *orig)
775 static char buf[80];
776 if (strchr(orig, '@')) {
777 g_snprintf(buf, 80, "%s", orig);
778 } else {
779 g_snprintf(buf, 80, "%s@%s", orig, ZGetRealm());
781 return buf;
784 static void zephyr_zloc(struct gaim_connection *gc, char *who)
786 ZAsyncLocateData_t ald;
788 if (ZRequestLocations(zephyr_normalize(who), &ald, UNACKED, ZAUTH)
789 != ZERR_NONE) {
790 return;
792 pending_zloc_names = g_list_append(pending_zloc_names,
793 g_strdup(zephyr_normalize(who)));
796 static GList *zephyr_buddy_menu(struct gaim_connection *gc, char *who)
798 GList *m = NULL;
799 struct proto_buddy_menu *pbm;
801 pbm = g_new0(struct proto_buddy_menu, 1);
802 pbm->label = _("ZLocate");
803 pbm->callback = zephyr_zloc;
804 pbm->gc = gc;
805 m = g_list_append(m, pbm);
807 return m;
810 static void zephyr_set_away(struct gaim_connection *gc, char *state, char *msg)
812 if (gc->away)
813 g_free(gc->away);
814 gc->away = NULL;
815 if (!g_strcasecmp(state, "Hidden"))
816 ZSetLocation(EXPOSE_OPSTAFF);
817 else if (!g_strcasecmp(state, "Online"))
818 ZSetLocation(get_exposure_level());
819 else /* state is GAIM_AWAY_CUSTOM */ if (msg)
820 gc->away = g_strdup(msg);
823 static GList *zephyr_away_states(struct gaim_connection *gc)
825 GList *m = NULL;
827 m = g_list_append(m, "Online");
828 m = g_list_append(m, GAIM_AWAY_CUSTOM);
829 m = g_list_append(m, "Hidden");
831 return m;
834 static GList *zephyr_chat_info(struct gaim_connection *gc) {
835 GList *m = NULL;
836 struct proto_chat_entry *pce;
838 pce = g_new0(struct proto_chat_entry, 1);
839 pce->label = _("Class:");
840 m = g_list_append(m, NULL);
842 pce = g_new0(struct proto_chat_entry, 1);
843 pce->label = _("Instance:");
844 m = g_list_append(m, NULL);
846 pce = g_new0(struct proto_chat_entry, 1);
847 pce->label = _("Recipient:");
848 m = g_list_append(m, NULL);
850 return m;
853 static void zephyr_join_chat(struct gaim_connection *gc, GList *data)
855 ZSubscription_t sub;
856 zephyr_triple *zt1, *zt2;
857 const char *classname;
858 const char *instname;
859 const char *recip;
861 if (!data || !data->next || !data->next->next)
862 return;
864 classname = data->data;
865 instname = data->next->data;
866 recip = data->next->next->data;
867 if (!g_strcasecmp(recip, "%me%"))
868 recip = g_getenv("USER");
870 zt1 = new_triple(classname, instname, recip);
871 zt2 = find_sub_by_triple(zt1);
872 if (zt2) {
873 free_triple(zt1);
874 if (!zt2->open)
875 serv_got_joined_chat(gc, zt2->id, zt2->name);
876 return;
879 sub.zsub_class = zt1->class;
880 sub.zsub_classinst = zt1->instance;
881 sub.zsub_recipient = zt1->recipient;
883 if (ZSubscribeTo(&sub, 1, 0) != ZERR_NONE) {
884 free_triple(zt1);
885 return;
888 subscrips = g_slist_append(subscrips, zt1);
889 zt1->open = TRUE;
890 serv_got_joined_chat(gc, zt1->id, zt1->name);
893 static void zephyr_chat_leave(struct gaim_connection *gc, int id)
895 zephyr_triple *zt;
896 zt = find_sub_by_id(id);
897 if (zt) {
898 zt->open = FALSE;
899 zt->id = ++last_id;
903 static struct prpl *my_protocol = NULL;
905 void zephyr_init(struct prpl *ret)
907 ret->protocol = PROTO_ZEPHYR;
908 ret->options = OPT_PROTO_NO_PASSWORD;
909 ret->name = zephyr_name;
910 ret->login = zephyr_login;
911 ret->close = zephyr_close;
912 ret->add_buddy = zephyr_add_buddy;
913 ret->remove_buddy = zephyr_remove_buddy;
914 ret->send_im = zephyr_send_im;
915 ret->get_info = zephyr_zloc;
916 ret->normalize = zephyr_normalize;
917 ret->buddy_menu = zephyr_buddy_menu;
918 ret->away_states = zephyr_away_states;
919 ret->set_away = zephyr_set_away;
920 ret->chat_info = zephyr_chat_info;
921 ret->join_chat = zephyr_join_chat;
922 ret->chat_send = zephyr_chat_send;
923 ret->chat_leave = zephyr_chat_leave;
925 my_protocol = ret;
928 #ifndef STATIC
930 char *gaim_plugin_init(GModule *handle)
932 load_protocol(zephyr_init, sizeof(struct prpl));
933 return NULL;
936 void gaim_plugin_remove()
938 struct prpl *p = find_prpl(PROTO_ZEPHYR);
939 if (p == my_protocol)
940 unload_protocol(p);
943 char *name()
945 return "Zephyr";
948 char *description()
950 return PRPL_DESC("Zephyr");
953 #endif