source: zephyr.c @ 2adaf1d

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2adaf1d was 2adaf1d, checked in by James M. Kretchmar <kretch@mit.edu>, 22 years ago
Added the dump command removed old zlog functions
  • Property mode set to 100644
File size: 12.8 KB
RevLine 
[7d4fbcd]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
[1aee7d9]10static const char fileIdent[] = "$Id$";
11
[7d4fbcd]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 {
[1c6c4d3]112        subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
[7d4fbcd]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
[56330ff]261  notice.z_message_len=strlen(zsig)+1+strlen(message);
[7d4fbcd]262  notice.z_message=owl_malloc(notice.z_message_len+10);
[56330ff]263  strcpy(notice.z_message, zsig);
264  memcpy(notice.z_message+strlen(zsig)+1, message, strlen(message));
[7d4fbcd]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")) {
[4b464a4]303      tmp=short_zuser(retnotice->z_recipient);
[1c6c4d3]304      owl_function_makemsg("Message sent to %s.", tmp);
[7d4fbcd]305      free(tmp);
306    } else {
[1c6c4d3]307      owl_function_makemsg("Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst);
[7d4fbcd]308    }
309  } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) {
310    if (strcasecmp(retnotice->z_class, "message")) {
[1c6c4d3]311      owl_function_makemsg("Not logged in or not subscribing to class %s, instance %s",
[7d4fbcd]312              retnotice->z_class, retnotice->z_class_inst);
313    } else {
[4b464a4]314      tmp = short_zuser(retnotice->z_recipient);
[1c6c4d3]315      owl_function_makemsg("%s: Not logged in or subscribing to messages.", 
316                           tmp);
317      owl_free(tmp);
[7d4fbcd]318    }
319  } else {
[1c6c4d3]320    owl_function_makemsg("Internal error on ack (%s)", retnotice->z_message);
[7d4fbcd]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_notice(m)->z_message, "Automated reply:")) return;
342  if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return;
343  /* add one to not send to ourselves */
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  /* display the message as an admin message in the receive window */
[1c6c4d3]353  tmpbuff = owl_sprintf("Message sent to %s", owl_message_get_sender(m));
[7d4fbcd]354  owl_function_adminmsg(tmpbuff, owl_global_get_zaway_msg(&g));
355  owl_free(tmpbuff);
356}
357
358
359void owl_zephyr_hackaway_cr(ZNotice_t *n) {
360  /* replace \r's with ' '.  Gross-ish */
361  int i;
362
363  for (i=0; i<n->z_message_len; i++) {
364    if (n->z_message[i]=='\r') {
365      n->z_message[i]=' ';
366    }
367  }
368}
369
370void owl_zephyr_zlocate(char *user, char *out, int auth) {
371  int ret, numlocs;
372  int one = 1;
373  ZLocations_t locations;
374  char *myuser;
375 
376  strcpy(out, "");
377
378  ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH);
379  if (ret != ZERR_NONE) {
380    owl_function_makemsg("Error locating user");
381  }
382
383  if (numlocs==0) {
384    strcpy(out, "Hidden or not logged-in\n");
385    return;
386  }
387   
388  for (;numlocs;numlocs--) {
389    ZGetLocations(&locations,&one);
[4b464a4]390    myuser=short_zuser(user);
[7d4fbcd]391    sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time);
392    owl_free(myuser);
393  }
394}
[bde7714]395
396void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip) {
397  char *line, subsfile[LINE], buff[LINE];
398  FILE *file;
399
400  line=owl_zephyr_makesubline(class, inst, recip);
401
402  if (filename==NULL) {
403    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
404  } else {
405    strcpy(subsfile, filename);
406  }
407
408  /* first check if it exists already */
409  file=fopen(subsfile, "r");
410  if (!file) {
411    owl_function_makemsg("Error opening file %s", subsfile);
412    owl_free(line);
413    return;
414  }
415  while (fgets(buff, LINE, file)!=NULL) {
416    if (!strcasecmp(buff, line)) {
417      owl_function_makemsg("Subscription already present in %s", subsfile);
418      owl_free(line);
419      return;
420    }
421  }
422
423  /* if we get here then we didn't find it */
424  fclose(file);
425  file=fopen(subsfile, "a");
426  if (!file) {
427    owl_function_makemsg("Error opening file %s for writing", subsfile);
428    owl_free(line);
429    return;
430  }
431  fputs(line, file);
432  fclose(file);
433  owl_function_makemsg("Subscription added");
434 
435  owl_free(line);
436}
437
438void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip) {
439  char *line, subsfile[LINE], buff[LINE], *text;
440  char backupfilename[LINE];
441  FILE *file, *backupfile;
442  int size;
443 
444  line=owl_zephyr_makesubline(class, inst, recip);
445
446  /* open the subsfile for reading */
447  if (filename==NULL) {
448    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
449  } else {
450    strcpy(subsfile, filename);
451  }
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
459  /* open the backup file for writing */
460  sprintf(backupfilename, "%s.backup", subsfile);
461  backupfile=fopen(backupfilename, "w");
462  if (!backupfile) {
463    owl_function_makemsg("Error opening file %s for writing", backupfilename);
464    owl_free(line);
465    return;
466  }
467
468  /* we'll read the entire file into memory, minus the line we don't want and
469   * and at the same time create a backup file */
470  text=malloc(LINE);
471  size=LINE;
472  while (fgets(buff, LINE, file)!=NULL) {
473    /* if we don't match the line, add to text */
474    if (strcasecmp(buff, line)) {
475      size+=LINE;
476      text=realloc(text, size);
477      strcat(text, buff);
478    }
479
480    /* write to backupfile */
481    fputs(buff, backupfile);
482  }
483  fclose(backupfile);
484  fclose(file);
485
486  /* now open the original subs file for writing and write out the
487   * subs */
488  file=fopen(subsfile, "w");
489  if (!file) {
490    owl_function_makemsg("WARNING: Error opening %s to rewrite subscriptions.  Use %s to restore", subsfile, backupfilename);
491    owl_function_beep();
492    owl_free(line);
493    return;
494  }
495
496  fputs(text, file);
497  fclose(file);
498  owl_free(line);
499
500  owl_function_makemsg("Subscription removed");
501}
502
503char *owl_zephyr_makesubline(char *class, char *inst, char *recip) {
504  /* caller must free the return */
505  char *out;
506
507  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
508  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
509  return(out);
510}
Note: See TracBrowser for help on using the repository browser.