source: zephyr.c @ 2c14f40

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2c14f40 was 2c14f40, checked in by Geoffrey Thomas <geofft@mit.edu>, 12 years ago
owl_function_info: Explain the auth status better. Signed-off-by: Geoffrey Thomas <geofft@mit.edu>
  • Property mode set to 100644
File size: 32.1 KB
RevLine 
[7d4fbcd]1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
[4357be8]5#include <sys/stat.h>
[7d4fbcd]6#include <string.h>
7#include "owl.h"
8
[a5e7ed6]9static GList *deferred_subs = NULL;
[79d7d98]10
11#ifdef HAVE_LIBZEPHYR
[a5e7ed6]12typedef struct _owl_sub_list {                            /* noproto */
13  ZSubscription_t *subs;
14  int nsubs;
15} owl_sub_list;
16
[8262340]17Code_t ZResetAuthentication();
[09489b89]18#endif
[8262340]19
[02f55dc]20#define HM_SVC_FALLBACK         htons((unsigned short) 2104)
21
[52a0f14]22#ifdef HAVE_LIBZEPHYR
23void owl_zephyr_initialize()
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;
56  req.z_class = HM_STAT_CLASS;
57  req.z_class_inst = HM_STAT_CLIENT;
58  req.z_opcode = HM_GIMMESTATS;
59  req.z_sender = "";
60  req.z_recipient = "";
61  req.z_default_format = "";
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;
84
85  owl_select_remove_dispatch(d->fd);
86
87  ZClosePort();
88
89  if ((code = ZInitialize()) != ZERR_NONE) {
90    owl_function_error("Initializing Zephyr: %s", error_message(code));
91    return;
92  }
93
94  if ((code = ZOpenPort(NULL)) != ZERR_NONE) {
95    owl_function_error("Initializing Zephyr: %s", error_message(code));
96    return;
97  }
98
99  d = owl_malloc(sizeof(owl_dispatch));
100  d->fd = ZGetFD();
101  d->cfunc = &owl_zephyr_process_events;
102  d->destroy = NULL;
103  owl_select_add_dispatch(d);
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  }
[52a0f14]122}
123
124void owl_zephyr_load_initial_subs() {
[7451af9]125  int ret_sd, ret_bd, ret_u;
[52a0f14]126
127  owl_function_debugmsg("startup: loading initial zephyr subs");
128
129  /* load default subscriptions */
[7451af9]130  ret_sd = owl_zephyr_loaddefaultsubs();
131
132  /* load Barnowl default subscriptions */
133  ret_bd = owl_zephyr_loadbarnowldefaultsubs();
[52a0f14]134
135  /* load subscriptions from subs file */
[7451af9]136  ret_u = owl_zephyr_loadsubs(NULL, 0);
[52a0f14]137
[7451af9]138  if (ret_sd || ret_bd || ret_u) {
[52a0f14]139    owl_function_error("Error loading zephyr subscriptions");
[7451af9]140  } else if (ret_u!=-1) {
[52a0f14]141    owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
142  }
143
144  /* load login subscriptions */
145  if (owl_global_is_loginsubs(&g)) {
146    owl_function_debugmsg("startup: loading login subs");
147    owl_function_loadloginsubs(NULL);
148  }
149}
150#else
151void owl_zephyr_initialize()
152{
153}
[02f55dc]154#endif
155
[52a0f14]156
[09489b89]157int owl_zephyr_shutdown()
158{
159#ifdef HAVE_LIBZEPHYR
[bfbf590]160  if(owl_global_is_havezephyr(&g)) {
161    unsuball();
162    ZClosePort();
163  }
[09489b89]164#endif
[be0a79f]165  return(0);
166}
167
168int owl_zephyr_zpending()
169{
170#ifdef HAVE_LIBZEPHYR
[bfbf590]171  if(owl_global_is_havezephyr(&g))
172    return(ZPending());
173  else
174    return 0;
[be0a79f]175#else
176  return(0);
177#endif
178}
179
[7f6a8a2]180const char *owl_zephyr_get_realm()
[09489b89]181{
182#ifdef HAVE_LIBZEPHYR
183  return(ZGetRealm());
184#else
185  return("");
186#endif
187}
188
189char *owl_zephyr_get_sender()
190{
191#ifdef HAVE_LIBZEPHYR
192  return(ZGetSender());
193#else
194  return("");
195#endif
196}
197
[f6050ee]198#ifdef HAVE_LIBZEPHYR
[93e883d]199int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
200{
[18105584]201  int ret = 0;
[a5e7ed6]202  if (owl_global_is_havezephyr(&g)) {
203    int i;
204    /* sub without defaults */
205    if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
206      owl_function_error("Error subscribing to zephyr notifications.");
207      ret=-2;
208    }
[93e883d]209
[a5e7ed6]210    /* free stuff */
211    for (i=0; i<count; i++) {
212      owl_free(subs[i].zsub_class);
213      owl_free(subs[i].zsub_classinst);
214      owl_free(subs[i].zsub_recipient);
215    }
[bb2c60d]216
[a5e7ed6]217    owl_free(subs);
218  } else {
219    owl_sub_list *s = owl_malloc(sizeof(owl_sub_list));
220    s->subs = subs;
221    s->nsubs = count;
222    deferred_subs = g_list_append(deferred_subs, s);
223  }
[d21efbc]224
[93e883d]225  return ret;
226}
[f6050ee]227#endif
[93e883d]228
[7451af9]229/* Load zephyr subscriptions from 'filename'.  If 'filename' is NULL,
[95474d7]230 * the default file $HOME/.zephyr.subs will be used.
231 *
232 * Returns 0 on success.  If the file does not exist, return -1 if
233 * 'error_on_nofile' is 1, otherwise return 0.  Return -1 if the file
234 * exists but can not be read.  Return -2 if there is a failure from
235 * zephyr to load the subscriptions.
[2de4f20]236 */
[95474d7]237int owl_zephyr_loadsubs(char *filename, int error_on_nofile)
[31e48a3]238{
[be0a79f]239#ifdef HAVE_LIBZEPHYR
[7d4fbcd]240  FILE *file;
241  char *tmp, *start;
242  char buffer[1024], subsfile[1024];
[bb2c60d]243  ZSubscription_t *subs;
244  int subSize = 1024;
[93e883d]245  int count, ret;
[4357be8]246  struct stat statbuff;
[7d4fbcd]247
[bb2c60d]248  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
[7d4fbcd]249  if (filename==NULL) {
250    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
251  } else {
252    strcpy(subsfile, filename);
253  }
[8262340]254
[4357be8]255  ret=stat(subsfile, &statbuff);
[95474d7]256  if (ret) {
257    if (error_on_nofile==1) return(-1);
258    return(0);
259  }
[4357be8]260
[8262340]261  ZResetAuthentication();
[7d4fbcd]262  count=0;
263  file=fopen(subsfile, "r");
[95474d7]264  if (!file) return(-1);
265  while ( fgets(buffer, 1024, file)!=NULL ) {
266    if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
267   
268    if (buffer[0]=='-') {
269      start=buffer+1;
270    } else {
271      start=buffer;
[7d4fbcd]272    }
[95474d7]273   
[bb2c60d]274    if (count >= subSize) {
[d21efbc]275      subSize *= 2;
276      subs = owl_realloc(subs, sizeof(ZSubscription_t) * subSize);
[93e883d]277    }
[95474d7]278   
279    /* add it to the list of subs */
[4d86e06]280    if ((tmp=strtok(start, ",\n\r"))==NULL) continue;
[95474d7]281    subs[count].zsub_class=owl_strdup(tmp);
[4d86e06]282    if ((tmp=strtok(NULL, ",\n\r"))==NULL) continue;
[95474d7]283    subs[count].zsub_classinst=owl_strdup(tmp);
[4d86e06]284    if ((tmp=strtok(NULL, " \t\n\r"))==NULL) continue;
[95474d7]285    subs[count].zsub_recipient=owl_strdup(tmp);
286   
[bb2c60d]287    /* if it started with '-' then add it to the global punt list, and
288     * remove it from the list of subs. */
[95474d7]289    if (buffer[0]=='-') {
290      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
[bb2c60d]291      owl_free(subs[count].zsub_class);
292      owl_free(subs[count].zsub_classinst);
293      owl_free(subs[count].zsub_recipient);
294    }
295    else {
296      count++;
[95474d7]297    }
[7d4fbcd]298  }
[95474d7]299  fclose(file);
[7d4fbcd]300
[7451af9]301  ret = owl_zephyr_loadsubs_helper(subs, count);
302  return(ret);
303#else
304  return(0);
305#endif
306}
307
308/* Load default Barnowl subscriptions
309 *
310 * Returns 0 on success.
311 * Return -2 if there is a failure from zephyr to load the subscriptions.
312 */
313int owl_zephyr_loadbarnowldefaultsubs()
314{
315#ifdef HAVE_LIBZEPHYR
316  ZSubscription_t *subs;
317  int subSize = 10; /* Max Barnowl default subs we allow */
318  int count, ret;
319
320  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
321  ret = 0;
322  ZResetAuthentication();
323  count=0;
324
325  subs[count].zsub_class=owl_strdup("message");
326  subs[count].zsub_classinst=owl_strdup("*");
327  subs[count].zsub_recipient=owl_strdup("%me%");
328  count++;
329
330  ret = owl_zephyr_loadsubs_helper(subs, count);
[7d4fbcd]331  return(ret);
[be0a79f]332#else
333  return(0);
334#endif
[7d4fbcd]335}
336
[4357be8]337int owl_zephyr_loaddefaultsubs()
338{
[40d834a]339#ifdef HAVE_LIBZEPHYR
[4357be8]340  ZSubscription_t subs[10];
341   
342  if (ZSubscribeTo(subs,0,0) != ZERR_NONE) {
343    owl_function_error("Error subscribing to default zephyr notifications.");
344    return(-1);
345  }
346  return(0);
[40d834a]347#else
348  return(0);
349#endif
[4357be8]350}
351
[31e48a3]352int owl_zephyr_loadloginsubs(char *filename)
353{
[be0a79f]354#ifdef HAVE_LIBZEPHYR
[7d4fbcd]355  FILE *file;
[d21efbc]356  ZSubscription_t *subs;
357  int numSubs = 100;
[7d4fbcd]358  char subsfile[1024], buffer[1024];
[d21efbc]359  int count, ret;
[4357be8]360  struct stat statbuff;
[7d4fbcd]361
[d21efbc]362  subs = owl_malloc(numSubs * sizeof(ZSubscription_t));
363
[7d4fbcd]364  if (filename==NULL) {
365    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
366  } else {
367    strcpy(subsfile, filename);
368  }
[4357be8]369 
370  ret=stat(subsfile, &statbuff);
371  if (ret) return(0);
372
373  ret=0;
[7d4fbcd]374
[8262340]375  ZResetAuthentication();
[7d4fbcd]376  count=0;
377  file=fopen(subsfile, "r");
378  if (file) {
379    while ( fgets(buffer, 1024, file)!=NULL ) {
380      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
381     
[d21efbc]382      if (count == numSubs) {
383        numSubs *= 2;
384        subs = owl_realloc(subs, numSubs * sizeof(ZSubscription_t));
385      }
[7d4fbcd]386
387      buffer[strlen(buffer)-1]='\0';
[d21efbc]388      subs[count].zsub_class=owl_strdup("login");
389      subs[count].zsub_recipient=owl_strdup("*");
[7d4fbcd]390      if (strchr(buffer, '@')) {
[d21efbc]391        subs[count].zsub_classinst=owl_strdup(buffer);
[7d4fbcd]392      } else {
[d21efbc]393        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
[7d4fbcd]394      }
395
396      count++;
397    }
398    fclose(file);
399  } else {
400    count=0;
401    ret=-1;
402  }
403
[d21efbc]404  ret = owl_zephyr_loadsubs_helper(subs, count);
[7d4fbcd]405  return(ret);
[be0a79f]406#else
407  return(0);
408#endif
[7d4fbcd]409}
410
[31e48a3]411void unsuball()
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
[31e48a3]424int owl_zephyr_sub(char *class, char *inst, char *recip)
425{
[be0a79f]426#ifdef HAVE_LIBZEPHYR
[7d4fbcd]427  ZSubscription_t subs[5];
428
429  subs[0].zsub_class=class;
430  subs[0].zsub_classinst=inst;
431  subs[0].zsub_recipient=recip;
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
[31e48a3]445int owl_zephyr_unsub(char *class, char *inst, char *recip)
446{
[be0a79f]447#ifdef HAVE_LIBZEPHYR
[7d4fbcd]448  ZSubscription_t subs[5];
449
450  subs[0].zsub_class=class;
451  subs[0].zsub_classinst=inst;
452  subs[0].zsub_recipient=recip;
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
[b0430a6]469char *owl_zephyr_get_field(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
501char *owl_zephyr_get_field_as_utf8(ZNotice_t *n, int j)
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
[31e48a3]547int owl_zephyr_get_num_fields(ZNotice_t *n)
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
562int owl_zephyr_get_num_fields(void *n)
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 */
[85d1795]572char *owl_zephyr_get_message(ZNotice_t *n, 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
[31e48a3]637char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
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
658char *owl_zephyr_get_zsig(void *n, int *k)
659{
660  return("");
661}
[be0a79f]662#endif
[7d4fbcd]663
[31e48a3]664int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
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;
678  notice.z_class=class;
679  notice.z_class_inst=instance;
[21882032]680  notice.z_sender=NULL;
[7d4fbcd]681  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
682    notice.z_recipient="";
[21882032]683    if (*owl_global_get_zsender(&g))
684        notice.z_sender=owl_global_get_zsender(&g);
[7d4fbcd]685  } else {
686    notice.z_recipient=recipient;
687  }
688  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
689  if (opcode) notice.z_opcode=opcode;
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
[3ef779b]719void send_ping(char *to, char *zclass, 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
[31e48a3]727void owl_zephyr_handle_ack(ZNotice_t *retnotice)
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        }
753        free(tmp);
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];
[4b464a4]770      tmp = short_zuser(retnotice->z_recipient);
[44f32fb]771      owl_function_error("%s: Not logged in or subscribing.", tmp);
[3ef779b]772      /*
773       * These error messages are often over 80 chars, but users who want to
774       * see the whole thing can scroll to the side, and for those with wide
775       * terminals or who don't care, not splitting saves a line in the UI
776       */
777      if(strcasecmp(retnotice->z_class, "message")) {
[1151f0b]778        snprintf(buff, BUFFLEN,
[10e3963]779                 "Could not send message to %s: "
[3ef779b]780                 "not logged in or subscribing to class %s, instance %s.\n",
[10e3963]781                 tmp,
[1151f0b]782                 retnotice->z_class,
783                 retnotice->z_class_inst);
[3ef779b]784      } else if(strcasecmp(retnotice->z_class_inst, "personal")) {
785        snprintf(buff, BUFFLEN,
786                 "Could not send message to %s: "
787                 "not logged in or subscribing to instance %s.\n",
788                 tmp,
789                 retnotice->z_class_inst);
[44f32fb]790      } else {
[1151f0b]791        snprintf(buff, BUFFLEN,
[10e3963]792                 "Could not send message to %s: "
793                 "not logged in or subscribing to messages.\n",
794                 tmp);
[44f32fb]795      }
[9119a47]796      owl_function_adminmsg("", buff);
[180cd15]797      owl_log_outgoing_zephyr_error(tmp, buff);
[1c6c4d3]798      owl_free(tmp);
[7d4fbcd]799    }
800  } else {
[ec6ff52]801    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]802  }
803}
[09489b89]804#else
805void owl_zephyr_handle_ack(void *retnotice)
806{
807}
[be0a79f]808#endif
[7d4fbcd]809
[be0a79f]810#ifdef HAVE_LIBZEPHYR
[31e48a3]811int owl_zephyr_notice_is_ack(ZNotice_t *n)
812{
[7d4fbcd]813  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
814    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
815    return(1);
816  }
817  return(0);
818}
[09489b89]819#else
820int owl_zephyr_notice_is_ack(void *n)
821{
822  return(0);
823}
[be0a79f]824#endif
[7d4fbcd]825 
[31e48a3]826void owl_zephyr_zaway(owl_message *m)
827{
[be0a79f]828#ifdef HAVE_LIBZEPHYR
[7c8060d0]829  char *tmpbuff, *myuser, *to;
[15b34fd]830  owl_message *mout;
[7d4fbcd]831 
[aa2f6364]832  /* bail if it doesn't look like a message we should reply to.  Some
[2de4f20]833   * of this defined by the way zaway(1) works
834   */
[7d4fbcd]835  if (strcasecmp(owl_message_get_class(m), "message")) return;
836  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
837  if (!strcasecmp(owl_message_get_sender(m), "")) return;
838  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
839  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
[d023c25]840  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
[7d4fbcd]841  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
[9854278]842  if (owl_message_get_attribute_value(m, "isauto")) return;
[7d4fbcd]843
[7c8060d0]844  if (owl_global_is_smartstrip(&g)) {
[e3d9c77]845    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
[7c8060d0]846  } else {
847    to=owl_strdup(owl_message_get_sender(m));
848  }
849
[7d4fbcd]850  send_zephyr("",
851              "Automated reply:",
852              owl_message_get_class(m),
853              owl_message_get_instance(m),
[7c8060d0]854              to,
[7d4fbcd]855              owl_global_get_zaway_msg(&g));
856
[7c8060d0]857  myuser=short_zuser(to);
[aa2f6364]858  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
859    tmpbuff = owl_sprintf("zwrite %s", myuser);
860  } else {
861    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
862  }
863  owl_free(myuser);
[7c8060d0]864  owl_free(to);
[aa2f6364]865
[7d4fbcd]866  /* display the message as an admin message in the receive window */
[15b34fd]867  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
[13a3c1db]868  owl_global_messagequeue_addmsg(&g, mout);
[7d4fbcd]869  owl_free(tmpbuff);
[be0a79f]870#endif
[7d4fbcd]871}
872
[be0a79f]873#ifdef HAVE_LIBZEPHYR
[31e48a3]874void owl_zephyr_hackaway_cr(ZNotice_t *n)
875{
[7d4fbcd]876  /* replace \r's with ' '.  Gross-ish */
877  int i;
878
879  for (i=0; i<n->z_message_len; i++) {
880    if (n->z_message[i]=='\r') {
881      n->z_message[i]=' ';
882    }
883  }
884}
[be0a79f]885#endif
[7d4fbcd]886
[31e48a3]887void owl_zephyr_zlocate(char *user, char *out, int auth)
888{
[be0a79f]889#ifdef HAVE_LIBZEPHYR
[7d4fbcd]890  int ret, numlocs;
891  int one = 1;
892  ZLocations_t locations;
893  char *myuser;
894 
895  strcpy(out, "");
[8262340]896  ZResetAuthentication();
[7d4fbcd]897  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
898  if (ret != ZERR_NONE) {
[667a1b6]899    sprintf(out, "Error locating user %s\n", user);
900    return;
[7d4fbcd]901  }
902
903  if (numlocs==0) {
[2527615]904    myuser=short_zuser(user);
[451db9e]905    sprintf(out, "%s: Hidden or not logged in\n", myuser);
[2527615]906    owl_free(myuser);
[7d4fbcd]907    return;
908  }
909   
910  for (;numlocs;numlocs--) {
911    ZGetLocations(&locations,&one);
[4b464a4]912    myuser=short_zuser(user);
[b9cb41b]913    sprintf(out + strlen(out), "%s: %s\t%s\t%s\n", myuser,
[667a1b6]914            locations.host ? locations.host : "?",
915            locations.tty ? locations.tty : "?",
916            locations.time ? locations.time : "?");
[7d4fbcd]917    owl_free(myuser);
918  }
[be0a79f]919#endif
[7d4fbcd]920}
[bde7714]921
[31e48a3]922void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
923{
[be0a79f]924#ifdef HAVE_LIBZEPHYR
[bde7714]925  char *line, subsfile[LINE], buff[LINE];
926  FILE *file;
927
928  line=owl_zephyr_makesubline(class, inst, recip);
929
930  if (filename==NULL) {
931    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
932  } else {
933    strcpy(subsfile, filename);
934  }
935
[74037d9]936  /* if the file already exists, check to see if the sub is already there */
[bde7714]937  file=fopen(subsfile, "r");
[74037d9]938  if (file) {
939    while (fgets(buff, LINE, file)!=NULL) {
940      if (!strcasecmp(buff, line)) {
941        owl_function_error("Subscription already present in %s", subsfile);
942        owl_free(line);
[e78397d]943        fclose(file);
[74037d9]944        return;
945      }
[bde7714]946    }
[99dabee]947    fclose(file);
[bde7714]948  }
949
950  /* if we get here then we didn't find it */
951  file=fopen(subsfile, "a");
952  if (!file) {
[ec6ff52]953    owl_function_error("Error opening file %s for writing", subsfile);
[bde7714]954    owl_free(line);
955    return;
956  }
957  fputs(line, file);
958  fclose(file);
959  owl_function_makemsg("Subscription added");
960 
961  owl_free(line);
[be0a79f]962#endif
[bde7714]963}
964
[31e48a3]965void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
966{
[be0a79f]967#ifdef HAVE_LIBZEPHYR
[b2a91b6]968  char *line, *subsfile;
[bde7714]969 
970  line=owl_zephyr_makesubline(class, inst, recip);
[b2a91b6]971  line[strlen(line)-1]='\0';
[bde7714]972
[b2a91b6]973  if (!filename) {
974    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
[bde7714]975  } else {
[b2a91b6]976    subsfile=owl_strdup(filename);
[bde7714]977  }
[b2a91b6]978 
979  owl_util_file_deleteline(subsfile, line, 1);
980  owl_free(subsfile);
[bde7714]981  owl_free(line);
982  owl_function_makemsg("Subscription removed");
[be0a79f]983#endif
[bde7714]984}
985
[b2a91b6]986/* caller must free the return */
[31e48a3]987char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
988{
[bde7714]989  char *out;
990
991  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
992  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
993  return(out);
994}
[31e48a3]995
996
997void owl_zephyr_zlog_in(void)
998{
[be0a79f]999#ifdef HAVE_LIBZEPHYR
[31e48a3]1000  char *exposure, *eset;
1001  int ret;
1002
1003  ZResetAuthentication();
1004   
1005  eset=EXPOSE_REALMVIS;
1006  exposure=ZGetVariable("exposure");
1007  if (exposure==NULL) {
1008    eset=EXPOSE_REALMVIS;
1009  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
1010    eset = EXPOSE_NONE;
1011  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
1012    eset = EXPOSE_OPSTAFF;
1013  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
1014    eset = EXPOSE_REALMVIS;
1015  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
1016    eset = EXPOSE_REALMANN;
1017  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
1018    eset = EXPOSE_NETVIS;
1019  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
1020    eset = EXPOSE_NETANN;
1021  }
1022   
1023  ret=ZSetLocation(eset);
1024  if (ret != ZERR_NONE) {
1025    /*
1026      char buff[LINE];
1027      sprintf(buff, "Error setting location: %s", error_message(ret));
1028      owl_function_makemsg(buff);
1029    */
1030  }
[be0a79f]1031#endif
[31e48a3]1032}
1033
1034void owl_zephyr_zlog_out(void)
1035{
[be0a79f]1036#ifdef HAVE_LIBZEPHYR
[31e48a3]1037  int ret;
1038
1039  ZResetAuthentication();
1040  ret=ZUnsetLocation();
1041  if (ret != ZERR_NONE) {
1042    /*
1043      char buff[LINE];
1044      sprintf(buff, "Error unsetting location: %s", error_message(ret));
1045      owl_function_makemsg(buff);
1046    */
1047  }
[be0a79f]1048#endif
[31e48a3]1049}
1050
[65ad073]1051void owl_zephyr_addbuddy(char *name)
1052{
1053  char *filename;
1054  FILE *file;
1055 
1056  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1057  file=fopen(filename, "a");
1058  owl_free(filename);
1059  if (!file) {
[ec6ff52]1060    owl_function_error("Error opening zephyr buddy file for append");
[65ad073]1061    return;
1062  }
1063  fprintf(file, "%s\n", name);
1064  fclose(file);
1065}
1066
1067void owl_zephyr_delbuddy(char *name)
1068{
1069  char *filename;
1070
1071  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1072  owl_util_file_deleteline(filename, name, 0);
1073  owl_free(filename);
1074}
[9381782]1075
1076/* return auth string */
[09489b89]1077#ifdef HAVE_LIBZEPHYR
[2c14f40]1078char *owl_zephyr_get_authstr(int a)
[9381782]1079{
1080
[2c14f40]1081  if (a == ZAUTH_FAILED) {
[9381782]1082    return ("FAILED");
[2c14f40]1083  } else if (a == ZAUTH_NO) {
[9381782]1084    return ("NO");
[2c14f40]1085  } else if (a == ZAUTH_YES) {
[9381782]1086    return ("YES");
1087  } else {
1088    return ("UNKNOWN");
[2c14f40]1089  }
[9381782]1090}
[09489b89]1091#else
1092char *owl_zephyr_get_authstr(void *n)
1093{
1094  return("");
1095}
1096#endif
[9381782]1097
[2de4f20]1098/* Returns a buffer of subscriptions or an error message.  Caller must
1099 * free the return.
[09489b89]1100 */
1101char *owl_zephyr_getsubs()
1102{
1103#ifdef HAVE_LIBZEPHYR
1104  int ret, num, i, one;
[ddacb9c]1105  int buffsize;
[09489b89]1106  ZSubscription_t sub;
[ddacb9c]1107  char *out;
[09489b89]1108  one=1;
1109
1110  ret=ZRetrieveSubscriptions(0, &num);
1111  if (ret==ZERR_TOOMANYSUBS) {
[c8735aa]1112    return(owl_strdup("Zephyr: too many subscriptions\n"));
[ddacb9c]1113  } else if (ret || (num <= 0)) {
[c8735aa]1114    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
[09489b89]1115  }
1116
[ddacb9c]1117  buffsize = (num + 1) * 50;
1118  out=owl_malloc(buffsize);
[09489b89]1119  strcpy(out, "");
1120  for (i=0; i<num; i++) {
1121    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1122      owl_free(out);
1123      ZFlushSubscriptions();
1124      out=owl_strdup("Error while getting subscriptions\n");
1125      return(out);
1126    } else {
[ddacb9c]1127      int tmpbufflen;
1128      char *tmpbuff;
1129      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
1130      tmpbufflen = strlen(tmpbuff) + 1;
1131      if (tmpbufflen > buffsize) {
1132        char *out2;
1133        buffsize = tmpbufflen * 2;
1134        out2 = owl_realloc(out, buffsize);
1135        if (out2 == NULL) {
1136          owl_free(out);
1137          owl_free(tmpbuff);
1138          ZFlushSubscriptions();
1139          out=owl_strdup("Realloc error while getting subscriptions\n");
1140          return(out);   
1141        }
1142        out = out2;
1143      }
[09489b89]1144      strcpy(out, tmpbuff);
[ddacb9c]1145      owl_free(tmpbuff);
[09489b89]1146    }
1147  }
1148
1149  ZFlushSubscriptions();
1150  return(out);
1151#else
[12c35df]1152  return(owl_strdup("Zephyr not available"));
[09489b89]1153#endif
1154}
1155
1156char *owl_zephyr_get_variable(char *var)
1157{
1158#ifdef HAVE_LIBZEPHYR
1159  return(ZGetVariable(var));
1160#else
1161  return("");
1162#endif
1163}
1164
1165void owl_zephyr_set_locationinfo(char *host, char *val)
1166{
1167#ifdef HAVE_LIBZEPHYR
1168  ZInitLocationInfo(host, val);
1169#endif
1170}
1171 
[e3d9c77]1172/* Strip a local realm fron the zephyr user name.
1173 * The caller must free the return
1174 */
1175char *short_zuser(char *in)
1176{
1177  char *out, *ptr;
1178
1179  out=owl_strdup(in);
1180  ptr=strchr(out, '@');
1181  if (ptr) {
1182    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
1183      *ptr='\0';
1184    }
1185  }
1186  return(out);
1187}
1188
1189/* Append a local realm to the zephyr user name if necessary.
1190 * The caller must free the return.
1191 */
1192char *long_zuser(char *in)
1193{
[1971b59]1194  if (strchr(in, '@')) {
1195    return(owl_strdup(in));
[e3d9c77]1196  }
[1971b59]1197  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
[e3d9c77]1198}
1199
1200/* strip out the instance from a zsender's principal.  Preserves the
1201 * realm if present.  daemon.webzephyr is a special case.  The
1202 * caller must free the return
1203 */
1204char *owl_zephyr_smartstripped_user(char *in)
1205{
1206  char *ptr, *realm, *out;
1207
1208  out=owl_strdup(in);
1209
1210  /* bail immeaditly if we don't have to do any work */
1211  ptr=strchr(in, '.');
1212  if (!strchr(in, '/') && !ptr) {
1213    /* no '/' and no '.' */
1214    return(out);
1215  }
1216  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
1217    /* There's a '.' but it's in the realm */
1218    return(out);
1219  }
1220  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
1221    return(out);
1222  }
1223
1224  /* remove the realm from ptr, but hold on to it */
1225  realm=strchr(out, '@');
1226  if (realm) realm[0]='\0';
1227
1228  /* strip */
1229  ptr=strchr(out, '.');
1230  if (!ptr) ptr=strchr(out, '/');
1231  ptr[0]='\0';
1232
1233  /* reattach the realm if we had one */
1234  if (realm) {
1235    strcat(out, "@");
1236    strcat(out, realm+1);
1237  }
1238
1239  return(out);
1240}
[5a95b69]1241
1242/* read the list of users in 'filename' as a .anyone file, and put the
1243 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1244 * use the default .anyone file in the users home directory.  Returns
1245 * -1 on failure, 0 on success.
1246 */
1247int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
1248{
1249#ifdef HAVE_LIBZEPHYR
1250  char *ourfile, *tmp, buff[LINE];
1251  FILE *f;
1252
1253  if (filename==NULL) {
1254    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1255  } else {
1256    ourfile=owl_strdup(filename);
1257  }
1258 
1259  f=fopen(ourfile, "r");
1260  if (!f) {
1261    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1262    owl_free(ourfile);
1263    return(-1);
1264  }
1265
1266  while (fgets(buff, LINE, f)!=NULL) {
1267    /* ignore comments, blank lines etc. */
1268    if (buff[0]=='#') continue;
1269    if (buff[0]=='\n') continue;
1270    if (buff[0]=='\0') continue;
1271   
1272    /* strip the \n */
1273    buff[strlen(buff)-1]='\0';
1274   
1275    /* ingore from # on */
1276    tmp=strchr(buff, '#');
1277    if (tmp) tmp[0]='\0';
1278   
1279    /* ingore from SPC */
1280    tmp=strchr(buff, ' ');
1281    if (tmp) tmp[0]='\0';
1282   
1283    /* stick on the local realm. */
1284    if (!strchr(buff, '@')) {
1285      strcat(buff, "@");
1286      strcat(buff, ZGetRealm());
1287    }
1288    owl_list_append_element(in, owl_strdup(buff));
1289  }
1290  fclose(f);
1291  owl_free(ourfile);
1292  return(0);
1293#else
1294  return(-1);
1295#endif
1296}
[13a3c1db]1297
1298#ifdef HAVE_LIBZEPHYR
[f36cd97]1299void owl_zephyr_process_events(owl_dispatch *d) {
[13a3c1db]1300  int zpendcount=0;
1301  ZNotice_t notice;
1302  struct sockaddr_in from;
1303  owl_message *m=NULL;
1304
1305  while(owl_zephyr_zpending() && zpendcount < 20) {
1306    if (owl_zephyr_zpending()) {
1307      ZReceiveNotice(&notice, &from);
1308      zpendcount++;
1309
1310      /* is this an ack from a zephyr we sent? */
1311      if (owl_zephyr_notice_is_ack(&notice)) {
1312        owl_zephyr_handle_ack(&notice);
1313        continue;
1314      }
1315
1316      /* if it's a ping and we're not viewing pings then skip it */
1317      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1318        continue;
1319      }
1320
1321      /* create the new message */
1322      m=owl_malloc(sizeof(owl_message));
1323      owl_message_create_from_znotice(m, &notice);
1324
1325      owl_global_messagequeue_addmsg(&g, m);
1326    }
1327  }
1328}
1329
1330#else
[f36cd97]1331void owl_zephyr_process_events(owl_dispatch *d) {
[13a3c1db]1332 
1333}
1334#endif
Note: See TracBrowser for help on using the repository browser.