source: zephyr.c @ ce35060

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