source: zephyr.c @ 245d586

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