source: zephyr.c @ 5376a95

barnowl_perlaimdebianrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 5376a95 was 5376a95, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 13 years ago
First pass at incoming zephyr -> UTF-8 sanitizing. This only operates on incoming data so far. We still need to clean outgoing data -- the plan is to attempt conversion to ISO-8859-1, and use that if it works.
  • Property mode set to 100644
File size: 26.2 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, -1));
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    return owl_validate_or_convert(n->z_message + save, n->z_message_len - save);
380  }
381
382  return(owl_strdup(""));
383}
384#else
385char *owl_zephyr_get_field(void *n, int j)
386{
387  return(owl_strdup(""));
388}
389char *owl_zephyr_get_field_as_utf8(ZNotice_t *n, int j)
390{
391  return owl_zephyr_get_field(n, j);
392}
393#endif
394
395
396#ifdef HAVE_LIBZEPHYR
397int owl_zephyr_get_num_fields(ZNotice_t *n)
398{
399  int i, fields;
400
401  if(n->z_message_len == 0)
402    return 0;
403
404  fields=1;
405  for (i=0; i<n->z_message_len; i++) {
406    if (n->z_message[i]=='\0') fields++;
407  }
408 
409  return(fields);
410}
411#else
412int owl_zephyr_get_num_fields(void *n)
413{
414  return(0);
415}
416#endif
417
418#ifdef HAVE_LIBZEPHYR
419/* return a pointer to the message, place the message length in k
420 * caller must free the return
421 */
422char *owl_zephyr_get_message(ZNotice_t *n, owl_message *m)
423{
424  /* don't let ping messages have a body */
425  if (!strcasecmp(n->z_opcode, "ping")) {
426    return(owl_strdup(""));
427  }
428
429  /* deal with MIT NOC messages */
430  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")) {
431    char *msg, *field3, *field4;
432
433    field3 = owl_zephyr_get_field(n, 3);
434    field4 = owl_zephyr_get_field(n, 4);
435
436    msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4);
437    owl_free(field3);
438    owl_free(field4);
439    if (msg) {
440      return msg;
441    }
442  }
443  /* deal with MIT Discuss messages */
444  else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4")) {
445    char *msg, *field1, *field2, *field3, *field4, *field5;
446   
447    field1 = owl_zephyr_get_field(n, 1);
448    field2 = owl_zephyr_get_field(n, 2);
449    field3 = owl_zephyr_get_field(n, 3);
450    field4 = owl_zephyr_get_field(n, 4);
451    field5 = owl_zephyr_get_field(n, 5);
452   
453    msg = owl_sprintf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s", field1, field2, field3, field5, field4);
454    owl_free(field1);
455    owl_free(field2);
456    owl_free(field3);
457    owl_free(field4);
458    owl_free(field5);
459    if (msg) {
460      return msg;
461    }
462  }
463  /* deal with MIT Moira messages */
464  else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
465    char *msg, *field1;
466   
467    field1 = owl_zephyr_get_field(n, 1);
468   
469    msg = owl_sprintf("MOIRA %s on %s: %s", n->z_class_inst, owl_message_get_hostname(m), field1);
470    owl_free(field1);
471    if (msg) {
472      return msg;
473    }
474  }
475
476  if (owl_zephyr_get_num_fields(n) == 1) {
477    return(owl_zephyr_get_field(n, 1));
478  }
479  else {
480    return(owl_zephyr_get_field(n, 2));
481  }
482}
483#endif
484
485#ifdef HAVE_LIBZEPHYR
486char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
487{
488  /* return a pointer to the zsig if there is one */
489
490  /* message length 0? No zsig */
491  if (n->z_message_len==0) {
492    *k=0;
493    return("");
494  }
495
496  /* If there's only one field, no zsig */
497  if (owl_zephyr_get_num_fields(n) == 1) {
498    *k=0;
499    return("");
500  }
501
502  /* Everything else is field 1 */
503  *k=strlen(n->z_message);
504  return(n->z_message);
505}
506#else
507char *owl_zephyr_get_zsig(void *n, int *k)
508{
509  return("");
510}
511#endif
512
513int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
514{
515#ifdef HAVE_LIBZEPHYR
516  int ret;
517  ZNotice_t notice;
518   
519  memset(&notice, 0, sizeof(notice));
520
521  ZResetAuthentication();
522
523  if (!zsig) zsig="";
524 
525  notice.z_kind=ACKED;
526  notice.z_port=0;
527  notice.z_class=class;
528  notice.z_class_inst=instance;
529  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
530    notice.z_recipient="";
531  } else {
532    notice.z_recipient=recipient;
533  }
534  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
535  notice.z_sender=NULL;
536  if (opcode) notice.z_opcode=opcode;
537
538  notice.z_message_len=strlen(zsig)+1+strlen(message);
539  notice.z_message=owl_malloc(notice.z_message_len+10);
540  strcpy(notice.z_message, zsig);
541  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
542
543  /* ret=ZSendNotice(&notice, ZAUTH); */
544  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
545 
546  /* free then check the return */
547  owl_free(notice.z_message);
548  ZFreeNotice(&notice);
549  if (ret!=ZERR_NONE) {
550    owl_function_error("Error sending zephyr");
551    return(ret);
552  }
553  return(0);
554#else
555  return(0);
556#endif
557}
558
559#ifdef HAVE_LIBZEPHYR
560Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
561{
562  return(ZSendPacket(buf, len, 0));
563}
564#endif
565
566void send_ping(char *to)
567{
568#ifdef HAVE_LIBZEPHYR
569  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
570#endif
571}
572
573#ifdef HAVE_LIBZEPHYR
574void owl_zephyr_handle_ack(ZNotice_t *retnotice)
575{
576  char *tmp;
577 
578  /* if it's an HMACK ignore it */
579  if (retnotice->z_kind == HMACK) return;
580
581  if (retnotice->z_kind == SERVNAK) {
582    owl_function_error("Authorization failure sending zephyr");
583  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
584    owl_function_error("Detected server failure while receiving acknowledgement");
585  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
586    if (!strcasecmp(retnotice->z_opcode, "ping")) {
587      return;
588    } else if (!strcasecmp(retnotice->z_class, "message") &&
589               !strcasecmp(retnotice->z_class_inst, "personal")) {
590      tmp=short_zuser(retnotice->z_recipient);
591      owl_function_makemsg("Message sent to %s.", tmp);
592      free(tmp);
593    } else {
594      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
595    }
596  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
597    if (strcasecmp(retnotice->z_class, "message")) {
598      char buff[1024];
599      owl_function_error("No one subscribed to class class %s", retnotice->z_class);
600      sprintf(buff, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
601      owl_function_adminmsg("", buff);
602    } else {
603      char buff[1024];
604      tmp = short_zuser(retnotice->z_recipient);
605      owl_function_error("%s: Not logged in or subscribing to messages.", tmp);
606      sprintf(buff, "Could not send message to %s: not logged in or subscribing to messages.\n", tmp);
607      owl_function_adminmsg("", buff);
608      owl_log_outgoing_zephyr_error(tmp, buff);
609      owl_free(tmp);
610    }
611  } else {
612    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
613  }
614}
615#else
616void owl_zephyr_handle_ack(void *retnotice)
617{
618}
619#endif
620
621#ifdef HAVE_LIBZEPHYR
622int owl_zephyr_notice_is_ack(ZNotice_t *n)
623{
624  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
625    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
626    return(1);
627  }
628  return(0);
629}
630#else
631int owl_zephyr_notice_is_ack(void *n)
632{
633  return(0);
634}
635#endif
636 
637void owl_zephyr_zaway(owl_message *m)
638{
639#ifdef HAVE_LIBZEPHYR
640  char *tmpbuff, *myuser, *to;
641  owl_message *mout;
642 
643  /* bail if it doesn't look like a message we should reply to.  Some
644   * of this defined by the way zaway(1) works
645   */
646  if (strcasecmp(owl_message_get_class(m), "message")) return;
647  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
648  if (!strcasecmp(owl_message_get_sender(m), "")) return;
649  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
650  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
651  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
652  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
653  if (owl_message_get_attribute_value(m, "isauto")) return;
654
655  if (owl_global_is_smartstrip(&g)) {
656    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
657  } else {
658    to=owl_strdup(owl_message_get_sender(m));
659  }
660
661  send_zephyr("",
662              "Automated reply:",
663              owl_message_get_class(m),
664              owl_message_get_instance(m),
665              to,
666              owl_global_get_zaway_msg(&g));
667
668  myuser=short_zuser(to);
669  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
670    tmpbuff = owl_sprintf("zwrite %s", myuser);
671  } else {
672    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
673  }
674  owl_free(myuser);
675  owl_free(to);
676
677  /* display the message as an admin message in the receive window */
678  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
679  owl_global_messagequeue_addmsg(&g, mout);
680  owl_free(tmpbuff);
681#endif
682}
683
684#ifdef HAVE_LIBZEPHYR
685void owl_zephyr_hackaway_cr(ZNotice_t *n)
686{
687  /* replace \r's with ' '.  Gross-ish */
688  int i;
689
690  for (i=0; i<n->z_message_len; i++) {
691    if (n->z_message[i]=='\r') {
692      n->z_message[i]=' ';
693    }
694  }
695}
696#endif
697
698void owl_zephyr_zlocate(char *user, char *out, int auth)
699{
700#ifdef HAVE_LIBZEPHYR
701  int ret, numlocs;
702  int one = 1;
703  ZLocations_t locations;
704  char *myuser;
705 
706  strcpy(out, "");
707  ZResetAuthentication();
708  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
709  if (ret != ZERR_NONE) {
710    sprintf(out, "Error locating user %s\n", user);
711    return;
712  }
713
714  if (numlocs==0) {
715    myuser=short_zuser(user);
716    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
717    owl_free(myuser);
718    return;
719  }
720   
721  for (;numlocs;numlocs--) {
722    ZGetLocations(&locations,&one);
723    myuser=short_zuser(user);
724    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser,
725            locations.host ? locations.host : "?",
726            locations.tty ? locations.tty : "?",
727            locations.time ? locations.time : "?");
728    owl_free(myuser);
729  }
730#endif
731}
732
733void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
734{
735#ifdef HAVE_LIBZEPHYR
736  char *line, subsfile[LINE], buff[LINE];
737  FILE *file;
738
739  line=owl_zephyr_makesubline(class, inst, recip);
740
741  if (filename==NULL) {
742    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
743  } else {
744    strcpy(subsfile, filename);
745  }
746
747  /* if the file already exists, check to see if the sub is already there */
748  file=fopen(subsfile, "r");
749  if (file) {
750    while (fgets(buff, LINE, file)!=NULL) {
751      if (!strcasecmp(buff, line)) {
752        owl_function_error("Subscription already present in %s", subsfile);
753        owl_free(line);
754        fclose(file);
755        return;
756      }
757    }
758    fclose(file);
759  }
760
761  /* if we get here then we didn't find it */
762  file=fopen(subsfile, "a");
763  if (!file) {
764    owl_function_error("Error opening file %s for writing", subsfile);
765    owl_free(line);
766    return;
767  }
768  fputs(line, file);
769  fclose(file);
770  owl_function_makemsg("Subscription added");
771 
772  owl_free(line);
773#endif
774}
775
776void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
777{
778#ifdef HAVE_LIBZEPHYR
779  char *line, *subsfile;
780 
781  line=owl_zephyr_makesubline(class, inst, recip);
782  line[strlen(line)-1]='\0';
783
784  if (!filename) {
785    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
786  } else {
787    subsfile=owl_strdup(filename);
788  }
789 
790  owl_util_file_deleteline(subsfile, line, 1);
791  owl_free(subsfile);
792  owl_free(line);
793  owl_function_makemsg("Subscription removed");
794#endif
795}
796
797/* caller must free the return */
798char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
799{
800  char *out;
801
802  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
803  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
804  return(out);
805}
806
807
808void owl_zephyr_zlog_in(void)
809{
810#ifdef HAVE_LIBZEPHYR
811  char *exposure, *eset;
812  int ret;
813
814  ZResetAuthentication();
815   
816  eset=EXPOSE_REALMVIS;
817  exposure=ZGetVariable("exposure");
818  if (exposure==NULL) {
819    eset=EXPOSE_REALMVIS;
820  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
821    eset = EXPOSE_NONE;
822  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
823    eset = EXPOSE_OPSTAFF;
824  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
825    eset = EXPOSE_REALMVIS;
826  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
827    eset = EXPOSE_REALMANN;
828  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
829    eset = EXPOSE_NETVIS;
830  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
831    eset = EXPOSE_NETANN;
832  }
833   
834  ret=ZSetLocation(eset);
835  if (ret != ZERR_NONE) {
836    /*
837      char buff[LINE];
838      sprintf(buff, "Error setting location: %s", error_message(ret));
839      owl_function_makemsg(buff);
840    */
841  }
842#endif
843}
844
845void owl_zephyr_zlog_out(void)
846{
847#ifdef HAVE_LIBZEPHYR
848  int ret;
849
850  ZResetAuthentication();
851  ret=ZUnsetLocation();
852  if (ret != ZERR_NONE) {
853    /*
854      char buff[LINE];
855      sprintf(buff, "Error unsetting location: %s", error_message(ret));
856      owl_function_makemsg(buff);
857    */
858  }
859#endif
860}
861
862void owl_zephyr_addbuddy(char *name)
863{
864  char *filename;
865  FILE *file;
866 
867  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
868  file=fopen(filename, "a");
869  owl_free(filename);
870  if (!file) {
871    owl_function_error("Error opening zephyr buddy file for append");
872    return;
873  }
874  fprintf(file, "%s\n", name);
875  fclose(file);
876}
877
878void owl_zephyr_delbuddy(char *name)
879{
880  char *filename;
881
882  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
883  owl_util_file_deleteline(filename, name, 0);
884  owl_free(filename);
885}
886
887/* return auth string */
888#ifdef HAVE_LIBZEPHYR
889char *owl_zephyr_get_authstr(ZNotice_t *n)
890{
891
892  if (!n) return("UNKNOWN");
893
894  if (n->z_auth == ZAUTH_FAILED) {
895    return ("FAILED");
896  } else if (n->z_auth == ZAUTH_NO) {
897    return ("NO");
898  } else if (n->z_auth == ZAUTH_YES) {
899    return ("YES");
900  } else {
901    return ("UNKNOWN");
902  }           
903}
904#else
905char *owl_zephyr_get_authstr(void *n)
906{
907  return("");
908}
909#endif
910
911/* Returns a buffer of subscriptions or an error message.  Caller must
912 * free the return.
913 */
914char *owl_zephyr_getsubs()
915{
916#ifdef HAVE_LIBZEPHYR
917  int ret, num, i, one;
918  ZSubscription_t sub;
919  char *out, *tmpbuff;
920  one=1;
921
922  ret=ZRetrieveSubscriptions(0, &num);
923  if (ret==ZERR_TOOMANYSUBS) {
924    return(owl_strdup("Zephyr: too many subscriptions\n"));
925  } else if (ret) {
926    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
927  }
928
929  out=owl_malloc(num*500);
930  tmpbuff=owl_malloc(num*500);
931  strcpy(out, "");
932  for (i=0; i<num; i++) {
933    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
934      owl_free(out);
935      owl_free(tmpbuff);
936      ZFlushSubscriptions();
937      out=owl_strdup("Error while getting subscriptions\n");
938      return(out);
939    } else {
940      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
941      strcpy(out, tmpbuff);
942    }
943  }
944
945  owl_free(tmpbuff);
946  ZFlushSubscriptions();
947  return(out);
948#else
949  return(owl_strdup("Zephyr not available"));
950#endif
951}
952
953char *owl_zephyr_get_variable(char *var)
954{
955#ifdef HAVE_LIBZEPHYR
956  return(ZGetVariable(var));
957#else
958  return("");
959#endif
960}
961
962void owl_zephyr_set_locationinfo(char *host, char *val)
963{
964#ifdef HAVE_LIBZEPHYR
965  ZInitLocationInfo(host, val);
966#endif
967}
968 
969/* Strip a local realm fron the zephyr user name.
970 * The caller must free the return
971 */
972char *short_zuser(char *in)
973{
974  char *out, *ptr;
975
976  out=owl_strdup(in);
977  ptr=strchr(out, '@');
978  if (ptr) {
979    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
980      *ptr='\0';
981    }
982  }
983  return(out);
984}
985
986/* Append a local realm to the zephyr user name if necessary.
987 * The caller must free the return.
988 */
989char *long_zuser(char *in)
990{
991  if (strchr(in, '@')) {
992    return(owl_strdup(in));
993  }
994  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
995}
996
997/* strip out the instance from a zsender's principal.  Preserves the
998 * realm if present.  daemon.webzephyr is a special case.  The
999 * caller must free the return
1000 */
1001char *owl_zephyr_smartstripped_user(char *in)
1002{
1003  char *ptr, *realm, *out;
1004
1005  out=owl_strdup(in);
1006
1007  /* bail immeaditly if we don't have to do any work */
1008  ptr=strchr(in, '.');
1009  if (!strchr(in, '/') && !ptr) {
1010    /* no '/' and no '.' */
1011    return(out);
1012  }
1013  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
1014    /* There's a '.' but it's in the realm */
1015    return(out);
1016  }
1017  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
1018    return(out);
1019  }
1020
1021  /* remove the realm from ptr, but hold on to it */
1022  realm=strchr(out, '@');
1023  if (realm) realm[0]='\0';
1024
1025  /* strip */
1026  ptr=strchr(out, '.');
1027  if (!ptr) ptr=strchr(out, '/');
1028  ptr[0]='\0';
1029
1030  /* reattach the realm if we had one */
1031  if (realm) {
1032    strcat(out, "@");
1033    strcat(out, realm+1);
1034  }
1035
1036  return(out);
1037}
1038
1039/* read the list of users in 'filename' as a .anyone file, and put the
1040 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1041 * use the default .anyone file in the users home directory.  Returns
1042 * -1 on failure, 0 on success.
1043 */
1044int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
1045{
1046#ifdef HAVE_LIBZEPHYR
1047  char *ourfile, *tmp, buff[LINE];
1048  FILE *f;
1049
1050  if (filename==NULL) {
1051    tmp=owl_global_get_homedir(&g);
1052    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
1053  } else {
1054    ourfile=owl_strdup(filename);
1055  }
1056 
1057  f=fopen(ourfile, "r");
1058  if (!f) {
1059    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1060    owl_free(ourfile);
1061    return(-1);
1062  }
1063
1064  while (fgets(buff, LINE, f)!=NULL) {
1065    /* ignore comments, blank lines etc. */
1066    if (buff[0]=='#') continue;
1067    if (buff[0]=='\n') continue;
1068    if (buff[0]=='\0') continue;
1069   
1070    /* strip the \n */
1071    buff[strlen(buff)-1]='\0';
1072   
1073    /* ingore from # on */
1074    tmp=strchr(buff, '#');
1075    if (tmp) tmp[0]='\0';
1076   
1077    /* ingore from SPC */
1078    tmp=strchr(buff, ' ');
1079    if (tmp) tmp[0]='\0';
1080   
1081    /* stick on the local realm. */
1082    if (!strchr(buff, '@')) {
1083      strcat(buff, "@");
1084      strcat(buff, ZGetRealm());
1085    }
1086    owl_list_append_element(in, owl_strdup(buff));
1087  }
1088  fclose(f);
1089  owl_free(ourfile);
1090  return(0);
1091#else
1092  return(-1);
1093#endif
1094}
1095
1096#ifdef HAVE_LIBZEPHYR
1097void owl_zephyr_process_events() {
1098  int zpendcount=0;
1099  ZNotice_t notice;
1100  struct sockaddr_in from;
1101  owl_message *m=NULL;
1102
1103  while(owl_zephyr_zpending() && zpendcount < 20) {
1104    if (owl_zephyr_zpending()) {
1105      ZReceiveNotice(&notice, &from);
1106      zpendcount++;
1107
1108      /* is this an ack from a zephyr we sent? */
1109      if (owl_zephyr_notice_is_ack(&notice)) {
1110        owl_zephyr_handle_ack(&notice);
1111        continue;
1112      }
1113
1114      /* if it's a ping and we're not viewing pings then skip it */
1115      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1116        continue;
1117      }
1118
1119      /* create the new message */
1120      m=owl_malloc(sizeof(owl_message));
1121      owl_message_create_from_znotice(m, &notice);
1122
1123      owl_global_messagequeue_addmsg(&g, m);
1124    }
1125  }
1126}
1127
1128#else
1129void owl_zephyr_process_events() {
1130 
1131}
1132#endif
Note: See TracBrowser for help on using the repository browser.