source: zephyr.c @ 7c8060d0

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