comparison libpurple/protocols/zephyr/zephyr.c @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; 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 * Some code borrowed from kzephyr, by
11 * Chris Colohan <colohan+@cs.cmu.edu>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27
28 */
29 #include "libpurple/internal.h"
30
31 #include "accountopt.h"
32 #include "debug.h"
33 #include "notify.h"
34 #include "prpl.h"
35 #include "server.h"
36 #include "util.h"
37 #include "cmds.h"
38 #include "privacy.h"
39 #include "version.h"
40
41 #include "zephyr.h"
42 #include "internal.h"
43
44 #include <strings.h>
45
46 #define ZEPHYR_FALLBACK_CHARSET "ISO-8859-1"
47
48 /* these are deliberately high, since most people don't send multiple "PING"s */
49 #define ZEPHYR_TYPING_SEND_TIMEOUT 15
50 #define ZEPHYR_TYPING_RECV_TIMEOUT 10
51 #define ZEPHYR_FD_READ 0
52 #define ZEPHYR_FD_WRITE 1
53
54 extern Code_t ZGetLocations(ZLocations_t *, int *);
55 extern Code_t ZSetLocation(char *);
56 extern Code_t ZUnsetLocation();
57 extern Code_t ZGetSubscriptions(ZSubscription_t *, int*);
58 extern char __Zephyr_realm[];
59 typedef struct _zframe zframe;
60 typedef struct _zephyr_triple zephyr_triple;
61 typedef struct _zephyr_account zephyr_account;
62 typedef struct _parse_tree parse_tree;
63
64 typedef enum {
65 GAIM_ZEPHYR_NONE, /* Non-kerberized ZEPH0.2 */
66 GAIM_ZEPHYR_KRB4, /* ZEPH0.2 w/ KRB4 support */
67 GAIM_ZEPHYR_TZC, /* tzc executable proxy */
68 GAIM_ZEPHYR_INTERGALACTIC_KRB4, /* Kerberized ZEPH0.3 */
69 } zephyr_connection_type;
70
71 struct _zephyr_account {
72 GaimAccount* account;
73 char *username;
74 char *realm;
75 char *encoding;
76 char* galaxy; /* not yet useful */
77 char* krbtkfile; /* not yet useful */
78 guint32 nottimer;
79 guint32 loctimer;
80 GList *pending_zloc_names;
81 GSList *subscrips;
82 int last_id;
83 unsigned short port;
84 char ourhost[HOST_NAME_MAX + 1];
85 char ourhostcanon[HOST_NAME_MAX + 1];
86 zephyr_connection_type connection_type;
87 int totzc[2];
88 int fromtzc[2];
89 char *exposure;
90 pid_t tzc_pid;
91 gchar *away;
92 };
93
94 #define MAXCHILDREN 20
95
96 struct _parse_tree {
97 gchar* contents;
98 parse_tree *children[MAXCHILDREN];
99 int num_children;
100 };
101
102 parse_tree null_parse_tree = {
103 "",
104 {NULL},
105 0,
106 };
107
108 #define use_none(zephyr) ((zephyr->connection_type == GAIM_ZEPHYR_NONE)?1:0)
109 #define use_krb4(zephyr) ((zephyr->connection_type == GAIM_ZEPHYR_KRB4)?1:0)
110 #define use_tzc(zephyr) ((zephyr->connection_type == GAIM_ZEPHYR_TZC)?1:0)
111
112 #define use_zeph02(zephyr) ( (zephyr->connection_type == GAIM_ZEPHYR_NONE)?1: ((zephyr->connection_type == GAIM_ZEPHYR_KRB4)?1:0))
113
114 /* struct I need for zephyr_to_html */
115 struct _zframe {
116 /* true for everything but @color, since inside the parens of that one is
117 * the color. */
118 gboolean has_closer;
119 /* @i, @b, etc. */
120 const char *env;
121 /* }=1, ]=2, )=4, >=8 */
122 int closer_mask;
123 /* }, ], ), > */
124 char *closer;
125 /* </i>, </font>, </b>, etc. */
126 const char *closing;
127 /* text including the opening html thingie. */
128 GString *text;
129 /* href for links */
130 gboolean is_href;
131 GString *href;
132 struct _zframe *enclosing;
133 };
134
135 struct _zephyr_triple {
136 char *class;
137 char *instance;
138 char *recipient;
139 char *name;
140 gboolean open;
141 int id;
142 };
143
144 #define z_call(func) if (func != ZERR_NONE)\
145 return;
146 #define z_call_r(func) if (func != ZERR_NONE)\
147 return TRUE;
148
149 #define z_call_s(func, err) if (func != ZERR_NONE) {\
150 gaim_connection_error(gc, err);\
151 return;\
152 }
153
154 #ifdef WIN32
155 extern const char *username;
156 #endif
157
158 static Code_t zephyr_subscribe_to(zephyr_account* zephyr, char* class, char *instance, char *recipient, char* galaxy) {
159
160 if (use_tzc(zephyr)) {
161 /* ((tzcfodder . subscribe) ("class" "instance" "recipient")) */
162 gchar *zsubstr = g_strdup_printf("((tzcfodder . subscribe) (\"%s\" \"%s\" \"%s\"))\n",class,instance,recipient);
163 write(zephyr->totzc[ZEPHYR_FD_WRITE],zsubstr,strlen(zsubstr));
164 g_free(zsubstr);
165 return ZERR_NONE;
166 }
167 else {
168 if (use_zeph02(zephyr)) {
169 ZSubscription_t sub;
170 sub.zsub_class = class;
171 sub.zsub_classinst = instance;
172 sub.zsub_recipient = recipient;
173 return ZSubscribeTo(&sub,1,0);
174 } else {
175 /* This should not happen */
176 return -1;
177 }
178 }
179 return -1;
180 }
181
182 char *local_zephyr_normalize(zephyr_account* zephyr,const char *);
183 static void zephyr_chat_set_topic(GaimConnection * gc, int id, const char *topic);
184 char* zephyr_tzc_deescape_str(const char *message);
185
186 static char *zephyr_strip_local_realm(zephyr_account* zephyr,const char* user){
187 /*
188 Takes in a username of the form username or username@realm
189 and returns:
190 username, if there is no realm, or the realm is the local realm
191 or:
192 username@realm if there is a realm and it is foreign
193 */
194 char *tmp = g_strdup(user);
195 char *at = strchr(tmp,'@');
196 if (at && !g_ascii_strcasecmp(at+1,zephyr->realm)) {
197 /* We're passed in a username of the form user@users-realm */
198 char* tmp2;
199 *at = '\0';
200 tmp2 = g_strdup(tmp);
201 g_free(tmp);
202 return tmp2;
203 }
204 else {
205 /* We're passed in a username of the form user or user@foreign-realm */
206 return tmp;
207 }
208 }
209
210 /* this is so bad, and if Zephyr weren't so fucked up to begin with I
211 * wouldn't do this. but it is so i will. */
212
213 /* just for debugging */
214 static void handle_unknown(ZNotice_t notice)
215 {
216 gaim_debug_error("zephyr","z_packet: %s\n", notice.z_packet);
217 gaim_debug_error("zephyr","z_version: %s\n", notice.z_version);
218 gaim_debug_error("zephyr","z_kind: %d\n", (int)(notice.z_kind));
219 gaim_debug_error("zephyr","z_class: %s\n", notice.z_class);
220 gaim_debug_error("zephyr","z_class_inst: %s\n", notice.z_class_inst);
221 gaim_debug_error("zephyr","z_opcode: %s\n", notice.z_opcode);
222 gaim_debug_error("zephyr","z_sender: %s\n", notice.z_sender);
223 gaim_debug_error("zephyr","z_recipient: %s\n", notice.z_recipient);
224 gaim_debug_error("zephyr","z_message: %s\n", notice.z_message);
225 gaim_debug_error("zephyr","z_message_len: %d\n", notice.z_message_len);
226 }
227
228
229 static zephyr_triple *new_triple(zephyr_account *zephyr,const char *c, const char *i, const char *r)
230 {
231 zephyr_triple *zt;
232
233 zt = g_new0(zephyr_triple, 1);
234 zt->class = g_strdup(c);
235 zt->instance = g_strdup(i);
236 zt->recipient = g_strdup(r);
237 zt->name = g_strdup_printf("%s,%s,%s", c, i?i:"", r?r:"");
238 zt->id = ++(zephyr->last_id);
239 zt->open = FALSE;
240 return zt;
241 }
242
243 static void free_triple(zephyr_triple * zt)
244 {
245 g_free(zt->class);
246 g_free(zt->instance);
247 g_free(zt->recipient);
248 g_free(zt->name);
249 g_free(zt);
250 }
251
252 /* returns true if zt1 is a subset of zt2. This function is used to
253 determine whether a zephyr sent to zt1 should be placed in the chat
254 with triple zt2
255
256 zt1 is a subset of zt2
257 iff. the classnames are identical ignoring case
258 AND. the instance names are identical (ignoring case), or zt2->instance is *.
259 AND. the recipient names are identical
260 */
261
262 static gboolean triple_subset(zephyr_triple * zt1, zephyr_triple * zt2)
263 {
264
265 if (!zt2) {
266 gaim_debug_error("zephyr","zt2 doesn't exist\n");
267 return FALSE;
268 }
269 if (!zt1) {
270 gaim_debug_error("zephyr","zt1 doesn't exist\n");
271 return FALSE;
272 }
273 if (!(zt1->class)) {
274 gaim_debug_error("zephyr","zt1c doesn't exist\n");
275 return FALSE;
276 }
277 if (!(zt1->instance)) {
278 gaim_debug_error("zephyr","zt1i doesn't exist\n");
279 return FALSE;
280 }
281 if (!(zt1->recipient)) {
282 gaim_debug_error("zephyr","zt1r doesn't exist\n");
283 return FALSE;
284 }
285 if (!(zt2->class)) {
286 gaim_debug_error("zephyr","zt2c doesn't exist\n");
287 return FALSE;
288 }
289 if (!(zt2->recipient)) {
290 gaim_debug_error("zephyr","zt2r doesn't exist\n");
291 return FALSE;
292 }
293 if (!(zt2->instance)) {
294 gaim_debug_error("zephyr","zt2i doesn't exist\n");
295 return FALSE;
296 }
297
298 if (g_ascii_strcasecmp(zt2->class, zt1->class)) {
299 return FALSE;
300 }
301 if (g_ascii_strcasecmp(zt2->instance, zt1->instance) && g_ascii_strcasecmp(zt2->instance, "*")) {
302 return FALSE;
303 }
304 if (g_ascii_strcasecmp(zt2->recipient, zt1->recipient)) {
305 return FALSE;
306 }
307 gaim_debug_info("zephyr","<%s,%s,%s> is in <%s,%s,%s>\n",zt1->class,zt1->instance,zt1->recipient,zt2->class,zt2->instance,zt2->recipient);
308 return TRUE;
309 }
310
311 static zephyr_triple *find_sub_by_triple(zephyr_account *zephyr,zephyr_triple * zt)
312 {
313 zephyr_triple *curr_t;
314 GSList *curr = zephyr->subscrips;
315
316 while (curr) {
317 curr_t = curr->data;
318 if (triple_subset(zt, curr_t))
319 return curr_t;
320 curr = curr->next;
321 }
322 return NULL;
323 }
324
325 static zephyr_triple *find_sub_by_id(zephyr_account *zephyr,int id)
326 {
327 zephyr_triple *zt;
328 GSList *curr = zephyr->subscrips;
329
330 while (curr) {
331 zt = curr->data;
332 if (zt->id == id)
333 return zt;
334 curr = curr->next;
335 }
336 return NULL;
337 }
338
339 /*
340 Converts strings to utf-8 if necessary using user specified encoding
341 */
342
343 static gchar *zephyr_recv_convert(GaimConnection *gc,gchar *string, int len)
344 {
345 gchar *utf8;
346 GError *err = NULL;
347 zephyr_account *zephyr = gc->proto_data;
348 if (g_utf8_validate(string, len, NULL)) {
349 return g_strdup(string);
350 } else {
351 utf8 = g_convert(string, len, "UTF-8", zephyr->encoding, NULL, NULL, &err);
352 if (err) {
353 gaim_debug_error("zephyr", "recv conversion error: %s\n", err->message);
354 utf8 = g_strdup(_("(There was an error converting this message. Check the 'Encoding' option in the Account Editor)"));
355 g_error_free(err);
356 }
357
358 return utf8;
359 }
360 }
361
362 /* This parses HTML formatting (put out by one of the gtkimhtml widgets
363 And converts it to zephyr formatting.
364 It currently deals properly with <b>, <br>, <i>, <font face=...>, <font color=...>,
365 It ignores <font back=...>
366 It does
367 <font size = "1 or 2" -> @small
368 3 or 4 @medium()
369 5,6, or 7 @large()
370 <a href is dealt with by outputting "description <link>" or just "description" as appropriate
371 */
372
373 static char *html_to_zephyr(const char *message)
374 {
375 zframe *frames, *new_f;
376 char *ret;
377
378 if (*message == '\0')
379 return g_strdup("");
380
381 frames = g_new(zframe, 1);
382 frames->text = g_string_new("");
383 frames->href = NULL;
384 frames->is_href = FALSE;
385 frames->enclosing = NULL;
386 frames->closing = NULL;
387 frames->env = "";
388 frames->has_closer = FALSE;
389 frames->closer_mask = 15;
390
391 gaim_debug_info("zephyr","html received %s\n",message);
392 while (*message) {
393 if (frames->closing && !g_ascii_strncasecmp(message, frames->closing, strlen(frames->closing))) {
394 zframe *popped;
395 message += strlen(frames->closing);
396 popped = frames;
397 frames = frames->enclosing;
398 if (popped->is_href) {
399 frames->href = popped->text;
400 } else {
401 g_string_append(frames->text, popped->env);
402 if (popped->has_closer) {
403 g_string_append_c(frames->text,
404 (popped->closer_mask & 1) ? '{' :
405 (popped->closer_mask & 2) ? '[' :
406 (popped->closer_mask & 4) ? '(' :
407 '<');
408 }
409 g_string_append(frames->text, popped->text->str);
410 if (popped->href)
411 {
412 int text_len = strlen(popped->text->str), href_len = strlen(popped->href->str);
413 if (!((text_len == href_len && !strncmp(popped->href->str, popped->text->str, text_len)) ||
414 (7 + text_len == href_len && !strncmp(popped->href->str, "http://", 7) &&
415 !strncmp(popped->href->str + 7, popped->text->str, text_len)) ||
416 (7 + text_len == href_len && !strncmp(popped->href->str, "mailto:", 7) &&
417 !strncmp(popped->href->str + 7, popped->text->str, text_len)))) {
418 g_string_append(frames->text, " <");
419 g_string_append(frames->text, popped->href->str);
420 if (popped->closer_mask & ~8) {
421 g_string_append_c(frames->text, '>');
422 popped->closer_mask &= ~8;
423 } else {
424 g_string_append(frames->text, "@{>}");
425 }
426 }
427 g_string_free(popped->href, TRUE);
428 }
429 if (popped->has_closer) {
430 g_string_append_c(frames->text,
431 (popped->closer_mask & 1) ? '}' :
432 (popped->closer_mask & 2) ? ']' :
433 (popped->closer_mask & 4) ? ')' :
434 '>');
435 }
436 if (!popped->has_closer)
437 frames->closer_mask = popped->closer_mask;
438 g_string_free(popped->text, TRUE);
439 }
440 g_free(popped);
441 } else if (*message == '<') {
442 if (!g_ascii_strncasecmp(message + 1, "i>", 2)) {
443 new_f = g_new(zframe, 1);
444 new_f->enclosing = frames;
445 new_f->text = g_string_new("");
446 new_f->href = NULL;
447 new_f->is_href = FALSE;
448 new_f->closing = "</i>";
449 new_f->env = "@i";
450 new_f->has_closer = TRUE;
451 new_f->closer_mask = 15;
452 frames = new_f;
453 message += 3;
454 } else if (!g_ascii_strncasecmp(message + 1, "b>", 2)) {
455 new_f = g_new(zframe, 1);
456 new_f->enclosing = frames;
457 new_f->text = g_string_new("");
458 new_f->href = NULL;
459 new_f->is_href = FALSE;
460 new_f->closing = "</b>";
461 new_f->env = "@b";
462 new_f->has_closer = TRUE;
463 new_f->closer_mask = 15;
464 frames = new_f;
465 message += 3;
466 } else if (!g_ascii_strncasecmp(message + 1, "br>", 3)) {
467 g_string_append_c(frames->text, '\n');
468 message += 4;
469 } else if (!g_ascii_strncasecmp(message + 1, "a href=\"", 8)) {
470 message += 9;
471 new_f = g_new(zframe, 1);
472 new_f->enclosing = frames;
473 new_f->text = g_string_new("");
474 new_f->href = NULL;
475 new_f->is_href = FALSE;
476 new_f->closing = "</a>";
477 new_f->env = "";
478 new_f->has_closer = FALSE;
479 new_f->closer_mask = frames->closer_mask;
480 frames = new_f;
481 new_f = g_new(zframe, 1);
482 new_f->enclosing = frames;
483 new_f->text = g_string_new("");
484 new_f->href = NULL;
485 new_f->is_href = TRUE;
486 new_f->closing = "\">";
487 new_f->has_closer = FALSE;
488 new_f->closer_mask = frames->closer_mask;
489 frames = new_f;
490 } else if (!g_ascii_strncasecmp(message + 1, "font", 4)) {
491 new_f = g_new(zframe, 1);
492 new_f->enclosing = frames;
493 new_f->text = g_string_new("");
494 new_f->href = NULL;
495 new_f->is_href = FALSE;
496 new_f->closing = "</font>";
497 new_f->has_closer = TRUE;
498 new_f->closer_mask = 15;
499 message += 5;
500 while (*message == ' ')
501 message++;
502 if (!g_ascii_strncasecmp(message, "color=\"", 7)) {
503 message += 7;
504 new_f->env = "@";
505 frames = new_f;
506 new_f = g_new(zframe, 1);
507 new_f->enclosing = frames;
508 new_f->env = "@color";
509 new_f->text = g_string_new("");
510 new_f->href = NULL;
511 new_f->is_href = FALSE;
512 new_f->closing = "\">";
513 new_f->has_closer = TRUE;
514 new_f->closer_mask = 15;
515 } else if (!g_ascii_strncasecmp(message, "face=\"", 6)) {
516 message += 6;
517 new_f->env = "@";
518 frames = new_f;
519 new_f = g_new(zframe, 1);
520 new_f->enclosing = frames;
521 new_f->env = "@font";
522 new_f->text = g_string_new("");
523 new_f->href = NULL;
524 new_f->is_href = FALSE;
525 new_f->closing = "\">";
526 new_f->has_closer = TRUE;
527 new_f->closer_mask = 15;
528 } else if (!g_ascii_strncasecmp(message, "size=\"", 6)) {
529 message += 6;
530 if ((*message == '1') || (*message == '2')) {
531 new_f->env = "@small";
532 } else if ((*message == '3')
533 || (*message == '4')) {
534 new_f->env = "@medium";
535 } else if ((*message == '5')
536 || (*message == '6')
537 || (*message == '7')) {
538 new_f->env = "@large";
539 } else {
540 new_f->env = "";
541 new_f->has_closer = FALSE;
542 new_f->closer_mask = frames->closer_mask;
543 }
544 message += 3;
545 } else {
546 /* Drop all unrecognized/misparsed font tags */
547 new_f->env = "";
548 new_f->has_closer = FALSE;
549 new_f->closer_mask = frames->closer_mask;
550 while (g_ascii_strncasecmp(message, "\">", 2) != 0) {
551 message++;
552 }
553 if (*message != '\0')
554 message += 2;
555 }
556 frames = new_f;
557 } else {
558 /* Catch all for all unrecognized/misparsed <foo> tage */
559 g_string_append_c(frames->text, *message++);
560 }
561 } else if (*message == '@') {
562 g_string_append(frames->text, "@@");
563 message++;
564 } else if (*message == '}') {
565 if (frames->closer_mask & ~1) {
566 frames->closer_mask &= ~1;
567 g_string_append_c(frames->text, *message++);
568 } else {
569 g_string_append(frames->text, "@[}]");
570 message++;
571 }
572 } else if (*message == ']') {
573 if (frames->closer_mask & ~2) {
574 frames->closer_mask &= ~2;
575 g_string_append_c(frames->text, *message++);
576 } else {
577 g_string_append(frames->text, "@{]}");
578 message++;
579 }
580 } else if (*message == ')') {
581 if (frames->closer_mask & ~4) {
582 frames->closer_mask &= ~4;
583 g_string_append_c(frames->text, *message++);
584 } else {
585 g_string_append(frames->text, "@{)}");
586 message++;
587 }
588 } else if (!g_ascii_strncasecmp(message, "&gt;", 4)) {
589 if (frames->closer_mask & ~8) {
590 frames->closer_mask &= ~8;
591 g_string_append_c(frames->text, *message++);
592 } else {
593 g_string_append(frames->text, "@{>}");
594 message += 4;
595 }
596 } else {
597 g_string_append_c(frames->text, *message++);
598 }
599 }
600 ret = frames->text->str;
601 g_string_free(frames->text, FALSE);
602 g_free(frames);
603 gaim_debug_info("zephyr","zephyr outputted %s\n",ret);
604 return ret;
605 }
606
607 /* this parses zephyr formatting and converts it to html. For example, if
608 * you pass in "@{@color(blue)@i(hello)}" you should get out
609 * "<font color=blue><i>hello</i></font>". */
610 static char *zephyr_to_html(const char *message)
611 {
612 zframe *frames, *curr;
613 char *ret;
614
615 frames = g_new(zframe, 1);
616 frames->text = g_string_new("");
617 frames->enclosing = NULL;
618 frames->closing = "";
619 frames->has_closer = FALSE;
620 frames->closer = NULL;
621
622 while (*message) {
623 if (*message == '@' && message[1] == '@') {
624 g_string_append(frames->text, "@");
625 message += 2;
626 } else if (*message == '@') {
627 int end;
628 for (end = 1; message[end] && (isalnum(message[end]) || message[end] == '_'); end++);
629 if (message[end] &&
630 (message[end] == '{' || message[end] == '[' || message[end] == '(' ||
631 !g_ascii_strncasecmp(message + end, "&lt;", 4))) {
632 zframe *new_f;
633 char *buf;
634 buf = g_new0(char, end);
635 g_snprintf(buf, end, "%s", message + 1);
636 message += end;
637 new_f = g_new(zframe, 1);
638 new_f->enclosing = frames;
639 new_f->has_closer = TRUE;
640 new_f->closer = (*message == '{' ? "}" :
641 *message == '[' ? "]" :
642 *message == '(' ? ")" :
643 "&gt;");
644 message += (*message == '&' ? 4 : 1);
645 if (!g_ascii_strcasecmp(buf, "italic") || !g_ascii_strcasecmp(buf, "i")) {
646 new_f->text = g_string_new("<i>");
647 new_f->closing = "</i>";
648 } else if (!g_ascii_strcasecmp(buf, "small")) {
649 new_f->text = g_string_new("<font size=\"1\">");
650 new_f->closing = "</font>";
651 } else if (!g_ascii_strcasecmp(buf, "medium")) {
652 new_f->text = g_string_new("<font size=\"3\">");
653 new_f->closing = "</font>";
654 } else if (!g_ascii_strcasecmp(buf, "large")) {
655 new_f->text = g_string_new("<font size=\"7\">");
656 new_f->closing = "</font>";
657 } else if (!g_ascii_strcasecmp(buf, "bold")
658 || !g_ascii_strcasecmp(buf, "b")) {
659 new_f->text = g_string_new("<b>");
660 new_f->closing = "</b>";
661 } else if (!g_ascii_strcasecmp(buf, "font")) {
662 zframe *extra_f;
663 extra_f = g_new(zframe, 1);
664 extra_f->enclosing = frames;
665 new_f->enclosing = extra_f;
666 extra_f->text = g_string_new("");
667 extra_f->has_closer = FALSE;
668 extra_f->closer = frames->closer;
669 extra_f->closing = "</font>";
670 new_f->text = g_string_new("<font face=\"");
671 new_f->closing = "\">";
672 } else if (!g_ascii_strcasecmp(buf, "color")) {
673 zframe *extra_f;
674 extra_f = g_new(zframe, 1);
675 extra_f->enclosing = frames;
676 new_f->enclosing = extra_f;
677 extra_f->text = g_string_new("");
678 extra_f->has_closer = FALSE;
679 extra_f->closer = frames->closer;
680 extra_f->closing = "</font>";
681 new_f->text = g_string_new("<font color=\"");
682 new_f->closing = "\">";
683 } else {
684 new_f->text = g_string_new("");
685 new_f->closing = "";
686 }
687 frames = new_f;
688 } else {
689 /* Not a formatting tag, add the character as normal. */
690 g_string_append_c(frames->text, *message++);
691 }
692 } else if (frames->closer && !g_ascii_strncasecmp(message, frames->closer, strlen(frames->closer))) {
693 zframe *popped;
694 gboolean last_had_closer;
695
696 message += strlen(frames->closer);
697 if (frames && frames->enclosing) {
698 do {
699 popped = frames;
700 frames = frames->enclosing;
701 g_string_append(frames->text, popped->text->str);
702 g_string_append(frames->text, popped->closing);
703 g_string_free(popped->text, TRUE);
704 last_had_closer = popped->has_closer;
705 g_free(popped);
706 } while (frames && frames->enclosing && !last_had_closer);
707 } else {
708 g_string_append_c(frames->text, *message);
709 }
710 } else if (*message == '\n') {
711 g_string_append(frames->text, "<br>");
712 message++;
713 } else {
714 g_string_append_c(frames->text, *message++);
715 }
716 }
717 /* go through all the stuff that they didn't close */
718 while (frames->enclosing) {
719 curr = frames;
720 g_string_append(frames->enclosing->text, frames->text->str);
721 g_string_append(frames->enclosing->text, frames->closing);
722 g_string_free(frames->text, TRUE);
723 frames = frames->enclosing;
724 g_free(curr);
725 }
726 ret = frames->text->str;
727 g_string_free(frames->text, FALSE);
728 g_free(frames);
729 return ret;
730 }
731
732 static gboolean pending_zloc(zephyr_account *zephyr,char *who)
733 {
734 GList *curr;
735
736 for (curr = zephyr->pending_zloc_names; curr != NULL; curr = curr->next) {
737 char* normalized_who = local_zephyr_normalize(zephyr,who);
738 if (!g_ascii_strcasecmp(normalized_who, (char *)curr->data)) {
739 g_free((char *)curr->data);
740 zephyr->pending_zloc_names = g_list_remove(zephyr->pending_zloc_names, curr->data);
741 return TRUE;
742 }
743 }
744 return FALSE;
745 }
746
747 /* Called when the server notifies us a message couldn't get sent */
748
749 static void message_failed(GaimConnection *gc, ZNotice_t notice, struct sockaddr_in from)
750 {
751 if (g_ascii_strcasecmp(notice.z_class, "message")) {
752 gchar* chat_failed = g_strdup_printf(_("Unable to send to chat %s,%s,%s"),notice.z_class,notice.z_class_inst,notice.z_recipient);
753 gaim_notify_error(gc,"",chat_failed,NULL);
754 g_free(chat_failed);
755 } else {
756 gaim_notify_error(gc, notice.z_recipient, _("User is offline"), NULL);
757 }
758 }
759
760 static void handle_message(GaimConnection *gc,ZNotice_t notice)
761 {
762 zephyr_account* zephyr = gc->proto_data;
763
764 if (!g_ascii_strcasecmp(notice.z_class, LOGIN_CLASS)) {
765 /* well, we'll be updating in 20 seconds anyway, might as well ignore this. */
766 } else if (!g_ascii_strcasecmp(notice.z_class, LOCATE_CLASS)) {
767 if (!g_ascii_strcasecmp(notice.z_opcode, LOCATE_LOCATE)) {
768 int nlocs;
769 char *user;
770 GaimBuddy *b;
771 /* XXX add real error reporting */
772 if (ZParseLocations(&notice, NULL, &nlocs, &user) != ZERR_NONE)
773 return;
774
775 if ((b = gaim_find_buddy(gc->account, user)) == NULL) {
776 char* stripped_user = zephyr_strip_local_realm(zephyr,user);
777 b = gaim_find_buddy(gc->account,stripped_user);
778 g_free(stripped_user);
779 }
780 if ((b && pending_zloc(zephyr,b->name)) || pending_zloc(zephyr,user)) {
781 ZLocations_t locs;
782 int one = 1;
783 GaimNotifyUserInfo *user_info = gaim_notify_user_info_new();
784 char *tmp;
785
786 gaim_notify_user_info_add_pair(user_info, _("User"), (b ? b->name : user));
787 if (b && b->alias)
788 gaim_notify_user_info_add_pair(user_info, _("Alias"), b->alias);
789
790 if (!nlocs) {
791 gaim_notify_user_info_add_pair(user_info, NULL, _("Hidden or not logged-in"));
792 }
793 for (; nlocs > 0; nlocs--) {
794 /* XXX add real error reporting */
795
796 ZGetLocations(&locs, &one);
797 tmp = g_strdup_printf(_("<br>At %s since %s"), locs.host, locs.time);
798 gaim_notify_user_info_add_pair(user_info, _("Location"), tmp);
799 g_free(tmp);
800 }
801 gaim_notify_userinfo(gc, (b ? b->name : user),
802 user_info, NULL, NULL);
803 gaim_notify_user_info_destroy(user_info);
804 } else {
805 if (nlocs>0)
806 gaim_prpl_got_user_status(gc->account, b ? b->name : user, "available", NULL);
807 else
808 gaim_prpl_got_user_status(gc->account, b ? b->name : user, "offline", NULL);
809 }
810
811 g_free(user);
812 }
813 } else {
814 char *buf, *buf2, *buf3;
815 char *send_inst;
816 GaimConversation *gconv1;
817 GaimConvChat *gcc;
818 char *ptr = (char *) notice.z_message + (strlen(notice.z_message) + 1);
819 int len;
820 char *sendertmp = g_strdup_printf("%s", zephyr->username);
821 int signature_length = strlen(notice.z_message);
822 int message_has_no_body = 0;
823 GaimMessageFlags flags = 0;
824 gchar *tmpescape;
825
826 /* Need to deal with 0 length messages to handle typing notification (OPCODE) ping messages */
827 /* One field zephyrs would have caused gaim to crash */
828 if ( (notice.z_message_len == 0) || (signature_length >= notice.z_message_len - 1)) {
829 message_has_no_body = 1;
830 len = 0;
831 gaim_debug_info("zephyr","message_size %d %d %d\n",len,notice.z_message_len,signature_length);
832 buf3 = g_strdup("");
833
834 } else {
835 len = notice.z_message_len - ( signature_length +1);
836 gaim_debug_info("zephyr","message_size %d %d %d\n",len,notice.z_message_len,signature_length);
837 buf = g_malloc(len + 1);
838 g_snprintf(buf, len + 1, "%s", ptr);
839 g_strchomp(buf);
840 tmpescape = g_markup_escape_text(buf, -1);
841 g_free(buf);
842 buf2 = zephyr_to_html(tmpescape);
843 buf3 = zephyr_recv_convert(gc,buf2, strlen(buf2));
844 g_free(buf2);
845 g_free(tmpescape);
846 }
847
848 if (!g_ascii_strcasecmp(notice.z_class, "MESSAGE") && !g_ascii_strcasecmp(notice.z_class_inst, "PERSONAL")
849 && !g_ascii_strcasecmp(notice.z_recipient,zephyr->username)) {
850 gchar* stripped_sender;
851 if (!g_ascii_strcasecmp(notice.z_message, "Automated reply:"))
852 flags |= GAIM_MESSAGE_AUTO_RESP;
853 stripped_sender = zephyr_strip_local_realm(zephyr,notice.z_sender);
854
855 if (!g_ascii_strcasecmp(notice.z_opcode,"PING"))
856 serv_got_typing(gc,stripped_sender,ZEPHYR_TYPING_RECV_TIMEOUT, GAIM_TYPING);
857 else {
858 /* Based on the values of
859 account->permit_deny,
860 account->permit, account>deny , and
861 the buddylist */
862
863 GSList* l;
864 gboolean in_deny;
865
866 switch (gc->account->perm_deny) {
867 case GAIM_PRIVACY_ALLOW_ALL:
868 in_deny = 0; break;
869 case GAIM_PRIVACY_DENY_ALL:
870 in_deny = 1; break;
871 case GAIM_PRIVACY_ALLOW_USERS: /* See if stripped_sender is in gc->account->permit and allow appropriately */
872 in_deny = 1;
873 for(l=gc->account->permit;l!=NULL;l=l->next) {
874 if (!gaim_utf8_strcasecmp(stripped_sender, gaim_normalize(gc->account, (char *)l->data))) {
875 in_deny=0;
876 break;
877 }
878 }
879 break;
880 case GAIM_PRIVACY_DENY_USERS: /* See if stripped_sender is in gc->account->deny and deny if so */
881 in_deny = 0;
882 for(l=gc->account->deny;l!=NULL;l=l->next) {
883 if (!gaim_utf8_strcasecmp(stripped_sender, gaim_normalize(gc->account, (char *)l->data))) {
884 in_deny=1;
885 break;
886 }
887 }
888 break;
889 case GAIM_PRIVACY_ALLOW_BUDDYLIST:
890 in_deny = 1;
891 if (gaim_find_buddy(gc->account,stripped_sender)!=NULL) {
892 in_deny = 0;
893 }
894 break;
895 default:
896 in_deny=0; break;
897 }
898
899 if (!in_deny) {
900 serv_got_im(gc, stripped_sender, buf3, flags, time(NULL));
901 }
902 }
903
904 g_free(stripped_sender);
905 } else {
906 zephyr_triple *zt1, *zt2;
907 gchar *send_inst_utf8;
908 zephyr_account *zephyr = gc->proto_data;
909 zt1 = new_triple(gc->proto_data,notice.z_class, notice.z_class_inst, notice.z_recipient);
910 zt2 = find_sub_by_triple(gc->proto_data,zt1);
911 if (!zt2) {
912 /* This is a server supplied subscription */
913 zephyr->subscrips = g_slist_append(zephyr->subscrips, new_triple(zephyr,zt1->class,zt1->instance,zt1->recipient));
914 zt2 = find_sub_by_triple(gc->proto_data,zt1);
915 }
916
917 if (!zt2->open) {
918 zt2->open = TRUE;
919 serv_got_joined_chat(gc, zt2->id, zt2->name);
920 zephyr_chat_set_topic(gc,zt2->id,notice.z_class_inst);
921 }
922 g_free(sendertmp); /* fix memory leak? */
923 /* If the person is in the default Realm, then strip the
924 Realm from the sender field */
925 sendertmp = zephyr_strip_local_realm(zephyr,notice.z_sender);
926 send_inst = g_strdup_printf("%s %s",sendertmp,notice.z_class_inst);
927 send_inst_utf8 = zephyr_recv_convert(gc,send_inst, strlen(send_inst));
928 if (!send_inst_utf8) {
929 gaim_debug_error("zephyr","send_inst %s became null\n", send_inst);
930 send_inst_utf8 = "malformed instance";
931 }
932
933 gconv1 = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT,
934 zt2->name, gc->account);
935 gcc = gaim_conversation_get_chat_data(gconv1);
936
937 if (!gaim_conv_chat_find_user(gcc, sendertmp)) {
938 gchar ipaddr[INET_ADDRSTRLEN];
939 inet_ntop(AF_INET, &notice.z_sender_addr.s_addr, ipaddr, sizeof(ipaddr));
940
941 gaim_conv_chat_add_user(gcc, sendertmp, ipaddr, GAIM_CBFLAGS_NONE, TRUE);
942 }
943 g_free(sendertmp);
944 serv_got_chat_in(gc, zt2->id, send_inst_utf8, 0, buf3, time(NULL));
945 g_free(send_inst);
946 g_free(send_inst_utf8);
947
948 free_triple(zt1);
949 }
950 g_free(buf3);
951
952 }
953 }
954
955 static int free_parse_tree(parse_tree* tree) {
956 if (!tree) {
957 return 0;
958 }
959 else {
960 int i;
961 if (tree->children) {
962 for(i=0;i<tree->num_children;i++){
963 if (tree->children[i]) {
964 free_parse_tree(tree->children[i]);
965 g_free(tree->children[i]);
966 }
967 }
968 }
969 if ((tree != &null_parse_tree) && (tree->contents != NULL))
970 g_free(tree->contents);
971
972 }
973 return 0;
974 }
975
976 static parse_tree *tree_child(parse_tree* tree,int index) {
977 if (index < tree->num_children) {
978 return tree->children[index];
979 } else {
980 return &null_parse_tree;
981 }
982 }
983
984 static parse_tree *find_node(parse_tree* ptree,gchar* key)
985 {
986 gchar* tc;
987
988 if (!ptree || ! key)
989 return &null_parse_tree;
990
991 tc = tree_child(ptree,0)->contents;
992
993 if (ptree->num_children > 0 && tc && !strcasecmp(tc, key)) {
994 return ptree;
995 } else {
996 parse_tree *result = &null_parse_tree;
997 int i;
998 for(i = 0; i < ptree->num_children; i++) {
999 result = find_node(ptree->children[i],key);
1000 if(result != &null_parse_tree) {
1001 break;
1002 }
1003 }
1004 return result;
1005 }
1006 }
1007
1008 static parse_tree *parse_buffer(gchar* source, gboolean do_parse) {
1009
1010 parse_tree *ptree = g_new0(parse_tree,1);
1011 ptree->contents = NULL;
1012 ptree->num_children=0;
1013 if (do_parse) {
1014 unsigned int p = 0;
1015 while(p < strlen(source)) {
1016 unsigned int end;
1017 gchar *newstr;
1018
1019 /* Eat white space: */
1020 if(g_ascii_isspace(source[p]) || source[p] == '\001') {
1021 p++;
1022 continue;
1023 }
1024
1025 /* Skip comments */
1026 if(source[p] == ';') {
1027 while(source[p] != '\n' && p < strlen(source)) {
1028 p++;
1029 }
1030 continue;
1031 }
1032
1033 if(source[p] == '(') {
1034 int nesting = 0;
1035 gboolean in_quote = FALSE;
1036 gboolean escape_next = FALSE;
1037 p++;
1038 end = p;
1039 while(!(source[end] == ')' && nesting == 0 && !in_quote) && end < strlen(source)) {
1040 if(!escape_next) {
1041 if(source[end] == '\\') {
1042 escape_next = TRUE;
1043 }
1044 if(!in_quote) {
1045 if(source[end] == '(') {
1046 nesting++;
1047 }
1048 if(source[end] == ')') {
1049 nesting--;
1050 }
1051 }
1052 if(source[end] == '"') {
1053 in_quote = !in_quote;
1054 }
1055 } else {
1056 escape_next = FALSE;
1057 }
1058 end++;
1059 }
1060 do_parse = TRUE;
1061
1062 } else {
1063 gchar end_char;
1064 if(source[p] == '"') {
1065 end_char = '"';
1066 p++;
1067 } else {
1068 end_char = ' ';
1069 }
1070 do_parse = FALSE;
1071
1072 end = p;
1073 while(source[end] != end_char && end < strlen(source)) {
1074 if(source[end] == '\\')
1075 end++;
1076 end++;
1077 }
1078 }
1079 newstr = g_new0(gchar, end+1-p);
1080 strncpy(newstr,source+p,end-p);
1081 if (ptree->num_children < MAXCHILDREN) {
1082 /* In case we surpass maxchildren, ignore this */
1083 ptree->children[ptree->num_children++] = parse_buffer( newstr, do_parse);
1084 } else {
1085 gaim_debug_error("zephyr","too many children in tzc output. skipping\n");
1086 }
1087 g_free(newstr);
1088 p = end + 1;
1089 }
1090 return ptree;
1091 } else {
1092 /* XXX does this have to be strdup'd */
1093 ptree->contents = g_strdup(source);
1094 return ptree;
1095 }
1096 }
1097
1098 static parse_tree *read_from_tzc(zephyr_account* zephyr){
1099 struct timeval tv;
1100 fd_set rfds;
1101 int bufsize = 2048;
1102 char *buf = (char *)calloc(bufsize, 1);
1103 char *bufcur = buf;
1104 int selected = 0;
1105 parse_tree *incoming_msg;
1106
1107 FD_ZERO(&rfds);
1108 FD_SET(zephyr->fromtzc[ZEPHYR_FD_READ], &rfds);
1109 tv.tv_sec = 0;
1110 tv.tv_usec = 0;
1111 incoming_msg=NULL;
1112
1113 while (select(zephyr->fromtzc[ZEPHYR_FD_READ] + 1, &rfds, NULL, NULL, &tv)) {
1114 selected = 1;
1115 read(zephyr->fromtzc[ZEPHYR_FD_READ], bufcur, 1);
1116 bufcur++;
1117 if ((bufcur - buf) > (bufsize - 1)) {
1118 if ((buf = realloc(buf, bufsize * 2)) == NULL) {
1119 gaim_debug_error("zephyr","Ran out of memory");
1120 exit(-1);
1121 } else {
1122 bufcur = buf + bufsize;
1123 bufsize *= 2;
1124 }
1125 }
1126 }
1127 *bufcur = '\0';
1128
1129 if (selected) {
1130 incoming_msg = parse_buffer(buf,TRUE);
1131 }
1132 free(buf);
1133 return incoming_msg;
1134 }
1135
1136 static gint check_notify_tzc(gpointer data)
1137 {
1138 GaimConnection *gc = (GaimConnection *)data;
1139 zephyr_account* zephyr = gc->proto_data;
1140 parse_tree *newparsetree = read_from_tzc(zephyr);
1141 if (newparsetree != NULL) {
1142 gchar *spewtype;
1143 if ( (spewtype = tree_child(find_node(newparsetree,"tzcspew"),2)->contents) ) {
1144 if (!g_ascii_strncasecmp(spewtype,"message",7)) {
1145 ZNotice_t notice;
1146 parse_tree *msgnode = tree_child(find_node(newparsetree,"message"),2);
1147 parse_tree *bodynode = tree_child(msgnode,1);
1148 /* char *zsig = g_strdup(" "); */ /* gaim doesn't care about zsigs */
1149 char *msg = zephyr_tzc_deescape_str(bodynode->contents);
1150 size_t bufsize = strlen(msg) + 3;
1151 char *buf = g_new0(char,bufsize);
1152 g_snprintf(buf,1+strlen(msg)+2," %c%s",'\0',msg);
1153 bzero((char *)&notice, sizeof(notice));
1154 notice.z_kind = ACKED;
1155 notice.z_port = 0;
1156 notice.z_opcode = tree_child(find_node(newparsetree,"opcode"),2)->contents;
1157 notice.z_class = zephyr_tzc_deescape_str(tree_child(find_node(newparsetree,"class"),2)->contents);
1158 notice.z_class_inst = tree_child(find_node(newparsetree,"instance"),2)->contents;
1159 notice.z_recipient = local_zephyr_normalize(zephyr,tree_child(find_node(newparsetree,"recipient"),2)->contents);
1160 notice.z_sender = local_zephyr_normalize(zephyr,tree_child(find_node(newparsetree,"sender"),2)->contents);
1161 notice.z_default_format = "Class $class, Instance $instance:\n" "To: @bold($recipient) at $time $date\n" "From: @bold($1) <$sender>\n\n$2";
1162 notice.z_message_len = strlen(msg) + 3;
1163 notice.z_message = buf;
1164 handle_message(gc, notice);
1165 g_free(msg);
1166 /* g_free(zsig); */
1167 g_free(buf);
1168 /* free_parse_tree(msgnode);
1169 free_parse_tree(bodynode);
1170 g_free(msg);
1171 g_free(zsig);
1172 g_free(buf);
1173 */
1174 }
1175 else if (!g_ascii_strncasecmp(spewtype,"zlocation",9)) {
1176 /* check_loc or zephyr_zloc respectively */
1177 /* XXX fix */
1178 char *user;
1179 GaimBuddy *b;
1180 int nlocs = 0;
1181 parse_tree *locations;
1182 gchar *locval;
1183 user = tree_child(find_node(newparsetree,"user"),2)->contents;
1184
1185 if ((b = gaim_find_buddy(gc->account, user)) == NULL) {
1186 gchar *stripped_user = zephyr_strip_local_realm(zephyr,user);
1187 b = gaim_find_buddy(gc->account, stripped_user);
1188 g_free(stripped_user);
1189 }
1190 locations = find_node(newparsetree,"locations");
1191 locval = tree_child(tree_child(tree_child(tree_child(locations,2),0),0),2)->contents;
1192
1193 if (!locval || !g_ascii_strcasecmp(locval," ") || (strlen(locval) == 0)) {
1194 nlocs = 0;
1195 } else {
1196 nlocs = 1;
1197 }
1198
1199 if ((b && pending_zloc(zephyr,b->name)) || pending_zloc(zephyr,user) || pending_zloc(zephyr,local_zephyr_normalize(zephyr,user))){
1200 GaimNotifyUserInfo *user_info = gaim_notify_user_info_new();
1201 char *tmp;
1202
1203 gaim_notify_user_info_add_pair(user_info, _("User"), (b ? b->name : user));
1204
1205 if (b && b->alias)
1206 gaim_notify_user_info_add_pair(user_info, _("Alias"), b->alias);
1207
1208 if (!nlocs) {
1209 gaim_notify_user_info_add_pair(user_info, NULL, _("Hidden or not logged-in"));
1210 } else {
1211 tmp = g_strdup_printf(_("<br>At %s since %s"),
1212 tree_child(tree_child(tree_child(tree_child(locations,2),0),0),2)->contents,
1213 tree_child(tree_child(tree_child(tree_child(locations,2),0),2),2)->contents);
1214 gaim_notify_user_info_add_pair(user_info, _("Location"), tmp);
1215 g_free(tmp);
1216 }
1217
1218 gaim_notify_userinfo(gc, b ? b->name : user,
1219 user_info, NULL, NULL);
1220 gaim_notify_user_info_destroy(user_info);
1221 } else {
1222 if (nlocs>0)
1223 gaim_prpl_got_user_status(gc->account, b ? b->name : user, "available", NULL);
1224 else
1225 gaim_prpl_got_user_status(gc->account, b ? b->name : user, "offline", NULL);
1226 }
1227 }
1228 else if (!g_ascii_strncasecmp(spewtype,"subscribed",10)) {
1229 }
1230 else if (!g_ascii_strncasecmp(spewtype,"start",5)) {
1231 }
1232 else if (!g_ascii_strncasecmp(spewtype,"error",5)) {
1233 /* XXX handle */
1234 }
1235 } else {
1236 }
1237 } else {
1238 }
1239
1240 free_parse_tree(newparsetree);
1241 return TRUE;
1242 }
1243
1244 static gint check_notify_zeph02(gpointer data)
1245 {
1246 /* XXX add real error reporting */
1247 GaimConnection *gc = (GaimConnection*) data;
1248 while (ZPending()) {
1249 ZNotice_t notice;
1250 struct sockaddr_in from;
1251 /* XXX add real error reporting */
1252
1253 z_call_r(ZReceiveNotice(&notice, &from));
1254
1255 switch (notice.z_kind) {
1256 case UNSAFE:
1257 case UNACKED:
1258 case ACKED:
1259 handle_message(gc,notice);
1260 break;
1261 case SERVACK:
1262 if (!(g_ascii_strcasecmp(notice.z_message, ZSRVACK_NOTSENT))) {
1263 message_failed(gc,notice, from);
1264 }
1265 break;
1266 case CLIENTACK:
1267 gaim_debug_error("zephyr", "Client ack received\n");
1268 default:
1269 /* we'll just ignore things for now */
1270 handle_unknown(notice);
1271 gaim_debug_error("zephyr", "Unhandled notice.\n");
1272 break;
1273 }
1274 /* XXX add real error reporting */
1275 ZFreeNotice(&notice);
1276 }
1277
1278 return TRUE;
1279 }
1280
1281 #ifdef WIN32
1282
1283 static gint check_loc(gpointer_data)
1284 {
1285 GaimBlistNode *gnode, *cnode, *bnode;
1286 ZLocations_t locations;
1287 int numlocs;
1288 int one = 1;
1289
1290 for (gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) {
1291 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
1292 continue;
1293 for (cnode = gnode->child; cnode; cnode = cnode->next) {
1294 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode))
1295 continue;
1296 for (bnode = cnode->child; bnode; bnode = bnode->next) {
1297 GaimBuddy *b = (GaimBuddy *) bnode;
1298
1299 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode))
1300 continue;
1301 if (b->account->gc == zgc) {
1302 char *chk;
1303 chk = local_zephyr_normalize(b->name);
1304 ZLocateUser(chk,&numlocs, ZAUTH);
1305 if (numlocs) {
1306 int i;
1307 for(i=0;i<numlocs;i++) {
1308 ZGetLocations(&locations,&one);
1309 serv_got_update(zgc,b->name,1,0,0,0,0);
1310 }
1311 }
1312 }
1313 }
1314 }
1315 }
1316 return TRUE;
1317 }
1318
1319 #else
1320
1321 static gint check_loc(gpointer data)
1322 {
1323 GaimBlistNode *gnode, *cnode, *bnode;
1324 ZAsyncLocateData_t ald;
1325 GaimConnection *gc = (GaimConnection *)data;
1326 zephyr_account *zephyr = gc->proto_data;
1327
1328 if (use_zeph02(zephyr)) {
1329 ald.user = NULL;
1330 memset(&(ald.uid), 0, sizeof(ZUnique_Id_t));
1331 ald.version = NULL;
1332 }
1333
1334 for (gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) {
1335 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
1336 continue;
1337 for (cnode = gnode->child; cnode; cnode = cnode->next) {
1338 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode))
1339 continue;
1340 for (bnode = cnode->child; bnode; bnode = bnode->next) {
1341 GaimBuddy *b = (GaimBuddy *) bnode;
1342
1343 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode))
1344 continue;
1345 if (b->account->gc == gc) {
1346 const char *chk;
1347
1348 chk = local_zephyr_normalize(zephyr,b->name);
1349 gaim_debug_info("zephyr","chk: %s b->name %s\n",chk,b->name);
1350 /* XXX add real error reporting */
1351 /* doesn't matter if this fails or not; we'll just move on to the next one */
1352 if (use_zeph02(zephyr)) {
1353 #ifdef WIN32
1354 int numlocs;
1355 int one=1;
1356 ZLocateUser(chk,&numlocs,ZAUTH);
1357 if (numlocs) {
1358 int i;
1359 for(i=0;i<numlocs;i++) {
1360 ZGetLocations(&locations,&one);
1361 if (nlocs>0)
1362 gaim_prpl_got_user_status(gc->account,b->name,"available",NULL);
1363 else
1364 gaim_prpl_got_user_status(gc->account,b->name,"offline",NULL);
1365 }
1366 }
1367 #else
1368 ZRequestLocations(chk, &ald, UNACKED, ZAUTH);
1369 g_free(ald.user);
1370 g_free(ald.version);
1371 #endif /* WIN32 */
1372 } else
1373 if (use_tzc(zephyr)) {
1374 gchar *zlocstr = g_strdup_printf("((tzcfodder . zlocate) \"%s\")\n",chk);
1375 write(zephyr->totzc[ZEPHYR_FD_WRITE],zlocstr,strlen(zlocstr));
1376 g_free(zlocstr);
1377 }
1378 }
1379 }
1380 }
1381 }
1382
1383 return TRUE;
1384 }
1385
1386 #endif /* WIN32 */
1387
1388 static char *get_exposure_level()
1389 {
1390 /* XXX add real error reporting */
1391 char *exposure = ZGetVariable("exposure");
1392
1393 if (!exposure)
1394 return EXPOSE_REALMVIS;
1395 if (!g_ascii_strcasecmp(exposure, EXPOSE_NONE))
1396 return EXPOSE_NONE;
1397 if (!g_ascii_strcasecmp(exposure, EXPOSE_OPSTAFF))
1398 return EXPOSE_OPSTAFF;
1399 if (!g_ascii_strcasecmp(exposure, EXPOSE_REALMANN))
1400 return EXPOSE_REALMANN;
1401 if (!g_ascii_strcasecmp(exposure, EXPOSE_NETVIS))
1402 return EXPOSE_NETVIS;
1403 if (!g_ascii_strcasecmp(exposure, EXPOSE_NETANN))
1404 return EXPOSE_NETANN;
1405 return EXPOSE_REALMVIS;
1406 }
1407
1408 static void strip_comments(char *str)
1409 {
1410 char *tmp = strchr(str, '#');
1411
1412 if (tmp)
1413 *tmp = '\0';
1414 g_strchug(str);
1415 g_strchomp(str);
1416 }
1417
1418 static void zephyr_inithosts(zephyr_account *zephyr)
1419 {
1420 /* XXX This code may not be Win32 clean */
1421 struct hostent *hent;
1422
1423 if (gethostname(zephyr->ourhost, sizeof(zephyr->ourhost)) == -1) {
1424 gaim_debug_error("zephyr", "unable to retrieve hostname, %%host%% and %%canon%% will be wrong in subscriptions and have been set to unknown\n");
1425 g_strlcpy(zephyr->ourhost, "unknown", sizeof(zephyr->ourhost));
1426 g_strlcpy(zephyr->ourhostcanon, "unknown", sizeof(zephyr->ourhostcanon));
1427 return;
1428 }
1429
1430 if (!(hent = gethostbyname(zephyr->ourhost))) {
1431 gaim_debug_error("zephyr", "unable to resolve hostname, %%canon%% will be wrong in subscriptions.and has been set to the value of %%host%%, %s\n",zephyr->ourhost);
1432 g_strlcpy(zephyr->ourhostcanon, zephyr->ourhost, sizeof(zephyr->ourhostcanon));
1433 return;
1434 }
1435
1436 g_strlcpy(zephyr->ourhostcanon, hent->h_name, sizeof(zephyr->ourhostcanon));
1437
1438 return;
1439 }
1440
1441 static void process_zsubs(zephyr_account *zephyr)
1442 {
1443 /* Loads zephyr chats "(subscriptions) from ~/.zephyr.subs, and
1444 registers (subscribes to) them on the server */
1445
1446 /* XXX deal with unsubscriptions */
1447 /* XXX deal with punts */
1448
1449 FILE *f;
1450 gchar *fname;
1451 gchar buff[BUFSIZ];
1452
1453 fname = g_strdup_printf("%s/.zephyr.subs", gaim_home_dir());
1454 f = g_fopen(fname, "r");
1455 if (f) {
1456 char **triple;
1457 char *recip;
1458 char *z_class;
1459 char *z_instance;
1460 char *z_galaxy = NULL;
1461
1462 while (fgets(buff, BUFSIZ, f)) {
1463 strip_comments(buff);
1464 if (buff[0]) {
1465 triple = g_strsplit(buff, ",", 3);
1466 if (triple[0] && triple[1]) {
1467 char *tmp = g_strdup_printf("%s", zephyr->username);
1468 char *atptr;
1469
1470 z_class = triple[0];
1471 z_instance = triple[1];
1472 if (triple[2] == NULL) {
1473 recip = g_malloc0(1);
1474 } else if (!g_ascii_strcasecmp(triple[2], "%me%")) {
1475 recip = g_strdup_printf("%s", zephyr->username);
1476 } else if (!g_ascii_strcasecmp(triple[2], "*")) {
1477 /* wildcard
1478 * form of class,instance,* */
1479 recip = g_malloc0(1);
1480 } else if (!g_ascii_strcasecmp(triple[2], tmp)) {
1481 /* form of class,instance,aatharuv@ATHENA.MIT.EDU */
1482 recip = g_strdup(triple[2]);
1483 } else if ((atptr = strchr(triple[2], '@')) != NULL) {
1484 /* form of class,instance,*@ANDREW.CMU.EDU
1485 * class,instance,@ANDREW.CMU.EDU
1486 * If realm is local realm, blank recipient, else
1487 * @REALM-NAME
1488 */
1489 char *realmat = g_strdup_printf("@%s",zephyr->realm);
1490
1491 if (!g_ascii_strcasecmp(atptr, realmat))
1492 recip = g_malloc0(1);
1493 else
1494 recip = g_strdup(atptr);
1495 g_free(realmat);
1496 } else {
1497 recip = g_strdup(triple[2]);
1498 }
1499 g_free(tmp);
1500
1501 if (!g_ascii_strcasecmp(triple[0],"%host%")) {
1502 z_class = g_strdup(zephyr->ourhost);
1503 } else if (!g_ascii_strcasecmp(triple[0],"%canon%")) {
1504 z_class = g_strdup(zephyr->ourhostcanon);
1505 } else {
1506 z_class = g_strdup(triple[0]);
1507 }
1508
1509 if (!g_ascii_strcasecmp(triple[1],"%host%")) {
1510 z_instance = g_strdup(zephyr->ourhost);
1511 } else if (!g_ascii_strcasecmp(triple[1],"%canon%")) {
1512 z_instance = g_strdup(zephyr->ourhostcanon);
1513 } else {
1514 z_instance = g_strdup(triple[1]);
1515 }
1516
1517 /* There should be some sort of error report listing classes that couldn't be subbed to.
1518 Not important right now though */
1519
1520 if (zephyr_subscribe_to(zephyr,z_class, z_instance, recip,z_galaxy) != ZERR_NONE) {
1521
1522 gaim_debug_error("zephyr", "Couldn't subscribe to %s, %s, %s\n", z_class,z_instance,recip);
1523 }
1524
1525 zephyr->subscrips = g_slist_append(zephyr->subscrips, new_triple(zephyr,z_class,z_instance,recip));
1526 /* g_hash_table_destroy(sub_hash_table); */
1527 g_free(z_instance);
1528 g_free(z_class);
1529 g_free(recip);
1530 }
1531 g_strfreev(triple);
1532 }
1533 }
1534 fclose(f);
1535 }
1536 }
1537
1538 static void process_anyone(GaimConnection *gc)
1539 {
1540 FILE *fd;
1541 gchar buff[BUFSIZ], *filename;
1542 GaimGroup *g;
1543 GaimBuddy *b;
1544
1545 if (!(g = gaim_find_group(_("Anyone")))) {
1546 g = gaim_group_new(_("Anyone"));
1547 gaim_blist_add_group(g, NULL);
1548 }
1549
1550 filename = g_strconcat(gaim_home_dir(), "/.anyone", NULL);
1551 if ((fd = g_fopen(filename, "r")) != NULL) {
1552 while (fgets(buff, BUFSIZ, fd)) {
1553 strip_comments(buff);
1554 if (buff[0]) {
1555 if (!(b = gaim_find_buddy(gc->account, buff))) {
1556 char *stripped_user = zephyr_strip_local_realm(gc->proto_data,buff);
1557 gaim_debug_info("zephyr","stripped_user %s\n",stripped_user);
1558 if (!(b = gaim_find_buddy(gc->account,stripped_user))){
1559 b = gaim_buddy_new(gc->account, stripped_user, NULL);
1560 gaim_blist_add_buddy(b, NULL, g, NULL);
1561 }
1562 g_free(stripped_user);
1563 }
1564 }
1565 }
1566 fclose(fd);
1567 }
1568 g_free(filename);
1569 }
1570
1571 static char* normalize_zephyr_exposure(const char* exposure) {
1572 char *exp2 = g_strstrip(g_ascii_strup(exposure,-1));
1573
1574 if (!exp2)
1575 return EXPOSE_REALMVIS;
1576 if (!g_ascii_strcasecmp(exp2, EXPOSE_NONE))
1577 return EXPOSE_NONE;
1578 if (!g_ascii_strcasecmp(exp2, EXPOSE_OPSTAFF))
1579 return EXPOSE_OPSTAFF;
1580 if (!g_ascii_strcasecmp(exp2, EXPOSE_REALMANN))
1581 return EXPOSE_REALMANN;
1582 if (!g_ascii_strcasecmp(exp2, EXPOSE_NETVIS))
1583 return EXPOSE_NETVIS;
1584 if (!g_ascii_strcasecmp(exp2, EXPOSE_NETANN))
1585 return EXPOSE_NETANN;
1586 return EXPOSE_REALMVIS;
1587 }
1588
1589 static void zephyr_login(GaimAccount * account)
1590 {
1591 GaimConnection *gc;
1592 zephyr_account *zephyr;
1593 gboolean read_anyone;
1594 gboolean read_zsubs;
1595 gchar *exposure;
1596
1597 gc = gaim_account_get_connection(account);
1598 read_anyone = gaim_account_get_bool(gc->account,"read_anyone",TRUE);
1599 read_zsubs = gaim_account_get_bool(gc->account,"read_zsubs",TRUE);
1600 exposure = (gchar *)gaim_account_get_string(gc->account, "exposure_level", EXPOSE_REALMVIS);
1601
1602 #ifdef WIN32
1603 username = gaim_account_get_username(account);
1604 #endif
1605 gc->flags |= GAIM_CONNECTION_HTML | GAIM_CONNECTION_NO_BGCOLOR | GAIM_CONNECTION_NO_URLDESC;
1606 gc->proto_data = zephyr=g_new0(zephyr_account,1);
1607
1608 zephyr->account = account;
1609
1610 /* Make sure that the exposure (visibility) is set to a sane value */
1611 zephyr->exposure=g_strdup(normalize_zephyr_exposure(exposure));
1612
1613 if (gaim_account_get_bool(gc->account,"use_tzc",0)) {
1614 zephyr->connection_type = GAIM_ZEPHYR_TZC;
1615 } else {
1616 zephyr->connection_type = GAIM_ZEPHYR_KRB4;
1617 }
1618
1619 zephyr->encoding = (char *)gaim_account_get_string(gc->account, "encoding", ZEPHYR_FALLBACK_CHARSET);
1620 gaim_connection_update_progress(gc, _("Connecting"), 0, 8);
1621
1622 /* XXX z_call_s should actually try to report the com_err determined error */
1623 if (use_tzc(zephyr)) {
1624 pid_t pid;
1625 /* gaim_connection_error(gc,"tzc not supported yet"); */
1626 if ((pipe(zephyr->totzc) != 0) || (pipe(zephyr->fromtzc) != 0)) {
1627 gaim_debug_error("zephyr", "pipe creation failed. killing\n");
1628 exit(-1);
1629 }
1630
1631 pid = fork();
1632
1633 if (pid == -1) {
1634 gaim_debug_error("zephyr", "forking failed\n");
1635 exit(-1);
1636 }
1637 if (pid == 0) {
1638 unsigned int i=0;
1639 gboolean found_ps = FALSE;
1640 gchar ** tzc_cmd_array = g_strsplit(gaim_account_get_string(gc->account,"tzc_command","/usr/bin/tzc -e %s")," ",0);
1641 if (close(1) == -1) {
1642 gaim_debug_error("zephyr", "stdout couldn't be closed. dying\n");
1643 exit(-1);
1644 }
1645 if (dup2(zephyr->fromtzc[1], 1) == -1) {
1646 gaim_debug_error("zephyr", "dup2 of stdout failed \n");
1647 exit(-1);
1648 }
1649 if (close(zephyr->fromtzc[1]) == -1) {
1650 gaim_debug_error("zephyr", "closing of piped stdout failed\n");
1651 exit(-1);
1652 }
1653 if (close(0) == -1) {
1654 gaim_debug_error("zephyr", "stdin couldn't be closed. dying\n");
1655 exit(-1);
1656 }
1657 if (dup2(zephyr->totzc[0], 0) == -1) {
1658 gaim_debug_error("zephyr", "dup2 of stdin failed \n");
1659 exit(-1);
1660 }
1661 if (close(zephyr->totzc[0]) == -1) {
1662 gaim_debug_error("zephyr", "closing of piped stdin failed\n");
1663 exit(-1);
1664 }
1665 /* tzc_command should really be of the form
1666 path/to/tzc -e %s
1667 or
1668 ssh username@hostname pathtotzc -e %s
1669 -- this should not require a password, and ideally should be kerberized ssh --
1670 or
1671 fsh username@hostname pathtotzc -e %s
1672 */
1673 while(tzc_cmd_array[i] != NULL){
1674 if (!g_ascii_strncasecmp(tzc_cmd_array[i],"%s",2)) {
1675 /* fprintf(stderr,"replacing %%s with %s\n",zephyr->exposure); */
1676 tzc_cmd_array[i] = g_strdup(zephyr->exposure);
1677 found_ps = TRUE;
1678
1679 } else {
1680 /* fprintf(stderr,"keeping %s\n",tzc_cmd_array[i]); */
1681 }
1682 i++;
1683 }
1684
1685 if (!found_ps) {
1686 gaim_connection_error(gc,"Tzc command needs %s to set the exposure\n");
1687 return;
1688 }
1689
1690 execvp(tzc_cmd_array[0], tzc_cmd_array);
1691 }
1692 else {
1693 fd_set rfds;
1694 int bufsize = 2048;
1695 char *buf = (char *)calloc(bufsize, 1);
1696 char *bufcur = buf;
1697 struct timeval tv;
1698 char *ptr;
1699 int parenlevel=0;
1700 char* tempstr;
1701 int tempstridx;
1702
1703 zephyr->tzc_pid = pid;
1704 /* wait till we have data to read from ssh */
1705 FD_ZERO(&rfds);
1706 FD_SET(zephyr->fromtzc[ZEPHYR_FD_READ], &rfds);
1707
1708 tv.tv_sec = 10;
1709 tv.tv_usec = 0;
1710
1711 gaim_debug_info("zephyr", "about to read from tzc\n");
1712
1713 select(zephyr->fromtzc[ZEPHYR_FD_READ] + 1, &rfds, NULL, NULL, NULL);
1714
1715 FD_ZERO(&rfds);
1716 FD_SET(zephyr->fromtzc[ZEPHYR_FD_READ], &rfds);
1717 while (select(zephyr->fromtzc[ZEPHYR_FD_READ] + 1, &rfds, NULL, NULL, &tv)) {
1718 read(zephyr->fromtzc[ZEPHYR_FD_READ], bufcur, 1);
1719 bufcur++;
1720 if ((bufcur - buf) > (bufsize - 1)) {
1721 if ((buf = realloc(buf, bufsize * 2)) == NULL) {
1722 exit(-1);
1723 } else {
1724 bufcur = buf + bufsize;
1725 bufsize *= 2;
1726 }
1727 }
1728 FD_ZERO(&rfds);
1729 FD_SET(zephyr->fromtzc[ZEPHYR_FD_READ], &rfds);
1730 tv.tv_sec = 10;
1731 tv.tv_usec = 0;
1732
1733 }
1734 /* fprintf(stderr, "read from tzc\n"); */
1735 *bufcur = '\0';
1736 ptr = buf;
1737
1738 /* ignore all tzcoutput till we've received the first (*/
1739 while (ptr < bufcur && (*ptr !='(')) {
1740 ptr++;
1741 }
1742 if (ptr >=bufcur) {
1743 gaim_connection_error(gc,"invalid output by tzc (or bad parsing code)");
1744 free(buf);
1745 return;
1746 }
1747
1748 while(ptr < bufcur) {
1749 if (*ptr == '(') {
1750 parenlevel++;
1751 }
1752 else if (*ptr == ')') {
1753 parenlevel--;
1754 }
1755 gaim_debug_info("zephyr","tzc parenlevel is %d\n",parenlevel);
1756 switch (parenlevel) {
1757 case 0:
1758 break;
1759 case 1:
1760 /* Search for next beginning (, or for the ending */
1761 ptr++;
1762 while((*ptr != '(') && (*ptr != ')') && (ptr <bufcur))
1763 ptr++;
1764 if (ptr >= bufcur)
1765 gaim_debug_error("zephyr","tzc parsing error\n");
1766 break;
1767 case 2:
1768 /* You are probably at
1769 (foo . bar ) or (foo . "bar") or (foo . chars) or (foo . numbers) or (foo . () )
1770 Parse all the data between the first and last f, and move past )
1771 */
1772 tempstr = g_malloc0(20000);
1773 tempstridx=0;
1774 while(parenlevel >1) {
1775 ptr++;
1776 if (*ptr == '(')
1777 parenlevel++;
1778 if (*ptr == ')')
1779 parenlevel--;
1780 if (parenlevel > 1) {
1781 tempstr[tempstridx++]=*ptr;
1782 } else {
1783 ptr++;
1784 }
1785 }
1786 gaim_debug_info("zephyr","tempstr parsed\n");
1787 /* tempstr should now be a tempstridx length string containing all characters
1788 from that after the first ( to the one before the last paren ). */
1789 /* We should have the following possible lisp strings but we don't care
1790 (tzcspew . start) (version . "something") (pid . number)*/
1791 /* We care about 'zephyrid . "username@REALM.NAME"' and 'exposure . "SOMETHING"' */
1792 tempstridx=0;
1793 if (!g_ascii_strncasecmp(tempstr,"zephyrid",8)) {
1794 gchar* username = g_malloc0(100);
1795 int username_idx=0;
1796 char *realm;
1797 gaim_debug_info("zephyr","zephyrid found\n");
1798 tempstridx+=8;
1799 while(tempstr[tempstridx] !='"' && tempstridx < 20000)
1800 tempstridx++;
1801 tempstridx++;
1802 while(tempstr[tempstridx] !='"' && tempstridx < 20000)
1803 username[username_idx++]=tempstr[tempstridx++];
1804
1805 zephyr->username = g_strdup_printf("%s",username);
1806 if ((realm = strchr(username,'@')))
1807 zephyr->realm = g_strdup_printf("%s",realm+1);
1808 else {
1809 realm = (gchar *)gaim_account_get_string(gc->account,"realm","");
1810 if (!*realm) {
1811 realm = "local-realm";
1812 }
1813 zephyr->realm = g_strdup(realm);
1814 g_strlcpy(__Zephyr_realm, (const char*)zephyr->realm, REALM_SZ-1);
1815 }
1816 /* else {
1817 zephyr->realm = g_strdup("local-realm");
1818 }*/
1819
1820 g_free(username);
1821 } else {
1822 gaim_debug_info("zephyr", "something that's not zephyr id found %s\n",tempstr);
1823 }
1824
1825 /* We don't care about anything else yet */
1826 g_free(tempstr);
1827 break;
1828 default:
1829 gaim_debug_info("zephyr","parenlevel is not 1 or 2\n");
1830 /* This shouldn't be happening */
1831 break;
1832 }
1833 if (parenlevel==0)
1834 break;
1835 } /* while (ptr < bufcur) */
1836 gaim_debug_info("zephyr", "tzc startup done\n");
1837 free(buf);
1838 }
1839 }
1840 else if ( use_zeph02(zephyr)) {
1841 gchar* realm;
1842 z_call_s(ZInitialize(), "Couldn't initialize zephyr");
1843 z_call_s(ZOpenPort(&(zephyr->port)), "Couldn't open port");
1844 z_call_s(ZSetLocation((char *)zephyr->exposure), "Couldn't set location");
1845
1846 realm = (gchar *)gaim_account_get_string(gc->account,"realm","");
1847 if (!*realm) {
1848 realm = ZGetRealm();
1849 }
1850 zephyr->realm = g_strdup(realm);
1851 g_strlcpy(__Zephyr_realm, (const char*)zephyr->realm, REALM_SZ-1);
1852 zephyr->username = g_strdup(ZGetSender());
1853
1854 /* zephyr->realm = g_strdup(ZGetRealm()); */
1855 gaim_debug_info("zephyr","realm: %s\n",zephyr->realm);
1856 }
1857 else {
1858 gaim_connection_error(gc,"Only ZEPH0.2 supported currently");
1859 return;
1860 }
1861 gaim_debug_info("zephyr","does it get here\n");
1862 gaim_debug_info("zephyr"," realm: %s username:%s\n", zephyr->realm, zephyr->username);
1863
1864 /* For now */
1865 zephyr->galaxy = NULL;
1866 zephyr->krbtkfile = NULL;
1867 zephyr_inithosts(zephyr);
1868
1869 if (zephyr_subscribe_to(zephyr,"MESSAGE","PERSONAL",zephyr->username,NULL) != ZERR_NONE) {
1870 /* XXX don't translate this yet. It could be written better */
1871 /* XXX error messages could be handled with more detail */
1872 gaim_notify_error(account->gc, NULL,
1873 "Unable to subscribe to messages", "Unable to subscribe to initial messages");
1874 return;
1875 }
1876
1877 gaim_connection_set_state(gc, GAIM_CONNECTED);
1878
1879 if (read_anyone)
1880 process_anyone(gc);
1881 if (read_zsubs)
1882 process_zsubs(zephyr);
1883
1884 if (use_zeph02(zephyr)) {
1885 zephyr->nottimer = gaim_timeout_add(100, check_notify_zeph02, gc);
1886 } else if (use_tzc(zephyr)) {
1887 zephyr->nottimer = gaim_timeout_add(100, check_notify_tzc, gc);
1888 }
1889 zephyr->loctimer = gaim_timeout_add(20000, check_loc, gc);
1890
1891 }
1892
1893 static void write_zsubs(zephyr_account *zephyr)
1894 {
1895 /* Exports subscription (chat) list back to
1896 * .zephyr.subs
1897 * XXX deal with %host%, %canon%, unsubscriptions, and negative subscriptions (punts?)
1898 */
1899
1900 GSList *s = zephyr->subscrips;
1901 zephyr_triple *zt;
1902 FILE *fd;
1903 char *fname;
1904
1905 char **triple;
1906
1907 fname = g_strdup_printf("%s/.zephyr.subs", gaim_home_dir());
1908 fd = g_fopen(fname, "w");
1909
1910 if (!fd) {
1911 g_free(fname);
1912 return;
1913 }
1914
1915 while (s) {
1916 char *zclass, *zinst, *zrecip;
1917 zt = s->data;
1918 triple = g_strsplit(zt->name, ",", 3);
1919
1920 /* deal with classes */
1921 if (!g_ascii_strcasecmp(triple[0],zephyr->ourhost)) {
1922 zclass = g_strdup("%host%");
1923 } else if (!g_ascii_strcasecmp(triple[0],zephyr->ourhostcanon)) {
1924 zclass = g_strdup("%canon%");
1925 } else {
1926 zclass = g_strdup(triple[0]);
1927 }
1928
1929 /* deal with instances */
1930
1931 if (!g_ascii_strcasecmp(triple[1],zephyr->ourhost)) {
1932 zinst = g_strdup("%host%");
1933 } else if (!g_ascii_strcasecmp(triple[1],zephyr->ourhostcanon)) {
1934 zinst = g_strdup("%canon%");;
1935 } else {
1936 zinst = g_strdup(triple[1]);
1937 }
1938
1939 /* deal with recipients */
1940 if (triple[2] == NULL) {
1941 zrecip = g_strdup("*");
1942 } else if (!g_ascii_strcasecmp(triple[2],"")){
1943 zrecip = g_strdup("*");
1944 } else if (!g_ascii_strcasecmp(triple[2], zephyr->username)) {
1945 zrecip = g_strdup("%me%");
1946 } else {
1947 zrecip = g_strdup(triple[2]);
1948 }
1949
1950 fprintf(fd, "%s,%s,%s\n",zclass,zinst,zrecip);
1951
1952 g_free(zclass);
1953 g_free(zinst);
1954 g_free(zrecip);
1955 g_free(triple);
1956 s = s->next;
1957 }
1958 g_free(fname);
1959 fclose(fd);
1960 }
1961
1962 static void write_anyone(GaimConnection *gc)
1963 {
1964 GaimBlistNode *gnode, *cnode, *bnode;
1965 GaimBuddy *b;
1966 char *fname;
1967 FILE *fd;
1968 zephyr_account* zephyr = gc->proto_data;
1969 fname = g_strdup_printf("%s/.anyone", gaim_home_dir());
1970 fd = g_fopen(fname, "w");
1971 if (!fd) {
1972 g_free(fname);
1973 return;
1974 }
1975
1976 for (gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) {
1977 if (!GAIM_BLIST_NODE_IS_GROUP(gnode))
1978 continue;
1979 for (cnode = gnode->child; cnode; cnode = cnode->next) {
1980 if (!GAIM_BLIST_NODE_IS_CONTACT(cnode))
1981 continue;
1982 for (bnode = cnode->child; bnode; bnode = bnode->next) {
1983 if (!GAIM_BLIST_NODE_IS_BUDDY(bnode))
1984 continue;
1985 b = (GaimBuddy *) bnode;
1986 if (b->account == gc->account) {
1987 gchar *stripped_user = zephyr_strip_local_realm(zephyr,b->name);
1988 fprintf(fd, "%s\n", stripped_user);
1989 g_free(stripped_user);
1990 }
1991 }
1992 }
1993 }
1994
1995 fclose(fd);
1996 g_free(fname);
1997 }
1998
1999 static void zephyr_close(GaimConnection * gc)
2000 {
2001 GList *l;
2002 GSList *s;
2003 zephyr_account *zephyr = gc->proto_data;
2004 pid_t tzc_pid = zephyr->tzc_pid;
2005
2006 l = zephyr->pending_zloc_names;
2007 while (l) {
2008 g_free((char *)l->data);
2009 l = l->next;
2010 }
2011 g_list_free(zephyr->pending_zloc_names);
2012
2013 if (gaim_account_get_bool(gc->account, "write_anyone", FALSE))
2014 write_anyone(gc);
2015
2016 if (gaim_account_get_bool(gc->account, "write_zsubs", FALSE))
2017 write_zsubs(gc->proto_data);
2018
2019 s = zephyr->subscrips;
2020 while (s) {
2021 free_triple((zephyr_triple *) s->data);
2022 s = s->next;
2023 }
2024 g_slist_free(zephyr->subscrips);
2025
2026 if (zephyr->nottimer)
2027 gaim_timeout_remove(zephyr->nottimer);
2028 zephyr->nottimer = 0;
2029 if (zephyr->loctimer)
2030 gaim_timeout_remove(zephyr->loctimer);
2031 zephyr->loctimer = 0;
2032 gc = NULL;
2033 if (use_zeph02(zephyr)) {
2034 z_call(ZCancelSubscriptions(0));
2035 z_call(ZUnsetLocation());
2036 z_call(ZClosePort());
2037 } else {
2038 /* assume tzc */
2039 if (kill(tzc_pid,SIGTERM) == -1) {
2040 int err=errno;
2041 if (err==EINVAL) {
2042 gaim_debug_error("zephyr","An invalid signal was specified when killing tzc\n");
2043 }
2044 else if (err==ESRCH) {
2045 gaim_debug_error("zephyr","Tzc's pid didn't exist while killing tzc\n");
2046 }
2047 else if (err==EPERM) {
2048 gaim_debug_error("zephyr","gaim didn't have permission to kill tzc\n");
2049 }
2050 else {
2051 gaim_debug_error("zephyr","miscellaneous error while attempting to close tzc\n");
2052 }
2053 }
2054 }
2055 }
2056
2057 static int zephyr_send_message(zephyr_account *zephyr,char* zclass, char* instance, char* recipient, const char *im,
2058 const char *sig, char *opcode) ;
2059
2060 static const char * zephyr_get_signature()
2061 {
2062 /* XXX add zephyr error reporting */
2063 const char * sig =ZGetVariable("zwrite-signature");
2064 if (!sig) {
2065 sig = g_get_real_name();
2066 }
2067 return sig;
2068 }
2069
2070 static int zephyr_chat_send(GaimConnection * gc, int id, const char *im, GaimMessageFlags flags)
2071 {
2072 zephyr_triple *zt;
2073 const char *sig;
2074 GaimConversation *gconv1;
2075 GaimConvChat *gcc;
2076 char *inst;
2077 char *recipient;
2078 zephyr_account *zephyr = gc->proto_data;
2079
2080 zt = find_sub_by_id(gc->proto_data,id);
2081 if (!zt)
2082 /* this should never happen. */
2083 return -EINVAL;
2084
2085 sig = zephyr_get_signature();
2086
2087 gconv1 = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, zt->name,
2088 gc->account);
2089 gcc = gaim_conversation_get_chat_data(gconv1);
2090
2091 if (!(inst = (char *)gaim_conv_chat_get_topic(gcc)))
2092 inst = g_strdup("PERSONAL");
2093
2094 if (!g_ascii_strcasecmp(zt->recipient, "*"))
2095 recipient = local_zephyr_normalize(zephyr,"");
2096 else
2097 recipient = local_zephyr_normalize(zephyr,zt->recipient);
2098
2099 zephyr_send_message(zephyr,zt->class,inst,recipient,im,sig,"");
2100 return 0;
2101 }
2102
2103
2104 static int zephyr_send_im(GaimConnection * gc, const char *who, const char *im, GaimMessageFlags flags)
2105 {
2106 const char *sig;
2107 zephyr_account *zephyr = gc->proto_data;
2108 if (flags & GAIM_MESSAGE_AUTO_RESP)
2109 sig = "Automated reply:";
2110 else {
2111 sig = zephyr_get_signature();
2112 }
2113 zephyr_send_message(zephyr,"MESSAGE","PERSONAL",local_zephyr_normalize(zephyr,who),im,sig,"");
2114
2115 return 1;
2116 }
2117
2118 /* Munge the outgoing zephyr so that any quotes or backslashes are
2119 escaped and do not confuse tzc: */
2120
2121 static char* zephyr_tzc_escape_msg(const char *message)
2122 {
2123 int pos = 0;
2124 int pos2 = 0;
2125 char *newmsg;
2126
2127 if (message && (strlen(message) > 0)) {
2128 newmsg = g_new0(char,1+strlen(message)*2);
2129 while(pos < strlen(message)) {
2130 if (message[pos]=='\\') {
2131 newmsg[pos2]='\\';
2132 newmsg[pos2+1]='\\';
2133 pos2+=2;
2134 }
2135 else if (message[pos]=='"') {
2136 newmsg[pos2]='\\';
2137 newmsg[pos2+1]='"';
2138 pos2+=2;
2139 }
2140 else {
2141 newmsg[pos2] = message[pos];
2142 pos2++;
2143 }
2144 pos++;
2145 }
2146 } else {
2147 newmsg = g_strdup("");
2148 }
2149 /* fprintf(stderr,"newmsg %s message %s\n",newmsg,message); */
2150 return newmsg;
2151 }
2152
2153 char* zephyr_tzc_deescape_str(const char *message)
2154 {
2155 int pos = 0;
2156 int pos2 = 0;
2157 char *newmsg;
2158
2159 if (message && (strlen(message) > 0)) {
2160 newmsg = g_new0(char,strlen(message)+1);
2161 while(pos < strlen(message)) {
2162 if (message[pos]=='\\') {
2163 pos++;
2164 }
2165 newmsg[pos2] = message[pos];
2166 pos++;pos2++;
2167 }
2168 newmsg[pos2]='\0';
2169 } else {
2170 newmsg = g_strdup("");
2171 }
2172
2173 return newmsg;
2174 }
2175
2176 static int zephyr_send_message(zephyr_account *zephyr,char* zclass, char* instance, char* recipient, const char *im,
2177 const char *sig, char *opcode)
2178 {
2179
2180 /* (From the tzc source)
2181 * emacs sends something of the form:
2182 * ((class . "MESSAGE")
2183 * (auth . t)
2184 * (recipients ("PERSONAL" . "bovik") ("test" . ""))
2185 * (sender . "bovik")
2186 * (message . ("Harry Bovik" "my zgram"))
2187 * )
2188 */
2189 char *html_buf;
2190 char *html_buf2;
2191 html_buf = html_to_zephyr(im);
2192 html_buf2 = gaim_unescape_html(html_buf);
2193
2194 if(use_tzc(zephyr)) {
2195 char* zsendstr;
2196 /* CMU cclub tzc doesn't grok opcodes for now */
2197 char* tzc_sig = zephyr_tzc_escape_msg(sig);
2198 char *tzc_body = zephyr_tzc_escape_msg(html_buf2);
2199 zsendstr = g_strdup_printf("((tzcfodder . send) (class . \"%s\") (auth . t) (recipients (\"%s\" . \"%s\")) (message . (\"%s\" \"%s\")) ) \n",
2200 zclass, instance, recipient, tzc_sig, tzc_body);
2201 /* fprintf(stderr,"zsendstr = %s\n",zsendstr); */
2202 write(zephyr->totzc[ZEPHYR_FD_WRITE],zsendstr,strlen(zsendstr));
2203 g_free(zsendstr);
2204 } else if (use_zeph02(zephyr)) {
2205 ZNotice_t notice;
2206 char *buf = g_strdup_printf("%s%c%s", sig, '\0', html_buf2);
2207 bzero((char *)&notice, sizeof(notice));
2208
2209 notice.z_kind = ACKED;
2210 notice.z_port = 0;
2211 notice.z_opcode = "";
2212 notice.z_class = zclass;
2213 notice.z_class_inst = instance;
2214 notice.z_recipient = recipient;
2215 notice.z_sender = 0;
2216 notice.z_default_format = "Class $class, Instance $instance:\n" "To: @bold($recipient) at $time $date\n" "From: @bold($1) <$sender>\n\n$2";
2217 notice.z_message_len = strlen(html_buf2) + strlen(sig) + 2;
2218 notice.z_message = buf;
2219 notice.z_opcode = g_strdup(opcode);
2220 gaim_debug_info("zephyr","About to send notice");
2221 if (! ZSendNotice(&notice, ZAUTH) == ZERR_NONE) {
2222 /* XXX handle errors here */
2223 return 0;
2224 }
2225 gaim_debug_info("zephyr","notice sent");
2226 g_free(buf);
2227 }
2228
2229 g_free(html_buf2);
2230 g_free(html_buf);
2231
2232 return 1;
2233 }
2234
2235 char *local_zephyr_normalize(zephyr_account *zephyr,const char *orig)
2236 {
2237 /*
2238 Basically the inverse of zephyr_strip_local_realm
2239 */
2240 char* buf;
2241
2242 if (!g_ascii_strcasecmp(orig, "")) {
2243 return g_strdup("");
2244 }
2245
2246 if (strchr(orig,'@')) {
2247 buf = g_strdup_printf("%s",orig);
2248 } else {
2249 buf = g_strdup_printf("%s@%s",orig,zephyr->realm);
2250 }
2251 return buf;
2252 }
2253
2254 static void zephyr_zloc(GaimConnection *gc, const char *who)
2255 {
2256 ZAsyncLocateData_t ald;
2257 zephyr_account *zephyr = gc->proto_data;
2258 gchar* normalized_who = local_zephyr_normalize(zephyr,who);
2259
2260 if (use_zeph02(zephyr)) {
2261 if (ZRequestLocations(normalized_who, &ald, UNACKED, ZAUTH) == ZERR_NONE) {
2262 zephyr->pending_zloc_names = g_list_append(zephyr->pending_zloc_names,
2263 g_strdup(normalized_who));
2264 } else {
2265 /* XXX deal with errors somehow */
2266 }
2267 } else if (use_tzc(zephyr)) {
2268 char* zlocstr = g_strdup_printf("((tzcfodder . zlocate) \"%s\")\n",normalized_who);
2269 zephyr->pending_zloc_names = g_list_append(zephyr->pending_zloc_names, g_strdup(normalized_who));
2270 write(zephyr->totzc[ZEPHYR_FD_WRITE],zlocstr,strlen(zlocstr));
2271 g_free(zlocstr);
2272 }
2273 }
2274
2275 static void zephyr_set_status(GaimAccount *account, GaimStatus *status) {
2276 zephyr_account *zephyr = gaim_account_get_connection(account)->proto_data;
2277 GaimStatusPrimitive primitive = gaim_status_type_get_primitive(gaim_status_get_type(status));
2278
2279 if (zephyr->away) {
2280 g_free(zephyr->away);
2281 zephyr->away=NULL;
2282 }
2283
2284 if (primitive == GAIM_STATUS_AWAY) {
2285 zephyr->away = g_strdup(gaim_status_get_attr_string(status,"message"));
2286 }
2287 else if (primitive == GAIM_STATUS_AVAILABLE) {
2288 if (use_zeph02(zephyr)) {
2289 ZSetLocation(zephyr->exposure);
2290 }
2291 else {
2292 char *zexpstr = g_strdup_printf("((tzcfodder . set-location) (hostname . \"%s\") (exposure . \"%s\"))\n",zephyr->ourhost,zephyr->exposure);
2293 write(zephyr->totzc[ZEPHYR_FD_WRITE],zexpstr,strlen(zexpstr));
2294 g_free(zexpstr);
2295 }
2296 }
2297 else if (primitive == GAIM_STATUS_INVISIBLE) {
2298 /* XXX handle errors */
2299 if (use_zeph02(zephyr)) {
2300 ZSetLocation(EXPOSE_OPSTAFF);
2301 } else {
2302 char *zexpstr = g_strdup_printf("((tzcfodder . set-location) (hostname . \"%s\") (exposure . \"%s\"))\n",zephyr->ourhost,EXPOSE_OPSTAFF);
2303 write(zephyr->totzc[ZEPHYR_FD_WRITE],zexpstr,strlen(zexpstr));
2304 g_free(zexpstr);
2305 }
2306 }
2307 }
2308
2309 static GList *zephyr_status_types(GaimAccount *account)
2310 {
2311 GaimStatusType *type;
2312 GList *types = NULL;
2313
2314 /* zephyr has several exposures
2315 NONE (where you are hidden, and zephyrs to you are in practice silently dropped -- yes this is wrong)
2316 OPSTAFF "hidden"
2317 REALM-VISIBLE visible to people in local realm
2318 REALM-ANNOUNCED REALM-VISIBLE+ plus your logins/logouts are announced to <login,username,*>
2319 NET-VISIBLE REALM-ANNOUNCED, plus visible to people in foreign realm
2320 NET-ANNOUNCED NET-VISIBLE, plus logins/logouts are announced to <login,username,*>
2321
2322 Online will set the user to the exposure they have in their options (defaulting to REALM-VISIBLE),
2323 Hidden, will set the user's exposure to OPSTAFF
2324
2325 Away won't change their exposure but will set an auto away message (for IMs only)
2326 */
2327
2328 type = gaim_status_type_new(GAIM_STATUS_AVAILABLE, NULL, NULL, TRUE);
2329 types = g_list_append(types,type);
2330
2331 type = gaim_status_type_new(GAIM_STATUS_INVISIBLE, NULL, NULL, TRUE);
2332 types = g_list_append(types,type);
2333
2334 type = gaim_status_type_new_with_attrs(
2335 GAIM_STATUS_AWAY, NULL, NULL, TRUE, TRUE, FALSE,
2336 "message", _("Message"), gaim_value_new(GAIM_TYPE_STRING),
2337 NULL);
2338 types = g_list_append(types, type);
2339
2340 type = gaim_status_type_new(GAIM_STATUS_OFFLINE, NULL, NULL, TRUE);
2341 types = g_list_append(types,type);
2342
2343 return types;
2344 }
2345
2346 static GList *zephyr_chat_info(GaimConnection * gc)
2347 {
2348 GList *m = NULL;
2349 struct proto_chat_entry *pce;
2350
2351 pce = g_new0(struct proto_chat_entry, 1);
2352
2353 pce->label = _("_Class:");
2354 pce->identifier = "class";
2355 m = g_list_append(m, pce);
2356
2357 pce = g_new0(struct proto_chat_entry, 1);
2358
2359 pce->label = _("_Instance:");
2360 pce->identifier = "instance";
2361 m = g_list_append(m, pce);
2362
2363 pce = g_new0(struct proto_chat_entry, 1);
2364
2365 pce->label = _("_Recipient:");
2366 pce->identifier = "recipient";
2367 m = g_list_append(m, pce);
2368
2369 return m;
2370 }
2371
2372 /* Called when the server notifies us a message couldn't get sent */
2373
2374 static void zephyr_subscribe_failed(GaimConnection *gc,char * z_class, char *z_instance, char * z_recipient, char* z_galaxy)
2375 {
2376 gchar* subscribe_failed = g_strdup_printf(_("Attempt to subscribe to %s,%s,%s failed"), z_class, z_instance,z_recipient);
2377 gaim_notify_error(gc,"", subscribe_failed, NULL);
2378 g_free(subscribe_failed);
2379 }
2380
2381 static char *zephyr_get_chat_name(GHashTable *data) {
2382 gchar* zclass = g_hash_table_lookup(data,"class");
2383 gchar* inst = g_hash_table_lookup(data,"instance");
2384 gchar* recipient = g_hash_table_lookup(data, "recipient");
2385 if (!zclass) /* This should never happen */
2386 zclass = "";
2387 if (!inst)
2388 inst = "*";
2389 if (!recipient)
2390 recipient = "";
2391 return g_strdup_printf("%s,%s,%s",zclass,inst,recipient);
2392 }
2393
2394
2395 static void zephyr_join_chat(GaimConnection * gc, GHashTable * data)
2396 {
2397 /* ZSubscription_t sub; */
2398 zephyr_triple *zt1, *zt2;
2399 const char *classname;
2400 const char *instname;
2401 const char *recip;
2402 zephyr_account *zephyr=gc->proto_data;
2403 classname = g_hash_table_lookup(data, "class");
2404 instname = g_hash_table_lookup(data, "instance");
2405 recip = g_hash_table_lookup(data, "recipient");
2406
2407
2408 if (!classname)
2409 return;
2410
2411 if (!g_ascii_strcasecmp(classname,"%host%"))
2412 classname = g_strdup(zephyr->ourhost);
2413 if (!g_ascii_strcasecmp(classname,"%canon%"))
2414 classname = g_strdup(zephyr->ourhostcanon);
2415
2416 if (!instname || !strlen(instname))
2417 instname = "*";
2418
2419 if (!g_ascii_strcasecmp(instname,"%host%"))
2420 instname = g_strdup(zephyr->ourhost);
2421 if (!g_ascii_strcasecmp(instname,"%canon%"))
2422 instname = g_strdup(zephyr->ourhostcanon);
2423
2424 if (!recip || (*recip == '*'))
2425 recip = "";
2426 if (!g_ascii_strcasecmp(recip, "%me%"))
2427 recip = zephyr->username;
2428
2429 zt1 = new_triple(gc->proto_data,classname, instname, recip);
2430 zt2 = find_sub_by_triple(gc->proto_data,zt1);
2431 if (zt2) {
2432 free_triple(zt1);
2433 if (!zt2->open) {
2434 if (!g_ascii_strcasecmp(instname,"*"))
2435 instname = "PERSONAL";
2436 serv_got_joined_chat(gc, zt2->id, zt2->name);
2437 zephyr_chat_set_topic(gc,zt2->id,instname);
2438 zt2->open = TRUE;
2439 }
2440 return;
2441 }
2442
2443 /* sub.zsub_class = zt1->class;
2444 sub.zsub_classinst = zt1->instance;
2445 sub.zsub_recipient = zt1->recipient; */
2446
2447 if (zephyr_subscribe_to(zephyr,zt1->class,zt1->instance,zt1->recipient,NULL) != ZERR_NONE) {
2448 /* XXX output better subscription information */
2449 zephyr_subscribe_failed(gc,zt1->class,zt1->instance,zt1->recipient,NULL);
2450 free_triple(zt1);
2451 return;
2452 }
2453
2454 zephyr->subscrips = g_slist_append(zephyr->subscrips, zt1);
2455 zt1->open = TRUE;
2456 serv_got_joined_chat(gc, zt1->id, zt1->name);
2457 if (!g_ascii_strcasecmp(instname,"*"))
2458 instname = "PERSONAL";
2459 zephyr_chat_set_topic(gc,zt1->id,instname);
2460 }
2461
2462 static void zephyr_chat_leave(GaimConnection * gc, int id)
2463 {
2464 zephyr_triple *zt;
2465 zephyr_account *zephyr = gc->proto_data;
2466 zt = find_sub_by_id(zephyr,id);
2467
2468 if (zt) {
2469 zt->open = FALSE;
2470 zt->id = ++(zephyr->last_id);
2471 }
2472 }
2473
2474 static GaimChat *zephyr_find_blist_chat(GaimAccount *account, const char *name)
2475 {
2476 GaimBlistNode *gnode, *cnode;
2477
2478 /* XXX needs to be %host%,%canon%, and %me% clean */
2479 for(gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) {
2480 for(cnode = gnode->child; cnode; cnode = cnode->next) {
2481 GaimChat *chat = (GaimChat*)cnode;
2482 char *zclass, *inst, *recip;
2483 char** triple;
2484 if(!GAIM_BLIST_NODE_IS_CHAT(cnode))
2485 continue;
2486 if(chat->account !=account)
2487 continue;
2488 if(!(zclass = g_hash_table_lookup(chat->components, "class")))
2489 continue;
2490 if(!(inst = g_hash_table_lookup(chat->components, "instance")))
2491 inst = g_strdup("");
2492 if(!(recip = g_hash_table_lookup(chat->components, "recipient")))
2493 recip = g_strdup("");
2494 /* gaim_debug_info("zephyr","in zephyr_find_blist_chat name: %s\n",name?name:""); */
2495 triple = g_strsplit(name,",",3);
2496 if (!g_ascii_strcasecmp(triple[0],zclass) && !g_ascii_strcasecmp(triple[1],inst) && !g_ascii_strcasecmp(triple[2],recip))
2497 return chat;
2498
2499 }
2500 }
2501 return NULL;
2502 }
2503 static const char *zephyr_list_icon(GaimAccount * a, GaimBuddy * b)
2504 {
2505 return "zephyr";
2506 }
2507
2508 static unsigned int zephyr_send_typing(GaimConnection *gc, const char *who, GaimTypingState state) {
2509 gchar *recipient;
2510 zephyr_account *zephyr = gc->proto_data;
2511 if (use_tzc(zephyr))
2512 return 0;
2513
2514 if (state == GAIM_NOT_TYPING)
2515 return 0;
2516
2517 /* XXX We probably should care if this fails. Or maybe we don't want to */
2518 if (!who) {
2519 gaim_debug_info("zephyr", "who is null\n");
2520 recipient = local_zephyr_normalize(zephyr,"");
2521 } else {
2522 char *comma = strrchr(who, ',');
2523 /* Don't ping broadcast (chat) recipients */
2524 /* The strrchr case finds a realm-stripped broadcast subscription
2525 e.g. comma is the last character in the string */
2526 if (comma && ( (*(comma+1) == '\0') || (*(comma+1) == '@')))
2527 return 0;
2528
2529 recipient = local_zephyr_normalize(zephyr,who);
2530 }
2531
2532 gaim_debug_info("zephyr","about to send typing notification to %s\n",recipient);
2533 zephyr_send_message(zephyr,"MESSAGE","PERSONAL",recipient,"","","PING");
2534 gaim_debug_info("zephyr","sent typing notification\n");
2535
2536 /*
2537 * TODO: Is this correct? It means we will call
2538 * serv_send_typing(gc, who, GAIM_TYPING) once every 15 seconds
2539 * until the Gaim user stops typing.
2540 */
2541 return ZEPHYR_TYPING_SEND_TIMEOUT;
2542 }
2543
2544
2545
2546 static void zephyr_chat_set_topic(GaimConnection * gc, int id, const char *topic)
2547 {
2548 zephyr_triple *zt;
2549 GaimConversation *gconv;
2550 GaimConvChat *gcc;
2551 gchar *topic_utf8;
2552 zephyr_account* zephyr = gc->proto_data;
2553 char *sender = (char *)zephyr->username;
2554
2555 zt = find_sub_by_id(gc->proto_data,id);
2556 /* find_sub_by_id can return NULL */
2557 if (!zt)
2558 return;
2559 gconv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, zt->name,
2560 gc->account);
2561 gcc = gaim_conversation_get_chat_data(gconv);
2562
2563 topic_utf8 = zephyr_recv_convert(gc,(gchar *)topic,strlen(topic));
2564 gaim_conv_chat_set_topic(gcc,sender,topic_utf8);
2565 g_free(topic_utf8);
2566 return;
2567 }
2568
2569 /* commands */
2570
2571 static GaimCmdRet zephyr_gaim_cmd_msg(GaimConversation *conv,
2572 const char *cmd, char **args, char **error, void *data)
2573 {
2574 char *recipient;
2575 zephyr_account *zephyr = gaim_conversation_get_gc(conv)->proto_data;
2576 if (!g_ascii_strcasecmp(args[0],"*"))
2577 return GAIM_CMD_RET_FAILED; /* "*" is not a valid argument */
2578 else
2579 recipient = local_zephyr_normalize(zephyr,args[0]);
2580
2581 if (strlen(recipient) < 1)
2582 return GAIM_CMD_RET_FAILED; /* a null recipient is a chat message, not an IM */
2583
2584 if (zephyr_send_message(zephyr,"MESSAGE","PERSONAL",recipient,args[1],zephyr_get_signature(),""))
2585 return GAIM_CMD_RET_OK;
2586 else
2587 return GAIM_CMD_RET_FAILED;
2588 }
2589
2590 static GaimCmdRet zephyr_gaim_cmd_zlocate(GaimConversation *conv,
2591 const char *cmd, char **args, char **error, void *data)
2592 {
2593 zephyr_zloc(gaim_conversation_get_gc(conv),args[0]);
2594 return GAIM_CMD_RET_OK;
2595 }
2596
2597 static GaimCmdRet zephyr_gaim_cmd_instance(GaimConversation *conv,
2598 const char *cmd, char **args, char **error, void *data)
2599 {
2600 /* Currently it sets the instance with leading spaces and
2601 * all. This might not be the best thing to do, though having
2602 * one word isn't ideal either. */
2603
2604 GaimConvChat *gcc = gaim_conversation_get_chat_data(conv);
2605 int id = gcc->id;
2606 const char* instance = args[0];
2607 zephyr_chat_set_topic(gaim_conversation_get_gc(conv),id,instance);
2608 return GAIM_CMD_RET_OK;
2609 }
2610
2611 static GaimCmdRet zephyr_gaim_cmd_joinchat_cir(GaimConversation *conv,
2612 const char *cmd, char **args, char **error, void *data)
2613 {
2614 /* Join a new zephyr chat */
2615 GHashTable *triple = g_hash_table_new(NULL,NULL);
2616 g_hash_table_insert(triple,"class",args[0]);
2617 g_hash_table_insert(triple,"instance",args[1]);
2618 g_hash_table_insert(triple,"recipient",args[2]);
2619 zephyr_join_chat(gaim_conversation_get_gc(conv),triple);
2620 return GAIM_CMD_RET_OK;
2621 }
2622
2623 static GaimCmdRet zephyr_gaim_cmd_zi(GaimConversation *conv,
2624 const char *cmd, char **args, char **error, void *data)
2625 {
2626 /* args = instance, message */
2627 zephyr_account *zephyr = gaim_conversation_get_gc(conv)->proto_data;
2628 if ( zephyr_send_message(zephyr,"message",args[0],"",args[1],zephyr_get_signature(),""))
2629 return GAIM_CMD_RET_OK;
2630 else
2631 return GAIM_CMD_RET_FAILED;
2632 }
2633
2634 static GaimCmdRet zephyr_gaim_cmd_zci(GaimConversation *conv,
2635 const char *cmd, char **args, char **error, void *data)
2636 {
2637 /* args = class, instance, message */
2638 zephyr_account *zephyr = gaim_conversation_get_gc(conv)->proto_data;
2639 if ( zephyr_send_message(zephyr,args[0],args[1],"",args[2],zephyr_get_signature(),""))
2640 return GAIM_CMD_RET_OK;
2641 else
2642 return GAIM_CMD_RET_FAILED;
2643 }
2644
2645 static GaimCmdRet zephyr_gaim_cmd_zcir(GaimConversation *conv,
2646 const char *cmd, char **args, char **error, void *data)
2647 {
2648 /* args = class, instance, recipient, message */
2649 zephyr_account *zephyr = gaim_conversation_get_gc(conv)->proto_data;
2650 if ( zephyr_send_message(zephyr,args[0],args[1],args[2],args[3],zephyr_get_signature(),""))
2651 return GAIM_CMD_RET_OK;
2652 else
2653 return GAIM_CMD_RET_FAILED;
2654 }
2655
2656 static GaimCmdRet zephyr_gaim_cmd_zir(GaimConversation *conv,
2657 const char *cmd, char **args, char **error, void *data)
2658 {
2659 /* args = instance, recipient, message */
2660 zephyr_account *zephyr = gaim_conversation_get_gc(conv)->proto_data;
2661 if ( zephyr_send_message(zephyr,"message",args[0],args[1],args[2],zephyr_get_signature(),""))
2662 return GAIM_CMD_RET_OK;
2663 else
2664 return GAIM_CMD_RET_FAILED;
2665 }
2666
2667 static GaimCmdRet zephyr_gaim_cmd_zc(GaimConversation *conv,
2668 const char *cmd, char **args, char **error, void *data)
2669 {
2670 /* args = class, message */
2671 zephyr_account *zephyr = gaim_conversation_get_gc(conv)->proto_data;
2672 if ( zephyr_send_message(zephyr,args[0],"PERSONAL","",args[1],zephyr_get_signature(),""))
2673 return GAIM_CMD_RET_OK;
2674 else
2675 return GAIM_CMD_RET_FAILED;
2676 }
2677
2678 static void zephyr_register_slash_commands()
2679 {
2680
2681 gaim_cmd_register("msg","ws", GAIM_CMD_P_PRPL,
2682 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2683 "prpl-zephyr",
2684 zephyr_gaim_cmd_msg, _("msg &lt;nick&gt; &lt;message&gt;: Send a private message to a user"), NULL);
2685
2686 gaim_cmd_register("zlocate","w", GAIM_CMD_P_PRPL,
2687 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2688 "prpl-zephyr",
2689 zephyr_gaim_cmd_zlocate, _("zlocate &lt;nick&gt;: Locate user"), NULL);
2690
2691 gaim_cmd_register("zl","w", GAIM_CMD_P_PRPL,
2692 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2693 "prpl-zephyr",
2694 zephyr_gaim_cmd_zlocate, _("zl &lt;nick&gt;: Locate user"), NULL);
2695
2696 gaim_cmd_register("instance","s", GAIM_CMD_P_PRPL,
2697 GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2698 "prpl-zephyr",
2699 zephyr_gaim_cmd_instance, _("instance &lt;instance&gt;: Set the instance to be used on this class"), NULL);
2700
2701 gaim_cmd_register("inst","s", GAIM_CMD_P_PRPL,
2702 GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2703 "prpl-zephyr",
2704 zephyr_gaim_cmd_instance, _("inst &lt;instance&gt;: Set the instance to be used on this class"), NULL);
2705
2706 gaim_cmd_register("topic","s", GAIM_CMD_P_PRPL,
2707 GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2708 "prpl-zephyr",
2709 zephyr_gaim_cmd_instance, _("topic &lt;instance&gt;: Set the instance to be used on this class"), NULL);
2710
2711 gaim_cmd_register("sub", "www", GAIM_CMD_P_PRPL,
2712 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2713 "prpl-zephyr",
2714 zephyr_gaim_cmd_joinchat_cir,
2715 _("sub &lt;class&gt; &lt;instance&gt; &lt;recipient&gt;: Join a new chat"), NULL);
2716
2717 gaim_cmd_register("zi","ws", GAIM_CMD_P_PRPL,
2718 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2719 "prpl-zephyr",
2720 zephyr_gaim_cmd_zi, _("zi &lt;instance&gt;: Send a message to &lt;message,<i>instance</i>,*&gt;"), NULL);
2721
2722 gaim_cmd_register("zci","wws",GAIM_CMD_P_PRPL,
2723 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2724 "prpl-zephyr",
2725 zephyr_gaim_cmd_zci,
2726 _("zci &lt;class&gt; &lt;instance&gt;: Send a message to &lt;<i>class</i>,<i>instance</i>,*&gt;"), NULL);
2727
2728 gaim_cmd_register("zcir","wwws",GAIM_CMD_P_PRPL,
2729 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2730 "prpl-zephyr",
2731 zephyr_gaim_cmd_zcir,
2732 _("zcir &lt;class&gt; &lt;instance&gt; &lt;recipient&gt;: Send a message to &lt;<i>class</i>,<i>instance</i>,<i>recipient</i>&gt;"), NULL);
2733
2734 gaim_cmd_register("zir","wws",GAIM_CMD_P_PRPL,
2735 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2736 "prpl-zephyr",
2737 zephyr_gaim_cmd_zir,
2738 _("zir &lt;instance&gt; &lt;recipient&gt;: Send a message to &lt;MESSAGE,<i>instance</i>,<i>recipient</i>&gt;"), NULL);
2739
2740 gaim_cmd_register("zc","ws", GAIM_CMD_P_PRPL,
2741 GAIM_CMD_FLAG_IM | GAIM_CMD_FLAG_CHAT | GAIM_CMD_FLAG_PRPL_ONLY,
2742 "prpl-zephyr",
2743 zephyr_gaim_cmd_zc, _("zc &lt;class&gt;: Send a message to &lt;<i>class</i>,PERSONAL,*&gt;"), NULL);
2744
2745 }
2746
2747
2748 static void
2749 zephyr_add_deny(GaimConnection *gc, const char *who)
2750 {
2751 gaim_privacy_deny_add(gc->account,who,1);
2752 }
2753
2754 static void
2755 zephyr_remove_deny(GaimConnection *gc, const char *who)
2756 {
2757 gaim_privacy_deny_remove(gc->account,who,1);
2758 }
2759
2760 static void
2761 zephyr_add_permit(GaimConnection *gc, const char *who)
2762 {
2763 gaim_privacy_permit_add(gc->account,who,1);
2764 }
2765
2766 static void
2767 zephyr_remove_permit(GaimConnection *gc, const char *who)
2768 {
2769 gaim_privacy_permit_remove(gc->account,who,1);
2770 }
2771
2772 static void
2773 zephyr_set_permit_deny(GaimConnection *gc)
2774 {
2775 /* This doesn't have to do anything, since really, we can just check account->perm_deny when deciding whether to di */
2776 return;
2777 }
2778 static int zephyr_resubscribe(GaimConnection *gc)
2779 {
2780 /* Resubscribe to the in-memory list of subscriptions and also
2781 unsubscriptions*/
2782 zephyr_account *zephyr = gc->proto_data;
2783 GSList *s = zephyr->subscrips;
2784 zephyr_triple *zt;
2785 while (s) {
2786 zt = s->data;
2787 /* XXX We really should care if this fails */
2788 zephyr_subscribe_to(zephyr,zt->class,zt->instance,zt->recipient,NULL);
2789 s = s->next;
2790 }
2791 /* XXX handle unsubscriptions */
2792 return 1;
2793 }
2794
2795
2796 static void zephyr_action_resubscribe(GaimPluginAction *action)
2797 {
2798
2799 GaimConnection *gc = (GaimConnection *) action->context;
2800 zephyr_resubscribe(gc);
2801 }
2802
2803
2804 static void zephyr_action_get_subs_from_server(GaimPluginAction *action)
2805 {
2806 GaimConnection *gc = (GaimConnection *) action->context;
2807 zephyr_account *zephyr = gc->proto_data;
2808 gchar *title;
2809 int retval, nsubs, one,i;
2810 ZSubscription_t subs;
2811 if (use_zeph02(zephyr)) {
2812 GString* subout = g_string_new("Subscription list<br>");
2813
2814 title = g_strdup_printf("Server subscriptions for %s", zephyr->username);
2815
2816 if (zephyr->port == 0) {
2817 gaim_debug_error("zephyr", "error while retrieving port");
2818 return;
2819 }
2820 if ((retval = ZRetrieveSubscriptions(zephyr->port,&nsubs)) != ZERR_NONE) {
2821 /* XXX better error handling */
2822 gaim_debug_error("zephyr", "error while retrieving subscriptions from server");
2823 return;
2824 }
2825 for(i=0;i<nsubs;i++) {
2826 one = 1;
2827 if ((retval = ZGetSubscriptions(&subs,&one)) != ZERR_NONE) {
2828 /* XXX better error handling */
2829 gaim_debug_error("zephyr", "error while retrieving individual subscription");
2830 return;
2831 }
2832 g_string_append_printf(subout, "Class %s Instance %s Recipient %s<br>",
2833 subs.zsub_class, subs.zsub_classinst,
2834 subs.zsub_recipient);
2835 }
2836 gaim_notify_formatted(gc, title, title, NULL, subout->str, NULL, NULL);
2837 } else {
2838 /* XXX fix */
2839 gaim_notify_error(gc,"","tzc doesn't support this action",NULL);
2840 }
2841 }
2842
2843
2844 static GList *zephyr_actions(GaimPlugin *plugin, gpointer context)
2845 {
2846 GList *list = NULL;
2847 GaimPluginAction *act = NULL;
2848
2849 act = gaim_plugin_action_new(_("Resubscribe"), zephyr_action_resubscribe);
2850 list = g_list_append(list, act);
2851
2852 act = gaim_plugin_action_new(_("Retrieve subscriptions from server"), zephyr_action_get_subs_from_server);
2853 list = g_list_append(list,act);
2854
2855 return list;
2856 }
2857
2858 static GaimPlugin *my_protocol = NULL;
2859
2860 static GaimPluginProtocolInfo prpl_info = {
2861 OPT_PROTO_CHAT_TOPIC | OPT_PROTO_NO_PASSWORD,
2862 NULL, /* ??? user_splits */
2863 NULL, /* ??? protocol_options */
2864 NO_BUDDY_ICONS,
2865 zephyr_list_icon,
2866 NULL, /* ??? list_emblems */
2867 NULL, /* ??? status_text */
2868 NULL, /* ??? tooltip_text */
2869 zephyr_status_types, /* status_types */
2870 NULL, /* ??? blist_node_menu - probably all useful actions are already handled*/
2871 zephyr_chat_info, /* chat_info */
2872 NULL, /* chat_info_defaults */
2873 zephyr_login, /* login */
2874 zephyr_close, /* close */
2875 zephyr_send_im, /* send_im */
2876 NULL, /* XXX set info (Location?) */
2877 zephyr_send_typing, /* send_typing */
2878 zephyr_zloc, /* get_info */
2879 zephyr_set_status, /* set_status */
2880 NULL, /* ??? set idle */
2881 NULL, /* change password */
2882 NULL, /* add_buddy */
2883 NULL, /* add_buddies */
2884 NULL, /* remove_buddy */
2885 NULL, /* remove_buddies */
2886 zephyr_add_permit, /* add_permit */
2887 zephyr_add_deny, /* add_deny */
2888 zephyr_remove_permit, /* remove_permit */
2889 zephyr_remove_deny, /* remove_deny */
2890 zephyr_set_permit_deny, /* set_permit_deny */
2891 zephyr_join_chat, /* join_chat */
2892 NULL, /* reject_chat -- No chat invites*/
2893 zephyr_get_chat_name, /* get_chat_name */
2894 NULL, /* chat_invite -- No chat invites*/
2895 zephyr_chat_leave, /* chat_leave */
2896 NULL, /* chat_whisper -- No "whispering"*/
2897 zephyr_chat_send, /* chat_send */
2898 NULL, /* keepalive -- Not necessary*/
2899 NULL, /* register_user -- Not supported*/
2900 NULL, /* XXX get_cb_info */
2901 NULL, /* get_cb_away */
2902 NULL, /* alias_buddy */
2903 NULL, /* group_buddy */
2904 NULL, /* rename_group */
2905 NULL, /* buddy_free */
2906 NULL, /* convo_closed */
2907 NULL, /* normalize */
2908 NULL, /* XXX set_buddy_icon */
2909 NULL, /* remove_group */
2910 NULL, /* XXX get_cb_real_name */
2911 zephyr_chat_set_topic, /* set_chat_topic */
2912 zephyr_find_blist_chat, /* find_blist_chat */
2913 NULL, /* roomlist_get_list */
2914 NULL, /* roomlist_cancel */
2915 NULL, /* roomlist_expand_category */
2916 NULL, /* can_receive_file */
2917 NULL, /* send_file */
2918 NULL, /* new_xfer */
2919 NULL, /* offline_message */
2920 NULL, /* whiteboard_prpl_ops */
2921 NULL, /* send_raw */
2922 NULL, /* roomlist_room_serialize */
2923 };
2924
2925 static GaimPluginInfo info = {
2926 GAIM_PLUGIN_MAGIC,
2927 GAIM_MAJOR_VERSION,
2928 GAIM_MINOR_VERSION,
2929 GAIM_PLUGIN_PROTOCOL, /**< type */
2930 NULL, /**< ui_requirement */
2931 0, /**< flags */
2932 NULL, /**< dependencies */
2933 GAIM_PRIORITY_DEFAULT, /**< priority */
2934
2935 "prpl-zephyr", /**< id */
2936 "Zephyr", /**< name */
2937 VERSION, /**< version */
2938 /** summary */
2939 N_("Zephyr Protocol Plugin"),
2940 /** description */
2941 N_("Zephyr Protocol Plugin"),
2942 NULL, /**< author */
2943 GAIM_WEBSITE, /**< homepage */
2944
2945 NULL, /**< load */
2946 NULL, /**< unload */
2947 NULL, /**< destroy */
2948
2949 NULL, /**< ui_info */
2950 &prpl_info, /**< extra_info */
2951 NULL,
2952 zephyr_actions,
2953 };
2954
2955 static void init_plugin(GaimPlugin * plugin)
2956 {
2957 GaimAccountOption *option;
2958 char *tmp = get_exposure_level();
2959
2960 option = gaim_account_option_bool_new(_("Use tzc"), "use_tzc", FALSE);
2961 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2962
2963 option = gaim_account_option_string_new(_("tzc command"), "tzc_command", "/usr/bin/tzc -e %s");
2964 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2965
2966 option = gaim_account_option_bool_new(_("Export to .anyone"), "write_anyone", FALSE);
2967 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2968
2969 option = gaim_account_option_bool_new(_("Export to .zephyr.subs"), "write_zsubs", FALSE);
2970 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2971
2972 option = gaim_account_option_bool_new(_("Import from .anyone"), "read_anyone", TRUE);
2973 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2974
2975 option = gaim_account_option_bool_new(_("Import from .zephyr.subs"), "read_zsubs", TRUE);
2976 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2977
2978 option = gaim_account_option_string_new(_("Realm"), "realm", "");
2979 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2980
2981 option = gaim_account_option_string_new(_("Exposure"), "exposure_level", tmp?tmp: EXPOSE_REALMVIS);
2982 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2983
2984 option = gaim_account_option_string_new(_("Encoding"), "encoding", ZEPHYR_FALLBACK_CHARSET);
2985 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
2986
2987 my_protocol = plugin;
2988 zephyr_register_slash_commands();
2989 }
2990
2991 GAIM_INIT_PLUGIN(zephyr, init_plugin, info);