source: zephyr.c @ 330c55a

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