source: zephyr.c @ 2058d7a

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