source: zephyr.c @ a1bb198

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