source: zephyr.c @ d199207

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