source: zephyr.c @ bf73bdd

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