source: zephyr.c @ 5008e51

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