source: zephyr.c

Last change on this file was 18380fd, checked in by Anders Kaseorg <andersk@mit.edu>, 7 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
RevLine 
[7d4fbcd]1#include "owl.h"
[f271129]2#include <stdio.h>
3#include <sys/stat.h>
[7d4fbcd]4
[b848e30]5#ifdef HAVE_LIBZEPHYR
[959cb85]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
[a5e7ed6]12static GList *deferred_subs = NULL;
[79d7d98]13
[a5e7ed6]14typedef struct _owl_sub_list {                            /* noproto */
15  ZSubscription_t *subs;
16  int nsubs;
17} owl_sub_list;
18
[c79a047]19Code_t ZResetAuthentication(void);
[959cb85]20
21static GSourceFuncs zephyr_event_funcs = {
22  owl_zephyr_event_prepare,
23  owl_zephyr_event_check,
24  owl_zephyr_event_dispatch,
25  NULL
26};
[09489b89]27#endif
[8262340]28
[02f55dc]29#define HM_SVC_FALLBACK         htons((unsigned short) 2104)
30
[60ae736]31static CALLER_OWN char *owl_zephyr_dotfile(const char *name, const char *input)
[6ea3890]32{
33  if (input != NULL)
[d4927a7]34    return g_strdup(input);
[6ea3890]35  else
[dde1b4d]36    return g_build_filename(owl_global_get_homedir(&g), name, NULL);
[6ea3890]37}
38
[52a0f14]39#ifdef HAVE_LIBZEPHYR
[c79a047]40void owl_zephyr_initialize(void)
[52a0f14]41{
[488913a]42  Code_t ret;
[02f55dc]43  struct servent *sp;
44  struct sockaddr_in sin;
45  ZNotice_t req;
[2d04312]46  GIOChannel *channel;
[52a0f14]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  }
[02f55dc]59
[4d86e06]60  (void) memset(&sin, 0, sizeof(struct sockaddr_in));
[02f55dc]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
[4d86e06]69  (void) memset(&req, 0, sizeof(req));
[02f55dc]70  req.z_kind = STAT;
71  req.z_port = 0;
[712caac]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("");
[02f55dc]78  req.z_message_len = 0;
[52a0f14]79
[488913a]80  if ((ret = ZSetDestAddr(&sin)) != ZERR_NONE) {
81    owl_function_error("Initializing Zephyr: %s", error_message(ret));
[52a0f14]82    return;
83  }
84
[488913a]85  if ((ret = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE) {
86    owl_function_error("Initializing Zephyr: %s", error_message(ret));
[52a0f14]87    return;
88  }
89
[2d04312]90  channel = g_io_channel_unix_new(ZGetFD());
[e146cd7]91  g_io_add_watch(channel, G_IO_IN | G_IO_ERR | G_IO_HUP,
[2d04312]92                 &owl_zephyr_finish_initialization, NULL);
93  g_io_channel_unref(channel);
[02f55dc]94}
95
[2d04312]96gboolean owl_zephyr_finish_initialization(GIOChannel *source, GIOCondition condition, void *data) {
[52a0f14]97  Code_t code;
[df569c5]98  char *perl;
[959cb85]99  GSource *event_source;
[52a0f14]100
101  ZClosePort();
102
103  if ((code = ZInitialize()) != ZERR_NONE) {
104    owl_function_error("Initializing Zephyr: %s", error_message(code));
[2d04312]105    return FALSE;
[52a0f14]106  }
107
108  if ((code = ZOpenPort(NULL)) != ZERR_NONE) {
109    owl_function_error("Initializing Zephyr: %s", error_message(code));
[2d04312]110    return FALSE;
[52a0f14]111  }
112
[959cb85]113  event_source = owl_zephyr_event_source_new(ZGetFD());
114  g_source_attach(event_source, NULL);
115  g_source_unref(event_source);
[18fdd5f9]116
[52a0f14]117  owl_global_set_havezephyr(&g);
118
119  if(g.load_initial_subs) {
120    owl_zephyr_load_initial_subs();
121  }
[a5e7ed6]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);
[7dcef03]127    g_slice_free(owl_sub_list, subs);
[a5e7ed6]128  }
[619d864]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  }
[27964fe]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  }
[df569c5]140
[12e291a]141  perl = owl_perlconfig_execute("BarnOwl::Zephyr::_zephyr_startup()");
[ddbbcffa]142  g_free(perl);
[2d04312]143  return FALSE;
[52a0f14]144}
145
[c79a047]146void owl_zephyr_load_initial_subs(void) {
[7451af9]147  int ret_sd, ret_bd, ret_u;
[52a0f14]148
149  owl_function_debugmsg("startup: loading initial zephyr subs");
150
151  /* load default subscriptions */
[7451af9]152  ret_sd = owl_zephyr_loaddefaultsubs();
153
[441fd42]154  /* load BarnOwl default subscriptions */
[7451af9]155  ret_bd = owl_zephyr_loadbarnowldefaultsubs();
[52a0f14]156
157  /* load subscriptions from subs file */
[7451af9]158  ret_u = owl_zephyr_loadsubs(NULL, 0);
[52a0f14]159
[7451af9]160  if (ret_sd || ret_bd || ret_u) {
[52a0f14]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
[c79a047]171void owl_zephyr_initialize(void)
[52a0f14]172{
173}
[02f55dc]174#endif
175
[52a0f14]176
[c79a047]177int owl_zephyr_shutdown(void)
[09489b89]178{
179#ifdef HAVE_LIBZEPHYR
[bfbf590]180  if(owl_global_is_havezephyr(&g)) {
181    unsuball();
182    ZClosePort();
183  }
[09489b89]184#endif
[32ad44d]185  return 0;
[be0a79f]186}
187
[c79a047]188int owl_zephyr_zpending(void)
[be0a79f]189{
190#ifdef HAVE_LIBZEPHYR
[8ab1f28]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  }
[be0a79f]200#endif
[32ad44d]201  return 0;
[be0a79f]202}
203
[959cb85]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
[c79a047]220const char *owl_zephyr_get_realm(void)
[09489b89]221{
222#ifdef HAVE_LIBZEPHYR
[32ad44d]223  if (owl_global_is_havezephyr(&g))
224    return(ZGetRealm());
[09489b89]225#endif
[32ad44d]226  return "";
[09489b89]227}
228
[c79a047]229const char *owl_zephyr_get_sender(void)
[09489b89]230{
231#ifdef HAVE_LIBZEPHYR
[32ad44d]232  if (owl_global_is_havezephyr(&g))
233    return(ZGetSender());
[09489b89]234#endif
[32ad44d]235  return "";
[09489b89]236}
237
[f6050ee]238#ifdef HAVE_LIBZEPHYR
[93e883d]239int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
240{
[18105584]241  int ret = 0;
[c73a22d]242  Code_t code;
243
[a5e7ed6]244  if (owl_global_is_havezephyr(&g)) {
245    int i;
246    /* sub without defaults */
[c73a22d]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));
[a5e7ed6]251      ret=-2;
252    }
[93e883d]253
[a5e7ed6]254    /* free stuff */
255    for (i=0; i<count; i++) {
[ddbbcffa]256      g_free(subs[i].zsub_class);
257      g_free(subs[i].zsub_classinst);
258      g_free(subs[i].zsub_recipient);
[a5e7ed6]259    }
[bb2c60d]260
[ddbbcffa]261    g_free(subs);
[a5e7ed6]262  } else {
[7dcef03]263    owl_sub_list *s = g_slice_new(owl_sub_list);
[a5e7ed6]264    s->subs = subs;
265    s->nsubs = count;
266    deferred_subs = g_list_append(deferred_subs, s);
267  }
[d21efbc]268
[93e883d]269  return ret;
270}
[f6050ee]271#endif
[93e883d]272
[7451af9]273/* Load zephyr subscriptions from 'filename'.  If 'filename' is NULL,
[95474d7]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.
[2de4f20]280 */
[e19eb97]281int owl_zephyr_loadsubs(const char *filename, int error_on_nofile)
[31e48a3]282{
[be0a79f]283#ifdef HAVE_LIBZEPHYR
[7d4fbcd]284  FILE *file;
[18380fd]285  int fopen_errno;
[7d4fbcd]286  char *tmp, *start;
[b7ee89b]287  char *buffer = NULL;
288  char *subsfile;
[bb2c60d]289  ZSubscription_t *subs;
290  int subSize = 1024;
[b7ee89b]291  int count;
[7d4fbcd]292
[6ea3890]293  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
[b7ee89b]294
295  count = 0;
296  file = fopen(subsfile, "r");
[18380fd]297  fopen_errno = errno;
[ddbbcffa]298  g_free(subsfile);
[18380fd]299  if (!file) {
300    if (error_on_nofile == 1 || fopen_errno != ENOENT)
301      return -1;
302    return 0;
303  }
[bbe7d4a]304
305  subs = g_new(ZSubscription_t, subSize);
[b7ee89b]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
[bb2c60d]315    if (count >= subSize) {
[d21efbc]316      subSize *= 2;
[35b6eb9]317      subs = g_renew(ZSubscription_t, subs, subSize);
[93e883d]318    }
[95474d7]319   
320    /* add it to the list of subs */
[b7ee89b]321    if ((tmp = strtok(start, ",\n\r")) == NULL)
322      continue;
[d4927a7]323    subs[count].zsub_class = g_strdup(tmp);
[b7ee89b]324    if ((tmp=strtok(NULL, ",\n\r")) == NULL)
325      continue;
[d4927a7]326    subs[count].zsub_classinst = g_strdup(tmp);
[b7ee89b]327    if ((tmp = strtok(NULL, " \t\n\r")) == NULL)
328      continue;
[d4927a7]329    subs[count].zsub_recipient = g_strdup(tmp);
[b7ee89b]330
[bb2c60d]331    /* if it started with '-' then add it to the global punt list, and
332     * remove it from the list of subs. */
[b7ee89b]333    if (buffer[0] == '-') {
[95474d7]334      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
[ddbbcffa]335      g_free(subs[count].zsub_class);
336      g_free(subs[count].zsub_classinst);
337      g_free(subs[count].zsub_recipient);
[b7ee89b]338    } else {
[bb2c60d]339      count++;
[95474d7]340    }
[7d4fbcd]341  }
[95474d7]342  fclose(file);
[b7ee89b]343  if (buffer)
[ddbbcffa]344    g_free(buffer);
[7d4fbcd]345
[18380fd]346  ZResetAuthentication();
[b7ee89b]347  return owl_zephyr_loadsubs_helper(subs, count);
[7451af9]348#else
[b7ee89b]349  return 0;
[7451af9]350#endif
351}
352
[441fd42]353/* Load default BarnOwl subscriptions
[7451af9]354 *
355 * Returns 0 on success.
356 * Return -2 if there is a failure from zephyr to load the subscriptions.
357 */
[c79a047]358int owl_zephyr_loadbarnowldefaultsubs(void)
[7451af9]359{
360#ifdef HAVE_LIBZEPHYR
361  ZSubscription_t *subs;
[441fd42]362  int subSize = 10; /* Max BarnOwl default subs we allow */
[7451af9]363  int count, ret;
364
[96828e4]365  subs = g_new(ZSubscription_t, subSize);
[7451af9]366  ZResetAuthentication();
367  count=0;
368
[d4927a7]369  subs[count].zsub_class=g_strdup("message");
370  subs[count].zsub_classinst=g_strdup("*");
371  subs[count].zsub_recipient=g_strdup("%me%");
[7451af9]372  count++;
373
374  ret = owl_zephyr_loadsubs_helper(subs, count);
[7d4fbcd]375  return(ret);
[be0a79f]376#else
377  return(0);
378#endif
[7d4fbcd]379}
380
[c79a047]381int owl_zephyr_loaddefaultsubs(void)
[4357be8]382{
[40d834a]383#ifdef HAVE_LIBZEPHYR
[c73a22d]384  Code_t ret;
385
[2c5ee3e]386  if (owl_global_is_havezephyr(&g)) {
387    ZSubscription_t subs[10];
[c73a22d]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));
[2c5ee3e]393      return(-1);
394    }
[4357be8]395  }
396  return(0);
[40d834a]397#else
398  return(0);
399#endif
[4357be8]400}
401
[e19eb97]402int owl_zephyr_loadloginsubs(const char *filename)
[31e48a3]403{
[be0a79f]404#ifdef HAVE_LIBZEPHYR
[7d4fbcd]405  FILE *file;
[d21efbc]406  ZSubscription_t *subs;
407  int numSubs = 100;
[b7ee89b]408  char *subsfile;
409  char *buffer = NULL;
410  int count;
[7d4fbcd]411
[96828e4]412  subs = g_new(ZSubscription_t, numSubs);
[6ea3890]413  subsfile = owl_zephyr_dotfile(".anyone", filename);
[4357be8]414
[b7ee89b]415  count = 0;
416  file = fopen(subsfile, "r");
[ddbbcffa]417  g_free(subsfile);
[7d4fbcd]418  if (file) {
[b7ee89b]419    while (owl_getline_chomp(&buffer, file)) {
420      if (buffer[0] == '\0' || buffer[0] == '#')
421        continue;
422
[d21efbc]423      if (count == numSubs) {
424        numSubs *= 2;
[35b6eb9]425        subs = g_renew(ZSubscription_t, subs, numSubs);
[d21efbc]426      }
[7d4fbcd]427
[d4927a7]428      subs[count].zsub_class = g_strdup("login");
429      subs[count].zsub_recipient = g_strdup("*");
[b7ee89b]430      subs[count].zsub_classinst = long_zuser(buffer);
[7d4fbcd]431
432      count++;
433    }
434    fclose(file);
435  } else {
[18380fd]436    g_free(subs);
[b7ee89b]437    return 0;
[7d4fbcd]438  }
[3b8a563]439  g_free(buffer);
[7d4fbcd]440
[18380fd]441  ZResetAuthentication();
[b7ee89b]442  return owl_zephyr_loadsubs_helper(subs, count);
[be0a79f]443#else
[b7ee89b]444  return 0;
[be0a79f]445#endif
[7d4fbcd]446}
447
[60e8617]448bool unsuball(void)
[31e48a3]449{
[be0a79f]450#if HAVE_LIBZEPHYR
[00842c3]451  Code_t ret;
[8262340]452
453  ZResetAuthentication();
[00842c3]454  ret = ZCancelSubscriptions(0);
455  if (ret != ZERR_NONE)
456    owl_function_error("Zephyr: Cancelling subscriptions: %s",
457                       error_message(ret));
[60e8617]458  return (ret == ZERR_NONE);
[be0a79f]459#endif
[60e8617]460  return true;
[7d4fbcd]461}
462
[e19eb97]463int owl_zephyr_sub(const char *class, const char *inst, const char *recip)
[31e48a3]464{
[be0a79f]465#ifdef HAVE_LIBZEPHYR
[7d4fbcd]466  ZSubscription_t subs[5];
[c73a22d]467  Code_t ret;
[7d4fbcd]468
[712caac]469  subs[0].zsub_class=zstr(class);
470  subs[0].zsub_classinst=zstr(inst);
471  subs[0].zsub_recipient=zstr(recip);
[7d4fbcd]472
[8262340]473  ZResetAuthentication();
[c73a22d]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));
[97cd00be]479    return(-2);
[7d4fbcd]480  }
481  return(0);
[be0a79f]482#else
483  return(0);
484#endif
[7d4fbcd]485}
486
487
[e19eb97]488int owl_zephyr_unsub(const char *class, const char *inst, const char *recip)
[31e48a3]489{
[be0a79f]490#ifdef HAVE_LIBZEPHYR
[7d4fbcd]491  ZSubscription_t subs[5];
[c73a22d]492  Code_t ret;
[7d4fbcd]493
[712caac]494  subs[0].zsub_class=zstr(class);
495  subs[0].zsub_classinst=zstr(inst);
496  subs[0].zsub_recipient=zstr(recip);
[7d4fbcd]497
[8262340]498  ZResetAuthentication();
[c73a22d]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));
[97cd00be]504    return(-2);
[7d4fbcd]505  }
506  return(0);
[be0a79f]507#else
508  return(0);
509#endif
[7d4fbcd]510}
511
[be0a79f]512#ifdef HAVE_LIBZEPHYR
[a03a409]513const char *owl_zephyr_first_raw_field(const ZNotice_t *n)
[31e48a3]514{
[128171a]515  if (n->z_message_len == 0)
[a03a409]516    return NULL;
517  return n->z_message;
518}
[128171a]519
[a03a409]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}
[b0430a6]528
[a03a409]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;
[7d4fbcd]537}
[5376a95]538
[a03a409]539CALLER_OWN char *owl_zephyr_field(const ZNotice_t *n, const char *f)
[5376a95]540{
[a03a409]541  if (f == NULL)
542    return g_strdup("");
543  return g_strndup(f, n->z_message + n->z_message_len - f);
544}
[5376a95]545
[a03a409]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}
[5376a95]553
[a03a409]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}
[5376a95]558
[a03a409]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));
[5376a95]562}
[09489b89]563#else
[a03a409]564const char *owl_zephyr_first_raw_field(const void *n)
[09489b89]565{
[a03a409]566  return NULL;
[09489b89]567}
[a03a409]568
569const char *owl_zephyr_next_raw_field(const void *n, const char *f)
[5376a95]570{
[a03a409]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));
[5376a95]597}
[be0a79f]598#endif
[7d4fbcd]599
[b0430a6]600
[be0a79f]601#ifdef HAVE_LIBZEPHYR
[1077891a]602int owl_zephyr_get_num_fields(const ZNotice_t *n)
[31e48a3]603{
[a03a409]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;
[7d4fbcd]610}
[09489b89]611#else
[1077891a]612int owl_zephyr_get_num_fields(const void *n)
[09489b89]613{
614  return(0);
615}
[be0a79f]616#endif
[7d4fbcd]617
[be0a79f]618#ifdef HAVE_LIBZEPHYR
[bf73bdd]619/* return a pointer to the message, place the message length in k
620 * caller must free the return
621 */
[6829afc]622CALLER_OWN char *owl_zephyr_get_message(const ZNotice_t *n, const owl_message *m)
[31e48a3]623{
[c518676]624#define OWL_NFIELDS     5
625  int i;
626  char *fields[OWL_NFIELDS + 1];
627  char *msg = NULL;
628
[405d5e6]629  /* don't let ping messages have a body */
[7d4fbcd]630  if (!strcasecmp(n->z_opcode, "ping")) {
[d4927a7]631    return(g_strdup(""));
[7d4fbcd]632  }
633
[c518676]634  for(i = 0; i < OWL_NFIELDS; i++)
635    fields[i + 1] = owl_zephyr_get_field(n, i + 1);
636
[a1bb198]637  /* deal with MIT NOC messages */
[85d1795]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")) {
[a1bb198]639
[c518676]640    msg = g_strdup_printf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, fields[3], fields[4]);
[a1bb198]641  }
[fba0f96]642  /* deal with MIT Discuss messages */
[e2a620b]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")) {
[fba0f96]645   
[c518676]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]);
[fba0f96]648  }
[85d1795]649  /* deal with MIT Moira messages */
650  else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
[c518676]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]);
[85d1795]660  }
[405d5e6]661
[c518676]662  for (i = 0; i < OWL_NFIELDS; i++)
663    g_free(fields[i + 1]);
664
665  return msg;
[7d4fbcd]666}
[be0a79f]667#endif
[7d4fbcd]668
[be0a79f]669#ifdef HAVE_LIBZEPHYR
[1077891a]670const char *owl_zephyr_get_zsig(const ZNotice_t *n, int *k)
[31e48a3]671{
[7d4fbcd]672  /* return a pointer to the zsig if there is one */
673
[405d5e6]674  /* message length 0? No zsig */
[7d4fbcd]675  if (n->z_message_len==0) {
676    *k=0;
677    return("");
678  }
[405d5e6]679
[85d1795]680  /* If there's only one field, no zsig */
681  if (owl_zephyr_get_num_fields(n) == 1) {
682    *k=0;
[405d5e6]683    return("");
684  }
685
686  /* Everything else is field 1 */
[7d4fbcd]687  *k=strlen(n->z_message);
688  return(n->z_message);
689}
[09489b89]690#else
[1077891a]691const char *owl_zephyr_get_zsig(const void *n, int *k)
[09489b89]692{
693  return("");
694}
[be0a79f]695#endif
[7d4fbcd]696
[e19eb97]697int send_zephyr(const char *opcode, const char *zsig, const char *class, const char *instance, const char *recipient, const char *message)
[31e48a3]698{
[be0a79f]699#ifdef HAVE_LIBZEPHYR
[c73a22d]700  Code_t ret;
[7d4fbcd]701  ZNotice_t notice;
[f7c0730]702  char *zsender = NULL;
[7d4fbcd]703   
704  memset(&notice, 0, sizeof(notice));
705
[8262340]706  ZResetAuthentication();
[8ba37ec]707
708  if (!zsig) zsig="";
[8262340]709 
[7d4fbcd]710  notice.z_kind=ACKED;
711  notice.z_port=0;
[80bea5b]712#ifdef ZCHARSET_UTF_8
713  notice.z_charset = ZCHARSET_UTF_8;
714#endif
[712caac]715  notice.z_class=zstr(class);
716  notice.z_class_inst=zstr(instance);
[bf70350]717  if (!strcmp(recipient, "@")) {
[712caac]718    notice.z_recipient=zstr("");
[7d4fbcd]719  } else {
[712caac]720    notice.z_recipient=zstr(recipient);
[7d4fbcd]721  }
[f183917]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));
[80d7b44]724  notice.z_default_format=zstr(ZEPHYR_DEFAULT_FORMAT);
[712caac]725  if (opcode) notice.z_opcode=zstr(opcode);
[7d4fbcd]726
[56330ff]727  notice.z_message_len=strlen(zsig)+1+strlen(message);
[96828e4]728  notice.z_message=g_new(char, notice.z_message_len+10);
[56330ff]729  strcpy(notice.z_message, zsig);
730  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
[7d4fbcd]731
732  /* ret=ZSendNotice(&notice, ZAUTH); */
733  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
734 
735  /* free then check the return */
[ddbbcffa]736  g_free(notice.z_message);
[f7c0730]737  g_free(zsender);
[c73a22d]738  if (ret != ZERR_NONE) {
739    owl_function_error("Error sending zephyr: %s", error_message(ret));
[7d4fbcd]740    return(ret);
741  }
742  return(0);
[be0a79f]743#else
744  return(0);
745#endif
[7d4fbcd]746}
747
[be0a79f]748#ifdef HAVE_LIBZEPHYR
[31e48a3]749Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
750{
[7d4fbcd]751  return(ZSendPacket(buf, len, 0));
752}
[be0a79f]753#endif
[7d4fbcd]754
[e19eb97]755void send_ping(const char *to, const char *zclass, const char *zinstance)
[31e48a3]756{
[be0a79f]757#ifdef HAVE_LIBZEPHYR
[3ef779b]758  send_zephyr("PING", "", zclass, zinstance, to, "");
[be0a79f]759#endif
[7d4fbcd]760}
761
[be0a79f]762#ifdef HAVE_LIBZEPHYR
[1077891a]763void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
[31e48a3]764{
[7d4fbcd]765  char *tmp;
766 
767  /* if it's an HMACK ignore it */
768  if (retnotice->z_kind == HMACK) return;
[aecf3e6]769
[7d4fbcd]770  if (retnotice->z_kind == SERVNAK) {
[ec6ff52]771    owl_function_error("Authorization failure sending zephyr");
[7d4fbcd]772  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
[ec6ff52]773    owl_function_error("Detected server failure while receiving acknowledgement");
[7d4fbcd]774  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
775    if (!strcasecmp(retnotice->z_opcode, "ping")) {
776      return;
777    } else {
[d73e3af]778      if (strcasecmp(retnotice->z_recipient, ""))
[1e550b2]779      { /* personal */
[d73e3af]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);
[1e550b2]784        } else if(!strcasecmp(retnotice->z_class, "message")) { /* instanced, but not classed, personal */
[d73e3af]785          owl_function_makemsg("Message sent to %s on -i %s\n", tmp, retnotice->z_class_inst);
[1e550b2]786        } else { /* classed personal */
[d73e3af]787          owl_function_makemsg("Message sent to %s on -c %s -i %s\n", tmp, retnotice->z_class, retnotice->z_class_inst);
788        }
[ddbbcffa]789        g_free(tmp);
[d73e3af]790      } else {
[1e550b2]791        /* class / instance message */
[d73e3af]792          owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
793      }
[7d4fbcd]794    }
795  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
[44f32fb]796    if (retnotice->z_recipient == NULL
[bff1f22]797        || !owl_zwrite_recip_is_personal(retnotice->z_recipient)) {
[e3a75ed]798      char *buff;
[44f32fb]799      owl_function_error("No one subscribed to class %s", retnotice->z_class);
[e3a75ed]800      buff = g_strdup_printf("Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
[9119a47]801      owl_function_adminmsg("", buff);
[e3a75ed]802      g_free(buff);
[7d4fbcd]803    } else {
[e3a75ed]804      char *buff;
[24ccc01]805      owl_zwrite zw;
806
[4b464a4]807      tmp = short_zuser(retnotice->z_recipient);
[44f32fb]808      owl_function_error("%s: Not logged in or subscribing.", tmp);
[3ef779b]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")) {
[e3a75ed]815        buff = g_strdup_printf(
[10e3963]816                 "Could not send message to %s: "
[3ef779b]817                 "not logged in or subscribing to class %s, instance %s.\n",
[10e3963]818                 tmp,
[1151f0b]819                 retnotice->z_class,
820                 retnotice->z_class_inst);
[3ef779b]821      } else if(strcasecmp(retnotice->z_class_inst, "personal")) {
[e3a75ed]822        buff = g_strdup_printf(
[3ef779b]823                 "Could not send message to %s: "
824                 "not logged in or subscribing to instance %s.\n",
825                 tmp,
826                 retnotice->z_class_inst);
[44f32fb]827      } else {
[e3a75ed]828        buff = g_strdup_printf(
[10e3963]829                 "Could not send message to %s: "
830                 "not logged in or subscribing to messages.\n",
831                 tmp);
[44f32fb]832      }
[9119a47]833      owl_function_adminmsg("", buff);
[24ccc01]834
835      memset(&zw, 0, sizeof(zw));
[d4927a7]836      zw.class = g_strdup(retnotice->z_class);
837      zw.inst  = g_strdup(retnotice->z_class_inst);
[fe3b017]838      zw.realm = g_strdup("");
[d4927a7]839      zw.opcode = g_strdup(retnotice->z_opcode);
840      zw.zsig   = g_strdup("");
[12294d2]841      zw.recips = g_ptr_array_new();
842      g_ptr_array_add(zw.recips, g_strdup(retnotice->z_recipient));
[24ccc01]843
844      owl_log_outgoing_zephyr_error(&zw, buff);
845
[c230bc1]846      owl_zwrite_cleanup(&zw);
[e3a75ed]847      g_free(buff);
[ddbbcffa]848      g_free(tmp);
[7d4fbcd]849    }
850  } else {
[ec6ff52]851    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]852  }
853}
[09489b89]854#else
[1077891a]855void owl_zephyr_handle_ack(const void *retnotice)
[09489b89]856{
857}
[be0a79f]858#endif
[7d4fbcd]859
[be0a79f]860#ifdef HAVE_LIBZEPHYR
[1077891a]861int owl_zephyr_notice_is_ack(const ZNotice_t *n)
[31e48a3]862{
[7d4fbcd]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}
[09489b89]869#else
[1077891a]870int owl_zephyr_notice_is_ack(const void *n)
[09489b89]871{
872  return(0);
873}
[be0a79f]874#endif
[7d4fbcd]875 
[c08c70a]876void owl_zephyr_zaway(const owl_message *m)
[31e48a3]877{
[be0a79f]878#ifdef HAVE_LIBZEPHYR
[7c8060d0]879  char *tmpbuff, *myuser, *to;
[987cf3f]880  owl_zwrite *z;
[7d4fbcd]881 
[aa2f6364]882  /* bail if it doesn't look like a message we should reply to.  Some
[2de4f20]883   * of this defined by the way zaway(1) works
884   */
[7d4fbcd]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;
[d023c25]890  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
[7d4fbcd]891  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
[9854278]892  if (owl_message_get_attribute_value(m, "isauto")) return;
[7d4fbcd]893
[7c8060d0]894  if (owl_global_is_smartstrip(&g)) {
[e3d9c77]895    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
[7c8060d0]896  } else {
[d4927a7]897    to=g_strdup(owl_message_get_sender(m));
[7c8060d0]898  }
899
[7d4fbcd]900  send_zephyr("",
901              "Automated reply:",
902              owl_message_get_class(m),
903              owl_message_get_instance(m),
[7c8060d0]904              to,
[7d4fbcd]905              owl_global_get_zaway_msg(&g));
906
[7c8060d0]907  myuser=short_zuser(to);
[aa2f6364]908  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
[eb8d9c1]909    tmpbuff = owl_string_build_quoted("zwrite %q", myuser);
[aa2f6364]910  } else {
[eb8d9c1]911    tmpbuff = owl_string_build_quoted("zwrite -i %q %q", owl_message_get_instance(m), myuser);
[aa2f6364]912  }
[ddbbcffa]913  g_free(myuser);
914  g_free(to);
[aa2f6364]915
[d953ede]916  z = owl_zwrite_new_from_line(tmpbuff);
[bb85767]917  g_free(tmpbuff);
918  if (z == NULL) {
919    owl_function_error("Error creating outgoing zephyr.");
920    return;
921  }
[987cf3f]922  owl_zwrite_set_message(z, owl_global_get_zaway_msg(&g));
923  owl_zwrite_set_zsig(z, "Automated reply:");
924
[7d4fbcd]925  /* display the message as an admin message in the receive window */
[e5da3fe]926  owl_function_add_outgoing_zephyrs(z);
[987cf3f]927  owl_zwrite_delete(z);
[be0a79f]928#endif
[7d4fbcd]929}
930
[be0a79f]931#ifdef HAVE_LIBZEPHYR
[31e48a3]932void owl_zephyr_hackaway_cr(ZNotice_t *n)
933{
[7d4fbcd]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}
[be0a79f]943#endif
[7d4fbcd]944
[6829afc]945CALLER_OWN char *owl_zephyr_zlocate(const char *user, int auth)
[31e48a3]946{
[be0a79f]947#ifdef HAVE_LIBZEPHYR
[7d4fbcd]948  int ret, numlocs;
949  int one = 1;
950  ZLocations_t locations;
951  char *myuser;
[dca3b27]952  char *p, *result;
953
[8262340]954  ZResetAuthentication();
[dca3b27]955  ret = ZLocateUser(zstr(user), &numlocs, auth ? ZAUTH : ZNOAUTH);
956  if (ret != ZERR_NONE)
[3472845]957    return g_strdup_printf("Error locating user %s: %s\n",
958                           user, error_message(ret));
[dca3b27]959
960  myuser = short_zuser(user);
961  if (numlocs == 0) {
[3472845]962    result = g_strdup_printf("%s: Hidden or not logged in\n", myuser);
[dca3b27]963  } else {
[d4927a7]964    result = g_strdup("");
[dca3b27]965    for (; numlocs; numlocs--) {
966      ZGetLocations(&locations, &one);
[3472845]967      p = g_strdup_printf("%s%s: %s\t%s\t%s\n",
[dca3b27]968                          result, myuser,
969                          locations.host ? locations.host : "?",
970                          locations.tty ? locations.tty : "?",
971                          locations.time ? locations.time : "?");
[ddbbcffa]972      g_free(result);
[dca3b27]973      result = p;
974    }
[7d4fbcd]975  }
[ddbbcffa]976  g_free(myuser);
[7d4fbcd]977
[dca3b27]978  return result;
979#else
[d4927a7]980  return g_strdup("");
[be0a79f]981#endif
[7d4fbcd]982}
[bde7714]983
[e19eb97]984void owl_zephyr_addsub(const char *filename, const char *class, const char *inst, const char *recip)
[31e48a3]985{
[be0a79f]986#ifdef HAVE_LIBZEPHYR
[b7ee89b]987  char *line, *subsfile, *s = NULL;
[bde7714]988  FILE *file;
[b7ee89b]989  int duplicate = 0;
[bde7714]990
[b7ee89b]991  line = owl_zephyr_makesubline(class, inst, recip);
[6ea3890]992  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
[bde7714]993
[74037d9]994  /* if the file already exists, check to see if the sub is already there */
[b7ee89b]995  file = fopen(subsfile, "r");
[74037d9]996  if (file) {
[b7ee89b]997    while (owl_getline(&s, file)) {
998      if (strcasecmp(s, line) == 0) {
[74037d9]999        owl_function_error("Subscription already present in %s", subsfile);
[b7ee89b]1000        duplicate++;
[74037d9]1001      }
[bde7714]1002    }
[99dabee]1003    fclose(file);
[ddbbcffa]1004    g_free(s);
[bde7714]1005  }
1006
[b7ee89b]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    }
[bde7714]1016  }
[b7ee89b]1017
[60ae736]1018  g_free(subsfile);
[ddbbcffa]1019  g_free(line);
[be0a79f]1020#endif
[bde7714]1021}
1022
[e19eb97]1023void owl_zephyr_delsub(const char *filename, const char *class, const char *inst, const char *recip)
[31e48a3]1024{
[be0a79f]1025#ifdef HAVE_LIBZEPHYR
[b2a91b6]1026  char *line, *subsfile;
[da60ba9]1027  int linesdeleted;
[bde7714]1028 
1029  line=owl_zephyr_makesubline(class, inst, recip);
[b2a91b6]1030  line[strlen(line)-1]='\0';
[bde7714]1031
[6ea3890]1032  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
[b2a91b6]1033 
[da60ba9]1034  linesdeleted = owl_util_file_deleteline(subsfile, line, 1);
1035  if (linesdeleted > 0) {
1036    owl_function_makemsg("Subscription removed");
[5fca55f]1037  } else if (linesdeleted == 0) {
[da60ba9]1038    owl_function_error("No subscription present in %s", subsfile);
1039  }
[ddbbcffa]1040  g_free(subsfile);
1041  g_free(line);
[be0a79f]1042#endif
[bde7714]1043}
1044
[b2a91b6]1045/* caller must free the return */
[6829afc]1046CALLER_OWN char *owl_zephyr_makesubline(const char *class, const char *inst, const char *recip)
[31e48a3]1047{
[3472845]1048  return g_strdup_printf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
[bde7714]1049}
[31e48a3]1050
1051
1052void owl_zephyr_zlog_in(void)
1053{
[be0a79f]1054#ifdef HAVE_LIBZEPHYR
[31e48a3]1055  ZResetAuthentication();
[7d969f3]1056
[f203cad]1057  /* ZSetLocation, and store the default value as the current value */
1058  owl_global_set_exposure(&g, owl_global_get_default_exposure(&g));
[be0a79f]1059#endif
[31e48a3]1060}
1061
1062void owl_zephyr_zlog_out(void)
1063{
[be0a79f]1064#ifdef HAVE_LIBZEPHYR
[7d969f3]1065  Code_t ret;
[31e48a3]1066
1067  ZResetAuthentication();
[7d969f3]1068  ret = ZUnsetLocation();
1069  if (ret != ZERR_NONE)
1070    owl_function_error("Error unsetting location: %s", error_message(ret));
[be0a79f]1071#endif
[31e48a3]1072}
1073
[e19eb97]1074void owl_zephyr_addbuddy(const char *name)
[65ad073]1075{
1076  char *filename;
1077  FILE *file;
1078 
[6ea3890]1079  filename = owl_zephyr_dotfile(".anyone", NULL);
1080  file = fopen(filename, "a");
[ddbbcffa]1081  g_free(filename);
[65ad073]1082  if (!file) {
[ec6ff52]1083    owl_function_error("Error opening zephyr buddy file for append");
[65ad073]1084    return;
1085  }
1086  fprintf(file, "%s\n", name);
1087  fclose(file);
1088}
1089
[e19eb97]1090void owl_zephyr_delbuddy(const char *name)
[65ad073]1091{
1092  char *filename;
1093
[6ea3890]1094  filename = owl_zephyr_dotfile(".anyone", NULL);
[65ad073]1095  owl_util_file_deleteline(filename, name, 0);
[ddbbcffa]1096  g_free(filename);
[65ad073]1097}
[9381782]1098
[c55930e]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
[9381782]1115/* return auth string */
[09489b89]1116#ifdef HAVE_LIBZEPHYR
[1077891a]1117const char *owl_zephyr_get_authstr(const ZNotice_t *n)
[9381782]1118{
1119
[f12d199]1120  if (!n) return("UNKNOWN");
1121
1122  if (n->z_auth == ZAUTH_FAILED) {
[9381782]1123    return ("FAILED");
[f12d199]1124  } else if (n->z_auth == ZAUTH_NO) {
[9381782]1125    return ("NO");
[f12d199]1126  } else if (n->z_auth == ZAUTH_YES) {
[9381782]1127    return ("YES");
1128  } else {
1129    return ("UNKNOWN");
[f12d199]1130  }           
[9381782]1131}
[09489b89]1132#else
[1077891a]1133const char *owl_zephyr_get_authstr(const void *n)
[09489b89]1134{
1135  return("");
1136}
1137#endif
[9381782]1138
[2de4f20]1139/* Returns a buffer of subscriptions or an error message.  Caller must
1140 * free the return.
[09489b89]1141 */
[6829afc]1142CALLER_OWN char *owl_zephyr_getsubs(void)
[09489b89]1143{
1144#ifdef HAVE_LIBZEPHYR
[c73a22d]1145  Code_t ret;
1146  int num, i, one;
[09489b89]1147  ZSubscription_t sub;
[df3a1f4]1148  GString *buf;
[09489b89]1149
[c73a22d]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");
[09489b89]1155
[df3a1f4]1156  buf = g_string_new("");
[09489b89]1157  for (i=0; i<num; i++) {
[df3a1f4]1158    one = 1;
[09489b89]1159    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1160      ZFlushSubscriptions();
[df3a1f4]1161      g_string_free(buf, true);
[c73a22d]1162      return g_strdup_printf("Zephyr: Getting subscriptions: %s\n", error_message(ret));
[09489b89]1163    } else {
[df3a1f4]1164      /* g_string_append_printf would be backwards. */
[3472845]1165      char *tmp = g_strdup_printf("<%s,%s,%s>\n",
1166                                  sub.zsub_class,
1167                                  sub.zsub_classinst,
1168                                  sub.zsub_recipient);
[df3a1f4]1169      g_string_prepend(buf, tmp);
[ddbbcffa]1170      g_free(tmp);
[09489b89]1171    }
1172  }
1173
1174  ZFlushSubscriptions();
[df3a1f4]1175  return g_string_free(buf, false);
[09489b89]1176#else
[d4927a7]1177  return(g_strdup("Zephyr not available"));
[09489b89]1178#endif
1179}
1180
[e19eb97]1181const char *owl_zephyr_get_variable(const char *var)
[09489b89]1182{
1183#ifdef HAVE_LIBZEPHYR
[712caac]1184  return(ZGetVariable(zstr(var)));
[09489b89]1185#else
1186  return("");
1187#endif
1188}
1189
[e19eb97]1190void owl_zephyr_set_locationinfo(const char *host, const char *val)
[09489b89]1191{
1192#ifdef HAVE_LIBZEPHYR
[712caac]1193  ZInitLocationInfo(zstr(host), zstr(val));
[09489b89]1194#endif
1195}
[f203cad]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));
[c855755]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     ) {
[f203cad]1260    owl_function_error("Unable to set exposure level: %s.", error_message(ret));
1261    return -1;
1262  }
1263#endif
1264  return 0;
1265}
[09489b89]1266 
[e3d9c77]1267/* Strip a local realm fron the zephyr user name.
1268 * The caller must free the return
1269 */
[6829afc]1270CALLER_OWN char *short_zuser(const char *in)
[e3d9c77]1271{
[5d56a27]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);
[e3d9c77]1275  }
[5d56a27]1276  return g_strdup(in);
[e3d9c77]1277}
1278
1279/* Append a local realm to the zephyr user name if necessary.
1280 * The caller must free the return.
1281 */
[6829afc]1282CALLER_OWN char *long_zuser(const char *in)
[e3d9c77]1283{
[5d56a27]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());
[e3d9c77]1290  }
[5d56a27]1291  return g_strdup_printf("%s@%s", in, owl_zephyr_get_realm());
[e3d9c77]1292}
1293
[4e29ecb]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
[e3d9c77]1305/* strip out the instance from a zsender's principal.  Preserves the
[9d21120]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.
[e3d9c77]1309 */
[6829afc]1310CALLER_OWN char *owl_zephyr_smartstripped_user(const char *in)
[e3d9c77]1311{
[ebcdf4d]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);
[e3d9c77]1323}
[5a95b69]1324
[ecffae6]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.
[5a95b69]1328 */
[ecffae6]1329GPtrArray *owl_zephyr_get_anyone_list(const char *filename)
[5a95b69]1330{
1331#ifdef HAVE_LIBZEPHYR
[b7ee89b]1332  char *ourfile, *tmp, *s = NULL;
[5a95b69]1333  FILE *f;
[ecffae6]1334  GPtrArray *list;
[5a95b69]1335
[6ea3890]1336  ourfile = owl_zephyr_dotfile(".anyone", filename);
[b7ee89b]1337
1338  f = fopen(ourfile, "r");
[5a95b69]1339  if (!f) {
1340    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
[ddbbcffa]1341    g_free(ourfile);
[ecffae6]1342    return NULL;
[5a95b69]1343  }
[ddbbcffa]1344  g_free(ourfile);
[5a95b69]1345
[ecffae6]1346  list = g_ptr_array_new();
[b7ee89b]1347  while (owl_getline_chomp(&s, f)) {
[5a95b69]1348    /* ignore comments, blank lines etc. */
[b7ee89b]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
[ecffae6]1362    g_ptr_array_add(list, long_zuser(s));
[5a95b69]1363  }
[ddbbcffa]1364  g_free(s);
[5a95b69]1365  fclose(f);
[ecffae6]1366  return list;
[5a95b69]1367#else
[ecffae6]1368  return NULL;
[5a95b69]1369#endif
1370}
[13a3c1db]1371
[f25812b]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! */
[7dcef03]1406            m = g_slice_new(owl_message);
[f25812b]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) {
[7dcef03]1419          m = g_slice_new(owl_message);
[f25812b]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);
[7dcef03]1428    g_slice_free(ZAsyncLocateData_t, zald);
[f25812b]1429  }
1430}
1431#else
1432void owl_zephyr_process_pseudologin(void *n)
1433{
1434}
1435#endif
1436
[72146c7]1437gboolean owl_zephyr_buddycheck_timer(void *data)
[3687413]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  }
[72146c7]1445  return TRUE;
[3687413]1446}
1447
[12e291a]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
[b848e30]1457#ifdef HAVE_LIBZEPHYR
[e9c6fc8]1458static int _owl_zephyr_process_events(void)
[12e291a]1459{
[13a3c1db]1460  int zpendcount=0;
1461  ZNotice_t notice;
[8ab1f28]1462  Code_t code;
[13a3c1db]1463  owl_message *m=NULL;
1464
[12e291a]1465  while(owl_zephyr_zpending() && zpendcount < OWL_MAX_ZEPHYRGRAMS_TO_PROCESS) {
[13a3c1db]1466    if (owl_zephyr_zpending()) {
[8ab1f28]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      }
[13a3c1db]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);
[46c7f5b]1477        ZFreeNotice(&notice);
[13a3c1db]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")) {
[46c7f5b]1483        ZFreeNotice(&notice);
[13a3c1db]1484        continue;
1485      }
1486
[f25812b]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
[13a3c1db]1494      /* create the new message */
[7dcef03]1495      m=g_slice_new(owl_message);
[13a3c1db]1496      owl_message_create_from_znotice(m, &notice);
1497
1498      owl_global_messagequeue_addmsg(&g, m);
1499    }
1500  }
[12e291a]1501  return zpendcount;
[13a3c1db]1502}
1503
[959cb85]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;
[e146cd7]1516  event_source->poll_fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
[959cb85]1517  g_source_add_poll(source, &event_source->poll_fd);
1518
1519  return source;
[12e291a]1520}
1521
[959cb85]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;
[13a3c1db]1537}
[b848e30]1538#endif
Note: See TracBrowser for help on using the repository browser.