source: zephyr.c @ 358eeae

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