source: zephyr.c @ 0f1f388

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