source: zephyr.c @ 5044ea9

release-1.8
Last change on this file since 5044ea9 was 6a08f16, checked in by David Benjamin <davidben@mit.edu>, 12 years ago
zephyr: Add an iterator interface to fields Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 39.7 KB
Line 
1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <sys/stat.h>
6#include <string.h>
7#include "owl.h"
8
9#ifdef HAVE_LIBZEPHYR
10static GSource *owl_zephyr_event_source_new(int fd);
11
12static gboolean owl_zephyr_event_prepare(GSource *source, int *timeout);
13static gboolean owl_zephyr_event_check(GSource *source);
14static gboolean owl_zephyr_event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data);
15
16static GList *deferred_subs = NULL;
17
18typedef struct _owl_sub_list {                            /* noproto */
19  ZSubscription_t *subs;
20  int nsubs;
21} owl_sub_list;
22
23Code_t ZResetAuthentication(void);
24
25static GSourceFuncs zephyr_event_funcs = {
26  owl_zephyr_event_prepare,
27  owl_zephyr_event_check,
28  owl_zephyr_event_dispatch,
29  NULL
30};
31#endif
32
33#define HM_SVC_FALLBACK         htons((unsigned short) 2104)
34
35static char *owl_zephyr_dotfile(const char *name, const char *input)
36{
37  if (input != NULL)
38    return g_strdup(input);
39  else
40    return g_build_filename(owl_global_get_homedir(&g), name, NULL);
41}
42
43#ifdef HAVE_LIBZEPHYR
44void owl_zephyr_initialize(void)
45{
46  Code_t ret;
47  struct servent *sp;
48  struct sockaddr_in sin;
49  ZNotice_t req;
50  GIOChannel *channel;
51
52  /*
53   * Code modified from libzephyr's ZhmStat.c
54   *
55   * Modified to add the fd to our select loop, rather than hanging
56   * until we get an ack.
57   */
58
59  if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
60    owl_function_error("Error opening Zephyr port: %s", error_message(ret));
61    return;
62  }
63
64  (void) memset(&sin, 0, sizeof(struct sockaddr_in));
65
66  sp = getservbyname(HM_SVCNAME, "udp");
67
68  sin.sin_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
69  sin.sin_family = AF_INET;
70
71  sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
72
73  (void) memset(&req, 0, sizeof(req));
74  req.z_kind = STAT;
75  req.z_port = 0;
76  req.z_class = zstr(HM_STAT_CLASS);
77  req.z_class_inst = zstr(HM_STAT_CLIENT);
78  req.z_opcode = zstr(HM_GIMMESTATS);
79  req.z_sender = zstr("");
80  req.z_recipient = zstr("");
81  req.z_default_format = zstr("");
82  req.z_message_len = 0;
83
84  if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
85    owl_function_error("Initializing Zephyr: %s", error_message(ret));
86    return;
87  }
88
89  if ((ret = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE) {
90    owl_function_error("Initializing Zephyr: %s", error_message(ret));
91    return;
92  }
93
94  channel = g_io_channel_unix_new(ZGetFD());
95  g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
96                 &owl_zephyr_finish_initialization, NULL);
97  g_io_channel_unref(channel);
98}
99
100gboolean owl_zephyr_finish_initialization(GIOChannel *source, GIOCondition condition, void *data) {
101  Code_t code;
102  char *perl;
103  GSource *event_source;
104
105  ZClosePort();
106
107  if ((code = ZInitialize()) != ZERR_NONE) {
108    owl_function_error("Initializing Zephyr: %s", error_message(code));
109    return FALSE;
110  }
111
112  if ((code = ZOpenPort(NULL)) != ZERR_NONE) {
113    owl_function_error("Initializing Zephyr: %s", error_message(code));
114    return FALSE;
115  }
116
117  event_source = owl_zephyr_event_source_new(ZGetFD());
118  g_source_attach(event_source, NULL);
119  g_source_unref(event_source);
120
121  owl_global_set_havezephyr(&g);
122
123  if(g.load_initial_subs) {
124    owl_zephyr_load_initial_subs();
125  }
126  while(deferred_subs != NULL) {
127    owl_sub_list *subs = deferred_subs->data;
128    owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs);
129    owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs);
130    deferred_subs = g_list_delete_link(deferred_subs, deferred_subs);
131    g_free(subs);
132  }
133
134  /* zlog in if we need to */
135  if (owl_global_is_startuplogin(&g)) {
136    owl_function_debugmsg("startup: doing zlog in");
137    owl_zephyr_zlog_in();
138  }
139  /* check pseudo-logins if we need to */
140  if (owl_global_is_pseudologins(&g)) {
141    owl_function_debugmsg("startup: checking pseudo-logins");
142    owl_function_zephyr_buddy_check(0);
143  }
144
145  perl = owl_perlconfig_execute("BarnOwl::Zephyr::_zephyr_startup()");
146  g_free(perl);
147  return FALSE;
148}
149
150void owl_zephyr_load_initial_subs(void) {
151  int ret_sd, ret_bd, ret_u;
152
153  owl_function_debugmsg("startup: loading initial zephyr subs");
154
155  /* load default subscriptions */
156  ret_sd = owl_zephyr_loaddefaultsubs();
157
158  /* load Barnowl default subscriptions */
159  ret_bd = owl_zephyr_loadbarnowldefaultsubs();
160
161  /* load subscriptions from subs file */
162  ret_u = owl_zephyr_loadsubs(NULL, 0);
163
164  if (ret_sd || ret_bd || ret_u) {
165    owl_function_error("Error loading zephyr subscriptions");
166  }
167
168  /* load login subscriptions */
169  if (owl_global_is_loginsubs(&g)) {
170    owl_function_debugmsg("startup: loading login subs");
171    owl_function_loadloginsubs(NULL);
172  }
173}
174#else
175void owl_zephyr_initialize(void)
176{
177}
178#endif
179
180
181int owl_zephyr_shutdown(void)
182{
183#ifdef HAVE_LIBZEPHYR
184  if(owl_global_is_havezephyr(&g)) {
185    unsuball();
186    ZClosePort();
187  }
188#endif
189  return 0;
190}
191
192int owl_zephyr_zpending(void)
193{
194#ifdef HAVE_LIBZEPHYR
195  Code_t code;
196  if(owl_global_is_havezephyr(&g)) {
197    if((code = ZPending()) < 0) {
198      owl_function_debugmsg("Error (%s) in ZPending()\n",
199                            error_message(code));
200      return 0;
201    }
202    return code;
203  }
204#endif
205  return 0;
206}
207
208int owl_zephyr_zqlength(void)
209{
210#ifdef HAVE_LIBZEPHYR
211  Code_t code;
212  if(owl_global_is_havezephyr(&g)) {
213    if((code = ZQLength()) < 0) {
214      owl_function_debugmsg("Error (%s) in ZQLength()\n",
215                            error_message(code));
216      return 0;
217    }
218    return code;
219  }
220#endif
221  return 0;
222}
223
224const char *owl_zephyr_get_realm(void)
225{
226#ifdef HAVE_LIBZEPHYR
227  if (owl_global_is_havezephyr(&g))
228    return(ZGetRealm());
229#endif
230  return "";
231}
232
233const char *owl_zephyr_get_sender(void)
234{
235#ifdef HAVE_LIBZEPHYR
236  if (owl_global_is_havezephyr(&g))
237    return(ZGetSender());
238#endif
239  return "";
240}
241
242#ifdef HAVE_LIBZEPHYR
243int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
244{
245  int ret = 0;
246  Code_t code;
247
248  if (owl_global_is_havezephyr(&g)) {
249    int i;
250    /* sub without defaults */
251    code = ZSubscribeToSansDefaults(subs, count, 0);
252    if (code != ZERR_NONE) {
253      owl_function_error("Error subscribing to zephyr notifications: %s",
254                         error_message(code));
255      ret=-2;
256    }
257
258    /* free stuff */
259    for (i=0; i<count; i++) {
260      g_free(subs[i].zsub_class);
261      g_free(subs[i].zsub_classinst);
262      g_free(subs[i].zsub_recipient);
263    }
264
265    g_free(subs);
266  } else {
267    owl_sub_list *s = g_new(owl_sub_list, 1);
268    s->subs = subs;
269    s->nsubs = count;
270    deferred_subs = g_list_append(deferred_subs, s);
271  }
272
273  return ret;
274}
275#endif
276
277/* Load zephyr subscriptions from 'filename'.  If 'filename' is NULL,
278 * the default file $HOME/.zephyr.subs will be used.
279 *
280 * Returns 0 on success.  If the file does not exist, return -1 if
281 * 'error_on_nofile' is 1, otherwise return 0.  Return -1 if the file
282 * exists but can not be read.  Return -2 if there is a failure from
283 * zephyr to load the subscriptions.
284 */
285int owl_zephyr_loadsubs(const char *filename, int error_on_nofile)
286{
287#ifdef HAVE_LIBZEPHYR
288  FILE *file;
289  char *tmp, *start;
290  char *buffer = NULL;
291  char *subsfile;
292  ZSubscription_t *subs;
293  int subSize = 1024;
294  int count;
295  struct stat statbuff;
296
297  subs = g_new(ZSubscription_t, subSize);
298  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
299
300  if (stat(subsfile, &statbuff) != 0) {
301    g_free(subsfile);
302    if (error_on_nofile == 1)
303      return -1;
304    return 0;
305  }
306
307  ZResetAuthentication();
308  count = 0;
309  file = fopen(subsfile, "r");
310  g_free(subsfile);
311  if (!file)
312    return -1;
313  while (owl_getline(&buffer, file)) {
314    if (buffer[0] == '#' || buffer[0] == '\n')
315        continue;
316
317    if (buffer[0] == '-')
318      start = buffer + 1;
319    else
320      start = buffer;
321
322    if (count >= subSize) {
323      subSize *= 2;
324      subs = g_renew(ZSubscription_t, subs, subSize);
325    }
326   
327    /* add it to the list of subs */
328    if ((tmp = strtok(start, ",\n\r")) == NULL)
329      continue;
330    subs[count].zsub_class = g_strdup(tmp);
331    if ((tmp=strtok(NULL, ",\n\r")) == NULL)
332      continue;
333    subs[count].zsub_classinst = g_strdup(tmp);
334    if ((tmp = strtok(NULL, " \t\n\r")) == NULL)
335      continue;
336    subs[count].zsub_recipient = g_strdup(tmp);
337
338    /* if it started with '-' then add it to the global punt list, and
339     * remove it from the list of subs. */
340    if (buffer[0] == '-') {
341      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
342      g_free(subs[count].zsub_class);
343      g_free(subs[count].zsub_classinst);
344      g_free(subs[count].zsub_recipient);
345    } else {
346      count++;
347    }
348  }
349  fclose(file);
350  if (buffer)
351    g_free(buffer);
352
353  return owl_zephyr_loadsubs_helper(subs, count);
354#else
355  return 0;
356#endif
357}
358
359/* Load default Barnowl subscriptions
360 *
361 * Returns 0 on success.
362 * Return -2 if there is a failure from zephyr to load the subscriptions.
363 */
364int owl_zephyr_loadbarnowldefaultsubs(void)
365{
366#ifdef HAVE_LIBZEPHYR
367  ZSubscription_t *subs;
368  int subSize = 10; /* Max Barnowl default subs we allow */
369  int count, ret;
370
371  subs = g_new(ZSubscription_t, subSize);
372  ZResetAuthentication();
373  count=0;
374
375  subs[count].zsub_class=g_strdup("message");
376  subs[count].zsub_classinst=g_strdup("*");
377  subs[count].zsub_recipient=g_strdup("%me%");
378  count++;
379
380  ret = owl_zephyr_loadsubs_helper(subs, count);
381  return(ret);
382#else
383  return(0);
384#endif
385}
386
387int owl_zephyr_loaddefaultsubs(void)
388{
389#ifdef HAVE_LIBZEPHYR
390  Code_t ret;
391
392  if (owl_global_is_havezephyr(&g)) {
393    ZSubscription_t subs[10];
394
395    ret = ZSubscribeTo(subs, 0, 0);
396    if (ret != ZERR_NONE) {
397      owl_function_error("Error subscribing to default zephyr notifications: %s.",
398                           error_message(ret));
399      return(-1);
400    }
401  }
402  return(0);
403#else
404  return(0);
405#endif
406}
407
408int owl_zephyr_loadloginsubs(const char *filename)
409{
410#ifdef HAVE_LIBZEPHYR
411  FILE *file;
412  ZSubscription_t *subs;
413  int numSubs = 100;
414  char *subsfile;
415  char *buffer = NULL;
416  int count;
417  struct stat statbuff;
418
419  subs = g_new(ZSubscription_t, numSubs);
420  subsfile = owl_zephyr_dotfile(".anyone", filename);
421
422  if (stat(subsfile, &statbuff) == -1) {
423    g_free(subs);
424    g_free(subsfile);
425    return 0;
426  }
427
428  ZResetAuthentication();
429  count = 0;
430  file = fopen(subsfile, "r");
431  g_free(subsfile);
432  if (file) {
433    while (owl_getline_chomp(&buffer, file)) {
434      if (buffer[0] == '\0' || buffer[0] == '#')
435        continue;
436
437      if (count == numSubs) {
438        numSubs *= 2;
439        subs = g_renew(ZSubscription_t, subs, numSubs);
440      }
441
442      subs[count].zsub_class = g_strdup("login");
443      subs[count].zsub_recipient = g_strdup("*");
444      subs[count].zsub_classinst = long_zuser(buffer);
445
446      count++;
447    }
448    fclose(file);
449  } else {
450    return 0;
451  }
452  g_free(buffer);
453
454  return owl_zephyr_loadsubs_helper(subs, count);
455#else
456  return 0;
457#endif
458}
459
460void unsuball(void)
461{
462#if HAVE_LIBZEPHYR
463  Code_t ret;
464
465  ZResetAuthentication();
466  ret = ZCancelSubscriptions(0);
467  if (ret != ZERR_NONE)
468    owl_function_error("Zephyr: Cancelling subscriptions: %s",
469                       error_message(ret));
470#endif
471}
472
473int owl_zephyr_sub(const char *class, const char *inst, const char *recip)
474{
475#ifdef HAVE_LIBZEPHYR
476  ZSubscription_t subs[5];
477  Code_t ret;
478
479  subs[0].zsub_class=zstr(class);
480  subs[0].zsub_classinst=zstr(inst);
481  subs[0].zsub_recipient=zstr(recip);
482
483  ZResetAuthentication();
484  ret = ZSubscribeTo(subs, 1, 0);
485  if (ret != ZERR_NONE) {
486    owl_function_error("Error subbing to <%s,%s,%s>: %s",
487                       class, inst, recip,
488                       error_message(ret));
489    return(-2);
490  }
491  return(0);
492#else
493  return(0);
494#endif
495}
496
497
498int owl_zephyr_unsub(const char *class, const char *inst, const char *recip)
499{
500#ifdef HAVE_LIBZEPHYR
501  ZSubscription_t subs[5];
502  Code_t ret;
503
504  subs[0].zsub_class=zstr(class);
505  subs[0].zsub_classinst=zstr(inst);
506  subs[0].zsub_recipient=zstr(recip);
507
508  ZResetAuthentication();
509  ret = ZUnsubscribeTo(subs, 1, 0);
510  if (ret != ZERR_NONE) {
511    owl_function_error("Error unsubbing from <%s,%s,%s>: %s",
512                       class, inst, recip,
513                       error_message(ret));
514    return(-2);
515  }
516  return(0);
517#else
518  return(0);
519#endif
520}
521
522#ifdef HAVE_LIBZEPHYR
523const char *owl_zephyr_first_raw_field(const ZNotice_t *n)
524{
525  if (n->z_message_len == 0)
526    return NULL;
527  return n->z_message;
528}
529
530const char *owl_zephyr_next_raw_field(const ZNotice_t *n, const char *f)
531{
532  const char *end = n->z_message + n->z_message_len;
533  f = memchr(f, '\0', end - f);
534  if (f == NULL)
535    return NULL;
536  return f + 1;
537}
538
539const char *owl_zephyr_get_raw_field(const ZNotice_t *n, int j)
540{
541  int i;
542  const char *f;
543  for (i = 1, f = owl_zephyr_first_raw_field(n); i < j && f != NULL;
544       i++, f = owl_zephyr_next_raw_field(n, f))
545    ;
546  return f;
547}
548
549CALLER_OWN char *owl_zephyr_field(const ZNotice_t *n, const char *f)
550{
551  if (f == NULL)
552    return g_strdup("");
553  return g_strndup(f, n->z_message + n->z_message_len - f);
554}
555
556CALLER_OWN char *owl_zephyr_field_as_utf8(const ZNotice_t *n, const char *f)
557{
558  char *tmp = owl_zephyr_field(n, f);
559  char *out = owl_validate_or_convert(tmp);
560  g_free(tmp);
561  return out;
562}
563
564CALLER_OWN char *owl_zephyr_get_field(const ZNotice_t *n, int j)
565{
566  return owl_zephyr_field(n, owl_zephyr_get_raw_field(n, j));
567}
568
569CALLER_OWN char *owl_zephyr_get_field_as_utf8(const ZNotice_t *n, int j)
570{
571  return owl_zephyr_field_as_utf8(n, owl_zephyr_get_raw_field(n, j));
572}
573#else
574const char *owl_zephyr_first_raw_field(const void *n)
575{
576  return NULL;
577}
578
579const char *owl_zephyr_next_raw_field(const void *n, const char *f)
580{
581  return NULL;
582}
583
584const char *owl_zephyr_get_raw_field(const void *n, int j)
585{
586  return NULL;
587}
588
589CALLER_OWN char *owl_zephyr_field(const void *n, const char *f)
590{
591  return g_strdup("");
592}
593
594CALLER_OWN char *owl_zephyr_field_as_utf8(const void *n, const char *f)
595{
596  return g_strdup("");
597}
598
599CALLER_OWN char *owl_zephyr_get_field(const void *n, int j)
600{
601  return g_strdup("");
602}
603
604CALLER_OWN char *owl_zephyr_get_field_as_utf8(const void *n, int j)
605{
606  return owl_zephyr_field(n, owl_zephyr_get_raw_field(n, j));
607}
608#endif
609
610
611#ifdef HAVE_LIBZEPHYR
612int owl_zephyr_get_num_fields(const ZNotice_t *n)
613{
614  int i;
615  const char *f;
616  for (i = 0, f = owl_zephyr_first_raw_field(n); f != NULL;
617       i++, f = owl_zephyr_next_raw_field(n, f))
618    ;
619  return i;
620}
621#else
622int owl_zephyr_get_num_fields(const void *n)
623{
624  return(0);
625}
626#endif
627
628#ifdef HAVE_LIBZEPHYR
629/* return a pointer to the message, place the message length in k
630 * caller must free the return
631 */
632CALLER_OWN char *owl_zephyr_get_message(const ZNotice_t *n, const owl_message *m)
633{
634#define OWL_NFIELDS     5
635  int i;
636  char *fields[OWL_NFIELDS + 1];
637  char *msg = NULL;
638
639  /* don't let ping messages have a body */
640  if (!strcasecmp(n->z_opcode, "ping")) {
641    return(g_strdup(""));
642  }
643
644  for(i = 0; i < OWL_NFIELDS; i++)
645    fields[i + 1] = owl_zephyr_get_field(n, i + 1);
646
647  /* deal with MIT NOC messages */
648  if (!strcasecmp(n->z_default_format, "@center(@bold(NOC Message))\n\n@bold(Sender:) $1 <$sender>\n@bold(Time:  ) $time\n\n@italic($opcode service on $instance $3.) $4\n")) {
649
650    msg = g_strdup_printf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, fields[3], fields[4]);
651  }
652  /* deal with MIT Discuss messages */
653  else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4") ||
654           !strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3\nSubject: $4")) {
655   
656    msg = g_strdup_printf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s",
657                          fields[1], fields[2], fields[3], fields[5], fields[4]);
658  }
659  /* deal with MIT Moira messages */
660  else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
661    msg = g_strdup_printf("MOIRA %s on %s: %s",
662                          n->z_class_inst,
663                          owl_message_get_hostname(m),
664                          fields[1]);
665  } else {
666    if (owl_zephyr_get_num_fields(n) == 1)
667      msg = g_strdup(fields[1]);
668    else
669      msg = g_strdup(fields[2]);
670  }
671
672  for (i = 0; i < OWL_NFIELDS; i++)
673    g_free(fields[i + 1]);
674
675  return msg;
676}
677#endif
678
679#ifdef HAVE_LIBZEPHYR
680const char *owl_zephyr_get_zsig(const ZNotice_t *n, int *k)
681{
682  /* return a pointer to the zsig if there is one */
683
684  /* message length 0? No zsig */
685  if (n->z_message_len==0) {
686    *k=0;
687    return("");
688  }
689
690  /* If there's only one field, no zsig */
691  if (owl_zephyr_get_num_fields(n) == 1) {
692    *k=0;
693    return("");
694  }
695
696  /* Everything else is field 1 */
697  *k=strlen(n->z_message);
698  return(n->z_message);
699}
700#else
701const char *owl_zephyr_get_zsig(const void *n, int *k)
702{
703  return("");
704}
705#endif
706
707int send_zephyr(const char *opcode, const char *zsig, const char *class, const char *instance, const char *recipient, const char *message)
708{
709#ifdef HAVE_LIBZEPHYR
710  Code_t ret;
711  ZNotice_t notice;
712   
713  memset(&notice, 0, sizeof(notice));
714
715  ZResetAuthentication();
716
717  if (!zsig) zsig="";
718 
719  notice.z_kind=ACKED;
720  notice.z_port=0;
721  notice.z_class=zstr(class);
722  notice.z_class_inst=zstr(instance);
723  notice.z_sender=NULL;
724  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
725    notice.z_recipient=zstr("");
726    if (*owl_global_get_zsender(&g))
727        notice.z_sender=zstr(owl_global_get_zsender(&g));
728  } else {
729    notice.z_recipient=zstr(recipient);
730  }
731  notice.z_default_format=zstr("Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2");
732  if (opcode) notice.z_opcode=zstr(opcode);
733
734  notice.z_message_len=strlen(zsig)+1+strlen(message);
735  notice.z_message=g_new(char, notice.z_message_len+10);
736  strcpy(notice.z_message, zsig);
737  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
738
739  /* ret=ZSendNotice(&notice, ZAUTH); */
740  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
741 
742  /* free then check the return */
743  g_free(notice.z_message);
744  ZFreeNotice(&notice);
745  if (ret != ZERR_NONE) {
746    owl_function_error("Error sending zephyr: %s", error_message(ret));
747    return(ret);
748  }
749  return(0);
750#else
751  return(0);
752#endif
753}
754
755#ifdef HAVE_LIBZEPHYR
756Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
757{
758  return(ZSendPacket(buf, len, 0));
759}
760#endif
761
762void send_ping(const char *to, const char *zclass, const char *zinstance)
763{
764#ifdef HAVE_LIBZEPHYR
765  send_zephyr("PING", "", zclass, zinstance, to, "");
766#endif
767}
768
769#ifdef HAVE_LIBZEPHYR
770void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
771{
772  char *tmp;
773 
774  /* if it's an HMACK ignore it */
775  if (retnotice->z_kind == HMACK) return;
776
777  if (retnotice->z_kind == SERVNAK) {
778    owl_function_error("Authorization failure sending zephyr");
779  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
780    owl_function_error("Detected server failure while receiving acknowledgement");
781  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
782    if (!strcasecmp(retnotice->z_opcode, "ping")) {
783      return;
784    } else {
785      if (strcasecmp(retnotice->z_recipient, ""))
786      { /* personal */
787        tmp=short_zuser(retnotice->z_recipient);
788        if(!strcasecmp(retnotice->z_class, "message") &&
789           !strcasecmp(retnotice->z_class_inst, "personal")) {
790          owl_function_makemsg("Message sent to %s.", tmp);
791        } else if(!strcasecmp(retnotice->z_class, "message")) { /* instanced, but not classed, personal */
792          owl_function_makemsg("Message sent to %s on -i %s\n", tmp, retnotice->z_class_inst);
793        } else { /* classed personal */
794          owl_function_makemsg("Message sent to %s on -c %s -i %s\n", tmp, retnotice->z_class, retnotice->z_class_inst);
795        }
796        g_free(tmp);
797      } else {
798        /* class / instance message */
799          owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
800      }
801    }
802  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
803    if (retnotice->z_recipient == NULL
804        || *retnotice->z_recipient == 0
805        || *retnotice->z_recipient == '@') {
806      char *buff;
807      owl_function_error("No one subscribed to class %s", retnotice->z_class);
808      buff = g_strdup_printf("Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
809      owl_function_adminmsg("", buff);
810      g_free(buff);
811    } else {
812      char *buff;
813      owl_zwrite zw;
814
815      tmp = short_zuser(retnotice->z_recipient);
816      owl_function_error("%s: Not logged in or subscribing.", tmp);
817      /*
818       * These error messages are often over 80 chars, but users who want to
819       * see the whole thing can scroll to the side, and for those with wide
820       * terminals or who don't care, not splitting saves a line in the UI
821       */
822      if(strcasecmp(retnotice->z_class, "message")) {
823        buff = g_strdup_printf(
824                 "Could not send message to %s: "
825                 "not logged in or subscribing to class %s, instance %s.\n",
826                 tmp,
827                 retnotice->z_class,
828                 retnotice->z_class_inst);
829      } else if(strcasecmp(retnotice->z_class_inst, "personal")) {
830        buff = g_strdup_printf(
831                 "Could not send message to %s: "
832                 "not logged in or subscribing to instance %s.\n",
833                 tmp,
834                 retnotice->z_class_inst);
835      } else {
836        buff = g_strdup_printf(
837                 "Could not send message to %s: "
838                 "not logged in or subscribing to messages.\n",
839                 tmp);
840      }
841      owl_function_adminmsg("", buff);
842
843      memset(&zw, 0, sizeof(zw));
844      zw.class = g_strdup(retnotice->z_class);
845      zw.inst  = g_strdup(retnotice->z_class_inst);
846      zw.realm = g_strdup("");
847      zw.opcode = g_strdup(retnotice->z_opcode);
848      zw.zsig   = g_strdup("");
849      zw.recips = g_ptr_array_new();
850      g_ptr_array_add(zw.recips, g_strdup(retnotice->z_recipient));
851
852      owl_log_outgoing_zephyr_error(&zw, buff);
853
854      owl_zwrite_cleanup(&zw);
855      g_free(buff);
856      g_free(tmp);
857    }
858  } else {
859    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
860  }
861}
862#else
863void owl_zephyr_handle_ack(const void *retnotice)
864{
865}
866#endif
867
868#ifdef HAVE_LIBZEPHYR
869int owl_zephyr_notice_is_ack(const ZNotice_t *n)
870{
871  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
872    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
873    return(1);
874  }
875  return(0);
876}
877#else
878int owl_zephyr_notice_is_ack(const void *n)
879{
880  return(0);
881}
882#endif
883 
884void owl_zephyr_zaway(const owl_message *m)
885{
886#ifdef HAVE_LIBZEPHYR
887  char *tmpbuff, *myuser, *to;
888  owl_zwrite *z;
889 
890  /* bail if it doesn't look like a message we should reply to.  Some
891   * of this defined by the way zaway(1) works
892   */
893  if (strcasecmp(owl_message_get_class(m), "message")) return;
894  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
895  if (!strcasecmp(owl_message_get_sender(m), "")) return;
896  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
897  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
898  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
899  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
900  if (owl_message_get_attribute_value(m, "isauto")) return;
901
902  if (owl_global_is_smartstrip(&g)) {
903    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
904  } else {
905    to=g_strdup(owl_message_get_sender(m));
906  }
907
908  send_zephyr("",
909              "Automated reply:",
910              owl_message_get_class(m),
911              owl_message_get_instance(m),
912              to,
913              owl_global_get_zaway_msg(&g));
914
915  myuser=short_zuser(to);
916  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
917    tmpbuff = owl_string_build_quoted("zwrite %q", myuser);
918  } else {
919    tmpbuff = owl_string_build_quoted("zwrite -i %q %q", owl_message_get_instance(m), myuser);
920  }
921  g_free(myuser);
922  g_free(to);
923
924  z = owl_zwrite_new(tmpbuff);
925  g_free(tmpbuff);
926  if (z == NULL) {
927    owl_function_error("Error creating outgoing zephyr.");
928    return;
929  }
930  owl_zwrite_set_message(z, owl_global_get_zaway_msg(&g));
931  owl_zwrite_set_zsig(z, "Automated reply:");
932
933  /* display the message as an admin message in the receive window */
934  owl_function_add_outgoing_zephyrs(z);
935  owl_zwrite_delete(z);
936#endif
937}
938
939#ifdef HAVE_LIBZEPHYR
940void owl_zephyr_hackaway_cr(ZNotice_t *n)
941{
942  /* replace \r's with ' '.  Gross-ish */
943  int i;
944
945  for (i=0; i<n->z_message_len; i++) {
946    if (n->z_message[i]=='\r') {
947      n->z_message[i]=' ';
948    }
949  }
950}
951#endif
952
953CALLER_OWN char *owl_zephyr_zlocate(const char *user, int auth)
954{
955#ifdef HAVE_LIBZEPHYR
956  int ret, numlocs;
957  int one = 1;
958  ZLocations_t locations;
959  char *myuser;
960  char *p, *result;
961
962  ZResetAuthentication();
963  ret = ZLocateUser(zstr(user), &numlocs, auth ? ZAUTH : ZNOAUTH);
964  if (ret != ZERR_NONE)
965    return g_strdup_printf("Error locating user %s: %s\n",
966                           user, error_message(ret));
967
968  myuser = short_zuser(user);
969  if (numlocs == 0) {
970    result = g_strdup_printf("%s: Hidden or not logged in\n", myuser);
971  } else {
972    result = g_strdup("");
973    for (; numlocs; numlocs--) {
974      ZGetLocations(&locations, &one);
975      p = g_strdup_printf("%s%s: %s\t%s\t%s\n",
976                          result, myuser,
977                          locations.host ? locations.host : "?",
978                          locations.tty ? locations.tty : "?",
979                          locations.time ? locations.time : "?");
980      g_free(result);
981      result = p;
982    }
983  }
984  g_free(myuser);
985
986  return result;
987#else
988  return g_strdup("");
989#endif
990}
991
992void owl_zephyr_addsub(const char *filename, const char *class, const char *inst, const char *recip)
993{
994#ifdef HAVE_LIBZEPHYR
995  char *line, *subsfile, *s = NULL;
996  FILE *file;
997  int duplicate = 0;
998
999  line = owl_zephyr_makesubline(class, inst, recip);
1000  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
1001
1002  /* if the file already exists, check to see if the sub is already there */
1003  file = fopen(subsfile, "r");
1004  if (file) {
1005    while (owl_getline(&s, file)) {
1006      if (strcasecmp(s, line) == 0) {
1007        owl_function_error("Subscription already present in %s", subsfile);
1008        duplicate++;
1009      }
1010    }
1011    fclose(file);
1012    g_free(s);
1013  }
1014
1015  if (!duplicate) {
1016    file = fopen(subsfile, "a");
1017    if (file) {
1018      fputs(line, file);
1019      fclose(file);
1020      owl_function_makemsg("Subscription added");
1021    } else {
1022      owl_function_error("Error opening file %s for writing", subsfile);
1023    }
1024  }
1025
1026  g_free(line);
1027#endif
1028}
1029
1030void owl_zephyr_delsub(const char *filename, const char *class, const char *inst, const char *recip)
1031{
1032#ifdef HAVE_LIBZEPHYR
1033  char *line, *subsfile;
1034  int linesdeleted;
1035 
1036  line=owl_zephyr_makesubline(class, inst, recip);
1037  line[strlen(line)-1]='\0';
1038
1039  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
1040 
1041  linesdeleted = owl_util_file_deleteline(subsfile, line, 1);
1042  if (linesdeleted > 0) {
1043    owl_function_makemsg("Subscription removed");
1044  } else if (linesdeleted == 0) {
1045    owl_function_error("No subscription present in %s", subsfile);
1046  }
1047  g_free(subsfile);
1048  g_free(line);
1049#endif
1050}
1051
1052/* caller must free the return */
1053CALLER_OWN char *owl_zephyr_makesubline(const char *class, const char *inst, const char *recip)
1054{
1055  return g_strdup_printf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
1056}
1057
1058
1059void owl_zephyr_zlog_in(void)
1060{
1061#ifdef HAVE_LIBZEPHYR
1062  ZResetAuthentication();
1063
1064  /* ZSetLocation, and store the default value as the current value */
1065  owl_global_set_exposure(&g, owl_global_get_default_exposure(&g));
1066#endif
1067}
1068
1069void owl_zephyr_zlog_out(void)
1070{
1071#ifdef HAVE_LIBZEPHYR
1072  Code_t ret;
1073
1074  ZResetAuthentication();
1075  ret = ZUnsetLocation();
1076  if (ret != ZERR_NONE)
1077    owl_function_error("Error unsetting location: %s", error_message(ret));
1078#endif
1079}
1080
1081void owl_zephyr_addbuddy(const char *name)
1082{
1083  char *filename;
1084  FILE *file;
1085 
1086  filename = owl_zephyr_dotfile(".anyone", NULL);
1087  file = fopen(filename, "a");
1088  g_free(filename);
1089  if (!file) {
1090    owl_function_error("Error opening zephyr buddy file for append");
1091    return;
1092  }
1093  fprintf(file, "%s\n", name);
1094  fclose(file);
1095}
1096
1097void owl_zephyr_delbuddy(const char *name)
1098{
1099  char *filename;
1100
1101  filename = owl_zephyr_dotfile(".anyone", NULL);
1102  owl_util_file_deleteline(filename, name, 0);
1103  g_free(filename);
1104}
1105
1106/* return auth string */
1107#ifdef HAVE_LIBZEPHYR
1108const char *owl_zephyr_get_authstr(const ZNotice_t *n)
1109{
1110
1111  if (!n) return("UNKNOWN");
1112
1113  if (n->z_auth == ZAUTH_FAILED) {
1114    return ("FAILED");
1115  } else if (n->z_auth == ZAUTH_NO) {
1116    return ("NO");
1117  } else if (n->z_auth == ZAUTH_YES) {
1118    return ("YES");
1119  } else {
1120    return ("UNKNOWN");
1121  }           
1122}
1123#else
1124const char *owl_zephyr_get_authstr(const void *n)
1125{
1126  return("");
1127}
1128#endif
1129
1130/* Returns a buffer of subscriptions or an error message.  Caller must
1131 * free the return.
1132 */
1133CALLER_OWN char *owl_zephyr_getsubs(void)
1134{
1135#ifdef HAVE_LIBZEPHYR
1136  Code_t ret;
1137  int num, i, one;
1138  ZSubscription_t sub;
1139  GString *buf;
1140
1141  ret = ZRetrieveSubscriptions(0, &num);
1142  if (ret != ZERR_NONE)
1143    return g_strdup_printf("Zephyr: Requesting subscriptions: %s\n", error_message(ret));
1144  if (num == 0)
1145    return g_strdup("Zephyr: No subscriptions retrieved\n");
1146
1147  buf = g_string_new("");
1148  for (i=0; i<num; i++) {
1149    one = 1;
1150    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1151      ZFlushSubscriptions();
1152      g_string_free(buf, true);
1153      return g_strdup_printf("Zephyr: Getting subscriptions: %s\n", error_message(ret));
1154    } else {
1155      /* g_string_append_printf would be backwards. */
1156      char *tmp = g_strdup_printf("<%s,%s,%s>\n",
1157                                  sub.zsub_class,
1158                                  sub.zsub_classinst,
1159                                  sub.zsub_recipient);
1160      g_string_prepend(buf, tmp);
1161      g_free(tmp);
1162    }
1163  }
1164
1165  ZFlushSubscriptions();
1166  return g_string_free(buf, false);
1167#else
1168  return(g_strdup("Zephyr not available"));
1169#endif
1170}
1171
1172const char *owl_zephyr_get_variable(const char *var)
1173{
1174#ifdef HAVE_LIBZEPHYR
1175  return(ZGetVariable(zstr(var)));
1176#else
1177  return("");
1178#endif
1179}
1180
1181void owl_zephyr_set_locationinfo(const char *host, const char *val)
1182{
1183#ifdef HAVE_LIBZEPHYR
1184  ZInitLocationInfo(zstr(host), zstr(val));
1185#endif
1186}
1187
1188const char *owl_zephyr_normalize_exposure(const char *exposure)
1189{
1190  if (exposure == NULL)
1191    return NULL;
1192#ifdef HAVE_LIBZEPHYR
1193  return ZParseExposureLevel(zstr(exposure));
1194#else
1195  return exposure;
1196#endif
1197}
1198
1199int owl_zephyr_set_default_exposure(const char *exposure)
1200{
1201#ifdef HAVE_LIBZEPHYR
1202  Code_t ret;
1203  if (exposure == NULL)
1204    return -1;
1205  exposure = ZParseExposureLevel(zstr(exposure));
1206  if (exposure == NULL)
1207    return -1;
1208  ret = ZSetVariable(zstr("exposure"), zstr(exposure)); /* ZSetVariable does file I/O */
1209  if (ret != ZERR_NONE) {
1210    owl_function_error("Unable to set default exposure location: %s", error_message(ret));
1211    return -1;
1212  }
1213#endif
1214  return 0;
1215}
1216
1217const char *owl_zephyr_get_default_exposure(void)
1218{
1219#ifdef HAVE_LIBZEPHYR
1220  const char *exposure = ZGetVariable(zstr("exposure")); /* ZGetVariable does file I/O */
1221  if (exposure == NULL)
1222    return EXPOSE_REALMVIS;
1223  exposure = ZParseExposureLevel(zstr(exposure));
1224  if (exposure == NULL) /* The user manually entered an invalid value in ~/.zephyr.vars, or something weird happened. */
1225    return EXPOSE_REALMVIS;
1226  return exposure;
1227#else
1228  return "";
1229#endif
1230}
1231
1232int owl_zephyr_set_exposure(const char *exposure)
1233{
1234#ifdef HAVE_LIBZEPHYR
1235  Code_t ret;
1236  if (exposure == NULL)
1237    return -1;
1238  exposure = ZParseExposureLevel(zstr(exposure));
1239  if (exposure == NULL)
1240    return -1;
1241  ret = ZSetLocation(zstr(exposure));
1242  if (ret != ZERR_NONE
1243#ifdef ZCONST
1244      /* Before zephyr 3.0, ZSetLocation had a bug where, if you were subscribed
1245       * to your own logins, it could wait for the wrong notice and return
1246       * ZERR_INTERNAL when found neither SERVACK nor SERVNAK. Suppress it when
1247       * building against the old ABI. */
1248      && ret != ZERR_INTERNAL
1249#endif
1250     ) {
1251    owl_function_error("Unable to set exposure level: %s.", error_message(ret));
1252    return -1;
1253  }
1254#endif
1255  return 0;
1256}
1257 
1258/* Strip a local realm fron the zephyr user name.
1259 * The caller must free the return
1260 */
1261CALLER_OWN char *short_zuser(const char *in)
1262{
1263  char *ptr = strrchr(in, '@');
1264  if (ptr && (ptr[1] == '\0' || !strcasecmp(ptr+1, owl_zephyr_get_realm()))) {
1265    return g_strndup(in, ptr - in);
1266  }
1267  return g_strdup(in);
1268}
1269
1270/* Append a local realm to the zephyr user name if necessary.
1271 * The caller must free the return.
1272 */
1273CALLER_OWN char *long_zuser(const char *in)
1274{
1275  char *ptr = strrchr(in, '@');
1276  if (ptr) {
1277    if (ptr[1])
1278      return g_strdup(in);
1279    /* Ends in @, so assume default realm. */
1280    return g_strdup_printf("%s%s", in, owl_zephyr_get_realm());
1281  }
1282  return g_strdup_printf("%s@%s", in, owl_zephyr_get_realm());
1283}
1284
1285/* Return the realm of the zephyr user name. Caller does /not/ free the return.
1286 * The string is valid at least as long as the input is.
1287 */
1288const char *zuser_realm(const char *in)
1289{
1290  char *ptr = strrchr(in, '@');
1291  /* If the name has an @ and does not end with @, use that. Otherwise, take
1292   * the default realm. */
1293  return (ptr && ptr[1]) ? (ptr+1) : owl_zephyr_get_realm();
1294}
1295
1296/* strip out the instance from a zsender's principal.  Preserves the
1297 * realm if present.  Leave host/ and daemon/ krb5 principals
1298 * alone. Also leave rcmd. and daemon. krb4 principals alone. The
1299 * caller must free the return.
1300 */
1301CALLER_OWN char *owl_zephyr_smartstripped_user(const char *in)
1302{
1303  char *slash, *dot, *realm, *out;
1304
1305  out = g_strdup(in);
1306
1307  /* bail immeaditly if we don't have to do any work */
1308  slash = strchr(out, '/');
1309  dot = strchr(out, '.');
1310  if (!slash && !dot) {
1311    return(out);
1312  }
1313
1314  if (!strncasecmp(out, OWL_ZEPHYR_NOSTRIP_HOST, strlen(OWL_ZEPHYR_NOSTRIP_HOST)) ||
1315      !strncasecmp(out, OWL_ZEPHYR_NOSTRIP_RCMD, strlen(OWL_ZEPHYR_NOSTRIP_RCMD)) ||
1316      !strncasecmp(out, OWL_ZEPHYR_NOSTRIP_DAEMON5, strlen(OWL_ZEPHYR_NOSTRIP_DAEMON5)) ||
1317      !strncasecmp(out, OWL_ZEPHYR_NOSTRIP_DAEMON4, strlen(OWL_ZEPHYR_NOSTRIP_DAEMON4))) {
1318    return(out);
1319  }
1320
1321  realm = strchr(out, '@');
1322  if (!slash && dot && realm && (dot > realm)) {
1323    /* There's no '/', and the first '.' is in the realm */
1324    return(out);
1325  }
1326
1327  /* remove the realm from out, but hold on to it */
1328  if (realm) realm[0]='\0';
1329
1330  /* strip */
1331  if (slash) slash[0] = '\0';  /* krb5 style user/instance */
1332  else if (dot) dot[0] = '\0'; /* krb4 style user.instance */
1333
1334  /* reattach the realm if we had one */
1335  if (realm) {
1336    strcat(out, "@");
1337    strcat(out, realm+1);
1338  }
1339
1340  return(out);
1341}
1342
1343/* Read the list of users in 'filename' as a .anyone file, and return as a
1344 * GPtrArray of strings.  If 'filename' is NULL, use the default .anyone file
1345 * in the users home directory.  Returns NULL on failure.
1346 */
1347GPtrArray *owl_zephyr_get_anyone_list(const char *filename)
1348{
1349#ifdef HAVE_LIBZEPHYR
1350  char *ourfile, *tmp, *s = NULL;
1351  FILE *f;
1352  GPtrArray *list;
1353
1354  ourfile = owl_zephyr_dotfile(".anyone", filename);
1355
1356  f = fopen(ourfile, "r");
1357  if (!f) {
1358    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1359    g_free(ourfile);
1360    return NULL;
1361  }
1362  g_free(ourfile);
1363
1364  list = g_ptr_array_new();
1365  while (owl_getline_chomp(&s, f)) {
1366    /* ignore comments, blank lines etc. */
1367    if (s[0] == '#' || s[0] == '\0')
1368      continue;
1369
1370    /* ignore from # on */
1371    tmp = strchr(s, '#');
1372    if (tmp)
1373      tmp[0] = '\0';
1374
1375    /* ignore from SPC */
1376    tmp = strchr(s, ' ');
1377    if (tmp)
1378      tmp[0] = '\0';
1379
1380    g_ptr_array_add(list, long_zuser(s));
1381  }
1382  g_free(s);
1383  fclose(f);
1384  return list;
1385#else
1386  return NULL;
1387#endif
1388}
1389
1390#ifdef HAVE_LIBZEPHYR
1391void owl_zephyr_process_pseudologin(ZNotice_t *n)
1392{
1393  owl_message *m;
1394  owl_zbuddylist *zbl;
1395  GList **zaldlist;
1396  GList *zaldptr;
1397  ZAsyncLocateData_t *zald = NULL;
1398  ZLocations_t location;
1399  int numlocs, ret, notify;
1400
1401  /* Find a ZALD to match this notice. */
1402  zaldlist = owl_global_get_zaldlist(&g);
1403  zaldptr = g_list_first(*zaldlist);
1404  while (zaldptr) {
1405    if (ZCompareALDPred(n, zaldptr->data)) {
1406      zald = zaldptr->data;
1407      *zaldlist = g_list_remove(*zaldlist, zaldptr->data);
1408      break;
1409    }
1410    zaldptr = g_list_next(zaldptr);
1411  }
1412  if (zald) {
1413    /* Deal with notice. */
1414    notify = owl_global_get_pseudologin_notify(&g);
1415    zbl = owl_global_get_zephyr_buddylist(&g);
1416    ret = ZParseLocations(n, zald, &numlocs, NULL);
1417    if (ret == ZERR_NONE) {
1418      if (numlocs > 0 && !owl_zbuddylist_contains_user(zbl, zald->user)) {
1419        if (notify) {
1420          numlocs = 1;
1421          ret = ZGetLocations(&location, &numlocs);
1422          if (ret == ZERR_NONE) {
1423            /* Send a PSEUDO LOGIN! */
1424            m = g_new(owl_message, 1);
1425            owl_message_create_pseudo_zlogin(m, 0, zald->user,
1426                                             location.host,
1427                                             location.time,
1428                                             location.tty);
1429            owl_global_messagequeue_addmsg(&g, m);
1430          }
1431          owl_zbuddylist_adduser(zbl, zald->user);
1432          owl_function_debugmsg("owl_function_zephyr_buddy_check: login for %s ", zald->user);
1433        }
1434      } else if (numlocs == 0 && owl_zbuddylist_contains_user(zbl, zald->user)) {
1435        /* Send a PSEUDO LOGOUT! */
1436        if (notify) {
1437          m = g_new(owl_message, 1);
1438          owl_message_create_pseudo_zlogin(m, 1, zald->user, "", "", "");
1439          owl_global_messagequeue_addmsg(&g, m);
1440        }
1441        owl_zbuddylist_deluser(zbl, zald->user);
1442        owl_function_debugmsg("owl_function_zephyr_buddy_check: logout for %s ", zald->user);
1443      }
1444    }
1445    ZFreeALD(zald);
1446    g_free(zald);
1447  }
1448}
1449#else
1450void owl_zephyr_process_pseudologin(void *n)
1451{
1452}
1453#endif
1454
1455gboolean owl_zephyr_buddycheck_timer(void *data)
1456{
1457  if (owl_global_is_pseudologins(&g)) {
1458    owl_function_debugmsg("Doing zephyr buddy check");
1459    owl_function_zephyr_buddy_check(1);
1460  } else {
1461    owl_function_debugmsg("Warning: owl_zephyr_buddycheck_timer call pointless; timer should have been disabled");
1462  }
1463  return TRUE;
1464}
1465
1466/*
1467 * Process zephyrgrams from libzephyr's queue. To prevent starvation,
1468 * process a maximum of OWL_MAX_ZEPHYRGRAMS_TO_PROCESS.
1469 *
1470 * Returns the number of zephyrgrams processed.
1471 */
1472
1473#define OWL_MAX_ZEPHYRGRAMS_TO_PROCESS 20
1474
1475#ifdef HAVE_LIBZEPHYR
1476static int _owl_zephyr_process_events(void)
1477{
1478  int zpendcount=0;
1479  ZNotice_t notice;
1480  Code_t code;
1481  owl_message *m=NULL;
1482
1483  while(owl_zephyr_zpending() && zpendcount < OWL_MAX_ZEPHYRGRAMS_TO_PROCESS) {
1484    if (owl_zephyr_zpending()) {
1485      if ((code = ZReceiveNotice(&notice, NULL)) != ZERR_NONE) {
1486        owl_function_debugmsg("Error: %s while calling ZReceiveNotice\n",
1487                              error_message(code));
1488        continue;
1489      }
1490      zpendcount++;
1491
1492      /* is this an ack from a zephyr we sent? */
1493      if (owl_zephyr_notice_is_ack(&notice)) {
1494        owl_zephyr_handle_ack(&notice);
1495        ZFreeNotice(&notice);
1496        continue;
1497      }
1498
1499      /* if it's a ping and we're not viewing pings then skip it */
1500      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1501        ZFreeNotice(&notice);
1502        continue;
1503      }
1504
1505      /* if it is a LOCATE message, it's for pseudologins. */
1506      if (strcmp(notice.z_opcode, LOCATE_LOCATE) == 0) {
1507        owl_zephyr_process_pseudologin(&notice);
1508        ZFreeNotice(&notice);
1509        continue;
1510      }
1511
1512      /* create the new message */
1513      m=g_new(owl_message, 1);
1514      owl_message_create_from_znotice(m, &notice);
1515
1516      owl_global_messagequeue_addmsg(&g, m);
1517    }
1518  }
1519  return zpendcount;
1520}
1521
1522typedef struct { /*noproto*/
1523  GSource source;
1524  GPollFD poll_fd;
1525} owl_zephyr_event_source;
1526
1527static GSource *owl_zephyr_event_source_new(int fd) {
1528  GSource *source;
1529  owl_zephyr_event_source *event_source;
1530
1531  source = g_source_new(&zephyr_event_funcs, sizeof(owl_zephyr_event_source));
1532  event_source = (owl_zephyr_event_source*) source;
1533  event_source->poll_fd.fd = fd;
1534  event_source->poll_fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
1535  g_source_add_poll(source, &event_source->poll_fd);
1536
1537  return source;
1538}
1539
1540static gboolean owl_zephyr_event_prepare(GSource *source, int *timeout) {
1541  *timeout = -1;
1542  return owl_zephyr_zqlength() > 0;
1543}
1544
1545static gboolean owl_zephyr_event_check(GSource *source) {
1546  owl_zephyr_event_source *event_source = (owl_zephyr_event_source*)source;
1547  if (event_source->poll_fd.revents & event_source->poll_fd.events)
1548    return owl_zephyr_zpending() > 0;
1549  return FALSE;
1550}
1551
1552static gboolean owl_zephyr_event_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
1553  _owl_zephyr_process_events();
1554  return TRUE;
1555}
1556#endif
Note: See TracBrowser for help on using the repository browser.