source: zephyr.c @ 36486be

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