source: zephyr.c @ 1b9d3cc

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