source: zephyr.c @ 33db995

debianrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 33db995 was 21882032, checked in by Nelson Elhage <nelhage@mit.edu>, 13 years ago
Only use zsender when not sending personals. Acked-by: Geoffrey Thomas <geofft@mit.edu>
  • 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  notice.z_sender=NULL;
640  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
641    notice.z_recipient="";
642    if (*owl_global_get_zsender(&g))
643        notice.z_sender=owl_global_get_zsender(&g);
644  } else {
645    notice.z_recipient=recipient;
646  }
647  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
648  if (opcode) notice.z_opcode=opcode;
649
650  notice.z_message_len=strlen(zsig)+1+strlen(message);
651  notice.z_message=owl_malloc(notice.z_message_len+10);
652  strcpy(notice.z_message, zsig);
653  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
654
655  /* ret=ZSendNotice(&notice, ZAUTH); */
656  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
657 
658  /* free then check the return */
659  owl_free(notice.z_message);
660  ZFreeNotice(&notice);
661  if (ret!=ZERR_NONE) {
662    owl_function_error("Error sending zephyr");
663    return(ret);
664  }
665  return(0);
666#else
667  return(0);
668#endif
669}
670
671#ifdef HAVE_LIBZEPHYR
672Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
673{
674  return(ZSendPacket(buf, len, 0));
675}
676#endif
677
678void send_ping(char *to)
679{
680#ifdef HAVE_LIBZEPHYR
681  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
682#endif
683}
684
685#ifdef HAVE_LIBZEPHYR
686void owl_zephyr_handle_ack(ZNotice_t *retnotice)
687{
688  char *tmp;
689 
690  /* if it's an HMACK ignore it */
691  if (retnotice->z_kind == HMACK) return;
692
693  if (retnotice->z_kind == SERVNAK) {
694    owl_function_error("Authorization failure sending zephyr");
695  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
696    owl_function_error("Detected server failure while receiving acknowledgement");
697  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
698    if (!strcasecmp(retnotice->z_opcode, "ping")) {
699      return;
700    } else if (!strcasecmp(retnotice->z_class, "message") &&
701               !strcasecmp(retnotice->z_class_inst, "personal")) {
702      tmp=short_zuser(retnotice->z_recipient);
703      owl_function_makemsg("Message sent to %s.", tmp);
704      free(tmp);
705    } else {
706      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
707    }
708  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
709    #define BUFFLEN 1024
710    if (retnotice->z_recipient == NULL
711        || *retnotice->z_recipient == 0
712        || *retnotice->z_recipient == '@') {
713      char buff[BUFFLEN];
714      owl_function_error("No one subscribed to class %s", retnotice->z_class);
715      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
716      owl_function_adminmsg("", buff);
717    } else {
718      char buff[BUFFLEN];
719      tmp = short_zuser(retnotice->z_recipient);
720      owl_function_error("%s: Not logged in or subscribing.", tmp);
721      if(strcmp(retnotice->z_class, "message")) {
722        snprintf(buff, BUFFLEN,
723                 "Could not send message to %s: "
724                 "not logged in or subscribing to class %s, instance %s.\n", 
725                 tmp,
726                 retnotice->z_class,
727                 retnotice->z_class_inst);
728      } else {
729        snprintf(buff, BUFFLEN,
730                 "Could not send message to %s: "
731                 "not logged in or subscribing to messages.\n",
732                 tmp);
733      }
734      owl_function_adminmsg("", buff);
735      owl_log_outgoing_zephyr_error(tmp, buff);
736      owl_free(tmp);
737    }
738  } else {
739    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
740  }
741}
742#else
743void owl_zephyr_handle_ack(void *retnotice)
744{
745}
746#endif
747
748#ifdef HAVE_LIBZEPHYR
749int owl_zephyr_notice_is_ack(ZNotice_t *n)
750{
751  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
752    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
753    return(1);
754  }
755  return(0);
756}
757#else
758int owl_zephyr_notice_is_ack(void *n)
759{
760  return(0);
761}
762#endif
763 
764void owl_zephyr_zaway(owl_message *m)
765{
766#ifdef HAVE_LIBZEPHYR
767  char *tmpbuff, *myuser, *to;
768  owl_message *mout;
769 
770  /* bail if it doesn't look like a message we should reply to.  Some
771   * of this defined by the way zaway(1) works
772   */
773  if (strcasecmp(owl_message_get_class(m), "message")) return;
774  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
775  if (!strcasecmp(owl_message_get_sender(m), "")) return;
776  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
777  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
778  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
779  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
780  if (owl_message_get_attribute_value(m, "isauto")) return;
781
782  if (owl_global_is_smartstrip(&g)) {
783    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
784  } else {
785    to=owl_strdup(owl_message_get_sender(m));
786  }
787
788  send_zephyr("",
789              "Automated reply:",
790              owl_message_get_class(m),
791              owl_message_get_instance(m),
792              to,
793              owl_global_get_zaway_msg(&g));
794
795  myuser=short_zuser(to);
796  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
797    tmpbuff = owl_sprintf("zwrite %s", myuser);
798  } else {
799    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
800  }
801  owl_free(myuser);
802  owl_free(to);
803
804  /* display the message as an admin message in the receive window */
805  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
806  owl_global_messagequeue_addmsg(&g, mout);
807  owl_free(tmpbuff);
808#endif
809}
810
811#ifdef HAVE_LIBZEPHYR
812void owl_zephyr_hackaway_cr(ZNotice_t *n)
813{
814  /* replace \r's with ' '.  Gross-ish */
815  int i;
816
817  for (i=0; i<n->z_message_len; i++) {
818    if (n->z_message[i]=='\r') {
819      n->z_message[i]=' ';
820    }
821  }
822}
823#endif
824
825void owl_zephyr_zlocate(char *user, char *out, int auth)
826{
827#ifdef HAVE_LIBZEPHYR
828  int ret, numlocs;
829  int one = 1;
830  ZLocations_t locations;
831  char *myuser;
832 
833  strcpy(out, "");
834  ZResetAuthentication();
835  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
836  if (ret != ZERR_NONE) {
837    sprintf(out, "Error locating user %s\n", user);
838    return;
839  }
840
841  if (numlocs==0) {
842    myuser=short_zuser(user);
843    sprintf(out, "%s: Hidden or not logged in\n", myuser);
844    owl_free(myuser);
845    return;
846  }
847   
848  for (;numlocs;numlocs--) {
849    ZGetLocations(&locations,&one);
850    myuser=short_zuser(user);
851    sprintf(out + strlen(out), "%s: %s\t%s\t%s\n", myuser,
852            locations.host ? locations.host : "?",
853            locations.tty ? locations.tty : "?",
854            locations.time ? locations.time : "?");
855    owl_free(myuser);
856  }
857#endif
858}
859
860void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
861{
862#ifdef HAVE_LIBZEPHYR
863  char *line, subsfile[LINE], buff[LINE];
864  FILE *file;
865
866  line=owl_zephyr_makesubline(class, inst, recip);
867
868  if (filename==NULL) {
869    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
870  } else {
871    strcpy(subsfile, filename);
872  }
873
874  /* if the file already exists, check to see if the sub is already there */
875  file=fopen(subsfile, "r");
876  if (file) {
877    while (fgets(buff, LINE, file)!=NULL) {
878      if (!strcasecmp(buff, line)) {
879        owl_function_error("Subscription already present in %s", subsfile);
880        owl_free(line);
881        fclose(file);
882        return;
883      }
884    }
885    fclose(file);
886  }
887
888  /* if we get here then we didn't find it */
889  file=fopen(subsfile, "a");
890  if (!file) {
891    owl_function_error("Error opening file %s for writing", subsfile);
892    owl_free(line);
893    return;
894  }
895  fputs(line, file);
896  fclose(file);
897  owl_function_makemsg("Subscription added");
898 
899  owl_free(line);
900#endif
901}
902
903void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
904{
905#ifdef HAVE_LIBZEPHYR
906  char *line, *subsfile;
907 
908  line=owl_zephyr_makesubline(class, inst, recip);
909  line[strlen(line)-1]='\0';
910
911  if (!filename) {
912    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
913  } else {
914    subsfile=owl_strdup(filename);
915  }
916 
917  owl_util_file_deleteline(subsfile, line, 1);
918  owl_free(subsfile);
919  owl_free(line);
920  owl_function_makemsg("Subscription removed");
921#endif
922}
923
924/* caller must free the return */
925char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
926{
927  char *out;
928
929  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
930  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
931  return(out);
932}
933
934
935void owl_zephyr_zlog_in(void)
936{
937#ifdef HAVE_LIBZEPHYR
938  char *exposure, *eset;
939  int ret;
940
941  ZResetAuthentication();
942   
943  eset=EXPOSE_REALMVIS;
944  exposure=ZGetVariable("exposure");
945  if (exposure==NULL) {
946    eset=EXPOSE_REALMVIS;
947  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
948    eset = EXPOSE_NONE;
949  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
950    eset = EXPOSE_OPSTAFF;
951  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
952    eset = EXPOSE_REALMVIS;
953  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
954    eset = EXPOSE_REALMANN;
955  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
956    eset = EXPOSE_NETVIS;
957  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
958    eset = EXPOSE_NETANN;
959  }
960   
961  ret=ZSetLocation(eset);
962  if (ret != ZERR_NONE) {
963    /*
964      char buff[LINE];
965      sprintf(buff, "Error setting location: %s", error_message(ret));
966      owl_function_makemsg(buff);
967    */
968  }
969#endif
970}
971
972void owl_zephyr_zlog_out(void)
973{
974#ifdef HAVE_LIBZEPHYR
975  int ret;
976
977  ZResetAuthentication();
978  ret=ZUnsetLocation();
979  if (ret != ZERR_NONE) {
980    /*
981      char buff[LINE];
982      sprintf(buff, "Error unsetting location: %s", error_message(ret));
983      owl_function_makemsg(buff);
984    */
985  }
986#endif
987}
988
989void owl_zephyr_addbuddy(char *name)
990{
991  char *filename;
992  FILE *file;
993 
994  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
995  file=fopen(filename, "a");
996  owl_free(filename);
997  if (!file) {
998    owl_function_error("Error opening zephyr buddy file for append");
999    return;
1000  }
1001  fprintf(file, "%s\n", name);
1002  fclose(file);
1003}
1004
1005void owl_zephyr_delbuddy(char *name)
1006{
1007  char *filename;
1008
1009  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1010  owl_util_file_deleteline(filename, name, 0);
1011  owl_free(filename);
1012}
1013
1014/* return auth string */
1015#ifdef HAVE_LIBZEPHYR
1016char *owl_zephyr_get_authstr(ZNotice_t *n)
1017{
1018
1019  if (!n) return("UNKNOWN");
1020
1021  if (n->z_auth == ZAUTH_FAILED) {
1022    return ("FAILED");
1023  } else if (n->z_auth == ZAUTH_NO) {
1024    return ("NO");
1025  } else if (n->z_auth == ZAUTH_YES) {
1026    return ("YES");
1027  } else {
1028    return ("UNKNOWN");
1029  }           
1030}
1031#else
1032char *owl_zephyr_get_authstr(void *n)
1033{
1034  return("");
1035}
1036#endif
1037
1038/* Returns a buffer of subscriptions or an error message.  Caller must
1039 * free the return.
1040 */
1041char *owl_zephyr_getsubs()
1042{
1043#ifdef HAVE_LIBZEPHYR
1044  int ret, num, i, one;
1045  int buffsize;
1046  ZSubscription_t sub;
1047  char *out;
1048  one=1;
1049
1050  ret=ZRetrieveSubscriptions(0, &num);
1051  if (ret==ZERR_TOOMANYSUBS) {
1052    return(owl_strdup("Zephyr: too many subscriptions\n"));
1053  } else if (ret || (num <= 0)) {
1054    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
1055  }
1056
1057  buffsize = (num + 1) * 50;
1058  out=owl_malloc(buffsize);
1059  strcpy(out, "");
1060  for (i=0; i<num; i++) {
1061    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1062      owl_free(out);
1063      ZFlushSubscriptions();
1064      out=owl_strdup("Error while getting subscriptions\n");
1065      return(out);
1066    } else {
1067      int tmpbufflen;
1068      char *tmpbuff;
1069      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
1070      tmpbufflen = strlen(tmpbuff) + 1;
1071      if (tmpbufflen > buffsize) {
1072        char *out2;
1073        buffsize = tmpbufflen * 2;
1074        out2 = owl_realloc(out, buffsize);
1075        if (out2 == NULL) {
1076          owl_free(out);
1077          owl_free(tmpbuff);
1078          ZFlushSubscriptions();
1079          out=owl_strdup("Realloc error while getting subscriptions\n");
1080          return(out);   
1081        }
1082        out = out2;
1083      }
1084      strcpy(out, tmpbuff);
1085      owl_free(tmpbuff);
1086    }
1087  }
1088
1089  ZFlushSubscriptions();
1090  return(out);
1091#else
1092  return(owl_strdup("Zephyr not available"));
1093#endif
1094}
1095
1096char *owl_zephyr_get_variable(char *var)
1097{
1098#ifdef HAVE_LIBZEPHYR
1099  return(ZGetVariable(var));
1100#else
1101  return("");
1102#endif
1103}
1104
1105void owl_zephyr_set_locationinfo(char *host, char *val)
1106{
1107#ifdef HAVE_LIBZEPHYR
1108  ZInitLocationInfo(host, val);
1109#endif
1110}
1111 
1112/* Strip a local realm fron the zephyr user name.
1113 * The caller must free the return
1114 */
1115char *short_zuser(char *in)
1116{
1117  char *out, *ptr;
1118
1119  out=owl_strdup(in);
1120  ptr=strchr(out, '@');
1121  if (ptr) {
1122    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
1123      *ptr='\0';
1124    }
1125  }
1126  return(out);
1127}
1128
1129/* Append a local realm to the zephyr user name if necessary.
1130 * The caller must free the return.
1131 */
1132char *long_zuser(char *in)
1133{
1134  if (strchr(in, '@')) {
1135    return(owl_strdup(in));
1136  }
1137  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
1138}
1139
1140/* strip out the instance from a zsender's principal.  Preserves the
1141 * realm if present.  daemon.webzephyr is a special case.  The
1142 * caller must free the return
1143 */
1144char *owl_zephyr_smartstripped_user(char *in)
1145{
1146  char *ptr, *realm, *out;
1147
1148  out=owl_strdup(in);
1149
1150  /* bail immeaditly if we don't have to do any work */
1151  ptr=strchr(in, '.');
1152  if (!strchr(in, '/') && !ptr) {
1153    /* no '/' and no '.' */
1154    return(out);
1155  }
1156  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
1157    /* There's a '.' but it's in the realm */
1158    return(out);
1159  }
1160  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
1161    return(out);
1162  }
1163
1164  /* remove the realm from ptr, but hold on to it */
1165  realm=strchr(out, '@');
1166  if (realm) realm[0]='\0';
1167
1168  /* strip */
1169  ptr=strchr(out, '.');
1170  if (!ptr) ptr=strchr(out, '/');
1171  ptr[0]='\0';
1172
1173  /* reattach the realm if we had one */
1174  if (realm) {
1175    strcat(out, "@");
1176    strcat(out, realm+1);
1177  }
1178
1179  return(out);
1180}
1181
1182/* read the list of users in 'filename' as a .anyone file, and put the
1183 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1184 * use the default .anyone file in the users home directory.  Returns
1185 * -1 on failure, 0 on success.
1186 */
1187int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
1188{
1189#ifdef HAVE_LIBZEPHYR
1190  char *ourfile, *tmp, buff[LINE];
1191  FILE *f;
1192
1193  if (filename==NULL) {
1194    tmp=owl_global_get_homedir(&g);
1195    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1196  } else {
1197    ourfile=owl_strdup(filename);
1198  }
1199 
1200  f=fopen(ourfile, "r");
1201  if (!f) {
1202    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1203    owl_free(ourfile);
1204    return(-1);
1205  }
1206
1207  while (fgets(buff, LINE, f)!=NULL) {
1208    /* ignore comments, blank lines etc. */
1209    if (buff[0]=='#') continue;
1210    if (buff[0]=='\n') continue;
1211    if (buff[0]=='\0') continue;
1212   
1213    /* strip the \n */
1214    buff[strlen(buff)-1]='\0';
1215   
1216    /* ingore from # on */
1217    tmp=strchr(buff, '#');
1218    if (tmp) tmp[0]='\0';
1219   
1220    /* ingore from SPC */
1221    tmp=strchr(buff, ' ');
1222    if (tmp) tmp[0]='\0';
1223   
1224    /* stick on the local realm. */
1225    if (!strchr(buff, '@')) {
1226      strcat(buff, "@");
1227      strcat(buff, ZGetRealm());
1228    }
1229    owl_list_append_element(in, owl_strdup(buff));
1230  }
1231  fclose(f);
1232  owl_free(ourfile);
1233  return(0);
1234#else
1235  return(-1);
1236#endif
1237}
1238
1239#ifdef HAVE_LIBZEPHYR
1240void owl_zephyr_process_events(owl_dispatch *d) {
1241  int zpendcount=0;
1242  ZNotice_t notice;
1243  struct sockaddr_in from;
1244  owl_message *m=NULL;
1245
1246  while(owl_zephyr_zpending() && zpendcount < 20) {
1247    if (owl_zephyr_zpending()) {
1248      ZReceiveNotice(&notice, &from);
1249      zpendcount++;
1250
1251      /* is this an ack from a zephyr we sent? */
1252      if (owl_zephyr_notice_is_ack(&notice)) {
1253        owl_zephyr_handle_ack(&notice);
1254        continue;
1255      }
1256
1257      /* if it's a ping and we're not viewing pings then skip it */
1258      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1259        continue;
1260      }
1261
1262      /* create the new message */
1263      m=owl_malloc(sizeof(owl_message));
1264      owl_message_create_from_znotice(m, &notice);
1265
1266      owl_global_messagequeue_addmsg(&g, m);
1267    }
1268  }
1269}
1270
1271#else
1272void owl_zephyr_process_events(owl_dispatch *d) {
1273 
1274}
1275#endif
Note: See TracBrowser for help on using the repository browser.