source: zephyr.c @ 619d864

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