source: zephyr.c @ 5f3f1e4

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