source: zephyr.c @ c08a725

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