source: zephyr.c @ 6af4068

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