source: zephyr.c @ 3965ec3d

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