source: zephyr.c @ f81176c

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