source: zephyr.c @ 52a0f14

debianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 52a0f14 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
Line 
1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <sys/stat.h>
6#include <string.h>
7#include "owl.h"
8
9static const char fileIdent[] = "$Id$";
10
11#ifdef HAVE_LIBZEPHYR
12Code_t ZResetAuthentication();
13#endif
14
15#define HM_SVC_FALLBACK         htons((unsigned short) 2104)
16
17#ifdef HAVE_LIBZEPHYR
18void owl_zephyr_initialize()
19{
20  int ret;
21  struct servent *sp;
22  struct sockaddr_in sin;
23  ZNotice_t req;
24  Code_t code;
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  }
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;
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);
75}
76
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}
133#endif
134
135
136int owl_zephyr_shutdown()
137{
138#ifdef HAVE_LIBZEPHYR
139  if(owl_global_is_havezephyr(&g)) {
140    unsuball();
141    ZClosePort();
142  }
143#endif
144  return(0);
145}
146
147int owl_zephyr_zpending()
148{
149#ifdef HAVE_LIBZEPHYR
150  if(owl_global_is_havezephyr(&g))
151    return(ZPending());
152  else
153    return 0;
154#else
155  return(0);
156#endif
157}
158
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
177#ifdef HAVE_LIBZEPHYR
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  }
193
194  return ret;
195}
196#endif
197
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.
205 */
206int owl_zephyr_loadsubs(char *filename, int error_on_nofile)
207{
208#ifdef HAVE_LIBZEPHYR
209  FILE *file;
210  char *tmp, *start;
211  char buffer[1024], subsfile[1024];
212  ZSubscription_t *subs;
213  int subSize = 1024;
214  int count, ret;
215  struct stat statbuff;
216
217  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
218  if (filename==NULL) {
219    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
220  } else {
221    strcpy(subsfile, filename);
222  }
223
224  ret=stat(subsfile, &statbuff);
225  if (ret) {
226    if (error_on_nofile==1) return(-1);
227    return(0);
228  }
229
230  ZResetAuthentication();
231  count=0;
232  file=fopen(subsfile, "r");
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;
241    }
242   
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;
258      }
259    }
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   
269    /* if it started with '-' then add it to the global punt list, and
270     * remove it from the list of subs. */
271    if (buffer[0]=='-') {
272      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
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++;
279    }
280  }
281  fclose(file);
282
283  ret=owl_zephyr_loadsubs_helper(subs, count);
284  owl_free(subs);
285  return(ret);
286#else
287  return(0);
288#endif
289}
290
291int owl_zephyr_loaddefaultsubs()
292{
293#ifdef HAVE_LIBZEPHYR
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);
301#else
302  return(0);
303#endif
304}
305
306int owl_zephyr_loadloginsubs(char *filename)
307{
308#ifdef HAVE_LIBZEPHYR
309  FILE *file;
310  ZSubscription_t subs[3001];
311  char subsfile[1024], buffer[1024];
312  int count, ret, i;
313  struct stat statbuff;
314
315  if (filename==NULL) {
316    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
317  } else {
318    strcpy(subsfile, filename);
319  }
320 
321  ret=stat(subsfile, &statbuff);
322  if (ret) return(0);
323
324  ret=0;
325
326  ZResetAuthentication();
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 {
342        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
343      }
344
345      count++;
346    }
347    fclose(file);
348  } else {
349    count=0;
350    ret=-1;
351  }
352
353  /* sub with defaults */
354  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
355    owl_function_error("Error subscribing to zephyr notifications.");
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);
365#else
366  return(0);
367#endif
368}
369
370void unsuball()
371{
372#if HAVE_LIBZEPHYR
373  int ret;
374
375  ZResetAuthentication();
376  ret=ZCancelSubscriptions(0);
377  if (ret != ZERR_NONE) {
378    com_err("owl",ret,"while unsubscribing");
379  }
380#endif
381}
382
383int owl_zephyr_sub(char *class, char *inst, char *recip)
384{
385#ifdef HAVE_LIBZEPHYR
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
392  ZResetAuthentication();
393  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
394    owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip);
395    return(-2);
396  }
397  return(0);
398#else
399  return(0);
400#endif
401}
402
403
404int owl_zephyr_unsub(char *class, char *inst, char *recip)
405{
406#ifdef HAVE_LIBZEPHYR
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
413  ZResetAuthentication();
414  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
415    owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip);
416    return(-2);
417  }
418  return(0);
419#else
420  return(0);
421#endif
422}
423
424/* return a pointer to the data in the Jth field, (NULL terminated by
425 * definition).  Caller must free the return.
426 */
427#ifdef HAVE_LIBZEPHYR
428char *owl_zephyr_get_field(ZNotice_t *n, int j)
429{
430  int i, count, save;
431  char *out;
432
433  /* If there's no message here, just run along now */
434  if (n->z_message_len == 0)
435    return(owl_strdup(""));
436
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 */
443        return(owl_strdup(n->z_message+save));
444      } else {
445        save=i+1;
446      }
447    }
448  }
449  /* catch the last field, which might not be null terminated */
450  if (count==j-1) {
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);
455  }
456
457  return(owl_strdup(""));
458}
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 */
474        return(owl_validate_or_convert(n->z_message + save));
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) {
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;
489  }
490
491  return(owl_strdup(""));
492}
493#else
494char *owl_zephyr_get_field(void *n, int j)
495{
496  return(owl_strdup(""));
497}
498char *owl_zephyr_get_field_as_utf8(void *n, int j)
499{
500  return owl_zephyr_get_field(n, j);
501}
502#endif
503
504
505#ifdef HAVE_LIBZEPHYR
506int owl_zephyr_get_num_fields(ZNotice_t *n)
507{
508  int i, fields;
509
510  if(n->z_message_len == 0)
511    return 0;
512
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}
520#else
521int owl_zephyr_get_num_fields(void *n)
522{
523  return(0);
524}
525#endif
526
527#ifdef HAVE_LIBZEPHYR
528/* return a pointer to the message, place the message length in k
529 * caller must free the return
530 */
531char *owl_zephyr_get_message(ZNotice_t *n, owl_message *m)
532{
533  /* don't let ping messages have a body */
534  if (!strcasecmp(n->z_opcode, "ping")) {
535    return(owl_strdup(""));
536  }
537
538  /* deal with MIT NOC messages */
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;
541
542    field3 = owl_zephyr_get_field(n, 3);
543    field4 = owl_zephyr_get_field(n, 4);
544
545    msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4);
546    owl_free(field3);
547    owl_free(field4);
548    if (msg) {
549      return msg;
550    }
551  }
552  /* deal with MIT Discuss messages */
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")) {
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  }
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  }
585
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  }
592}
593#endif
594
595#ifdef HAVE_LIBZEPHYR
596char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
597{
598  /* return a pointer to the zsig if there is one */
599
600  /* message length 0? No zsig */
601  if (n->z_message_len==0) {
602    *k=0;
603    return("");
604  }
605
606  /* If there's only one field, no zsig */
607  if (owl_zephyr_get_num_fields(n) == 1) {
608    *k=0;
609    return("");
610  }
611
612  /* Everything else is field 1 */
613  *k=strlen(n->z_message);
614  return(n->z_message);
615}
616#else
617char *owl_zephyr_get_zsig(void *n, int *k)
618{
619  return("");
620}
621#endif
622
623int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
624{
625#ifdef HAVE_LIBZEPHYR
626  int ret;
627  ZNotice_t notice;
628   
629  memset(&notice, 0, sizeof(notice));
630
631  ZResetAuthentication();
632
633  if (!zsig) zsig="";
634 
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";
645  if (*owl_global_get_zsender(&g))
646      notice.z_sender=owl_global_get_zsender(&g);
647  else
648      notice.z_sender=NULL;
649  if (opcode) notice.z_opcode=opcode;
650
651  notice.z_message_len=strlen(zsig)+1+strlen(message);
652  notice.z_message=owl_malloc(notice.z_message_len+10);
653  strcpy(notice.z_message, zsig);
654  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
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) {
663    owl_function_error("Error sending zephyr");
664    return(ret);
665  }
666  return(0);
667#else
668  return(0);
669#endif
670}
671
672#ifdef HAVE_LIBZEPHYR
673Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
674{
675  return(ZSendPacket(buf, len, 0));
676}
677#endif
678
679void send_ping(char *to)
680{
681#ifdef HAVE_LIBZEPHYR
682  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
683#endif
684}
685
686#ifdef HAVE_LIBZEPHYR
687void owl_zephyr_handle_ack(ZNotice_t *retnotice)
688{
689  char *tmp;
690 
691  /* if it's an HMACK ignore it */
692  if (retnotice->z_kind == HMACK) return;
693
694  if (retnotice->z_kind == SERVNAK) {
695    owl_function_error("Authorization failure sending zephyr");
696  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
697    owl_function_error("Detected server failure while receiving acknowledgement");
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")) {
703      tmp=short_zuser(retnotice->z_recipient);
704      owl_function_makemsg("Message sent to %s.", tmp);
705      free(tmp);
706    } else {
707      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
708    }
709  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
710    #define BUFFLEN 1024
711    if (retnotice->z_recipient == NULL
712        || *retnotice->z_recipient == 0
713        || *retnotice->z_recipient == '@') {
714      char buff[BUFFLEN];
715      owl_function_error("No one subscribed to class %s", retnotice->z_class);
716      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
717      owl_function_adminmsg("", buff);
718    } else {
719      char buff[BUFFLEN];
720      tmp = short_zuser(retnotice->z_recipient);
721      owl_function_error("%s: Not logged in or subscribing.", tmp);
722      if(strcmp(retnotice->z_class, "message")) {
723        snprintf(buff, BUFFLEN,
724                 "Could not send message to %s: "
725                 "not logged in or subscribing to class %s, instance %s.\n", 
726                 tmp,
727                 retnotice->z_class,
728                 retnotice->z_class_inst);
729      } else {
730        snprintf(buff, BUFFLEN,
731                 "Could not send message to %s: "
732                 "not logged in or subscribing to messages.\n",
733                 tmp);
734      }
735      owl_function_adminmsg("", buff);
736      owl_log_outgoing_zephyr_error(tmp, buff);
737      owl_free(tmp);
738    }
739  } else {
740    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
741  }
742}
743#else
744void owl_zephyr_handle_ack(void *retnotice)
745{
746}
747#endif
748
749#ifdef HAVE_LIBZEPHYR
750int owl_zephyr_notice_is_ack(ZNotice_t *n)
751{
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}
758#else
759int owl_zephyr_notice_is_ack(void *n)
760{
761  return(0);
762}
763#endif
764 
765void owl_zephyr_zaway(owl_message *m)
766{
767#ifdef HAVE_LIBZEPHYR
768  char *tmpbuff, *myuser, *to;
769  owl_message *mout;
770 
771  /* bail if it doesn't look like a message we should reply to.  Some
772   * of this defined by the way zaway(1) works
773   */
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;
779  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
780  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
781  if (owl_message_get_attribute_value(m, "isauto")) return;
782
783  if (owl_global_is_smartstrip(&g)) {
784    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
785  } else {
786    to=owl_strdup(owl_message_get_sender(m));
787  }
788
789  send_zephyr("",
790              "Automated reply:",
791              owl_message_get_class(m),
792              owl_message_get_instance(m),
793              to,
794              owl_global_get_zaway_msg(&g));
795
796  myuser=short_zuser(to);
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);
803  owl_free(to);
804
805  /* display the message as an admin message in the receive window */
806  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
807  owl_global_messagequeue_addmsg(&g, mout);
808  owl_free(tmpbuff);
809#endif
810}
811
812#ifdef HAVE_LIBZEPHYR
813void owl_zephyr_hackaway_cr(ZNotice_t *n)
814{
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}
824#endif
825
826void owl_zephyr_zlocate(char *user, char *out, int auth)
827{
828#ifdef HAVE_LIBZEPHYR
829  int ret, numlocs;
830  int one = 1;
831  ZLocations_t locations;
832  char *myuser;
833 
834  strcpy(out, "");
835  ZResetAuthentication();
836  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
837  if (ret != ZERR_NONE) {
838    sprintf(out, "Error locating user %s\n", user);
839    return;
840  }
841
842  if (numlocs==0) {
843    myuser=short_zuser(user);
844    sprintf(out, "%s: Hidden or not logged in\n", myuser);
845    owl_free(myuser);
846    return;
847  }
848   
849  for (;numlocs;numlocs--) {
850    ZGetLocations(&locations,&one);
851    myuser=short_zuser(user);
852    sprintf(out + strlen(out), "%s: %s\t%s\t%s\n", myuser,
853            locations.host ? locations.host : "?",
854            locations.tty ? locations.tty : "?",
855            locations.time ? locations.time : "?");
856    owl_free(myuser);
857  }
858#endif
859}
860
861void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
862{
863#ifdef HAVE_LIBZEPHYR
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
875  /* if the file already exists, check to see if the sub is already there */
876  file=fopen(subsfile, "r");
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);
882        fclose(file);
883        return;
884      }
885    }
886    fclose(file);
887  }
888
889  /* if we get here then we didn't find it */
890  file=fopen(subsfile, "a");
891  if (!file) {
892    owl_function_error("Error opening file %s for writing", subsfile);
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);
901#endif
902}
903
904void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
905{
906#ifdef HAVE_LIBZEPHYR
907  char *line, *subsfile;
908 
909  line=owl_zephyr_makesubline(class, inst, recip);
910  line[strlen(line)-1]='\0';
911
912  if (!filename) {
913    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
914  } else {
915    subsfile=owl_strdup(filename);
916  }
917 
918  owl_util_file_deleteline(subsfile, line, 1);
919  owl_free(subsfile);
920  owl_free(line);
921  owl_function_makemsg("Subscription removed");
922#endif
923}
924
925/* caller must free the return */
926char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
927{
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}
934
935
936void owl_zephyr_zlog_in(void)
937{
938#ifdef HAVE_LIBZEPHYR
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  }
970#endif
971}
972
973void owl_zephyr_zlog_out(void)
974{
975#ifdef HAVE_LIBZEPHYR
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  }
987#endif
988}
989
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) {
999    owl_function_error("Error opening zephyr buddy file for append");
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}
1014
1015/* return auth string */
1016#ifdef HAVE_LIBZEPHYR
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}
1032#else
1033char *owl_zephyr_get_authstr(void *n)
1034{
1035  return("");
1036}
1037#endif
1038
1039/* Returns a buffer of subscriptions or an error message.  Caller must
1040 * free the return.
1041 */
1042char *owl_zephyr_getsubs()
1043{
1044#ifdef HAVE_LIBZEPHYR
1045  int ret, num, i, one;
1046  int buffsize;
1047  ZSubscription_t sub;
1048  char *out;
1049  one=1;
1050
1051  ret=ZRetrieveSubscriptions(0, &num);
1052  if (ret==ZERR_TOOMANYSUBS) {
1053    return(owl_strdup("Zephyr: too many subscriptions\n"));
1054  } else if (ret || (num <= 0)) {
1055    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
1056  }
1057
1058  buffsize = (num + 1) * 50;
1059  out=owl_malloc(buffsize);
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 {
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      }
1085      strcpy(out, tmpbuff);
1086      owl_free(tmpbuff);
1087    }
1088  }
1089
1090  ZFlushSubscriptions();
1091  return(out);
1092#else
1093  return(owl_strdup("Zephyr not available"));
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 
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{
1135  if (strchr(in, '@')) {
1136    return(owl_strdup(in));
1137  }
1138  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
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}
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}
1239
1240#ifdef HAVE_LIBZEPHYR
1241void owl_zephyr_process_events(owl_dispatch *d) {
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
1273void owl_zephyr_process_events(owl_dispatch *d) {
1274 
1275}
1276#endif
Note: See TracBrowser for help on using the repository browser.