source: zephyr.c @ b12c219

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