source: zephyr.c @ 6337cb5

release-1.10release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 6337cb5 was 12e291a, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 15 years ago
Add a zephyr pre-select action. This replaces the solution commited in 5657d536d09861fe734329e12194b4c410d34d20.
  • Property mode set to 100644
File size: 33.5 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
[fea7992]9#ifdef HAVE_LIBZEPHYR
[a5e7ed6]10static GList *deferred_subs = NULL;
[79d7d98]11
[a5e7ed6]12typedef struct _owl_sub_list {                            /* noproto */
13  ZSubscription_t *subs;
14  int nsubs;
15} owl_sub_list;
16
[c79a047]17Code_t ZResetAuthentication(void);
[09489b89]18#endif
[8262340]19
[02f55dc]20#define HM_SVC_FALLBACK         htons((unsigned short) 2104)
21
[6ea3890]22static char *owl_zephyr_dotfile(const char *name, const char *input)
23{
24  if (input != NULL)
25    return owl_strdup(input);
26  else
27    return owl_sprintf("%s/%s", owl_global_get_homedir(&g), name);
28}
29
[52a0f14]30#ifdef HAVE_LIBZEPHYR
[c79a047]31void owl_zephyr_initialize(void)
[52a0f14]32{
33  int ret;
[02f55dc]34  struct servent *sp;
35  struct sockaddr_in sin;
36  ZNotice_t req;
37  Code_t code;
[52a0f14]38  owl_dispatch *dispatch;
39
40  /*
41   * Code modified from libzephyr's ZhmStat.c
42   *
43   * Modified to add the fd to our select loop, rather than hanging
44   * until we get an ack.
45   */
46
47  if ((ret = ZOpenPort(NULL)) != ZERR_NONE) {
48    owl_function_error("Error opening Zephyr port: %s", error_message(ret));
49    return;
50  }
[02f55dc]51
[4d86e06]52  (void) memset(&sin, 0, sizeof(struct sockaddr_in));
[02f55dc]53
54  sp = getservbyname(HM_SVCNAME, "udp");
55
56  sin.sin_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
57  sin.sin_family = AF_INET;
58
59  sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
60
[4d86e06]61  (void) memset(&req, 0, sizeof(req));
[02f55dc]62  req.z_kind = STAT;
63  req.z_port = 0;
[712caac]64  req.z_class = zstr(HM_STAT_CLASS);
65  req.z_class_inst = zstr(HM_STAT_CLIENT);
66  req.z_opcode = zstr(HM_GIMMESTATS);
67  req.z_sender = zstr("");
68  req.z_recipient = zstr("");
69  req.z_default_format = zstr("");
[02f55dc]70  req.z_message_len = 0;
[52a0f14]71
72  if ((code = ZSetDestAddr(&sin)) != ZERR_NONE) {
73    owl_function_error("Initializing Zephyr: %s", error_message(code));
74    return;
75  }
76
77  if ((code = ZSendNotice(&req, ZNOAUTH)) != ZERR_NONE) {
78    owl_function_error("Initializing Zephyr: %s", error_message(code));
79    return;
80  }
81
82  dispatch = owl_malloc(sizeof(*dispatch));
83  dispatch->fd = ZGetFD();
84  dispatch->cfunc = owl_zephyr_finish_initialization;
85  dispatch->destroy = (void(*)(owl_dispatch*))owl_free;
86
87  owl_select_add_dispatch(dispatch);
[02f55dc]88}
89
[52a0f14]90void owl_zephyr_finish_initialization(owl_dispatch *d) {
91  Code_t code;
[df569c5]92  char *perl;
[52a0f14]93
94  owl_select_remove_dispatch(d->fd);
95
96  ZClosePort();
97
98  if ((code = ZInitialize()) != ZERR_NONE) {
99    owl_function_error("Initializing Zephyr: %s", error_message(code));
100    return;
101  }
102
103  if ((code = ZOpenPort(NULL)) != ZERR_NONE) {
104    owl_function_error("Initializing Zephyr: %s", error_message(code));
105    return;
106  }
107
108  d = owl_malloc(sizeof(owl_dispatch));
109  d->fd = ZGetFD();
110  d->cfunc = &owl_zephyr_process_events;
111  d->destroy = NULL;
112  owl_select_add_dispatch(d);
113  owl_global_set_havezephyr(&g);
114
115  if(g.load_initial_subs) {
116    owl_zephyr_load_initial_subs();
117  }
[a5e7ed6]118  while(deferred_subs != NULL) {
119    owl_sub_list *subs = deferred_subs->data;
120    owl_function_debugmsg("Loading %d deferred subs.", subs->nsubs);
121    owl_zephyr_loadsubs_helper(subs->subs, subs->nsubs);
122    deferred_subs = g_list_delete_link(deferred_subs, deferred_subs);
123    owl_free(subs);
124  }
[619d864]125
126  /* zlog in if we need to */
127  if (owl_global_is_startuplogin(&g)) {
128    owl_function_debugmsg("startup: doing zlog in");
129    owl_zephyr_zlog_in();
130  }
[27964fe]131  /* check pseudo-logins if we need to */
132  if (owl_global_is_pseudologins(&g)) {
133    owl_function_debugmsg("startup: checking pseudo-logins");
134    owl_function_zephyr_buddy_check(0);
135  }
[df569c5]136
[12e291a]137  perl = owl_perlconfig_execute("BarnOwl::Zephyr::_zephyr_startup()");
138  owl_free(perl);
139
140  owl_select_add_pre_select_action(owl_zephyr_pre_select_action, NULL, NULL);
[52a0f14]141}
142
[c79a047]143void owl_zephyr_load_initial_subs(void) {
[7451af9]144  int ret_sd, ret_bd, ret_u;
[52a0f14]145
146  owl_function_debugmsg("startup: loading initial zephyr subs");
147
148  /* load default subscriptions */
[7451af9]149  ret_sd = owl_zephyr_loaddefaultsubs();
150
151  /* load Barnowl default subscriptions */
152  ret_bd = owl_zephyr_loadbarnowldefaultsubs();
[52a0f14]153
154  /* load subscriptions from subs file */
[7451af9]155  ret_u = owl_zephyr_loadsubs(NULL, 0);
[52a0f14]156
[7451af9]157  if (ret_sd || ret_bd || ret_u) {
[52a0f14]158    owl_function_error("Error loading zephyr subscriptions");
[7451af9]159  } else if (ret_u!=-1) {
[52a0f14]160    owl_global_add_userclue(&g, OWL_USERCLUE_CLASSES);
161  }
162
163  /* load login subscriptions */
164  if (owl_global_is_loginsubs(&g)) {
165    owl_function_debugmsg("startup: loading login subs");
166    owl_function_loadloginsubs(NULL);
167  }
168}
169#else
[c79a047]170void owl_zephyr_initialize(void)
[52a0f14]171{
172}
[02f55dc]173#endif
174
[52a0f14]175
[c79a047]176int owl_zephyr_shutdown(void)
[09489b89]177{
178#ifdef HAVE_LIBZEPHYR
[bfbf590]179  if(owl_global_is_havezephyr(&g)) {
180    unsuball();
181    ZClosePort();
182  }
[09489b89]183#endif
[32ad44d]184  return 0;
[be0a79f]185}
186
[c79a047]187int owl_zephyr_zpending(void)
[be0a79f]188{
189#ifdef HAVE_LIBZEPHYR
[bfbf590]190  if(owl_global_is_havezephyr(&g))
191    return(ZPending());
[be0a79f]192#endif
[32ad44d]193  return 0;
[be0a79f]194}
195
[c79a047]196const char *owl_zephyr_get_realm(void)
[09489b89]197{
198#ifdef HAVE_LIBZEPHYR
[32ad44d]199  if (owl_global_is_havezephyr(&g))
200    return(ZGetRealm());
[09489b89]201#endif
[32ad44d]202  return "";
[09489b89]203}
204
[c79a047]205const char *owl_zephyr_get_sender(void)
[09489b89]206{
207#ifdef HAVE_LIBZEPHYR
[32ad44d]208  if (owl_global_is_havezephyr(&g))
209    return(ZGetSender());
[09489b89]210#endif
[32ad44d]211  return "";
[09489b89]212}
213
[f6050ee]214#ifdef HAVE_LIBZEPHYR
[93e883d]215int owl_zephyr_loadsubs_helper(ZSubscription_t subs[], int count)
216{
[18105584]217  int ret = 0;
[a5e7ed6]218  if (owl_global_is_havezephyr(&g)) {
219    int i;
220    /* sub without defaults */
221    if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
222      owl_function_error("Error subscribing to zephyr notifications.");
223      ret=-2;
224    }
[93e883d]225
[a5e7ed6]226    /* free stuff */
227    for (i=0; i<count; i++) {
228      owl_free(subs[i].zsub_class);
229      owl_free(subs[i].zsub_classinst);
230      owl_free(subs[i].zsub_recipient);
231    }
[bb2c60d]232
[a5e7ed6]233    owl_free(subs);
234  } else {
235    owl_sub_list *s = owl_malloc(sizeof(owl_sub_list));
236    s->subs = subs;
237    s->nsubs = count;
238    deferred_subs = g_list_append(deferred_subs, s);
239  }
[d21efbc]240
[93e883d]241  return ret;
242}
[f6050ee]243#endif
[93e883d]244
[7451af9]245/* Load zephyr subscriptions from 'filename'.  If 'filename' is NULL,
[95474d7]246 * the default file $HOME/.zephyr.subs will be used.
247 *
248 * Returns 0 on success.  If the file does not exist, return -1 if
249 * 'error_on_nofile' is 1, otherwise return 0.  Return -1 if the file
250 * exists but can not be read.  Return -2 if there is a failure from
251 * zephyr to load the subscriptions.
[2de4f20]252 */
[e19eb97]253int owl_zephyr_loadsubs(const char *filename, int error_on_nofile)
[31e48a3]254{
[be0a79f]255#ifdef HAVE_LIBZEPHYR
[7d4fbcd]256  FILE *file;
257  char *tmp, *start;
[b7ee89b]258  char *buffer = NULL;
259  char *subsfile;
[bb2c60d]260  ZSubscription_t *subs;
261  int subSize = 1024;
[b7ee89b]262  int count;
[4357be8]263  struct stat statbuff;
[7d4fbcd]264
[bb2c60d]265  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
[6ea3890]266  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
[b7ee89b]267
268  if (stat(subsfile, &statbuff) != 0) {
269    if (error_on_nofile == 1)
270      return -1;
271    return 0;
[95474d7]272  }
[4357be8]273
[8262340]274  ZResetAuthentication();
[b7ee89b]275  count = 0;
276  file = fopen(subsfile, "r");
277  owl_free(subsfile);
278  if (!file)
279    return -1;
280  while (owl_getline(&buffer, file)) {
281    if (buffer[0] == '#' || buffer[0] == '\n')
282        continue;
283
284    if (buffer[0] == '-')
285      start = buffer + 1;
286    else
287      start = buffer;
288
[bb2c60d]289    if (count >= subSize) {
[d21efbc]290      subSize *= 2;
291      subs = owl_realloc(subs, sizeof(ZSubscription_t) * subSize);
[93e883d]292    }
[95474d7]293   
294    /* add it to the list of subs */
[b7ee89b]295    if ((tmp = strtok(start, ",\n\r")) == NULL)
296      continue;
297    subs[count].zsub_class = owl_strdup(tmp);
298    if ((tmp=strtok(NULL, ",\n\r")) == NULL)
299      continue;
300    subs[count].zsub_classinst = owl_strdup(tmp);
301    if ((tmp = strtok(NULL, " \t\n\r")) == NULL)
302      continue;
303    subs[count].zsub_recipient = owl_strdup(tmp);
304
[bb2c60d]305    /* if it started with '-' then add it to the global punt list, and
306     * remove it from the list of subs. */
[b7ee89b]307    if (buffer[0] == '-') {
[95474d7]308      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
[bb2c60d]309      owl_free(subs[count].zsub_class);
310      owl_free(subs[count].zsub_classinst);
311      owl_free(subs[count].zsub_recipient);
[b7ee89b]312    } else {
[bb2c60d]313      count++;
[95474d7]314    }
[7d4fbcd]315  }
[95474d7]316  fclose(file);
[b7ee89b]317  if (buffer)
318    owl_free(buffer);
[7d4fbcd]319
[b7ee89b]320  return owl_zephyr_loadsubs_helper(subs, count);
[7451af9]321#else
[b7ee89b]322  return 0;
[7451af9]323#endif
324}
325
326/* Load default Barnowl subscriptions
327 *
328 * Returns 0 on success.
329 * Return -2 if there is a failure from zephyr to load the subscriptions.
330 */
[c79a047]331int owl_zephyr_loadbarnowldefaultsubs(void)
[7451af9]332{
333#ifdef HAVE_LIBZEPHYR
334  ZSubscription_t *subs;
335  int subSize = 10; /* Max Barnowl default subs we allow */
336  int count, ret;
337
338  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
339  ret = 0;
340  ZResetAuthentication();
341  count=0;
342
343  subs[count].zsub_class=owl_strdup("message");
344  subs[count].zsub_classinst=owl_strdup("*");
345  subs[count].zsub_recipient=owl_strdup("%me%");
346  count++;
347
348  ret = owl_zephyr_loadsubs_helper(subs, count);
[7d4fbcd]349  return(ret);
[be0a79f]350#else
351  return(0);
352#endif
[7d4fbcd]353}
354
[c79a047]355int owl_zephyr_loaddefaultsubs(void)
[4357be8]356{
[40d834a]357#ifdef HAVE_LIBZEPHYR
[4357be8]358  ZSubscription_t subs[10];
359   
360  if (ZSubscribeTo(subs,0,0) != ZERR_NONE) {
361    owl_function_error("Error subscribing to default zephyr notifications.");
362    return(-1);
363  }
364  return(0);
[40d834a]365#else
366  return(0);
367#endif
[4357be8]368}
369
[e19eb97]370int owl_zephyr_loadloginsubs(const char *filename)
[31e48a3]371{
[be0a79f]372#ifdef HAVE_LIBZEPHYR
[7d4fbcd]373  FILE *file;
[d21efbc]374  ZSubscription_t *subs;
375  int numSubs = 100;
[b7ee89b]376  char *subsfile;
377  char *buffer = NULL;
378  int count;
[4357be8]379  struct stat statbuff;
[7d4fbcd]380
[d21efbc]381  subs = owl_malloc(numSubs * sizeof(ZSubscription_t));
[6ea3890]382  subsfile = owl_zephyr_dotfile(".anyone", filename);
[4357be8]383
[b7ee89b]384  if (stat(subsfile, &statbuff) == -1)
385    return 0;
[7d4fbcd]386
[8262340]387  ZResetAuthentication();
[b7ee89b]388  count = 0;
389  file = fopen(subsfile, "r");
390  owl_free(subsfile);
[7d4fbcd]391  if (file) {
[b7ee89b]392    while (owl_getline_chomp(&buffer, file)) {
393      if (buffer[0] == '\0' || buffer[0] == '#')
394        continue;
395
[d21efbc]396      if (count == numSubs) {
397        numSubs *= 2;
398        subs = owl_realloc(subs, numSubs * sizeof(ZSubscription_t));
399      }
[7d4fbcd]400
[b7ee89b]401      subs[count].zsub_class = owl_strdup("login");
402      subs[count].zsub_recipient = owl_strdup("*");
403      subs[count].zsub_classinst = long_zuser(buffer);
[7d4fbcd]404
405      count++;
406    }
407    fclose(file);
408  } else {
[b7ee89b]409    return 0;
[7d4fbcd]410  }
[b7ee89b]411  if (buffer)
412    owl_free(buffer);
[7d4fbcd]413
[b7ee89b]414  return owl_zephyr_loadsubs_helper(subs, count);
[be0a79f]415#else
[b7ee89b]416  return 0;
[be0a79f]417#endif
[7d4fbcd]418}
419
[c79a047]420void unsuball(void)
[31e48a3]421{
[be0a79f]422#if HAVE_LIBZEPHYR
[7d4fbcd]423  int ret;
[8262340]424
425  ZResetAuthentication();
[7d4fbcd]426  ret=ZCancelSubscriptions(0);
427  if (ret != ZERR_NONE) {
428    com_err("owl",ret,"while unsubscribing");
429  }
[be0a79f]430#endif
[7d4fbcd]431}
432
[e19eb97]433int owl_zephyr_sub(const char *class, const char *inst, const char *recip)
[31e48a3]434{
[be0a79f]435#ifdef HAVE_LIBZEPHYR
[7d4fbcd]436  ZSubscription_t subs[5];
437
[712caac]438  subs[0].zsub_class=zstr(class);
439  subs[0].zsub_classinst=zstr(inst);
440  subs[0].zsub_recipient=zstr(recip);
[7d4fbcd]441
[8262340]442  ZResetAuthentication();
[7d4fbcd]443  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
[97cd00be]444    owl_function_error("Error subbing to <%s,%s,%s>", class, inst, recip);
445    return(-2);
[7d4fbcd]446  }
447  return(0);
[be0a79f]448#else
449  return(0);
450#endif
[7d4fbcd]451}
452
453
[e19eb97]454int owl_zephyr_unsub(const char *class, const char *inst, const char *recip)
[31e48a3]455{
[be0a79f]456#ifdef HAVE_LIBZEPHYR
[7d4fbcd]457  ZSubscription_t subs[5];
458
[712caac]459  subs[0].zsub_class=zstr(class);
460  subs[0].zsub_classinst=zstr(inst);
461  subs[0].zsub_recipient=zstr(recip);
[7d4fbcd]462
[8262340]463  ZResetAuthentication();
[7d4fbcd]464  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
[97cd00be]465    owl_function_error("Error unsubbing from <%s,%s,%s>", class, inst, recip);
466    return(-2);
[7d4fbcd]467  }
468  return(0);
[be0a79f]469#else
470  return(0);
471#endif
[7d4fbcd]472}
473
[b0430a6]474/* return a pointer to the data in the Jth field, (NULL terminated by
475 * definition).  Caller must free the return.
476 */
[be0a79f]477#ifdef HAVE_LIBZEPHYR
[1077891a]478char *owl_zephyr_get_field(const ZNotice_t *n, int j)
[31e48a3]479{
[7d4fbcd]480  int i, count, save;
[b0430a6]481  char *out;
[7d4fbcd]482
[128171a]483  /* If there's no message here, just run along now */
484  if (n->z_message_len == 0)
485    return(owl_strdup(""));
486
[7d4fbcd]487  count=save=0;
488  for (i=0; i<n->z_message_len; i++) {
489    if (n->z_message[i]=='\0') {
490      count++;
491      if (count==j) {
492        /* just found the end of the field we're looking for */
[b0430a6]493        return(owl_strdup(n->z_message+save));
[7d4fbcd]494      } else {
495        save=i+1;
496      }
497    }
498  }
[b0430a6]499  /* catch the last field, which might not be null terminated */
[7d4fbcd]500  if (count==j-1) {
[b0430a6]501    out=owl_malloc(n->z_message_len-save+5);
502    memcpy(out, n->z_message+save, n->z_message_len-save);
503    out[n->z_message_len-save]='\0';
504    return(out);
[7d4fbcd]505  }
[b0430a6]506
507  return(owl_strdup(""));
[7d4fbcd]508}
[5376a95]509
[1077891a]510char *owl_zephyr_get_field_as_utf8(const ZNotice_t *n, int j)
[5376a95]511{
512  int i, count, save;
513
514  /* If there's no message here, just run along now */
515  if (n->z_message_len == 0)
516    return(owl_strdup(""));
517
518  count=save=0;
519  for (i = 0; i < n->z_message_len; i++) {
520    if (n->z_message[i]=='\0') {
521      count++;
522      if (count == j) {
523        /* just found the end of the field we're looking for */
[6201646]524        return(owl_validate_or_convert(n->z_message + save));
[5376a95]525      } else {
526        save = i + 1;
527      }
528    }
529  }
530  /* catch the last field, which might not be null terminated */
531  if (count == j - 1) {
[6201646]532    char *tmp, *out;
533    tmp = owl_malloc(n->z_message_len-save+5);
534    memcpy(tmp, n->z_message+save, n->z_message_len-save);
535    tmp[n->z_message_len-save]='\0';
536    out = owl_validate_or_convert(tmp);
537    owl_free(tmp);
538    return out;
[5376a95]539  }
540
541  return(owl_strdup(""));
542}
[09489b89]543#else
[701a184]544char *owl_zephyr_get_field(void *n, int j)
[09489b89]545{
[b0430a6]546  return(owl_strdup(""));
[09489b89]547}
[5577606]548char *owl_zephyr_get_field_as_utf8(void *n, int j)
[5376a95]549{
550  return owl_zephyr_get_field(n, j);
551}
[be0a79f]552#endif
[7d4fbcd]553
[b0430a6]554
[be0a79f]555#ifdef HAVE_LIBZEPHYR
[1077891a]556int owl_zephyr_get_num_fields(const ZNotice_t *n)
[31e48a3]557{
[7d4fbcd]558  int i, fields;
559
[50e29e3]560  if(n->z_message_len == 0)
561    return 0;
562
[7d4fbcd]563  fields=1;
564  for (i=0; i<n->z_message_len; i++) {
565    if (n->z_message[i]=='\0') fields++;
566  }
567 
568  return(fields);
569}
[09489b89]570#else
[1077891a]571int owl_zephyr_get_num_fields(const void *n)
[09489b89]572{
573  return(0);
574}
[be0a79f]575#endif
[7d4fbcd]576
[be0a79f]577#ifdef HAVE_LIBZEPHYR
[bf73bdd]578/* return a pointer to the message, place the message length in k
579 * caller must free the return
580 */
[c08c70a]581char *owl_zephyr_get_message(const ZNotice_t *n, const owl_message *m)
[31e48a3]582{
[405d5e6]583  /* don't let ping messages have a body */
[7d4fbcd]584  if (!strcasecmp(n->z_opcode, "ping")) {
[bf73bdd]585    return(owl_strdup(""));
[7d4fbcd]586  }
587
[a1bb198]588  /* deal with MIT NOC messages */
[85d1795]589  if (!strcasecmp(n->z_default_format, "@center(@bold(NOC Message))\n\n@bold(Sender:) $1 <$sender>\n@bold(Time:  ) $time\n\n@italic($opcode service on $instance $3.) $4\n")) {
590    char *msg, *field3, *field4;
[a1bb198]591
592    field3 = owl_zephyr_get_field(n, 3);
593    field4 = owl_zephyr_get_field(n, 4);
594
[85d1795]595    msg = owl_sprintf("%s service on %s %s\n%s", n->z_opcode, n->z_class_inst, field3, field4);
[272a4a0]596    owl_free(field3);
597    owl_free(field4);
[a1bb198]598    if (msg) {
599      return msg;
600    }
601  }
[fba0f96]602  /* deal with MIT Discuss messages */
[e2a620b]603  else if (!strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3 ($5)\nSubject: $4") ||
604           !strcasecmp(n->z_default_format, "New transaction [$1] entered in $2\nFrom: $3\nSubject: $4")) {
[fba0f96]605    char *msg, *field1, *field2, *field3, *field4, *field5;
606   
607    field1 = owl_zephyr_get_field(n, 1);
608    field2 = owl_zephyr_get_field(n, 2);
609    field3 = owl_zephyr_get_field(n, 3);
610    field4 = owl_zephyr_get_field(n, 4);
611    field5 = owl_zephyr_get_field(n, 5);
612   
613    msg = owl_sprintf("New transaction [%s] entered in %s\nFrom: %s (%s)\nSubject: %s", field1, field2, field3, field5, field4);
614    owl_free(field1);
615    owl_free(field2);
616    owl_free(field3);
617    owl_free(field4);
618    owl_free(field5);
619    if (msg) {
620      return msg;
621    }
622  }
[85d1795]623  /* deal with MIT Moira messages */
624  else if (!strcasecmp(n->z_default_format, "MOIRA $instance on $fromhost:\n $message\n")) {
625    char *msg, *field1;
626   
627    field1 = owl_zephyr_get_field(n, 1);
628   
629    msg = owl_sprintf("MOIRA %s on %s: %s", n->z_class_inst, owl_message_get_hostname(m), field1);
630    owl_free(field1);
631    if (msg) {
632      return msg;
633    }
634  }
[405d5e6]635
[85d1795]636  if (owl_zephyr_get_num_fields(n) == 1) {
637    return(owl_zephyr_get_field(n, 1));
638  }
639  else {
640    return(owl_zephyr_get_field(n, 2));
641  }
[7d4fbcd]642}
[be0a79f]643#endif
[7d4fbcd]644
[be0a79f]645#ifdef HAVE_LIBZEPHYR
[1077891a]646const char *owl_zephyr_get_zsig(const ZNotice_t *n, int *k)
[31e48a3]647{
[7d4fbcd]648  /* return a pointer to the zsig if there is one */
649
[405d5e6]650  /* message length 0? No zsig */
[7d4fbcd]651  if (n->z_message_len==0) {
652    *k=0;
653    return("");
654  }
[405d5e6]655
[85d1795]656  /* If there's only one field, no zsig */
657  if (owl_zephyr_get_num_fields(n) == 1) {
658    *k=0;
[405d5e6]659    return("");
660  }
661
662  /* Everything else is field 1 */
[7d4fbcd]663  *k=strlen(n->z_message);
664  return(n->z_message);
665}
[09489b89]666#else
[1077891a]667const char *owl_zephyr_get_zsig(const void *n, int *k)
[09489b89]668{
669  return("");
670}
[be0a79f]671#endif
[7d4fbcd]672
[e19eb97]673int send_zephyr(const char *opcode, const char *zsig, const char *class, const char *instance, const char *recipient, const char *message)
[31e48a3]674{
[be0a79f]675#ifdef HAVE_LIBZEPHYR
[7d4fbcd]676  int ret;
677  ZNotice_t notice;
678   
679  memset(&notice, 0, sizeof(notice));
680
[8262340]681  ZResetAuthentication();
[8ba37ec]682
683  if (!zsig) zsig="";
[8262340]684 
[7d4fbcd]685  notice.z_kind=ACKED;
686  notice.z_port=0;
[712caac]687  notice.z_class=zstr(class);
688  notice.z_class_inst=zstr(instance);
[21882032]689  notice.z_sender=NULL;
[7d4fbcd]690  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
[712caac]691    notice.z_recipient=zstr("");
[21882032]692    if (*owl_global_get_zsender(&g))
[712caac]693        notice.z_sender=zstr(owl_global_get_zsender(&g));
[7d4fbcd]694  } else {
[712caac]695    notice.z_recipient=zstr(recipient);
[7d4fbcd]696  }
[712caac]697  notice.z_default_format=zstr("Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2");
698  if (opcode) notice.z_opcode=zstr(opcode);
[7d4fbcd]699
[56330ff]700  notice.z_message_len=strlen(zsig)+1+strlen(message);
[7d4fbcd]701  notice.z_message=owl_malloc(notice.z_message_len+10);
[56330ff]702  strcpy(notice.z_message, zsig);
703  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
[7d4fbcd]704
705  /* ret=ZSendNotice(&notice, ZAUTH); */
706  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
707 
708  /* free then check the return */
709  owl_free(notice.z_message);
710  ZFreeNotice(&notice);
711  if (ret!=ZERR_NONE) {
[ec6ff52]712    owl_function_error("Error sending zephyr");
[7d4fbcd]713    return(ret);
714  }
715  return(0);
[be0a79f]716#else
717  return(0);
718#endif
[7d4fbcd]719}
720
[be0a79f]721#ifdef HAVE_LIBZEPHYR
[31e48a3]722Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
723{
[7d4fbcd]724  return(ZSendPacket(buf, len, 0));
725}
[be0a79f]726#endif
[7d4fbcd]727
[e19eb97]728void send_ping(const char *to, const char *zclass, const char *zinstance)
[31e48a3]729{
[be0a79f]730#ifdef HAVE_LIBZEPHYR
[3ef779b]731  send_zephyr("PING", "", zclass, zinstance, to, "");
[be0a79f]732#endif
[7d4fbcd]733}
734
[be0a79f]735#ifdef HAVE_LIBZEPHYR
[1077891a]736void owl_zephyr_handle_ack(const ZNotice_t *retnotice)
[31e48a3]737{
[7d4fbcd]738  char *tmp;
739 
740  /* if it's an HMACK ignore it */
741  if (retnotice->z_kind == HMACK) return;
[aecf3e6]742
[7d4fbcd]743  if (retnotice->z_kind == SERVNAK) {
[ec6ff52]744    owl_function_error("Authorization failure sending zephyr");
[7d4fbcd]745  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
[ec6ff52]746    owl_function_error("Detected server failure while receiving acknowledgement");
[7d4fbcd]747  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
748    if (!strcasecmp(retnotice->z_opcode, "ping")) {
749      return;
750    } else {
[d73e3af]751      if (strcasecmp(retnotice->z_recipient, ""))
[1e550b2]752      { /* personal */
[d73e3af]753        tmp=short_zuser(retnotice->z_recipient);
754        if(!strcasecmp(retnotice->z_class, "message") &&
755           !strcasecmp(retnotice->z_class_inst, "personal")) {
756          owl_function_makemsg("Message sent to %s.", tmp);
[1e550b2]757        } else if(!strcasecmp(retnotice->z_class, "message")) { /* instanced, but not classed, personal */
[d73e3af]758          owl_function_makemsg("Message sent to %s on -i %s\n", tmp, retnotice->z_class_inst);
[1e550b2]759        } else { /* classed personal */
[d73e3af]760          owl_function_makemsg("Message sent to %s on -c %s -i %s\n", tmp, retnotice->z_class, retnotice->z_class_inst);
761        }
[27f6487]762        owl_free(tmp);
[d73e3af]763      } else {
[1e550b2]764        /* class / instance message */
[d73e3af]765          owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
766      }
[7d4fbcd]767    }
768  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
[1151f0b]769    #define BUFFLEN 1024
[44f32fb]770    if (retnotice->z_recipient == NULL
[60c2e1e]771        || *retnotice->z_recipient == 0
[44f32fb]772        || *retnotice->z_recipient == '@') {
[1151f0b]773      char buff[BUFFLEN];
[44f32fb]774      owl_function_error("No one subscribed to class %s", retnotice->z_class);
[1151f0b]775      snprintf(buff, BUFFLEN, "Could not send message to class %s: no one subscribed.\n", retnotice->z_class);
[9119a47]776      owl_function_adminmsg("", buff);
[7d4fbcd]777    } else {
[1151f0b]778      char buff[BUFFLEN];
[24ccc01]779      owl_zwrite zw;
780      char *realm;
781
[4b464a4]782      tmp = short_zuser(retnotice->z_recipient);
[44f32fb]783      owl_function_error("%s: Not logged in or subscribing.", tmp);
[3ef779b]784      /*
785       * These error messages are often over 80 chars, but users who want to
786       * see the whole thing can scroll to the side, and for those with wide
787       * terminals or who don't care, not splitting saves a line in the UI
788       */
789      if(strcasecmp(retnotice->z_class, "message")) {
[1151f0b]790        snprintf(buff, BUFFLEN,
[10e3963]791                 "Could not send message to %s: "
[3ef779b]792                 "not logged in or subscribing to class %s, instance %s.\n",
[10e3963]793                 tmp,
[1151f0b]794                 retnotice->z_class,
795                 retnotice->z_class_inst);
[3ef779b]796      } else if(strcasecmp(retnotice->z_class_inst, "personal")) {
797        snprintf(buff, BUFFLEN,
798                 "Could not send message to %s: "
799                 "not logged in or subscribing to instance %s.\n",
800                 tmp,
801                 retnotice->z_class_inst);
[44f32fb]802      } else {
[1151f0b]803        snprintf(buff, BUFFLEN,
[10e3963]804                 "Could not send message to %s: "
805                 "not logged in or subscribing to messages.\n",
806                 tmp);
[44f32fb]807      }
[9119a47]808      owl_function_adminmsg("", buff);
[24ccc01]809
810      memset(&zw, 0, sizeof(zw));
811      zw.class = owl_strdup(retnotice->z_class);
812      zw.inst  = owl_strdup(retnotice->z_class_inst);
813      realm = strchr(retnotice->z_recipient, '@');
814      if(realm) {
815        zw.realm = owl_strdup(realm + 1);
816      } else {
817        zw.realm = owl_strdup(owl_zephyr_get_realm());
818      }
819      zw.opcode = owl_strdup(retnotice->z_opcode);
820      zw.zsig   = owl_strdup("");
821      owl_list_create(&(zw.recips));
822      owl_list_append_element(&(zw.recips), owl_strdup(tmp));
823
824      owl_log_outgoing_zephyr_error(&zw, buff);
825
826      owl_zwrite_free(&zw);
[1c6c4d3]827      owl_free(tmp);
[7d4fbcd]828    }
829  } else {
[ec6ff52]830    owl_function_error("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]831  }
832}
[09489b89]833#else
[1077891a]834void owl_zephyr_handle_ack(const void *retnotice)
[09489b89]835{
836}
[be0a79f]837#endif
[7d4fbcd]838
[be0a79f]839#ifdef HAVE_LIBZEPHYR
[1077891a]840int owl_zephyr_notice_is_ack(const ZNotice_t *n)
[31e48a3]841{
[7d4fbcd]842  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
843    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
844    return(1);
845  }
846  return(0);
847}
[09489b89]848#else
[1077891a]849int owl_zephyr_notice_is_ack(const void *n)
[09489b89]850{
851  return(0);
852}
[be0a79f]853#endif
[7d4fbcd]854 
[c08c70a]855void owl_zephyr_zaway(const owl_message *m)
[31e48a3]856{
[be0a79f]857#ifdef HAVE_LIBZEPHYR
[7c8060d0]858  char *tmpbuff, *myuser, *to;
[15b34fd]859  owl_message *mout;
[7d4fbcd]860 
[aa2f6364]861  /* bail if it doesn't look like a message we should reply to.  Some
[2de4f20]862   * of this defined by the way zaway(1) works
863   */
[7d4fbcd]864  if (strcasecmp(owl_message_get_class(m), "message")) return;
865  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
866  if (!strcasecmp(owl_message_get_sender(m), "")) return;
867  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
868  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
[d023c25]869  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
[7d4fbcd]870  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
[9854278]871  if (owl_message_get_attribute_value(m, "isauto")) return;
[7d4fbcd]872
[7c8060d0]873  if (owl_global_is_smartstrip(&g)) {
[e3d9c77]874    to=owl_zephyr_smartstripped_user(owl_message_get_sender(m));
[7c8060d0]875  } else {
876    to=owl_strdup(owl_message_get_sender(m));
877  }
878
[7d4fbcd]879  send_zephyr("",
880              "Automated reply:",
881              owl_message_get_class(m),
882              owl_message_get_instance(m),
[7c8060d0]883              to,
[7d4fbcd]884              owl_global_get_zaway_msg(&g));
885
[7c8060d0]886  myuser=short_zuser(to);
[aa2f6364]887  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
888    tmpbuff = owl_sprintf("zwrite %s", myuser);
889  } else {
890    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
891  }
892  owl_free(myuser);
[7c8060d0]893  owl_free(to);
[aa2f6364]894
[7d4fbcd]895  /* display the message as an admin message in the receive window */
[15b34fd]896  mout=owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
[13a3c1db]897  owl_global_messagequeue_addmsg(&g, mout);
[7d4fbcd]898  owl_free(tmpbuff);
[be0a79f]899#endif
[7d4fbcd]900}
901
[be0a79f]902#ifdef HAVE_LIBZEPHYR
[31e48a3]903void owl_zephyr_hackaway_cr(ZNotice_t *n)
904{
[7d4fbcd]905  /* replace \r's with ' '.  Gross-ish */
906  int i;
907
908  for (i=0; i<n->z_message_len; i++) {
909    if (n->z_message[i]=='\r') {
910      n->z_message[i]=' ';
911    }
912  }
913}
[be0a79f]914#endif
[7d4fbcd]915
[dca3b27]916char *owl_zephyr_zlocate(const char *user, int auth)
[31e48a3]917{
[be0a79f]918#ifdef HAVE_LIBZEPHYR
[7d4fbcd]919  int ret, numlocs;
920  int one = 1;
921  ZLocations_t locations;
922  char *myuser;
[dca3b27]923  char *p, *result;
924
[8262340]925  ZResetAuthentication();
[dca3b27]926  ret = ZLocateUser(zstr(user), &numlocs, auth ? ZAUTH : ZNOAUTH);
927  if (ret != ZERR_NONE)
928    return owl_sprintf("Error locating user %s: %s\n",
929                       user, error_message(ret));
930
931  myuser = short_zuser(user);
932  if (numlocs == 0) {
933    result = owl_sprintf("%s: Hidden or not logged in\n", myuser);
934  } else {
935    result = owl_strdup("");
936    for (; numlocs; numlocs--) {
937      ZGetLocations(&locations, &one);
938      p = owl_sprintf("%s%s: %s\t%s\t%s\n",
939                          result, myuser,
940                          locations.host ? locations.host : "?",
941                          locations.tty ? locations.tty : "?",
942                          locations.time ? locations.time : "?");
943      owl_free(result);
944      result = p;
945    }
[7d4fbcd]946  }
947
[dca3b27]948  return result;
949#else
950  return owl_strdup("");
[be0a79f]951#endif
[7d4fbcd]952}
[bde7714]953
[e19eb97]954void owl_zephyr_addsub(const char *filename, const char *class, const char *inst, const char *recip)
[31e48a3]955{
[be0a79f]956#ifdef HAVE_LIBZEPHYR
[b7ee89b]957  char *line, *subsfile, *s = NULL;
[bde7714]958  FILE *file;
[b7ee89b]959  int duplicate = 0;
[bde7714]960
[b7ee89b]961  line = owl_zephyr_makesubline(class, inst, recip);
[6ea3890]962  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
[bde7714]963
[74037d9]964  /* if the file already exists, check to see if the sub is already there */
[b7ee89b]965  file = fopen(subsfile, "r");
[74037d9]966  if (file) {
[b7ee89b]967    while (owl_getline(&s, file)) {
968      if (strcasecmp(s, line) == 0) {
[74037d9]969        owl_function_error("Subscription already present in %s", subsfile);
[b7ee89b]970        duplicate++;
[74037d9]971      }
[bde7714]972    }
[99dabee]973    fclose(file);
[b7ee89b]974    owl_free(s);
[bde7714]975  }
976
[b7ee89b]977  if (!duplicate) {
978    file = fopen(subsfile, "a");
979    if (file) {
980      fputs(line, file);
981      fclose(file);
982      owl_function_makemsg("Subscription added");
983    } else {
984      owl_function_error("Error opening file %s for writing", subsfile);
985    }
[bde7714]986  }
[b7ee89b]987
[bde7714]988  owl_free(line);
[be0a79f]989#endif
[bde7714]990}
991
[e19eb97]992void owl_zephyr_delsub(const char *filename, const char *class, const char *inst, const char *recip)
[31e48a3]993{
[be0a79f]994#ifdef HAVE_LIBZEPHYR
[b2a91b6]995  char *line, *subsfile;
[da60ba9]996  int linesdeleted;
[bde7714]997 
998  line=owl_zephyr_makesubline(class, inst, recip);
[b2a91b6]999  line[strlen(line)-1]='\0';
[bde7714]1000
[6ea3890]1001  subsfile = owl_zephyr_dotfile(".zephyr.subs", filename);
[b2a91b6]1002 
[da60ba9]1003  linesdeleted = owl_util_file_deleteline(subsfile, line, 1);
1004  if (linesdeleted > 0) {
1005    owl_function_makemsg("Subscription removed");
1006  } else {
1007    owl_function_error("No subscription present in %s", subsfile);
1008  }
[b2a91b6]1009  owl_free(subsfile);
[bde7714]1010  owl_free(line);
[be0a79f]1011#endif
[bde7714]1012}
1013
[b2a91b6]1014/* caller must free the return */
[e19eb97]1015char *owl_zephyr_makesubline(const char *class, const char *inst, const char *recip)
[31e48a3]1016{
[36486be]1017  return owl_sprintf("%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
[bde7714]1018}
[31e48a3]1019
1020
1021void owl_zephyr_zlog_in(void)
1022{
[be0a79f]1023#ifdef HAVE_LIBZEPHYR
[e19eb97]1024  const char *exposure, *eset;
[31e48a3]1025  int ret;
1026
1027  ZResetAuthentication();
1028   
1029  eset=EXPOSE_REALMVIS;
[712caac]1030  exposure=ZGetVariable(zstr("exposure"));
[31e48a3]1031  if (exposure==NULL) {
1032    eset=EXPOSE_REALMVIS;
1033  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
1034    eset = EXPOSE_NONE;
1035  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
1036    eset = EXPOSE_OPSTAFF;
1037  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
1038    eset = EXPOSE_REALMVIS;
1039  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
1040    eset = EXPOSE_REALMANN;
1041  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
1042    eset = EXPOSE_NETVIS;
1043  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
1044    eset = EXPOSE_NETANN;
1045  }
1046   
[712caac]1047  ret=ZSetLocation(zstr(eset));
[31e48a3]1048  if (ret != ZERR_NONE) {
1049    /*
[e440602]1050      owl_function_makemsg("Error setting location: %s", error_message(ret));
[31e48a3]1051    */
1052  }
[be0a79f]1053#endif
[31e48a3]1054}
1055
1056void owl_zephyr_zlog_out(void)
1057{
[be0a79f]1058#ifdef HAVE_LIBZEPHYR
[31e48a3]1059  int ret;
1060
1061  ZResetAuthentication();
1062  ret=ZUnsetLocation();
1063  if (ret != ZERR_NONE) {
1064    /*
[e440602]1065      owl_function_makemsg("Error unsetting location: %s", error_message(ret));
[31e48a3]1066    */
1067  }
[be0a79f]1068#endif
[31e48a3]1069}
1070
[e19eb97]1071void owl_zephyr_addbuddy(const char *name)
[65ad073]1072{
1073  char *filename;
1074  FILE *file;
1075 
[6ea3890]1076  filename = owl_zephyr_dotfile(".anyone", NULL);
1077  file = fopen(filename, "a");
[65ad073]1078  owl_free(filename);
1079  if (!file) {
[ec6ff52]1080    owl_function_error("Error opening zephyr buddy file for append");
[65ad073]1081    return;
1082  }
1083  fprintf(file, "%s\n", name);
1084  fclose(file);
1085}
1086
[e19eb97]1087void owl_zephyr_delbuddy(const char *name)
[65ad073]1088{
1089  char *filename;
1090
[6ea3890]1091  filename = owl_zephyr_dotfile(".anyone", NULL);
[65ad073]1092  owl_util_file_deleteline(filename, name, 0);
1093  owl_free(filename);
1094}
[9381782]1095
1096/* return auth string */
[09489b89]1097#ifdef HAVE_LIBZEPHYR
[1077891a]1098const char *owl_zephyr_get_authstr(const ZNotice_t *n)
[9381782]1099{
1100
[f12d199]1101  if (!n) return("UNKNOWN");
1102
1103  if (n->z_auth == ZAUTH_FAILED) {
[9381782]1104    return ("FAILED");
[f12d199]1105  } else if (n->z_auth == ZAUTH_NO) {
[9381782]1106    return ("NO");
[f12d199]1107  } else if (n->z_auth == ZAUTH_YES) {
[9381782]1108    return ("YES");
1109  } else {
1110    return ("UNKNOWN");
[f12d199]1111  }           
[9381782]1112}
[09489b89]1113#else
[1077891a]1114const char *owl_zephyr_get_authstr(const void *n)
[09489b89]1115{
1116  return("");
1117}
1118#endif
[9381782]1119
[2de4f20]1120/* Returns a buffer of subscriptions or an error message.  Caller must
1121 * free the return.
[09489b89]1122 */
[c79a047]1123char *owl_zephyr_getsubs(void)
[09489b89]1124{
1125#ifdef HAVE_LIBZEPHYR
1126  int ret, num, i, one;
[ddacb9c]1127  int buffsize;
[09489b89]1128  ZSubscription_t sub;
[ddacb9c]1129  char *out;
[09489b89]1130  one=1;
1131
1132  ret=ZRetrieveSubscriptions(0, &num);
1133  if (ret==ZERR_TOOMANYSUBS) {
[c8735aa]1134    return(owl_strdup("Zephyr: too many subscriptions\n"));
[ddacb9c]1135  } else if (ret || (num <= 0)) {
[c8735aa]1136    return(owl_strdup("Zephyr: error retriving subscriptions\n"));
[09489b89]1137  }
1138
[ddacb9c]1139  buffsize = (num + 1) * 50;
1140  out=owl_malloc(buffsize);
[09489b89]1141  strcpy(out, "");
1142  for (i=0; i<num; i++) {
1143    if ((ret = ZGetSubscriptions(&sub, &one)) != ZERR_NONE) {
1144      owl_free(out);
1145      ZFlushSubscriptions();
1146      out=owl_strdup("Error while getting subscriptions\n");
1147      return(out);
1148    } else {
[ddacb9c]1149      int tmpbufflen;
1150      char *tmpbuff;
1151      tmpbuff = owl_sprintf("<%s,%s,%s>\n%s", sub.zsub_class, sub.zsub_classinst, sub.zsub_recipient, out);
1152      tmpbufflen = strlen(tmpbuff) + 1;
1153      if (tmpbufflen > buffsize) {
1154        char *out2;
1155        buffsize = tmpbufflen * 2;
1156        out2 = owl_realloc(out, buffsize);
1157        if (out2 == NULL) {
1158          owl_free(out);
1159          owl_free(tmpbuff);
1160          ZFlushSubscriptions();
1161          out=owl_strdup("Realloc error while getting subscriptions\n");
1162          return(out);   
1163        }
1164        out = out2;
1165      }
[09489b89]1166      strcpy(out, tmpbuff);
[ddacb9c]1167      owl_free(tmpbuff);
[09489b89]1168    }
1169  }
1170
1171  ZFlushSubscriptions();
1172  return(out);
1173#else
[12c35df]1174  return(owl_strdup("Zephyr not available"));
[09489b89]1175#endif
1176}
1177
[e19eb97]1178const char *owl_zephyr_get_variable(const char *var)
[09489b89]1179{
1180#ifdef HAVE_LIBZEPHYR
[712caac]1181  return(ZGetVariable(zstr(var)));
[09489b89]1182#else
1183  return("");
1184#endif
1185}
1186
[e19eb97]1187void owl_zephyr_set_locationinfo(const char *host, const char *val)
[09489b89]1188{
1189#ifdef HAVE_LIBZEPHYR
[712caac]1190  ZInitLocationInfo(zstr(host), zstr(val));
[09489b89]1191#endif
1192}
1193 
[e3d9c77]1194/* Strip a local realm fron the zephyr user name.
1195 * The caller must free the return
1196 */
[e19eb97]1197char *short_zuser(const char *in)
[e3d9c77]1198{
1199  char *out, *ptr;
1200
1201  out=owl_strdup(in);
1202  ptr=strchr(out, '@');
1203  if (ptr) {
1204    if (!strcasecmp(ptr+1, owl_zephyr_get_realm())) {
1205      *ptr='\0';
1206    }
1207  }
1208  return(out);
1209}
1210
1211/* Append a local realm to the zephyr user name if necessary.
1212 * The caller must free the return.
1213 */
[e19eb97]1214char *long_zuser(const char *in)
[e3d9c77]1215{
[1971b59]1216  if (strchr(in, '@')) {
1217    return(owl_strdup(in));
[e3d9c77]1218  }
[1971b59]1219  return(owl_sprintf("%s@%s", in, owl_zephyr_get_realm()));
[e3d9c77]1220}
1221
1222/* strip out the instance from a zsender's principal.  Preserves the
1223 * realm if present.  daemon.webzephyr is a special case.  The
1224 * caller must free the return
1225 */
[e19eb97]1226char *owl_zephyr_smartstripped_user(const char *in)
[e3d9c77]1227{
1228  char *ptr, *realm, *out;
1229
1230  out=owl_strdup(in);
1231
1232  /* bail immeaditly if we don't have to do any work */
[fa4562c]1233  ptr=strchr(out, '.');
1234  if (!strchr(out, '/') && !ptr) {
[e3d9c77]1235    /* no '/' and no '.' */
1236    return(out);
1237  }
[fa4562c]1238  if (ptr && strchr(out, '@') && (ptr > strchr(out, '@'))) {
[e3d9c77]1239    /* There's a '.' but it's in the realm */
1240    return(out);
1241  }
[fa4562c]1242  if (!strncasecmp(out, OWL_WEBZEPHYR_PRINCIPAL, strlen(OWL_WEBZEPHYR_PRINCIPAL))) {
[e3d9c77]1243    return(out);
1244  }
1245
[fa4562c]1246  /* remove the realm from out, but hold on to it */
[e3d9c77]1247  realm=strchr(out, '@');
1248  if (realm) realm[0]='\0';
1249
1250  /* strip */
1251  ptr=strchr(out, '.');
1252  if (!ptr) ptr=strchr(out, '/');
1253  ptr[0]='\0';
1254
1255  /* reattach the realm if we had one */
1256  if (realm) {
1257    strcat(out, "@");
1258    strcat(out, realm+1);
1259  }
1260
1261  return(out);
1262}
[5a95b69]1263
1264/* read the list of users in 'filename' as a .anyone file, and put the
1265 * names of the zephyr users in the list 'in'.  If 'filename' is NULL,
1266 * use the default .anyone file in the users home directory.  Returns
1267 * -1 on failure, 0 on success.
1268 */
[e19eb97]1269int owl_zephyr_get_anyone_list(owl_list *in, const char *filename)
[5a95b69]1270{
1271#ifdef HAVE_LIBZEPHYR
[b7ee89b]1272  char *ourfile, *tmp, *s = NULL;
[5a95b69]1273  FILE *f;
1274
[6ea3890]1275  ourfile = owl_zephyr_dotfile(".anyone", filename);
[b7ee89b]1276
1277  f = fopen(ourfile, "r");
[5a95b69]1278  if (!f) {
1279    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
1280    owl_free(ourfile);
[b7ee89b]1281    return -1;
[5a95b69]1282  }
[b7ee89b]1283  owl_free(ourfile);
[5a95b69]1284
[b7ee89b]1285  while (owl_getline_chomp(&s, f)) {
[5a95b69]1286    /* ignore comments, blank lines etc. */
[b7ee89b]1287    if (s[0] == '#' || s[0] == '\0')
1288      continue;
1289
1290    /* ignore from # on */
1291    tmp = strchr(s, '#');
1292    if (tmp)
1293      tmp[0] = '\0';
1294
1295    /* ignore from SPC */
1296    tmp = strchr(s, ' ');
1297    if (tmp)
1298      tmp[0] = '\0';
1299
1300    owl_list_append_element(in, long_zuser(s));
[5a95b69]1301  }
1302  fclose(f);
[b7ee89b]1303  return 0;
[5a95b69]1304#else
[b7ee89b]1305  return -1;
[5a95b69]1306#endif
1307}
[13a3c1db]1308
[12e291a]1309/*
1310 * Process zephyrgrams from libzephyr's queue. To prevent starvation,
1311 * process a maximum of OWL_MAX_ZEPHYRGRAMS_TO_PROCESS.
1312 *
1313 * Returns the number of zephyrgrams processed.
1314 */
1315
1316#define OWL_MAX_ZEPHYRGRAMS_TO_PROCESS 20
1317
1318int _owl_zephyr_process_events(void) /* noproto */
1319{
[13a3c1db]1320  int zpendcount=0;
[12e291a]1321#ifdef HAVE_LIBZEPHYR
[13a3c1db]1322  ZNotice_t notice;
1323  owl_message *m=NULL;
1324
[12e291a]1325  while(owl_zephyr_zpending() && zpendcount < OWL_MAX_ZEPHYRGRAMS_TO_PROCESS) {
[13a3c1db]1326    if (owl_zephyr_zpending()) {
[cc1a6d4]1327      ZReceiveNotice(&notice, NULL);
[13a3c1db]1328      zpendcount++;
1329
1330      /* is this an ack from a zephyr we sent? */
1331      if (owl_zephyr_notice_is_ack(&notice)) {
1332        owl_zephyr_handle_ack(&notice);
1333        continue;
1334      }
1335
1336      /* if it's a ping and we're not viewing pings then skip it */
1337      if (!owl_global_is_rxping(&g) && !strcasecmp(notice.z_opcode, "ping")) {
1338        continue;
1339      }
1340
1341      /* create the new message */
1342      m=owl_malloc(sizeof(owl_message));
1343      owl_message_create_from_znotice(m, &notice);
1344
1345      owl_global_messagequeue_addmsg(&g, m);
1346    }
1347  }
[12e291a]1348#endif
1349  return zpendcount;
[13a3c1db]1350}
1351
[12e291a]1352void owl_zephyr_process_events(owl_dispatch *d)
1353{
1354  _owl_zephyr_process_events();
1355}
1356
1357int owl_zephyr_pre_select_action(owl_ps_action *a, void *p)
1358{
1359  return _owl_zephyr_process_events();
[13a3c1db]1360}
Note: See TracBrowser for help on using the repository browser.