source: zephyr.c @ 5fca55f

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