source: zephyr.c @ 0ff2528

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