source: zephyr.c @ 9381782

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 9381782 was 9381782, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
$owl::auth now works
  • Property mode set to 100644
File size: 16.6 KB
Line 
1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <string.h>
6#include <zephyr/zephyr.h>
7#include <com_err.h>
8#include "owl.h"
9
10static const char fileIdent[] = "$Id$";
11
12Code_t ZResetAuthentication();
13
14int owl_zephyr_initialize()
15{
16  int ret;
17 
18  if ((ret = ZInitialize()) != ZERR_NONE) {
19    com_err("owl",ret,"while initializing");
20    return(1);
21  }
22  if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
23    com_err("owl",ret,"while opening port");
24    return(1);
25  }
26  return(0);
27}
28
29int owl_zephyr_zpending()
30{
31#ifdef HAVE_LIBZEPHYR
32  return(ZPending());
33#else
34  return(0);
35#endif
36}
37
38int owl_zephyr_loadsubs(char *filename)
39{
40#ifdef HAVE_LIBZEPHYR
41  /* return 0  on success
42   *        -1 on file error
43   *        -2 on subscription error
44   */
45  FILE *file;
46  char *tmp, *start;
47  char buffer[1024], subsfile[1024];
48  ZSubscription_t subs[3001];
49  int count, ret, i;
50
51  ret=0;
52 
53  if (filename==NULL) {
54    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
55  } else {
56    strcpy(subsfile, filename);
57  }
58
59  ZResetAuthentication();
60  /* need to redo this to do chunks, not just bail after 3000 */
61  count=0;
62  file=fopen(subsfile, "r");
63  if (file) {
64    while ( fgets(buffer, 1024, file)!=NULL ) {
65      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
66
67      if (buffer[0]=='-') {
68        start=buffer+1;
69      } else {
70        start=buffer;
71      }
72     
73      if (count >= 3000) break; /* also tell the user */
74
75      /* add it to the list of subs */
76      if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue;
77      subs[count].zsub_class=owl_strdup(tmp);
78      if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue;
79      subs[count].zsub_classinst=owl_strdup(tmp);
80      if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue;
81      subs[count].zsub_recipient=owl_strdup(tmp);
82
83      /* if it started with '-' then add it to the global punt list */
84      if (buffer[0]=='-') {
85        owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
86      }
87     
88      count++;
89    }
90    fclose(file);
91  } else {
92    count=0;
93    ret=-1;
94  }
95
96  /* sub with defaults */
97  if (ZSubscribeTo(subs,count,0) != ZERR_NONE) {
98    fprintf(stderr, "Error subbing\n");
99    ret=-2;
100  }
101
102  /* free stuff */
103  for (i=0; i<count; i++) {
104    owl_free(subs[i].zsub_class);
105    owl_free(subs[i].zsub_classinst);
106    owl_free(subs[i].zsub_recipient);
107  }
108
109  return(ret);
110#else
111  return(0);
112#endif
113}
114
115int owl_zephyr_loadloginsubs(char *filename)
116{
117#ifdef HAVE_LIBZEPHYR
118  FILE *file;
119  ZSubscription_t subs[3001];
120  char subsfile[1024], buffer[1024];
121  int count, ret, i;
122
123  ret=0;
124
125  if (filename==NULL) {
126    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
127  } else {
128    strcpy(subsfile, filename);
129  }
130
131  ZResetAuthentication();
132  /* need to redo this to do chunks, not just bag out after 3000 */
133  count=0;
134  file=fopen(subsfile, "r");
135  if (file) {
136    while ( fgets(buffer, 1024, file)!=NULL ) {
137      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
138     
139      if (count >= 3000) break; /* also tell the user */
140
141      buffer[strlen(buffer)-1]='\0';
142      subs[count].zsub_class="login";
143      subs[count].zsub_recipient="*";
144      if (strchr(buffer, '@')) {
145        subs[count].zsub_classinst=owl_strdup(buffer);
146      } else {
147        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
148      }
149
150      count++;
151    }
152    fclose(file);
153  } else {
154    count=0;
155    ret=-1;
156  }
157
158  /* sub with defaults */
159  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
160    fprintf(stderr, "Error subbing\n");
161    ret=-2;
162  }
163
164  /* free stuff */
165  for (i=0; i<count; i++) {
166    owl_free(subs[i].zsub_classinst);
167  }
168
169  return(ret);
170#else
171  return(0);
172#endif
173}
174
175void unsuball()
176{
177#if HAVE_LIBZEPHYR
178  int ret;
179
180  ZResetAuthentication();
181  ret=ZCancelSubscriptions(0);
182  if (ret != ZERR_NONE) {
183    com_err("owl",ret,"while unsubscribing");
184  }
185#endif
186}
187
188int owl_zephyr_sub(char *class, char *inst, char *recip)
189{
190#ifdef HAVE_LIBZEPHYR
191  ZSubscription_t subs[5];
192  int ret;
193
194  subs[0].zsub_class=class;
195  subs[0].zsub_classinst=inst;
196  subs[0].zsub_recipient=recip;
197
198  ZResetAuthentication();
199  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
200    fprintf(stderr, "Error subbing\n");
201    ret=-2;
202  }
203  return(0);
204#else
205  return(0);
206#endif
207}
208
209
210int owl_zephyr_unsub(char *class, char *inst, char *recip)
211{
212#ifdef HAVE_LIBZEPHYR
213  ZSubscription_t subs[5];
214  int ret;
215
216  subs[0].zsub_class=class;
217  subs[0].zsub_classinst=inst;
218  subs[0].zsub_recipient=recip;
219
220  ZResetAuthentication();
221  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
222    fprintf(stderr, "Error unsubbing\n");
223    ret=-2;
224  }
225  return(0);
226#else
227  return(0);
228#endif
229}
230
231#ifdef HAVE_LIBZEPHYR
232char *owl_zephyr_get_field(ZNotice_t *n, int j, int *k)
233{
234  /* return a pointer to the Jth field, place the length in k.  If the
235     field doesn't exist return an emtpy string */
236  int i, count, save;
237
238  count=save=0;
239  for (i=0; i<n->z_message_len; i++) {
240    if (n->z_message[i]=='\0') {
241      count++;
242      if (count==j) {
243        /* just found the end of the field we're looking for */
244        *k=i-save;
245        return(n->z_message+save);
246      } else {
247        save=i+1;
248      }
249    }
250  }
251  /* catch the last field */
252  if (count==j-1) {
253    *k=n->z_message_len-save;
254    return(n->z_message+save);
255  }
256 
257  *k=0;
258  return("");
259}
260#endif
261
262#ifdef HAVE_LIBZEPHYR
263int owl_zephyr_get_num_fields(ZNotice_t *n)
264{
265  int i, fields;
266
267  fields=1;
268  for (i=0; i<n->z_message_len; i++) {
269    if (n->z_message[i]=='\0') fields++;
270  }
271 
272  return(fields);
273}
274#endif
275
276#ifdef HAVE_LIBZEPHYR
277char *owl_zephyr_get_message(ZNotice_t *n, int *k)
278{
279  /* return a pointer to the message, place the message length in k */
280  if (!strcasecmp(n->z_opcode, "ping")) {
281    *k=0;
282    return("");
283  }
284
285  return(owl_zephyr_get_field(n, 2, k));
286}
287#endif
288
289#ifdef HAVE_LIBZEPHYR
290char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
291{
292  /* return a pointer to the zsig if there is one */
293
294  if (n->z_message_len==0) {
295    *k=0;
296    return("");
297  }
298  *k=strlen(n->z_message);
299  return(n->z_message);
300}
301#endif
302
303int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
304{
305#ifdef HAVE_LIBZEPHYR
306  int ret;
307  ZNotice_t notice;
308   
309  memset(&notice, 0, sizeof(notice));
310
311  ZResetAuthentication();
312 
313  notice.z_kind=ACKED;
314  notice.z_port=0;
315  notice.z_class=class;
316  notice.z_class_inst=instance;
317  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
318    notice.z_recipient="";
319  } else {
320    notice.z_recipient=recipient;
321  }
322  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
323  notice.z_sender=NULL;
324  if (opcode) notice.z_opcode=opcode;
325
326  notice.z_message_len=strlen(zsig)+1+strlen(message);
327  notice.z_message=owl_malloc(notice.z_message_len+10);
328  strcpy(notice.z_message, zsig);
329  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
330
331  /* ret=ZSendNotice(&notice, ZAUTH); */
332  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
333 
334  /* free then check the return */
335  owl_free(notice.z_message);
336  ZFreeNotice(&notice);
337  if (ret!=ZERR_NONE) {
338    owl_function_makemsg("Error sending zephyr");
339    return(ret);
340  }
341  return(0);
342#else
343  return(0);
344#endif
345}
346
347#ifdef HAVE_LIBZEPHYR
348Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
349{
350  return(ZSendPacket(buf, len, 0));
351}
352#endif
353
354void send_ping(char *to)
355{
356#ifdef HAVE_LIBZEPHYR
357  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
358#endif
359}
360
361#ifdef HAVE_LIBZEPHYR
362void owl_zephyr_handle_ack(ZNotice_t *retnotice)
363{
364  char *tmp;
365 
366  /* if it's an HMACK ignore it */
367  if (retnotice->z_kind == HMACK) return;
368
369  if (retnotice->z_kind == SERVNAK) {
370    owl_function_makemsg("Authorization failure sending zephyr");
371  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
372    owl_function_makemsg("Detected server failure while receiving acknowledgement");
373  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
374    if (!strcasecmp(retnotice->z_opcode, "ping")) {
375      return;
376    } else if (!strcasecmp(retnotice->z_class, "message") &&
377               !strcasecmp(retnotice->z_class_inst, "personal")) {
378      tmp=short_zuser(retnotice->z_recipient);
379      owl_function_makemsg("Message sent to %s.", tmp);
380      free(tmp);
381    } else {
382      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
383    }
384  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
385    if (strcasecmp(retnotice->z_class, "message")) {
386      owl_function_makemsg("Not logged in or not subscribing to class %s, instance %s",
387                           retnotice->z_class, retnotice->z_class_inst);
388    } else {
389      tmp = short_zuser(retnotice->z_recipient);
390      owl_function_makemsg("%s: Not logged in or subscribing to messages.", 
391                           tmp);
392      owl_free(tmp);
393    }
394  } else {
395    owl_function_makemsg("Internal error on ack (%s)", retnotice->z_message);
396  }
397}
398#endif
399
400#ifdef HAVE_LIBZEPHYR
401int owl_zephyr_notice_is_ack(ZNotice_t *n)
402{
403  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
404    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
405    return(1);
406  }
407  return(0);
408}
409#endif
410 
411void owl_zephyr_zaway(owl_message *m)
412{
413#ifdef HAVE_LIBZEPHYR
414  char *tmpbuff, *myuser, *to;
415 
416  /* bail if it doesn't look like a message we should reply to.  Some
417     of this defined by the way zaway(1) works */
418  if (strcasecmp(owl_message_get_class(m), "message")) return;
419  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
420  if (!strcasecmp(owl_message_get_sender(m), "")) return;
421  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
422  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
423  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
424  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
425
426  if (owl_global_is_smartstrip(&g)) {
427    to=owl_util_smartstripped_user(owl_message_get_sender(m));
428  } else {
429    to=owl_strdup(owl_message_get_sender(m));
430  }
431
432  send_zephyr("",
433              "Automated reply:",
434              owl_message_get_class(m),
435              owl_message_get_instance(m),
436              to,
437              owl_global_get_zaway_msg(&g));
438
439  myuser=short_zuser(to);
440  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
441    tmpbuff = owl_sprintf("zwrite %s", myuser);
442  } else {
443    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
444  }
445  owl_free(myuser);
446  owl_free(to);
447
448  /* display the message as an admin message in the receive window */
449  owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
450  owl_free(tmpbuff);
451#endif
452}
453
454#ifdef HAVE_LIBZEPHYR
455void owl_zephyr_hackaway_cr(ZNotice_t *n)
456{
457  /* replace \r's with ' '.  Gross-ish */
458  int i;
459
460  for (i=0; i<n->z_message_len; i++) {
461    if (n->z_message[i]=='\r') {
462      n->z_message[i]=' ';
463    }
464  }
465}
466#endif
467
468void owl_zephyr_zlocate(char *user, char *out, int auth)
469{
470#ifdef HAVE_LIBZEPHYR
471  int ret, numlocs;
472  int one = 1;
473  ZLocations_t locations;
474  char *myuser;
475 
476  strcpy(out, "");
477  ZResetAuthentication();
478  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
479  if (ret != ZERR_NONE) {
480    owl_function_makemsg("Error locating user");
481  }
482
483  if (numlocs==0) {
484    myuser=short_zuser(user);
485    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
486    owl_free(myuser);
487    return;
488  }
489   
490  for (;numlocs;numlocs--) {
491    ZGetLocations(&locations,&one);
492    myuser=short_zuser(user);
493    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
494    owl_free(myuser);
495  }
496#endif
497}
498
499void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
500{
501#ifdef HAVE_LIBZEPHYR
502  char *line, subsfile[LINE], buff[LINE];
503  FILE *file;
504
505  line=owl_zephyr_makesubline(class, inst, recip);
506
507  if (filename==NULL) {
508    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
509  } else {
510    strcpy(subsfile, filename);
511  }
512
513  /* first check if it exists already */
514  file=fopen(subsfile, "r");
515  if (!file) {
516    owl_function_makemsg("Error opening file %s", subsfile);
517    owl_free(line);
518    return;
519  }
520  while (fgets(buff, LINE, file)!=NULL) {
521    if (!strcasecmp(buff, line)) {
522      owl_function_makemsg("Subscription already present in %s", subsfile);
523      owl_free(line);
524      return;
525    }
526  }
527
528  /* if we get here then we didn't find it */
529  fclose(file);
530  file=fopen(subsfile, "a");
531  if (!file) {
532    owl_function_makemsg("Error opening file %s for writing", subsfile);
533    owl_free(line);
534    return;
535  }
536  fputs(line, file);
537  fclose(file);
538  owl_function_makemsg("Subscription added");
539 
540  owl_free(line);
541#endif
542}
543
544void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
545{
546#ifdef HAVE_LIBZEPHYR
547  char *line, subsfile[LINE], buff[LINE], *text;
548  char backupfilename[LINE];
549  FILE *file, *backupfile;
550  int size;
551 
552  line=owl_zephyr_makesubline(class, inst, recip);
553
554  /* open the subsfile for reading */
555  if (filename==NULL) {
556    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
557  } else {
558    strcpy(subsfile, filename);
559  }
560  file=fopen(subsfile, "r");
561  if (!file) {
562    owl_function_makemsg("Error opening file %s", subsfile);
563    owl_free(line);
564    return;
565  }
566
567  /* open the backup file for writing */
568  sprintf(backupfilename, "%s.backup", subsfile);
569  backupfile=fopen(backupfilename, "w");
570  if (!backupfile) {
571    owl_function_makemsg("Error opening file %s for writing", backupfilename);
572    owl_free(line);
573    return;
574  }
575
576  /* we'll read the entire file into memory, minus the line we don't want and
577   * and at the same time create a backup file */
578  text=owl_malloc(LINE);
579  strcpy(text, "");
580  size=LINE;
581  while (fgets(buff, LINE, file)!=NULL) {
582    /* if we don't match the line, add to text */
583    if (strcasecmp(buff, line)) {
584      size+=LINE;
585      text=owl_realloc(text, size);
586      strcat(text, buff);
587    }
588
589    /* write to backupfile */
590    fputs(buff, backupfile);
591  }
592  fclose(backupfile);
593  fclose(file);
594
595  /* now open the original subs file for writing and write out the
596   * subs */
597  file=fopen(subsfile, "w");
598  if (!file) {
599    owl_function_makemsg("WARNING: Error opening %s to rewrite subscriptions.  Use %s to restore", subsfile, backupfilename);
600    owl_function_beep();
601    owl_free(line);
602    return;
603  }
604
605  fputs(text, file);
606  fclose(file);
607  owl_free(line);
608
609  owl_function_makemsg("Subscription removed");
610#endif
611}
612
613char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
614{
615  /* caller must free the return */
616  char *out;
617
618  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
619  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
620  return(out);
621}
622
623
624void owl_zephyr_zlog_in(void)
625{
626#ifdef HAVE_LIBZEPHYR
627  char *exposure, *eset;
628  int ret;
629
630  ZResetAuthentication();
631   
632  eset=EXPOSE_REALMVIS;
633  exposure=ZGetVariable("exposure");
634  if (exposure==NULL) {
635    eset=EXPOSE_REALMVIS;
636  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
637    eset = EXPOSE_NONE;
638  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
639    eset = EXPOSE_OPSTAFF;
640  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
641    eset = EXPOSE_REALMVIS;
642  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
643    eset = EXPOSE_REALMANN;
644  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
645    eset = EXPOSE_NETVIS;
646  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
647    eset = EXPOSE_NETANN;
648  }
649   
650  ret=ZSetLocation(eset);
651  if (ret != ZERR_NONE) {
652    /*
653      char buff[LINE];
654      sprintf(buff, "Error setting location: %s", error_message(ret));
655      owl_function_makemsg(buff);
656    */
657  }
658#endif
659}
660
661void owl_zephyr_zlog_out(void)
662{
663#ifdef HAVE_LIBZEPHYR
664  int ret;
665
666  ZResetAuthentication();
667  ret=ZUnsetLocation();
668  if (ret != ZERR_NONE) {
669    /*
670      char buff[LINE];
671      sprintf(buff, "Error unsetting location: %s", error_message(ret));
672      owl_function_makemsg(buff);
673    */
674  }
675#endif
676}
677
678void owl_zephyr_addbuddy(char *name)
679{
680  char *filename;
681  FILE *file;
682 
683  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
684  file=fopen(filename, "a");
685  owl_free(filename);
686  if (!file) {
687    owl_function_makemsg("Error opening zephyr buddy file for append");
688    return;
689  }
690  fprintf(file, "%s\n", name);
691  fclose(file);
692}
693
694void owl_zephyr_delbuddy(char *name)
695{
696  char *filename;
697
698  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
699  owl_util_file_deleteline(filename, name, 0);
700  owl_free(filename);
701}
702
703/* return auth string */
704char *owl_zephyr_get_authstr(ZNotice_t *n)
705{
706
707  if (!n) return("UNKNOWN");
708
709  if (n->z_auth == ZAUTH_FAILED) {
710    return ("FAILED");
711  } else if (n->z_auth == ZAUTH_NO) {
712    return ("NO");
713  } else if (n->z_auth == ZAUTH_YES) {
714    return ("YES");
715  } else {
716    return ("UNKNOWN");
717  }           
718}
719
720
Note: See TracBrowser for help on using the repository browser.