source: zephyr.c @ 6e3980e

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