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