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