source: zephyr.c @ 1e550b2

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