source: zephyr.c @ 7451af9

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7451af9 was 7451af9, checked in by Nelson Elhage <nelhage@mit.edu>, 12 years ago
Add <message,*,%me%> to default Barnowl subs. Also adds support for default Barnowl subs and fixes the return value of owl_zephyr_loadsubs.
  • Property mode set to 100644
File size: 31.7 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)
722{
723#ifdef HAVE_LIBZEPHYR
724  send_zephyr("PING", "", "MESSAGE", "PERSONAL", 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      if(strcmp(retnotice->z_class, "message")) {
775        snprintf(buff, BUFFLEN,
776                 "Could not send message to %s: "
777                 "not logged in or subscribing to class %s, instance %s.\n", 
778                 tmp,
779                 retnotice->z_class,
780                 retnotice->z_class_inst);
781      } else {
782        snprintf(buff, BUFFLEN,
783                 "Could not send message to %s: "
784                 "not logged in or subscribing to messages.\n",
785                 tmp);
786      }
787      owl_function_adminmsg("", buff);
788      owl_log_outgoing_zephyr_error(tmp, buff);
789      owl_free(tmp);
790    }
791  } else {
792    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
793  }
794}
795#else
796void owl_zephyr_handle_ack(void *retnotice)
797{
798}
799#endif
800
801#ifdef HAVE_LIBZEPHYR
802int owl_zephyr_notice_is_ack(ZNotice_t *n)
803{
804  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
805    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
806    return(1);
807  }
808  return(0);
809}
810#else
811int owl_zephyr_notice_is_ack(void *n)
812{
813  return(0);
814}
815#endif
816 
817void owl_zephyr_zaway(owl_message *m)
818{
819#ifdef HAVE_LIBZEPHYR
820  char *tmpbuff, *myuser, *to;
821  owl_message *mout;
822 
823  /* bail if it doesn't look like a message we should reply to.  Some
824   * of this defined by the way zaway(1) works
825   */
826  if (strcasecmp(owl_message_get_class(m), "message")) return;
827  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
828  if (!strcasecmp(owl_message_get_sender(m), "")) return;
829  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
830  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
831  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
832  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
833  if (owl_message_get_attribute_value(m, "isauto")) return;
834
835  if (owl_global_is_smartstrip(&g)) {
836    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
837  } else {
838    to=owl_strdup(owl_message_get_sender(m));
839  }
840
841  send_zephyr("",
842              "Automated reply:",
843              owl_message_get_class(m),
844              owl_message_get_instance(m),
845              to,
846              owl_global_get_zaway_msg(&g));
847
848  myuser=short_zuser(to);
849  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
850    tmpbuff = owl_sprintf("zwrite %s", myuser);
851  } else {
852    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
853  }
854  owl_free(myuser);
855  owl_free(to);
856
857  /* display the message as an admin message in the receive window */
858  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
859  owl_global_messagequeue_addmsg(&g, mout);
860  owl_free(tmpbuff);
861#endif
862}
863
864#ifdef HAVE_LIBZEPHYR
865void owl_zephyr_hackaway_cr(ZNotice_t *n)
866{
867  /* replace \r's with ' '.  Gross-ish */
868  int i;
869
870  for (i=0; i<n->z_message_len; i++) {
871    if (n->z_message[i]=='\r') {
872      n->z_message[i]=' ';
873    }
874  }
875}
876#endif
877
878void owl_zephyr_zlocate(char *user, char *out, int auth)
879{
880#ifdef HAVE_LIBZEPHYR
881  int ret, numlocs;
882  int one = 1;
883  ZLocations_t locations;
884  char *myuser;
885 
886  strcpy(out, "");
887  ZResetAuthentication();
888  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
889  if (ret != ZERR_NONE) {
890    sprintf(out, "Error locating user %s\n", user);
891    return;
892  }
893
894  if (numlocs==0) {
895    myuser=short_zuser(user);
896    sprintf(out, "%s: Hidden or not logged in\n", myuser);
897    owl_free(myuser);
898    return;
899  }
900   
901  for (;numlocs;numlocs--) {
902    ZGetLocations(&locations,&one);
903    myuser=short_zuser(user);
904    sprintf(out + strlen(out), "%s: %s\t%s\t%s\n", myuser,
905            locations.host ? locations.host : "?",
906            locations.tty ? locations.tty : "?",
907            locations.time ? locations.time : "?");
908    owl_free(myuser);
909  }
910#endif
911}
912
913void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
914{
915#ifdef HAVE_LIBZEPHYR
916  char *line, subsfile[LINE], buff[LINE];
917  FILE *file;
918
919  line=owl_zephyr_makesubline(class, inst, recip);
920
921  if (filename==NULL) {
922    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
923  } else {
924    strcpy(subsfile, filename);
925  }
926
927  /* if the file already exists, check to see if the sub is already there */
928  file=fopen(subsfile, "r");
929  if (file) {
930    while (fgets(buff, LINE, file)!=NULL) {
931      if (!strcasecmp(buff, line)) {
932        owl_function_error("Subscription already present in %s", subsfile);
933        owl_free(line);
934        fclose(file);
935        return;
936      }
937    }
938    fclose(file);
939  }
940
941  /* if we get here then we didn't find it */
942  file=fopen(subsfile, "a");
943  if (!file) {
944    owl_function_error("Error opening file %s for writing", subsfile);
945    owl_free(line);
946    return;
947  }
948  fputs(line, file);
949  fclose(file);
950  owl_function_makemsg("Subscription added");
951 
952  owl_free(line);
953#endif
954}
955
956void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
957{
958#ifdef HAVE_LIBZEPHYR
959  char *line, *subsfile;
960 
961  line=owl_zephyr_makesubline(class, inst, recip);
962  line[strlen(line)-1]='\0';
963
964  if (!filename) {
965    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
966  } else {
967    subsfile=owl_strdup(filename);
968  }
969 
970  owl_util_file_deleteline(subsfile, line, 1);
971  owl_free(subsfile);
972  owl_free(line);
973  owl_function_makemsg("Subscription removed");
974#endif
975}
976
977/* caller must free the return */
978char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
979{
980  char *out;
981
982  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
983  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
984  return(out);
985}
986
987
988void owl_zephyr_zlog_in(void)
989{
990#ifdef HAVE_LIBZEPHYR
991  char *exposure, *eset;
992  int ret;
993
994  ZResetAuthentication();
995   
996  eset=EXPOSE_REALMVIS;
997  exposure=ZGetVariable("exposure");
998  if (exposure==NULL) {
999    eset=EXPOSE_REALMVIS;
1000  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
1001    eset = EXPOSE_NONE;
1002  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
1003    eset = EXPOSE_OPSTAFF;
1004  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
1005    eset = EXPOSE_REALMVIS;
1006  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
1007    eset = EXPOSE_REALMANN;
1008  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
1009    eset = EXPOSE_NETVIS;
1010  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
1011    eset = EXPOSE_NETANN;
1012  }
1013   
1014  ret=ZSetLocation(eset);
1015  if (ret != ZERR_NONE) {
1016    /*
1017      char buff[LINE];
1018      sprintf(buff, "Error setting location: %s", error_message(ret));
1019      owl_function_makemsg(buff);
1020    */
1021  }
1022#endif
1023}
1024
1025void owl_zephyr_zlog_out(void)
1026{
1027#ifdef HAVE_LIBZEPHYR
1028  int ret;
1029
1030  ZResetAuthentication();
1031  ret=ZUnsetLocation();
1032  if (ret != ZERR_NONE) {
1033    /*
1034      char buff[LINE];
1035      sprintf(buff, "Error unsetting location: %s", error_message(ret));
1036      owl_function_makemsg(buff);
1037    */
1038  }
1039#endif
1040}
1041
1042void owl_zephyr_addbuddy(char *name)
1043{
1044  char *filename;
1045  FILE *file;
1046 
1047  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1048  file=fopen(filename, "a");
1049  owl_free(filename);
1050  if (!file) {
1051    owl_function_error("Error opening zephyr buddy file for append");
1052    return;
1053  }
1054  fprintf(file, "%s\n", name);
1055  fclose(file);
1056}
1057
1058void owl_zephyr_delbuddy(char *name)
1059{
1060  char *filename;
1061
1062  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1063  owl_util_file_deleteline(filename, name, 0);
1064  owl_free(filename);
1065}
1066
1067/* return auth string */
1068#ifdef HAVE_LIBZEPHYR
1069char *owl_zephyr_get_authstr(ZNotice_t *n)
1070{
1071
1072  if (!n) return("UNKNOWN");
1073
1074  if (n->z_auth == ZAUTH_FAILED) {
1075    return ("FAILED");
1076  } else if (n->z_auth == ZAUTH_NO) {
1077    return ("NO");
1078  } else if (n->z_auth == ZAUTH_YES) {
1079    return ("YES");
1080  } else {
1081    return ("UNKNOWN");
1082  }           
1083}
1084#else
1085char *owl_zephyr_get_authstr(void *n)
1086{
1087  return("");
1088}
1089#endif
1090
1091/* Returns a buffer of subscriptions or an error message.  Caller must
1092 * free the return.
1093 */
1094char *owl_zephyr_getsubs()
1095{
1096#ifdef HAVE_LIBZEPHYR
1097  int ret, num, i, one;
1098  int buffsize;
1099  ZSubscription_t sub;
1100  char *out;
1101  one=1;
1102
1103  ret=ZRetrieveSubscriptions(0, &num);
1104  if (ret==ZERR_TOOMANYSUBS) {
1105    return(owl_strdup("Zephyr: too many subscriptions\n"));
1106  } else if (ret || (num <= 0)) {
1107    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
1108  }
1109
1110  buffsize = (num + 1) * 50;
1111  out=owl_malloc(buffsize);
1112  strcpy(out, "");
1113  for (i=0; i<num; i++) {
1114    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1115      owl_free(out);
1116      ZFlushSubscriptions();
1117      out=owl_strdup("Error while getting subscriptions\n");
1118      return(out);
1119    } else {
1120      int tmpbufflen;
1121      char *tmpbuff;
1122      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
1123      tmpbufflen = strlen(tmpbuff) + 1;
1124      if (tmpbufflen > buffsize) {
1125        char *out2;
1126        buffsize = tmpbufflen * 2;
1127        out2 = owl_realloc(out, buffsize);
1128        if (out2 == NULL) {
1129          owl_free(out);
1130          owl_free(tmpbuff);
1131          ZFlushSubscriptions();
1132          out=owl_strdup("Realloc error while getting subscriptions\n");
1133          return(out);   
1134        }
1135        out = out2;
1136      }
1137      strcpy(out, tmpbuff);
1138      owl_free(tmpbuff);
1139    }
1140  }
1141
1142  ZFlushSubscriptions();
1143  return(out);
1144#else
1145  return(owl_strdup("Zephyr not available"));
1146#endif
1147}
1148
1149char *owl_zephyr_get_variable(char *var)
1150{
1151#ifdef HAVE_LIBZEPHYR
1152  return(ZGetVariable(var));
1153#else
1154  return("");
1155#endif
1156}
1157
1158void owl_zephyr_set_locationinfo(char *host, char *val)
1159{
1160#ifdef HAVE_LIBZEPHYR
1161  ZInitLocationInfo(host, val);
1162#endif
1163}
1164 
1165/* Strip a local realm fron the zephyr user name.
1166 * The caller must free the return
1167 */
1168char *short_zuser(char *in)
1169{
1170  char *out, *ptr;
1171
1172  out=owl_strdup(in);
1173  ptr=strchr(out, '@');
1174  if (ptr) {
1175    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
1176      *ptr='\0';
1177    }
1178  }
1179  return(out);
1180}
1181
1182/* Append a local realm to the zephyr user name if necessary.
1183 * The caller must free the return.
1184 */
1185char *long_zuser(char *in)
1186{
1187  if (strchr(in, '@')) {
1188    return(owl_strdup(in));
1189  }
1190  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
1191}
1192
1193/* strip out the instance from a zsender's principal.  Preserves the
1194 * realm if present.  daemon.webzephyr is a special case.  The
1195 * caller must free the return
1196 */
1197char *owl_zephyr_smartstripped_user(char *in)
1198{
1199  char *ptr, *realm, *out;
1200
1201  out=owl_strdup(in);
1202
1203  /* bail immeaditly if we don't have to do any work */
1204  ptr=strchr(in, '.');
1205  if (!strchr(in, '/') && !ptr) {
1206    /* no '/' and no '.' */
1207    return(out);
1208  }
1209  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
1210    /* There's a '.' but it's in the realm */
1211    return(out);
1212  }
1213  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
1214    return(out);
1215  }
1216
1217  /* remove the realm from ptr, but hold on to it */
1218  realm=strchr(out, '@');
1219  if (realm) realm[0]='\0';
1220
1221  /* strip */
1222  ptr=strchr(out, '.');
1223  if (!ptr) ptr=strchr(out, '/');
1224  ptr[0]='\0';
1225
1226  /* reattach the realm if we had one */
1227  if (realm) {
1228    strcat(out, "@");
1229    strcat(out, realm+1);
1230  }
1231
1232  return(out);
1233}
1234
1235/* read the list of users in 'filename' as a .anyone file, and put the
1236 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1237 * use the default .anyone file in the users home directory.  Returns
1238 * -1 on failure, 0 on success.
1239 */
1240int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
1241{
1242#ifdef HAVE_LIBZEPHYR
1243  char *ourfile, *tmp, buff[LINE];
1244  FILE *f;
1245
1246  if (filename==NULL) {
1247    tmp=owl_global_get_homedir(&g);
1248    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1249  } else {
1250    ourfile=owl_strdup(filename);
1251  }
1252 
1253  f=fopen(ourfile, "r");
1254  if (!f) {
1255    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1256    owl_free(ourfile);
1257    return(-1);
1258  }
1259
1260  while (fgets(buff, LINE, f)!=NULL) {
1261    /* ignore comments, blank lines etc. */
1262    if (buff[0]=='#') continue;
1263    if (buff[0]=='\n') continue;
1264    if (buff[0]=='\0') continue;
1265   
1266    /* strip the \n */
1267    buff[strlen(buff)-1]='\0';
1268   
1269    /* ingore from # on */
1270    tmp=strchr(buff, '#');
1271    if (tmp) tmp[0]='\0';
1272   
1273    /* ingore from SPC */
1274    tmp=strchr(buff, ' ');
1275    if (tmp) tmp[0]='\0';
1276   
1277    /* stick on the local realm. */
1278    if (!strchr(buff, '@')) {
1279      strcat(buff, "@");
1280      strcat(buff, ZGetRealm());
1281    }
1282    owl_list_append_element(in, owl_strdup(buff));
1283  }
1284  fclose(f);
1285  owl_free(ourfile);
1286  return(0);
1287#else
1288  return(-1);
1289#endif
1290}
1291
1292#ifdef HAVE_LIBZEPHYR
1293void owl_zephyr_process_events(owl_dispatch *d) {
1294  int zpendcount=0;
1295  ZNotice_t notice;
1296  struct sockaddr_in from;
1297  owl_message *m=NULL;
1298
1299  while(owl_zephyr_zpending() && zpendcount < 20) {
1300    if (owl_zephyr_zpending()) {
1301      ZReceiveNotice(&notice, &from);
1302      zpendcount++;
1303
1304      /* is this an ack from a zephyr we sent? */
1305      if (owl_zephyr_notice_is_ack(&notice)) {
1306        owl_zephyr_handle_ack(&notice);
1307        continue;
1308      }
1309
1310      /* if it's a ping and we're not viewing pings then skip it */
1311      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1312        continue;
1313      }
1314
1315      /* create the new message */
1316      m=owl_malloc(sizeof(owl_message));
1317      owl_message_create_from_znotice(m, &notice);
1318
1319      owl_global_messagequeue_addmsg(&g, m);
1320    }
1321  }
1322}
1323
1324#else
1325void owl_zephyr_process_events(owl_dispatch *d) {
1326 
1327}
1328#endif
Note: See TracBrowser for help on using the repository browser.