source: zephyr.c @ 488ebf6

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