source: zephyr.c @ 2c8d62e

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2c8d62e was 060b3b4, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Fixed bug in unsub command that could cause file corruption.
  • Property mode set to 100644
File size: 13.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
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
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;
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  send_zephyr("",
346              "Automated reply:",
347              owl_message_get_class(m),
348              owl_message_get_instance(m),
349              owl_message_get_sender(m),
350              owl_global_get_zaway_msg(&g));
351
352  myuser=short_zuser(owl_message_get_sender(m));
353  if (!strcasecmp(owl_message_get_instance(m), "personal")) {
354    tmpbuff = owl_sprintf("zwrite %s", myuser);
355  } else {
356    tmpbuff = owl_sprintf("zwrite -i %s %s", owl_message_get_instance(m), myuser);
357  }
358  owl_free(myuser);
359
360  /* display the message as an admin message in the receive window */
361  owl_function_make_outgoing_zephyr(owl_global_get_zaway_msg(&g), tmpbuff, "Automated reply:");
362  owl_free(tmpbuff);
363}
364
365
366void owl_zephyr_hackaway_cr(ZNotice_t *n) {
367  /* replace \r's with ' '.  Gross-ish */
368  int i;
369
370  for (i=0; i<n->z_message_len; i++) {
371    if (n->z_message[i]=='\r') {
372      n->z_message[i]=' ';
373    }
374  }
375}
376
377void owl_zephyr_zlocate(char *user, char *out, int auth) {
378  int ret, numlocs;
379  int one = 1;
380  ZLocations_t locations;
381  char *myuser;
382 
383  strcpy(out, "");
384
385  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
386  if (ret != ZERR_NONE) {
387    owl_function_makemsg("Error locating user");
388  }
389
390  if (numlocs==0) {
391    myuser=short_zuser(user);
392    sprintf(out, "%s: Hidden or not logged-in\n", myuser);
393    owl_free(myuser);
394    return;
395  }
396   
397  for (;numlocs;numlocs--) {
398    ZGetLocations(&locations,&one);
399    myuser=short_zuser(user);
400    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
401    owl_free(myuser);
402  }
403}
404
405void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip) {
406  char *line, subsfile[LINE], buff[LINE];
407  FILE *file;
408
409  line=owl_zephyr_makesubline(class, inst, recip);
410
411  if (filename==NULL) {
412    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
413  } else {
414    strcpy(subsfile, filename);
415  }
416
417  /* first check if it exists already */
418  file=fopen(subsfile, "r");
419  if (!file) {
420    owl_function_makemsg("Error opening file %s", subsfile);
421    owl_free(line);
422    return;
423  }
424  while (fgets(buff, LINE, file)!=NULL) {
425    if (!strcasecmp(buff, line)) {
426      owl_function_makemsg("Subscription already present in %s", subsfile);
427      owl_free(line);
428      return;
429    }
430  }
431
432  /* if we get here then we didn't find it */
433  fclose(file);
434  file=fopen(subsfile, "a");
435  if (!file) {
436    owl_function_makemsg("Error opening file %s for writing", subsfile);
437    owl_free(line);
438    return;
439  }
440  fputs(line, file);
441  fclose(file);
442  owl_function_makemsg("Subscription added");
443 
444  owl_free(line);
445}
446
447void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip) {
448  char *line, subsfile[LINE], buff[LINE], *text;
449  char backupfilename[LINE];
450  FILE *file, *backupfile;
451  int size;
452 
453  line=owl_zephyr_makesubline(class, inst, recip);
454
455  /* open the subsfile for reading */
456  if (filename==NULL) {
457    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
458  } else {
459    strcpy(subsfile, filename);
460  }
461  file=fopen(subsfile, "r");
462  if (!file) {
463    owl_function_makemsg("Error opening file %s", subsfile);
464    owl_free(line);
465    return;
466  }
467
468  /* open the backup file for writing */
469  sprintf(backupfilename, "%s.backup", subsfile);
470  backupfile=fopen(backupfilename, "w");
471  if (!backupfile) {
472    owl_function_makemsg("Error opening file %s for writing", backupfilename);
473    owl_free(line);
474    return;
475  }
476
477  /* we'll read the entire file into memory, minus the line we don't want and
478   * and at the same time create a backup file */
479  text=owl_malloc(LINE);
480  strcpy(text, "");
481  size=LINE;
482  while (fgets(buff, LINE, file)!=NULL) {
483    /* if we don't match the line, add to text */
484    if (strcasecmp(buff, line)) {
485      size+=LINE;
486      text=owl_realloc(text, size);
487      strcat(text, buff);
488    }
489
490    /* write to backupfile */
491    fputs(buff, backupfile);
492  }
493  fclose(backupfile);
494  fclose(file);
495
496  /* now open the original subs file for writing and write out the
497   * subs */
498  file=fopen(subsfile, "w");
499  if (!file) {
500    owl_function_makemsg("WARNING: Error opening %s to rewrite subscriptions.  Use %s to restore", subsfile, backupfilename);
501    owl_function_beep();
502    owl_free(line);
503    return;
504  }
505
506  fputs(text, file);
507  fclose(file);
508  owl_free(line);
509
510  owl_function_makemsg("Subscription removed");
511}
512
513char *owl_zephyr_makesubline(char *class, char *inst, char *recip) {
514  /* caller must free the return */
515  char *out;
516
517  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
518  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
519  return(out);
520}
Note: See TracBrowser for help on using the repository browser.