source: zephyr.c @ 65ad073

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 65ad073 was 65ad073, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
'addbuddy zephyr <user>' and 'delbuddy zephyr <user>' now work.
  • Property mode set to 100644
File size: 15.2 KB
Line 
1#include <stdlib.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/wait.h>
5#include <string.h>
6#include <zephyr/zephyr.h>
7#include <com_err.h>
8#include "owl.h"
9
10static const char fileIdent[] = "$Id$";
11
12Code_t ZResetAuthentication();
13
14int owl_zephyr_loadsubs(char *filename)
15{
16  /* return 0  on success
17   *        -1 on file error
18   *        -2 on subscription error
19   */
20  FILE *file;
21  char *tmp, *start;
22  char buffer[1024], subsfile[1024];
23  ZSubscription_t subs[3001];
24  int count, ret, i;
25
26  ret=0;
27 
28  if (filename==NULL) {
29    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
30  } else {
31    strcpy(subsfile, filename);
32  }
33
34  ZResetAuthentication();
35  /* need to redo this to do chunks, not just bail after 3000 */
36  count=0;
37  file=fopen(subsfile, "r");
38  if (file) {
39    while ( fgets(buffer, 1024, file)!=NULL ) {
40      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
41
42      if (buffer[0]=='-') {
43        start=buffer+1;
44      } else {
45        start=buffer;
46      }
47     
48      if (count >= 3000) break; /* also tell the user */
49
50      /* add it to the list of subs */
51      if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue;
52      subs[count].zsub_class=owl_strdup(tmp);
53      if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue;
54      subs[count].zsub_classinst=owl_strdup(tmp);
55      if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue;
56      subs[count].zsub_recipient=owl_strdup(tmp);
57
58      /* if it started with '-' then add it to the global punt list */
59      if (buffer[0]=='-') {
60        owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
61      }
62     
63      count++;
64    }
65    fclose(file);
66  } else {
67    count=0;
68    ret=-1;
69  }
70
71  /* sub with defaults */
72  if (ZSubscribeTo(subs,count,0) != ZERR_NONE) {
73    fprintf(stderr, "Error subbing\n");
74    ret=-2;
75  }
76
77  /* free stuff */
78  for (i=0; i<count; i++) {
79    owl_free(subs[i].zsub_class);
80    owl_free(subs[i].zsub_classinst);
81    owl_free(subs[i].zsub_recipient);
82  }
83
84  return(ret);
85}
86
87int owl_zephyr_loadloginsubs(char *filename)
88{
89  FILE *file;
90  ZSubscription_t subs[3001];
91  char subsfile[1024], buffer[1024];
92  int count, ret, i;
93
94  ret=0;
95
96  if (filename==NULL) {
97    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
98  } else {
99    strcpy(subsfile, filename);
100  }
101
102  ZResetAuthentication();
103  /* need to redo this to do chunks, not just bag out after 3000 */
104  count=0;
105  file=fopen(subsfile, "r");
106  if (file) {
107    while ( fgets(buffer, 1024, file)!=NULL ) {
108      if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
109     
110      if (count >= 3000) break; /* also tell the user */
111
112      buffer[strlen(buffer)-1]='\0';
113      subs[count].zsub_class="login";
114      subs[count].zsub_recipient="*";
115      if (strchr(buffer, '@')) {
116        subs[count].zsub_classinst=owl_strdup(buffer);
117      } else {
118        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
119      }
120
121      count++;
122    }
123    fclose(file);
124  } else {
125    count=0;
126    ret=-1;
127  }
128
129  /* sub with defaults */
130  if (ZSubscribeToSansDefaults(subs,count,0) != ZERR_NONE) {
131    fprintf(stderr, "Error subbing\n");
132    ret=-2;
133  }
134
135  /* free stuff */
136  for (i=0; i<count; i++) {
137    owl_free(subs[i].zsub_classinst);
138  }
139
140  return(ret);
141}
142
143void unsuball()
144{
145  int ret;
146
147  ZResetAuthentication();
148  ret=ZCancelSubscriptions(0);
149  if (ret != ZERR_NONE) {
150    com_err("owl",ret,"while unsubscribing");
151  }
152}
153
154int owl_zephyr_sub(char *class, char *inst, char *recip)
155{
156  ZSubscription_t subs[5];
157  int ret;
158
159  subs[0].zsub_class=class;
160  subs[0].zsub_classinst=inst;
161  subs[0].zsub_recipient=recip;
162
163  ZResetAuthentication();
164  if (ZSubscribeTo(subs,1,0) != ZERR_NONE) {
165    fprintf(stderr, "Error subbing\n");
166    ret=-2;
167  }
168  return(0);
169}
170
171
172int owl_zephyr_unsub(char *class, char *inst, char *recip)
173{
174  ZSubscription_t subs[5];
175  int ret;
176
177  subs[0].zsub_class=class;
178  subs[0].zsub_classinst=inst;
179  subs[0].zsub_recipient=recip;
180
181  ZResetAuthentication();
182  if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) {
183    fprintf(stderr, "Error unsubbing\n");
184    ret=-2;
185  }
186  return(0);
187}
188
189
190char *owl_zephyr_get_field(ZNotice_t *n, int j, int *k)
191{
192  /* return a pointer to the Jth field, place the length in k.  If the
193     field doesn't exist return an emtpy string */
194  int i, count, save;
195
196  count=save=0;
197  for (i=0; i<n->z_message_len; i++) {
198    if (n->z_message[i]=='\0') {
199      count++;
200      if (count==j) {
201        /* just found the end of the field we're looking for */
202        *k=i-save;
203        return(n->z_message+save);
204      } else {
205        save=i+1;
206      }
207    }
208  }
209  /* catch the last field */
210  if (count==j-1) {
211    *k=n->z_message_len-save;
212    return(n->z_message+save);
213  }
214 
215  *k=0;
216  return("");
217}
218
219
220int owl_zephyr_get_num_fields(ZNotice_t *n)
221{
222  int i, fields;
223
224  fields=1;
225  for (i=0; i<n->z_message_len; i++) {
226    if (n->z_message[i]=='\0') fields++;
227  }
228 
229  return(fields);
230}
231
232
233char *owl_zephyr_get_message(ZNotice_t *n, int *k)
234{
235  /* return a pointer to the message, place the message length in k */
236  if (!strcasecmp(n->z_opcode, "ping")) {
237    *k=0;
238    return("");
239  }
240
241  return(owl_zephyr_get_field(n, 2, k));
242}
243
244
245char *owl_zephyr_get_zsig(ZNotice_t *n, int *k)
246{
247  /* return a pointer to the zsig if there is one */
248
249  if (n->z_message_len==0) {
250    *k=0;
251    return("");
252  }
253  *k=strlen(n->z_message);
254  return(n->z_message);
255}
256
257
258int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message)
259{
260  int ret;
261  ZNotice_t notice;
262   
263  memset(&notice, 0, sizeof(notice));
264
265  ZResetAuthentication();
266 
267  notice.z_kind=ACKED;
268  notice.z_port=0;
269  notice.z_class=class;
270  notice.z_class_inst=instance;
271  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
272    notice.z_recipient="";
273  } else {
274    notice.z_recipient=recipient;
275  }
276  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
277  notice.z_sender=NULL;
278  if (opcode) notice.z_opcode=opcode;
279
280  notice.z_message_len=strlen(zsig)+1+strlen(message);
281  notice.z_message=owl_malloc(notice.z_message_len+10);
282  strcpy(notice.z_message, zsig);
283  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
284
285  /* ret=ZSendNotice(&notice, ZAUTH); */
286  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
287 
288  /* free then check the return */
289  owl_free(notice.z_message);
290  ZFreeNotice(&notice);
291  if (ret!=ZERR_NONE) {
292    owl_function_makemsg("Error sending zephyr");
293    return(ret);
294  }
295
296  return(0);
297}
298
299Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait)
300{
301  return(ZSendPacket(buf, len, 0));
302}
303
304void send_ping(char *to)
305{
306  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
307}
308
309void owl_zephyr_handle_ack(ZNotice_t *retnotice)
310{
311  char *tmp;
312 
313  /* if it's an HMACK ignore it */
314  if (retnotice->z_kind == HMACK) return;
315
316  if (retnotice->z_kind == SERVNAK) {
317    owl_function_makemsg("Authorization failure sending zephyr");
318  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
319    owl_function_makemsg("Detected server failure while receiving acknowledgement");
320  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
321    if (!strcasecmp(retnotice->z_opcode, "ping")) {
322      return;
323    } else if (!strcasecmp(retnotice->z_class, "message") &&
324               !strcasecmp(retnotice->z_class_inst, "personal")) {
325      tmp=short_zuser(retnotice->z_recipient);
326      owl_function_makemsg("Message sent to %s.", tmp);
327      free(tmp);
328    } else {
329      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
330    }
331  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
332    if (strcasecmp(retnotice->z_class, "message")) {
333      owl_function_makemsg("Not logged in or not subscribing to class %s, instance %s",
334                           retnotice->z_class, retnotice->z_class_inst);
335    } else {
336      tmp = short_zuser(retnotice->z_recipient);
337      owl_function_makemsg("%s: Not logged in or subscribing to messages.", 
338                           tmp);
339      owl_free(tmp);
340    }
341  } else {
342    owl_function_makemsg("Internal error on ack (%s)", retnotice->z_message);
343  }
344}
345
346int owl_zephyr_notice_is_ack(ZNotice_t *n)
347{
348  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
349    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
350    return(1);
351  }
352  return(0);
353}
354 
355void owl_zephyr_zaway(owl_message *m)
356{
357  char *tmpbuff, *myuser, *to;
358 
359  /* bail if it doesn't look like a message we should reply to.  Some
360     of this defined by the way zaway(1) works */
361  if (strcasecmp(owl_message_get_class(m), "message")) return;
362  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
363  if (!strcasecmp(owl_message_get_sender(m), "")) return;
364  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
365  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
366  if (!strcasecmp(owl_message_get_zsig(m), "Automated reply:")) return;
367  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
368
369  if (owl_global_is_smartstrip(&g)) {
370    to=owl_util_smartstripped_user(owl_message_get_sender(m));
371  } else {
372    to=owl_strdup(owl_message_get_sender(m));
373  }
374
375  send_zephyr("",
376              "Automated reply:",
377              owl_message_get_class(m),
378              owl_message_get_instance(m),
379              to,
380              owl_global_get_zaway_msg(&g));
381
382  myuser=short_zuser(to);
383  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
384    tmpbuff = owl_sprintf("zwrite %s", myuser);
385  } else {
386    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
387  }
388  owl_free(myuser);
389  owl_free(to);
390
391  /* display the message as an admin message in the receive window */
392  owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
393  owl_free(tmpbuff);
394}
395
396
397void owl_zephyr_hackaway_cr(ZNotice_t *n)
398{
399  /* replace \r's with ' '.  Gross-ish */
400  int i;
401
402  for (i=0; i<n->z_message_len; i++) {
403    if (n->z_message[i]=='\r') {
404      n->z_message[i]=' ';
405    }
406  }
407}
408
409void owl_zephyr_zlocate(char *user, char *out, int auth)
410{
411  int ret, numlocs;
412  int one = 1;
413  ZLocations_t locations;
414  char *myuser;
415 
416  strcpy(out, "");
417  ZResetAuthentication();
418  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
419  if (ret != ZERR_NONE) {
420    owl_function_makemsg("Error locating user");
421  }
422
423  if (numlocs==0) {
424    myuser=short_zuser(user);
425    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
426    owl_free(myuser);
427    return;
428  }
429   
430  for (;numlocs;numlocs--) {
431    ZGetLocations(&locations,&one);
432    myuser=short_zuser(user);
433    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
434    owl_free(myuser);
435  }
436}
437
438void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip)
439{
440  char *line, subsfile[LINE], buff[LINE];
441  FILE *file;
442
443  line=owl_zephyr_makesubline(class, inst, recip);
444
445  if (filename==NULL) {
446    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
447  } else {
448    strcpy(subsfile, filename);
449  }
450
451  /* first check if it exists already */
452  file=fopen(subsfile, "r");
453  if (!file) {
454    owl_function_makemsg("Error opening file %s", subsfile);
455    owl_free(line);
456    return;
457  }
458  while (fgets(buff, LINE, file)!=NULL) {
459    if (!strcasecmp(buff, line)) {
460      owl_function_makemsg("Subscription already present in %s", subsfile);
461      owl_free(line);
462      return;
463    }
464  }
465
466  /* if we get here then we didn't find it */
467  fclose(file);
468  file=fopen(subsfile, "a");
469  if (!file) {
470    owl_function_makemsg("Error opening file %s for writing", subsfile);
471    owl_free(line);
472    return;
473  }
474  fputs(line, file);
475  fclose(file);
476  owl_function_makemsg("Subscription added");
477 
478  owl_free(line);
479}
480
481void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip)
482{
483  char *line, subsfile[LINE], buff[LINE], *text;
484  char backupfilename[LINE];
485  FILE *file, *backupfile;
486  int size;
487 
488  line=owl_zephyr_makesubline(class, inst, recip);
489
490  /* open the subsfile for reading */
491  if (filename==NULL) {
492    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
493  } else {
494    strcpy(subsfile, filename);
495  }
496  file=fopen(subsfile, "r");
497  if (!file) {
498    owl_function_makemsg("Error opening file %s", subsfile);
499    owl_free(line);
500    return;
501  }
502
503  /* open the backup file for writing */
504  sprintf(backupfilename, "%s.backup", subsfile);
505  backupfile=fopen(backupfilename, "w");
506  if (!backupfile) {
507    owl_function_makemsg("Error opening file %s for writing", backupfilename);
508    owl_free(line);
509    return;
510  }
511
512  /* we'll read the entire file into memory, minus the line we don't want and
513   * and at the same time create a backup file */
514  text=owl_malloc(LINE);
515  strcpy(text, "");
516  size=LINE;
517  while (fgets(buff, LINE, file)!=NULL) {
518    /* if we don't match the line, add to text */
519    if (strcasecmp(buff, line)) {
520      size+=LINE;
521      text=owl_realloc(text, size);
522      strcat(text, buff);
523    }
524
525    /* write to backupfile */
526    fputs(buff, backupfile);
527  }
528  fclose(backupfile);
529  fclose(file);
530
531  /* now open the original subs file for writing and write out the
532   * subs */
533  file=fopen(subsfile, "w");
534  if (!file) {
535    owl_function_makemsg("WARNING: Error opening %s to rewrite subscriptions.  Use %s to restore", subsfile, backupfilename);
536    owl_function_beep();
537    owl_free(line);
538    return;
539  }
540
541  fputs(text, file);
542  fclose(file);
543  owl_free(line);
544
545  owl_function_makemsg("Subscription removed");
546}
547
548char *owl_zephyr_makesubline(char *class, char *inst, char *recip)
549{
550  /* caller must free the return */
551  char *out;
552
553  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
554  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
555  return(out);
556}
557
558
559void owl_zephyr_zlog_in(void)
560{
561  char *exposure, *eset;
562  int ret;
563
564  ZResetAuthentication();
565   
566  eset=EXPOSE_REALMVIS;
567  exposure=ZGetVariable("exposure");
568  if (exposure==NULL) {
569    eset=EXPOSE_REALMVIS;
570  } else if (!strcasecmp(exposure,EXPOSE_NONE)) {
571    eset = EXPOSE_NONE;
572  } else if (!strcasecmp(exposure,EXPOSE_OPSTAFF)) {
573    eset = EXPOSE_OPSTAFF;
574  } else if (!strcasecmp(exposure,EXPOSE_REALMVIS)) {
575    eset = EXPOSE_REALMVIS;
576  } else if (!strcasecmp(exposure,EXPOSE_REALMANN)) {
577    eset = EXPOSE_REALMANN;
578  } else if (!strcasecmp(exposure,EXPOSE_NETVIS)) {
579    eset = EXPOSE_NETVIS;
580  } else if (!strcasecmp(exposure,EXPOSE_NETANN)) {
581    eset = EXPOSE_NETANN;
582  }
583   
584  ret=ZSetLocation(eset);
585  if (ret != ZERR_NONE) {
586    /*
587      char buff[LINE];
588      sprintf(buff, "Error setting location: %s", error_message(ret));
589      owl_function_makemsg(buff);
590    */
591  }
592}
593
594void owl_zephyr_zlog_out(void)
595{
596  int ret;
597
598  ZResetAuthentication();
599  ret=ZUnsetLocation();
600  if (ret != ZERR_NONE) {
601    /*
602      char buff[LINE];
603      sprintf(buff, "Error unsetting location: %s", error_message(ret));
604      owl_function_makemsg(buff);
605    */
606  }
607}
608
609void owl_zephyr_addbuddy(char *name)
610{
611  char *filename;
612  FILE *file;
613 
614  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
615  file=fopen(filename, "a");
616  owl_free(filename);
617  if (!file) {
618    owl_function_makemsg("Error opening zephyr buddy file for append");
619    return;
620  }
621  fprintf(file, "%s\n", name);
622  fclose(file);
623}
624
625void owl_zephyr_delbuddy(char *name)
626{
627  char *filename;
628
629  filename=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
630  owl_util_file_deleteline(filename, name, 0);
631  owl_free(filename);
632}
Note: See TracBrowser for help on using the repository browser.