source: zephyr.c @ 9d206c6

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