source: zephyr.c @ 601a9e0

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 601a9e0 was bde7714, checked in by James M. Kretchmar <kretch@mit.edu>, 19 years ago
Started updating the intro document Small changes to help / about The 'subscribe' and 'unsubscribe' commands (and their aliases) update .zephyr.subs by default. If either is given the '-t' (for "temporary") option the .zephyr.subs will not be updated
  • Property mode set to 100644
File size: 13.0 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 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 (ZSubscribeTo(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
178void zlog_in() {
179  int ret;
180
181  ret=ZSetLocation(EXPOSE_NETVIS);
182  /* do something on failure */
183}
184
185void zlog_out() {
186  int ret;
187
188  ret=ZUnsetLocation();
189  /* do something on failure */
190}
191
192
193char *owl_zephyr_get_field(ZNotice_t *n, int j, int *k) {
194  /* return a pointer to the Jth field, place the length in k.  If the
195     field doesn't exist return an emtpy string */
196  int i, count, save;
197
198  count=save=0;
199  for (i=0; i<n->z_message_len; i++) {
200    if (n->z_message[i]=='\0') {
201      count++;
202      if (count==j) {
203        /* just found the end of the field we're looking for */
204        *k=i-save;
205        return(n->z_message+save);
206      } else {
207        save=i+1;
208      }
209    }
210  }
211  /* catch the last field */
212  if (count==j-1) {
213    *k=n->z_message_len-save;
214    return(n->z_message+save);
215  }
216 
217  *k=0;
218  return("");
219}
220
221
222int owl_zephyr_get_num_fields(ZNotice_t *n) {
223  int i, fields;
224
225  fields=1;
226  for (i=0; i<n->z_message_len; i++) {
227    if (n->z_message[i]=='\0') fields++;
228  }
229 
230  return(fields);
231}
232
233
234char *owl_zephyr_get_message(ZNotice_t *n, int *k) {
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  /* return a pointer to the zsig if there is one */
247
248  if (n->z_message_len==0) {
249    *k=0;
250    return("");
251  }
252  *k=strlen(n->z_message);
253  return(n->z_message);
254}
255
256
257int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message) {
258  int ret;
259  ZNotice_t notice;
260   
261  memset(&notice, 0, sizeof(notice));
262
263  notice.z_kind=ACKED;
264  notice.z_port=0;
265  notice.z_class=class;
266  notice.z_class_inst=instance;
267  if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) {
268    notice.z_recipient="";
269  } else {
270    notice.z_recipient=recipient;
271  }
272  notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2";
273  notice.z_sender=NULL;
274  if (opcode) notice.z_opcode=opcode;
275
276  notice.z_message_len=strlen(zsig)+1+strlen(message);
277  notice.z_message=owl_malloc(notice.z_message_len+10);
278  strcpy(notice.z_message, zsig);
279  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
280
281  /* ret=ZSendNotice(&notice, ZAUTH); */
282  ret=ZSrvSendNotice(&notice, ZAUTH, send_zephyr_helper);
283 
284  /* free then check the return */
285  owl_free(notice.z_message);
286  ZFreeNotice(&notice);
287  if (ret!=ZERR_NONE) {
288    owl_function_makemsg("Error sending zephyr");
289    return(ret);
290  }
291
292  return(0);
293}
294
295Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait) {
296  return(ZSendPacket(buf, len, 0));
297}
298
299void send_ping(char *to) {
300  send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, "");
301}
302
303void owl_zephyr_handle_ack(ZNotice_t *retnotice) {
304  char *tmp;
305 
306  /* if it's an HMACK ignore it */
307  if (retnotice->z_kind == HMACK) return;
308 
309  if (retnotice->z_kind == SERVNAK) {
310    owl_function_makemsg("Authorization failure sending zephyr");
311  } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) {
312    owl_function_makemsg("Detected server failure while receiving acknowledgement");
313  } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) {
314    if (!strcasecmp(retnotice->z_opcode, "ping")) {
315      return;
316    } else if (!strcasecmp(retnotice->z_class, "message") &&
317               !strcasecmp(retnotice->z_class_inst, "personal")) {
318      tmp=short_zuser(retnotice->z_recipient);
319      owl_function_makemsg("Message sent to %s.", tmp);
320      free(tmp);
321    } else {
322      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
323    }
324  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
325    if (strcasecmp(retnotice->z_class, "message")) {
326      owl_function_makemsg("Not logged in or not subscribing to class %s, instance %s",
327              retnotice->z_class, retnotice->z_class_inst);
328    } else {
329      tmp = short_zuser(retnotice->z_recipient);
330      owl_function_makemsg("%s: Not logged in or subscribing to messages.", 
331                           tmp);
332      owl_free(tmp);
333    }
334  } else {
335    owl_function_makemsg("Internal error on ack (%s)", retnotice->z_message);
336  }
337}
338
339int owl_zephyr_notice_is_ack(ZNotice_t *n) {
340  if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) {
341    if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0);
342    return(1);
343  }
344  return(0);
345}
346 
347void owl_zephyr_zaway(owl_message *m) {
348  char *tmpbuff;
349 
350  /* bail if it doesn't look like a message we should reply to */
351  if (strcasecmp(owl_message_get_class(m), "message")) return;
352  if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return;
353  if (!strcasecmp(owl_message_get_sender(m), "")) return;
354  if (!strcasecmp(owl_message_get_opcode(m), "ping")) return;
355  if (!strcasecmp(owl_message_get_opcode(m), "auto")) return;
356  if (!strcasecmp(owl_message_get_notice(m)->z_message, "Automated reply:")) return;
357  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
358  /* add one to not send to ourselves */
359
360  send_zephyr("",
361              "Automated reply:",
362              owl_message_get_class(m),
363              owl_message_get_instance(m),
364              owl_message_get_sender(m),
365              owl_global_get_zaway_msg(&g));
366
367  /* display the message as an admin message in the receive window */
368  tmpbuff = owl_sprintf("Message sent to %s", owl_message_get_sender(m));
369  owl_function_adminmsg(tmpbuff, owl_global_get_zaway_msg(&g));
370  owl_free(tmpbuff);
371}
372
373
374void owl_zephyr_hackaway_cr(ZNotice_t *n) {
375  /* replace \r's with ' '.  Gross-ish */
376  int i;
377
378  for (i=0; i<n->z_message_len; i++) {
379    if (n->z_message[i]=='\r') {
380      n->z_message[i]=' ';
381    }
382  }
383}
384
385void owl_zephyr_zlocate(char *user, char *out, int auth) {
386  int ret, numlocs;
387  int one = 1;
388  ZLocations_t locations;
389  char *myuser;
390 
391  strcpy(out, "");
392
393  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
394  if (ret != ZERR_NONE) {
395    owl_function_makemsg("Error locating user");
396  }
397
398  if (numlocs==0) {
399    strcpy(out, "Hidden or not logged-in\n");
400    return;
401  }
402   
403  for (;numlocs;numlocs--) {
404    ZGetLocations(&locations,&one);
405    myuser=short_zuser(user);
406    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
407    owl_free(myuser);
408  }
409}
410
411void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip) {
412  char *line, subsfile[LINE], buff[LINE];
413  FILE *file;
414
415  line=owl_zephyr_makesubline(class, inst, recip);
416
417  if (filename==NULL) {
418    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
419  } else {
420    strcpy(subsfile, filename);
421  }
422
423  /* first check if it exists already */
424  file=fopen(subsfile, "r");
425  if (!file) {
426    owl_function_makemsg("Error opening file %s", subsfile);
427    owl_free(line);
428    return;
429  }
430  while (fgets(buff, LINE, file)!=NULL) {
431    if (!strcasecmp(buff, line)) {
432      owl_function_makemsg("Subscription already present in %s", subsfile);
433      owl_free(line);
434      return;
435    }
436  }
437
438  /* if we get here then we didn't find it */
439  fclose(file);
440  file=fopen(subsfile, "a");
441  if (!file) {
442    owl_function_makemsg("Error opening file %s for writing", subsfile);
443    owl_free(line);
444    return;
445  }
446  fputs(line, file);
447  fclose(file);
448  owl_function_makemsg("Subscription added");
449 
450  owl_free(line);
451}
452
453void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip) {
454  char *line, subsfile[LINE], buff[LINE], *text;
455  char backupfilename[LINE];
456  FILE *file, *backupfile;
457  int size;
458 
459  line=owl_zephyr_makesubline(class, inst, recip);
460
461  /* open the subsfile for reading */
462  if (filename==NULL) {
463    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
464  } else {
465    strcpy(subsfile, filename);
466  }
467  file=fopen(subsfile, "r");
468  if (!file) {
469    owl_function_makemsg("Error opening file %s", subsfile);
470    owl_free(line);
471    return;
472  }
473
474  /* open the backup file for writing */
475  sprintf(backupfilename, "%s.backup", subsfile);
476  backupfile=fopen(backupfilename, "w");
477  if (!backupfile) {
478    owl_function_makemsg("Error opening file %s for writing", backupfilename);
479    owl_free(line);
480    return;
481  }
482
483  /* we'll read the entire file into memory, minus the line we don't want and
484   * and at the same time create a backup file */
485  text=malloc(LINE);
486  size=LINE;
487  while (fgets(buff, LINE, file)!=NULL) {
488    /* if we don't match the line, add to text */
489    if (strcasecmp(buff, line)) {
490      size+=LINE;
491      text=realloc(text, size);
492      strcat(text, buff);
493    }
494
495    /* write to backupfile */
496    fputs(buff, backupfile);
497  }
498  fclose(backupfile);
499  fclose(file);
500
501  /* now open the original subs file for writing and write out the
502   * subs */
503  file=fopen(subsfile, "w");
504  if (!file) {
505    owl_function_makemsg("WARNING: Error opening %s to rewrite subscriptions.  Use %s to restore", subsfile, backupfilename);
506    owl_function_beep();
507    owl_free(line);
508    return;
509  }
510
511  fputs(text, file);
512  fclose(file);
513  owl_free(line);
514
515  owl_function_makemsg("Subscription removed");
516}
517
518char *owl_zephyr_makesubline(char *class, char *inst, char *recip) {
519  /* caller must free the return */
520  char *out;
521
522  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
523  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
524  return(out);
525}
Note: See TracBrowser for help on using the repository browser.