source: zephyr.c @ 680ed23

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