source: zephyr.c @ dbf94e9

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