source: zephyr.c @ 79d7d98

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