source: zephyr.c @ ecffae6

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