source: zephyr.c @ 18a54ee

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