source: zephyr.c @ 946058b

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