source: zephyr.c @ 2de4f20

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2de4f20 was 2de4f20, checked in by James M. Kretchmar <kretch@mit.edu>, 17 years ago
Fixed bug in pseudo logouts Reformatted some comments
  • Property mode set to 100644
File size: 20.7 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
[2de4f20]68/* return 0  on success
69 *        -1 on file error
70 *        -2 on subscription error
71 */
[31e48a3]72int owl_zephyr_loadsubs(char *filename)
73{
[be0a79f]74#ifdef HAVE_LIBZEPHYR
[7d4fbcd]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
[b0430a6]259/* return a pointer to the data in the Jth field, (NULL terminated by
260 * definition).  Caller must free the return.
261 */
[be0a79f]262#ifdef HAVE_LIBZEPHYR
[b0430a6]263char *owl_zephyr_get_field(ZNotice_t *n, int j)
[31e48a3]264{
[7d4fbcd]265  int i, count, save;
[b0430a6]266  char *out;
[7d4fbcd]267
268  count=save=0;
269  for (i=0; i<n->z_message_len; i++) {
270    if (n->z_message[i]=='\0') {
271      count++;
272      if (count==j) {
273        /* just found the end of the field we're looking for */
[b0430a6]274        return(owl_strdup(n->z_message+save));
[7d4fbcd]275      } else {
276        save=i+1;
277      }
278    }
279  }
[b0430a6]280  /* catch the last field, which might not be null terminated */
[7d4fbcd]281  if (count==j-1) {
[b0430a6]282    out=owl_malloc(n->z_message_len-save+5);
283    memcpy(out, n->z_message+save, n->z_message_len-save);
284    out[n->z_message_len-save]='\0';
285    return(out);
[7d4fbcd]286  }
[b0430a6]287
288  return(owl_strdup(""));
[7d4fbcd]289}
[09489b89]290#else
291char *owl_zephyr_get_field(void *n, int j, int *k)
292{
[b0430a6]293  return(owl_strdup(""));
[09489b89]294}
[be0a79f]295#endif
[7d4fbcd]296
[b0430a6]297
[be0a79f]298#ifdef HAVE_LIBZEPHYR
[31e48a3]299int owl_zephyr_get_num_fields(ZNotice_t *n)
300{
[7d4fbcd]301  int i, fields;
302
303  fields=1;
304  for (i=0; i<n->z_message_len; i++) {
305    if (n->z_message[i]=='\0') fields++;
306  }
307 
308  return(fields);
309}
[09489b89]310#else
311int owl_zephyr_get_num_fields(void *n)
312{
313  return(0);
314}
[be0a79f]315#endif
[7d4fbcd]316
[be0a79f]317#ifdef HAVE_LIBZEPHYR
[b0430a6]318/* return a pointer to the message, place the message length in k */
319char *owl_zephyr_get_message(ZNotice_t *n)
[31e48a3]320{
[7d4fbcd]321  if (!strcasecmp(n->z_opcode, "ping")) {
322    return("");
323  }
324
[b0430a6]325  return(owl_zephyr_get_field(n, 2));
[7d4fbcd]326}
[be0a79f]327#endif
[7d4fbcd]328
[be0a79f]329#ifdef HAVE_LIBZEPHYR
[31e48a3]330char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
331{
[7d4fbcd]332  /* return a pointer to the zsig if there is one */
333
334  if (n->z_message_len==0) {
335    *k=0;
336    return("");
337  }
338  *k=strlen(n->z_message);
339  return(n->z_message);
340}
[09489b89]341#else
342char *owl_zephyr_get_zsig(void *n, int *k)
343{
344  return("");
345}
[be0a79f]346#endif
[7d4fbcd]347
[31e48a3]348int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
349{
[be0a79f]350#ifdef HAVE_LIBZEPHYR
[7d4fbcd]351  int ret;
352  ZNotice_t notice;
353   
354  memset(&notice, 0, sizeof(notice));
355
[8262340]356  ZResetAuthentication();
357 
[7d4fbcd]358  notice.z_kind=ACKED;
359  notice.z_port=0;
360  notice.z_class=class;
361  notice.z_class_inst=instance;
362  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
363    notice.z_recipient="";
364  } else {
365    notice.z_recipient=recipient;
366  }
367  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
368  notice.z_sender=NULL;
369  if (opcode) notice.z_opcode=opcode;
370
[56330ff]371  notice.z_message_len=strlen(zsig)+1+strlen(message);
[7d4fbcd]372  notice.z_message=owl_malloc(notice.z_message_len+10);
[56330ff]373  strcpy(notice.z_message, zsig);
374  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
[7d4fbcd]375
376  /* ret=ZSendNotice(&notice, ZAUTH); */
377  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
378 
379  /* free then check the return */
380  owl_free(notice.z_message);
381  ZFreeNotice(&notice);
382  if (ret!=ZERR_NONE) {
[ec6ff52]383    owl_function_error("Error sending zephyr");
[7d4fbcd]384    return(ret);
385  }
386  return(0);
[be0a79f]387#else
388  return(0);
389#endif
[7d4fbcd]390}
391
[be0a79f]392#ifdef HAVE_LIBZEPHYR
[31e48a3]393Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
394{
[7d4fbcd]395  return(ZSendPacket(buf, len, 0));
396}
[be0a79f]397#endif
[7d4fbcd]398
[31e48a3]399void send_ping(char *to)
400{
[be0a79f]401#ifdef HAVE_LIBZEPHYR
[7d4fbcd]402  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
[be0a79f]403#endif
[7d4fbcd]404}
405
[be0a79f]406#ifdef HAVE_LIBZEPHYR
[31e48a3]407void owl_zephyr_handle_ack(ZNotice_t *retnotice)
408{
[7d4fbcd]409  char *tmp;
410 
411  /* if it's an HMACK ignore it */
412  if (retnotice->z_kind == HMACK) return;
[aecf3e6]413
[7d4fbcd]414  if (retnotice->z_kind == SERVNAK) {
[ec6ff52]415    owl_function_error("Authorization failure sending zephyr");
[7d4fbcd]416  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
[ec6ff52]417    owl_function_error("Detected server failure while receiving acknowledgement");
[7d4fbcd]418  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
419    if (!strcasecmp(retnotice->z_opcode, "ping")) {
420      return;
421    } else if (!strcasecmp(retnotice->z_class, "message") &&
422               !strcasecmp(retnotice->z_class_inst, "personal")) {
[4b464a4]423      tmp=short_zuser(retnotice->z_recipient);
[b4db911]424      owl_function_makemsg("Message sent to %s.", tmp);
[7d4fbcd]425      free(tmp);
426    } else {
[b4db911]427      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
[7d4fbcd]428    }
429  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
430    if (strcasecmp(retnotice->z_class, "message")) {
[9119a47]431      char buff[1024];
[269ed34]432      owl_function_error("No one subscribed to class class %s", retnotice->z_class);
433      sprintf(buff, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
[9119a47]434      owl_function_adminmsg("", buff);
[7d4fbcd]435    } else {
[9119a47]436      char buff[1024];
[4b464a4]437      tmp = short_zuser(retnotice->z_recipient);
[ec6ff52]438      owl_function_error("%s: Not logged in or subscribing to messages.", 
[1c6c4d3]439                           tmp);
[9119a47]440
441      sprintf(buff, "Could not send message to %s: not logged in or subscribing to messages.\n", tmp);
442      owl_function_adminmsg("", buff);
[1c6c4d3]443      owl_free(tmp);
[7d4fbcd]444    }
445  } else {
[ec6ff52]446    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]447  }
448}
[09489b89]449#else
450void owl_zephyr_handle_ack(void *retnotice)
451{
452}
[be0a79f]453#endif
[7d4fbcd]454
[be0a79f]455#ifdef HAVE_LIBZEPHYR
[31e48a3]456int owl_zephyr_notice_is_ack(ZNotice_t *n)
457{
[7d4fbcd]458  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
459    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
460    return(1);
461  }
462  return(0);
463}
[09489b89]464#else
465int owl_zephyr_notice_is_ack(void *n)
466{
467  return(0);
468}
[be0a79f]469#endif
[7d4fbcd]470 
[31e48a3]471void owl_zephyr_zaway(owl_message *m)
472{
[be0a79f]473#ifdef HAVE_LIBZEPHYR
[7c8060d0]474  char *tmpbuff, *myuser, *to;
[7d4fbcd]475 
[aa2f6364]476  /* bail if it doesn't look like a message we should reply to.  Some
[2de4f20]477   * of this defined by the way zaway(1) works
478   */
[7d4fbcd]479  if (strcasecmp(owl_message_get_class(m), "message")) return;
480  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
481  if (!strcasecmp(owl_message_get_sender(m), "")) return;
482  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
483  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
[d023c25]484  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
[7d4fbcd]485  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
486
[7c8060d0]487  if (owl_global_is_smartstrip(&g)) {
[e3d9c77]488    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
[7c8060d0]489  } else {
490    to=owl_strdup(owl_message_get_sender(m));
491  }
492
[7d4fbcd]493  send_zephyr("",
494              "Automated reply:",
495              owl_message_get_class(m),
496              owl_message_get_instance(m),
[7c8060d0]497              to,
[7d4fbcd]498              owl_global_get_zaway_msg(&g));
499
[7c8060d0]500  myuser=short_zuser(to);
[aa2f6364]501  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
502    tmpbuff = owl_sprintf("zwrite %s", myuser);
503  } else {
504    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
505  }
506  owl_free(myuser);
[7c8060d0]507  owl_free(to);
[aa2f6364]508
[7d4fbcd]509  /* display the message as an admin message in the receive window */
[d023c25]510  owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
[7d4fbcd]511  owl_free(tmpbuff);
[be0a79f]512#endif
[7d4fbcd]513}
514
[be0a79f]515#ifdef HAVE_LIBZEPHYR
[31e48a3]516void owl_zephyr_hackaway_cr(ZNotice_t *n)
517{
[7d4fbcd]518  /* replace \r's with ' '.  Gross-ish */
519  int i;
520
521  for (i=0; i<n->z_message_len; i++) {
522    if (n->z_message[i]=='\r') {
523      n->z_message[i]=' ';
524    }
525  }
526}
[be0a79f]527#endif
[7d4fbcd]528
[31e48a3]529void owl_zephyr_zlocate(char *user, char *out, int auth)
530{
[be0a79f]531#ifdef HAVE_LIBZEPHYR
[7d4fbcd]532  int ret, numlocs;
533  int one = 1;
534  ZLocations_t locations;
535  char *myuser;
536 
537  strcpy(out, "");
[8262340]538  ZResetAuthentication();
[7d4fbcd]539  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
540  if (ret != ZERR_NONE) {
[ec6ff52]541    owl_function_error("Error locating user %s", user);
[7d4fbcd]542  }
543
544  if (numlocs==0) {
[2527615]545    myuser=short_zuser(user);
546    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
547    owl_free(myuser);
[7d4fbcd]548    return;
549  }
550   
551  for (;numlocs;numlocs--) {
552    ZGetLocations(&locations,&one);
[4b464a4]553    myuser=short_zuser(user);
[7d4fbcd]554    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
555    owl_free(myuser);
556  }
[be0a79f]557#endif
[7d4fbcd]558}
[bde7714]559
[31e48a3]560void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
561{
[be0a79f]562#ifdef HAVE_LIBZEPHYR
[bde7714]563  char *line, subsfile[LINE], buff[LINE];
564  FILE *file;
565
566  line=owl_zephyr_makesubline(class, inst, recip);
567
568  if (filename==NULL) {
569    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
570  } else {
571    strcpy(subsfile, filename);
572  }
573
574  /* first check if it exists already */
575  file=fopen(subsfile, "r");
576  if (!file) {
[ec6ff52]577    owl_function_error("Error opening file %s", subsfile);
[bde7714]578    owl_free(line);
579    return;
580  }
581  while (fgets(buff, LINE, file)!=NULL) {
582    if (!strcasecmp(buff, line)) {
[ec6ff52]583      owl_function_error("Subscription already present in %s", subsfile);
[bde7714]584      owl_free(line);
585      return;
586    }
587  }
588
589  /* if we get here then we didn't find it */
590  fclose(file);
591  file=fopen(subsfile, "a");
592  if (!file) {
[ec6ff52]593    owl_function_error("Error opening file %s for writing", subsfile);
[bde7714]594    owl_free(line);
595    return;
596  }
597  fputs(line, file);
598  fclose(file);
599  owl_function_makemsg("Subscription added");
600 
601  owl_free(line);
[be0a79f]602#endif
[bde7714]603}
604
[31e48a3]605void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
606{
[be0a79f]607#ifdef HAVE_LIBZEPHYR
[b2a91b6]608  char *line, *subsfile;
[bde7714]609 
610  line=owl_zephyr_makesubline(class, inst, recip);
[b2a91b6]611  line[strlen(line)-1]='\0';
[bde7714]612
[b2a91b6]613  if (!filename) {
614    subsfile=owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
[bde7714]615  } else {
[b2a91b6]616    subsfile=owl_strdup(filename);
[bde7714]617  }
[b2a91b6]618 
619  owl_util_file_deleteline(subsfile, line, 1);
620  owl_free(subsfile);
[bde7714]621  owl_free(line);
622  owl_function_makemsg("Subscription removed");
[be0a79f]623#endif
[bde7714]624}
625
[b2a91b6]626/* caller must free the return */
[31e48a3]627char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
628{
[bde7714]629  char *out;
630
631  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
632  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
633  return(out);
634}
[31e48a3]635
636
637void owl_zephyr_zlog_in(void)
638{
[be0a79f]639#ifdef HAVE_LIBZEPHYR
[31e48a3]640  char *exposure, *eset;
641  int ret;
642
643  ZResetAuthentication();
644   
645  eset=EXPOSE_REALMVIS;
646  exposure=ZGetVariable("exposure");
647  if (exposure==NULL) {
648    eset=EXPOSE_REALMVIS;
649  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
650    eset = EXPOSE_NONE;
651  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
652    eset = EXPOSE_OPSTAFF;
653  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
654    eset = EXPOSE_REALMVIS;
655  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
656    eset = EXPOSE_REALMANN;
657  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
658    eset = EXPOSE_NETVIS;
659  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
660    eset = EXPOSE_NETANN;
661  }
662   
663  ret=ZSetLocation(eset);
664  if (ret != ZERR_NONE) {
665    /*
666      char buff[LINE];
667      sprintf(buff, "Error setting location: %s", error_message(ret));
668      owl_function_makemsg(buff);
669    */
670  }
[be0a79f]671#endif
[31e48a3]672}
673
674void owl_zephyr_zlog_out(void)
675{
[be0a79f]676#ifdef HAVE_LIBZEPHYR
[31e48a3]677  int ret;
678
679  ZResetAuthentication();
680  ret=ZUnsetLocation();
681  if (ret != ZERR_NONE) {
682    /*
683      char buff[LINE];
684      sprintf(buff, "Error unsetting location: %s", error_message(ret));
685      owl_function_makemsg(buff);
686    */
687  }
[be0a79f]688#endif
[31e48a3]689}
690
[65ad073]691void owl_zephyr_addbuddy(char *name)
692{
693  char *filename;
694  FILE *file;
695 
696  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
697  file=fopen(filename, "a");
698  owl_free(filename);
699  if (!file) {
[ec6ff52]700    owl_function_error("Error opening zephyr buddy file for append");
[65ad073]701    return;
702  }
703  fprintf(file, "%s\n", name);
704  fclose(file);
705}
706
707void owl_zephyr_delbuddy(char *name)
708{
709  char *filename;
710
711  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
712  owl_util_file_deleteline(filename, name, 0);
713  owl_free(filename);
714}
[9381782]715
716/* return auth string */
[09489b89]717#ifdef HAVE_LIBZEPHYR
[9381782]718char *owl_zephyr_get_authstr(ZNotice_t *n)
719{
720
721  if (!n) return("UNKNOWN");
722
723  if (n->z_auth == ZAUTH_FAILED) {
724    return ("FAILED");
725  } else if (n->z_auth == ZAUTH_NO) {
726    return ("NO");
727  } else if (n->z_auth == ZAUTH_YES) {
728    return ("YES");
729  } else {
730    return ("UNKNOWN");
731  }           
732}
[09489b89]733#else
734char *owl_zephyr_get_authstr(void *n)
735{
736  return("");
737}
738#endif
[9381782]739
[2de4f20]740/* Returns a buffer of subscriptions or an error message.  Caller must
741 * free the return.
[09489b89]742 */
743char *owl_zephyr_getsubs()
744{
745#ifdef HAVE_LIBZEPHYR
746  int ret, num, i, one;
747  ZSubscription_t sub;
748  char *out, *tmpbuff;
749  one=1;
750
751  ret=ZRetrieveSubscriptions(0, &num);
752  if (ret==ZERR_TOOMANYSUBS) {
[c8735aa]753    return(owl_strdup("Zephyr: too many subscriptions\n"));
754  } else if (ret) {
755    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
[09489b89]756  }
757
758  out=owl_malloc(num*500);
759  tmpbuff=owl_malloc(num*500);
760  strcpy(out, "");
761  for (i=0; i<num; i++) {
762    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
763      owl_free(out);
764      owl_free(tmpbuff);
765      ZFlushSubscriptions();
766      out=owl_strdup("Error while getting subscriptions\n");
767      return(out);
768    } else {
[03955f3]769      sprintf(tmpbuff, "<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
[09489b89]770      strcpy(out, tmpbuff);
771    }
772  }
773
774  owl_free(tmpbuff);
775  ZFlushSubscriptions();
776  return(out);
777#else
[12c35df]778  return(owl_strdup("Zephyr not available"));
[09489b89]779#endif
780}
781
782char *owl_zephyr_get_variable(char *var)
783{
784#ifdef HAVE_LIBZEPHYR
785  return(ZGetVariable(var));
786#else
787  return("");
788#endif
789}
790
791void owl_zephyr_set_locationinfo(char *host, char *val)
792{
793#ifdef HAVE_LIBZEPHYR
794  ZInitLocationInfo(host, val);
795#endif
796}
797 
[e3d9c77]798/* Strip a local realm fron the zephyr user name.
799 * The caller must free the return
800 */
801char *short_zuser(char *in)
802{
803  char *out, *ptr;
804
805  out=owl_strdup(in);
806  ptr=strchr(out, '@');
807  if (ptr) {
808    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
809      *ptr='\0';
810    }
811  }
812  return(out);
813}
814
815/* Append a local realm to the zephyr user name if necessary.
816 * The caller must free the return.
817 */
818char *long_zuser(char *in)
819{
820  char *ptr;
821
822  if (NULL != (ptr=strchr(in, '@'))) {
823    return owl_strdup(in);
824  } else {
825    return owl_sprintf("%s@%s", in, owl_zephyr_get_realm());
826  }
827}
828
829
830/* strip out the instance from a zsender's principal.  Preserves the
831 * realm if present.  daemon.webzephyr is a special case.  The
832 * caller must free the return
833 */
834char *owl_zephyr_smartstripped_user(char *in)
835{
836  char *ptr, *realm, *out;
837
838  out=owl_strdup(in);
839
840  /* bail immeaditly if we don't have to do any work */
841  ptr=strchr(in, '.');
842  if (!strchr(in, '/') && !ptr) {
843    /* no '/' and no '.' */
844    return(out);
845  }
846  if (ptr && strchr(in, '@') && (ptr > strchr(in, '@'))) {
847    /* There's a '.' but it's in the realm */
848    return(out);
849  }
850  if (!strncasecmp(in, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
851    return(out);
852  }
853
854  /* remove the realm from ptr, but hold on to it */
855  realm=strchr(out, '@');
856  if (realm) realm[0]='\0';
857
858  /* strip */
859  ptr=strchr(out, '.');
860  if (!ptr) ptr=strchr(out, '/');
861  ptr[0]='\0';
862
863  /* reattach the realm if we had one */
864  if (realm) {
865    strcat(out, "@");
866    strcat(out, realm+1);
867  }
868
869  return(out);
870}
[5a95b69]871
872/* read the list of users in 'filename' as a .anyone file, and put the
873 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
874 * use the default .anyone file in the users home directory.  Returns
875 * -1 on failure, 0 on success.
876 */
877int owl_zephyr_get_anyone_list(owl_list *in, char *filename)
878{
879#ifdef HAVE_LIBZEPHYR
880  char *ourfile, *tmp, buff[LINE];
881  FILE *f;
882
883  if (filename==NULL) {
884    tmp=owl_global_get_homedir(&g);
885    ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
886  } else {
887    ourfile=owl_strdup(filename);
888  }
889 
890  f=fopen(ourfile, "r");
891  if (!f) {
892    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
893    owl_free(ourfile);
894    return(-1);
895  }
896
897  while (fgets(buff, LINE, f)!=NULL) {
898    /* ignore comments, blank lines etc. */
899    if (buff[0]=='#') continue;
900    if (buff[0]=='\n') continue;
901    if (buff[0]=='\0') continue;
902   
903    /* strip the \n */
904    buff[strlen(buff)-1]='\0';
905   
906    /* ingore from # on */
907    tmp=strchr(buff, '#');
908    if (tmp) tmp[0]='\0';
909   
910    /* ingore from SPC */
911    tmp=strchr(buff, ' ');
912    if (tmp) tmp[0]='\0';
913   
914    /* stick on the local realm. */
915    if (!strchr(buff, '@')) {
916      strcat(buff, "@");
917      strcat(buff, ZGetRealm());
918    }
919    owl_list_append_element(in, owl_strdup(buff));
920  }
921  fclose(f);
922  owl_free(ourfile);
923  return(0);
924#else
925  return(-1);
926#endif
927}
Note: See TracBrowser for help on using the repository browser.