source: zwrite.c @ 0b4aef2

release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 0b4aef2 was 4db2355, checked in by Nelson Elhage <nelhage@mit.edu>, 12 years ago
Fix another off-by-one in zwrite.c with zwrite -m.
  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[7d4fbcd]1#include <string.h>
[56330ff]2#include <pwd.h>
3#include <sys/types.h>
4#include <unistd.h>
[7d4fbcd]5#include "owl.h"
6
[e19eb97]7int owl_zwrite_create_from_line(owl_zwrite *z, const char *line)
[ce7db4d]8{
[a52d13a]9  int argc, badargs, myargc, i, len;
[65b2173]10  char **argv;
[e19eb97]11  const char *const *myargv;
[a52d13a]12  char *msg = NULL;
[7d4fbcd]13
14  badargs=0;
15 
[69f89c7]16  /* start with null entries */
[ce7db4d]17  z->realm=NULL;
18  z->class=NULL;
19  z->inst=NULL;
20  z->opcode=NULL;
21  z->zsig=NULL;
22  z->message=NULL;
[7d4fbcd]23  z->cc=0;
24  z->noping=0;
25  owl_list_create(&(z->recips));
26
27  /* parse the command line for options */
[c529ac8]28  argv=owl_parseline(line, &argc);
29  myargv=strs(argv);
[7d4fbcd]30  if (argc<0) {
[836ea3a3]31    owl_function_error("Unbalanced quotes in zwrite");
[7d4fbcd]32    return(-1);
33  }
34  myargc=argc;
[e1c4636]35  if (myargc && *(myargv[0])!='-') {
36    myargc--;
37    myargv++;
38  }
[7d4fbcd]39  while (myargc) {
40    if (!strcmp(myargv[0], "-c")) {
41      if (myargc<2) {
42        badargs=1;
43        break;
44      }
[4b17a6c]45      z->class=owl_validate_utf8(myargv[1]);
[7d4fbcd]46      myargv+=2;
47      myargc-=2;
48    } else if (!strcmp(myargv[0], "-i")) {
49      if (myargc<2) {
50        badargs=1;
51        break;
52      }
[4b17a6c]53      z->inst=owl_validate_utf8(myargv[1]);
[7d4fbcd]54      myargv+=2;
55      myargc-=2;
56    } else if (!strcmp(myargv[0], "-r")) {
57      if (myargc<2) {
58        badargs=1;
59        break;
60      }
[4b17a6c]61      z->realm=owl_validate_utf8(myargv[1]);
[ce7db4d]62      myargv+=2;
63      myargc-=2;
64    } else if (!strcmp(myargv[0], "-s")) {
65      if (myargc<2) {
66        badargs=1;
67        break;
68      }
[4b17a6c]69      z->zsig=owl_validate_utf8(myargv[1]);
[7d4fbcd]70      myargv+=2;
71      myargc-=2;
72    } else if (!strcmp(myargv[0], "-O")) {
73      if (myargc<2) {
74        badargs=1;
75        break;
76      }
[4b17a6c]77      z->opcode=owl_validate_utf8(myargv[1]);
[7d4fbcd]78      myargv+=2;
79      myargc-=2;
[ce7db4d]80    } else if (!strcmp(myargv[0], "-m")) {
81      if (myargc<2) {
82        badargs=1;
83        break;
84      }
[69f89c7]85      /* we must already have users or a class or an instance */
[e016fc2]86      if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) {
[ce7db4d]87        badargs=1;
88        break;
89      }
90
91      /* Once we have -m, gobble up everything else on the line */
92      myargv++;
93      myargc--;
[a52d13a]94      len = 0;
95      for (i=0;i<myargc;i++) {
[96cdeaf]96        len += strlen(myargv[i]) + 1;
[a52d13a]97      }
[4db2355]98      len++;    /* NULL after the last trailing ' ' in the loop below. */
[a52d13a]99      msg = owl_malloc(len);
100      msg[0] = '\0';
[ce7db4d]101      while (myargc) {
[a52d13a]102        strcat(msg, myargv[0]);
103        strcat(msg, " ");
104        myargc--;
105        myargv++;
[ce7db4d]106      }
[a52d13a]107      msg[strlen(msg)-1] = '\0';
[ce7db4d]108      break;
[7d4fbcd]109    } else if (!strcmp(myargv[0], "-C")) {
110      z->cc=1;
111      myargv++;
112      myargc--;
113    } else if (!strcmp(myargv[0], "-n")) {
114      z->noping=1;
115      myargv++;
116      myargc--;
117    } else {
118      /* anything unattached is a recipient */
[4b17a6c]119      owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0]));
[7d4fbcd]120      myargv++;
121      myargc--;
122    }
123  }
124
125  owl_parsefree(argv, argc);
126
127  if (badargs) {
128    return(-1);
129  }
130
[1fe100c]131  if (z->class == NULL &&
132      z->inst == NULL &&
133      owl_list_get_size(&(z->recips))==0) {
134    owl_function_error("You must specify a recipient for zwrite");
135    return(-1);
136  }
137
[ce7db4d]138  /* now deal with defaults */
139  if (z->class==NULL) z->class=owl_strdup("message");
140  if (z->inst==NULL) z->inst=owl_strdup("personal");
141  if (z->realm==NULL) z->realm=owl_strdup("");
142  if (z->opcode==NULL) z->opcode=owl_strdup("");
143  /* z->message is allowed to stay NULL */
[a52d13a]144
145  if(msg) {
146    owl_zwrite_set_message(z, msg);
147    owl_free(msg);
148  }
149
[3f3ee61]150  return(0);
151}
152
153void owl_zwrite_populate_zsig(owl_zwrite *z)
154{
[e19eb97]155  const char *zsigproc, *zsigowlvar, *zsigzvar;
[65b2173]156  char *ptr;
[3f3ee61]157  struct passwd *pw;
158
[ce7db4d]159  /* get a zsig, if not given */
160  if (z->zsig==NULL) {
161    zsigproc = owl_global_get_zsigproc(&g);
162    zsigowlvar = owl_global_get_zsig(&g);
[09489b89]163    zsigzvar = owl_zephyr_get_variable("zwrite-signature");
[ce7db4d]164
165    if (zsigowlvar && *zsigowlvar) {
[4b17a6c]166      z->zsig=owl_validate_utf8(zsigowlvar);
[ce7db4d]167    } else if (zsigproc && *zsigproc) {
168      FILE *file;
169      char buff[LINE], *openline;
170     
171      /* simple hack for now to nuke stderr */
172      openline=owl_malloc(strlen(zsigproc)+40);
173      strcpy(openline, zsigproc);
[2a2bb60]174#if !(OWL_STDERR_REDIR)
[ce7db4d]175      strcat(openline, " 2> /dev/null");
[2a2bb60]176#endif
[ce7db4d]177      file=popen(openline, "r");
178      owl_free(openline);
179      if (!file) {
180        if (zsigzvar && *zsigzvar) {
[4b17a6c]181          z->zsig=owl_validate_utf8(zsigzvar);
[ce7db4d]182        }
183      } else {
184        z->zsig=owl_malloc(LINE*5);
185        strcpy(z->zsig, "");
186        while (fgets(buff, LINE, file)) { /* wrong sizing */
187          strcat(z->zsig, buff);
188        }
189        pclose(file);
190        if (z->zsig[strlen(z->zsig)-1]=='\n') {
191          z->zsig[strlen(z->zsig)-1]='\0';
192        }
[56330ff]193      }
[ce7db4d]194    } else if (zsigzvar) {
[4b17a6c]195      z->zsig=owl_validate_utf8(zsigzvar);
[ce7db4d]196    } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) {
[4b17a6c]197      z->zsig=owl_validate_utf8(pw->pw_gecos);
[ce7db4d]198      ptr=strchr(z->zsig, ',');
199      if (ptr) {
200        ptr[0]='\0';
[56330ff]201      }
202    }
203  }
[7d4fbcd]204}
205
[a352029b]206void owl_zwrite_send_ping(const owl_zwrite *z)
[ce7db4d]207{
[7d4fbcd]208  int i, j;
[44a61ac]209  char *to;
[7d4fbcd]210
211  if (z->noping) return;
212 
[3ef779b]213  if (strcasecmp(z->class, "message")) {
[7d4fbcd]214    return;
215  }
216
217  /* if there are no recipients we won't send a ping, which
218     is what we want */
219  j=owl_list_get_size(&(z->recips));
220  for (i=0; i<j; i++) {
221    if (strcmp(z->realm, "")) {
[e19eb97]222      to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm);
[7d4fbcd]223    } else {
[44a61ac]224      to = owl_strdup(owl_list_get_element(&(z->recips), i));
[7d4fbcd]225    }
[3ef779b]226    send_ping(to, z->class, z->inst);
[44a61ac]227    owl_free(to);
[7d4fbcd]228  }
229
230}
231
[e19eb97]232void owl_zwrite_set_message(owl_zwrite *z, const char *msg)
[ce7db4d]233{
[db2dd3d]234  int i, j;
[3538bc8]235  char *toline = NULL;
[a52d13a]236  char *tmp = NULL, *tmp2;
[db2dd3d]237
[ce7db4d]238  if (z->message) owl_free(z->message);
[db2dd3d]239
240  j=owl_list_get_size(&(z->recips));
241  if (j>0 && z->cc) {
[3538bc8]242    toline = owl_strdup( "CC: ");
[db2dd3d]243    for (i=0; i<j; i++) {
[3538bc8]244      tmp = toline;
[db2dd3d]245      if (strcmp(z->realm, "")) {
[e19eb97]246        toline = owl_sprintf( "%s%s@%s ", toline, (const char *) owl_list_get_element(&(z->recips), i), z->realm);
[db2dd3d]247      } else {
[e19eb97]248        toline = owl_sprintf( "%s%s ", toline, (const char *) owl_list_get_element(&(z->recips), i));
[db2dd3d]249      }
[3538bc8]250      owl_free(tmp);
251      tmp = NULL;
[db2dd3d]252    }
[4b17a6c]253    tmp = owl_validate_utf8(msg);
[a52d13a]254    tmp2 = owl_text_expand_tabs(tmp);
255    z->message=owl_sprintf("%s\n%s", toline, tmp2);
[3538bc8]256    owl_free(toline);
[a52d13a]257    owl_free(tmp);
258    owl_free(tmp2);
[db2dd3d]259  } else {
[a52d13a]260    tmp=owl_validate_utf8(msg);
261    z->message=owl_text_expand_tabs(tmp);
262    owl_free(tmp);
[db2dd3d]263  }
[ce7db4d]264}
265
[a352029b]266const char *owl_zwrite_get_message(const owl_zwrite *z)
[ce7db4d]267{
268  if (z->message) return(z->message);
269  return("");
270}
271
[a352029b]272int owl_zwrite_is_message_set(const owl_zwrite *z)
[ce7db4d]273{
274  if (z->message) return(1);
275  return(0);
276}
277
[a352029b]278int owl_zwrite_send_message(const owl_zwrite *z)
[ce7db4d]279{
[7d4fbcd]280  int i, j;
[823671c]281  char *to = NULL;
[7d4fbcd]282
[ce7db4d]283  if (z->message==NULL) return(-1);
284
[7d4fbcd]285  j=owl_list_get_size(&(z->recips));
286  if (j>0) {
287    for (i=0; i<j; i++) {
288      if (strcmp(z->realm, "")) {
[e19eb97]289        to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm);
[7d4fbcd]290      } else {
[823671c]291        to = owl_strdup( owl_list_get_element(&(z->recips), i));
[7d4fbcd]292      }
[db2dd3d]293      send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
[823671c]294      owl_free(to);
295      to = NULL;
[7d4fbcd]296    }
297  } else {
[823671c]298    to = owl_sprintf( "@%s", z->realm);
[ce7db4d]299    send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
300  }
[823671c]301  owl_free(to);
[ce7db4d]302  return(0);
303}
304
[e19eb97]305int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg)
[ce7db4d]306{
307  owl_zwrite z;
308  int rv;
309  rv=owl_zwrite_create_from_line(&z, cmd);
310  if (rv) return(rv);
311  if (!owl_zwrite_is_message_set(&z)) {
312    owl_zwrite_set_message(&z, msg);
[7d4fbcd]313  }
[3f3ee61]314  owl_zwrite_populate_zsig(&z);
[ce7db4d]315  owl_zwrite_send_message(&z);
316  owl_zwrite_free(&z);
317  return(0);
[7d4fbcd]318}
319
[a352029b]320const char *owl_zwrite_get_class(const owl_zwrite *z)
[ce7db4d]321{
[7d4fbcd]322  return(z->class);
323}
324
[a352029b]325const char *owl_zwrite_get_instance(const owl_zwrite *z)
[ce7db4d]326{
[7d4fbcd]327  return(z->inst);
328}
329
[a352029b]330const char *owl_zwrite_get_opcode(const owl_zwrite *z)
[ce7db4d]331{
[4b464a4]332  return(z->opcode);
333}
334
[e19eb97]335void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode)
[9ceee9d]336{
337  if (z->opcode) owl_free(z->opcode);
[4b17a6c]338  z->opcode=owl_validate_utf8(opcode);
[9ceee9d]339}
340
[a352029b]341const char *owl_zwrite_get_realm(const owl_zwrite *z)
[ce7db4d]342{
[7d4fbcd]343  return(z->realm);
344}
345
[a352029b]346const char *owl_zwrite_get_zsig(const owl_zwrite *z)
[ce7db4d]347{
348  if (z->zsig) return(z->zsig);
349  return("");
[56330ff]350}
351
[a352029b]352void owl_zwrite_get_recipstr(const owl_zwrite *z, char *buff)
[ce7db4d]353{
[7d4fbcd]354  int i, j;
355
356  strcpy(buff, "");
357  j=owl_list_get_size(&(z->recips));
358  for (i=0; i<j; i++) {
359    strcat(buff, owl_list_get_element(&(z->recips), i));
360    strcat(buff, " ");
361  }
362  buff[strlen(buff)-1]='\0';
363}
364
[a352029b]365int owl_zwrite_get_numrecips(const owl_zwrite *z)
[ce7db4d]366{
[7d4fbcd]367  return(owl_list_get_size(&(z->recips)));
368}
369
[a352029b]370const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n)
[ce7db4d]371{
[7d4fbcd]372  return(owl_list_get_element(&(z->recips), n));
373}
374
[a352029b]375int owl_zwrite_is_personal(const owl_zwrite *z)
[ce7db4d]376{
[7d4fbcd]377  /* return true if at least one of the recipients is personal */
378  int i, j;
379  char *foo;
380
381  j=owl_list_get_size(&(z->recips));
382  for (i=0; i<j; i++) {
383    foo=owl_list_get_element(&(z->recips), i);
384    if (foo[0]!='@') return(1);
385  }
386  return(0);
387}
[3f3ee61]388
[ce7db4d]389void owl_zwrite_free(owl_zwrite *z)
390{
[7d4fbcd]391  owl_list_free_all(&(z->recips), &owl_free);
[ce7db4d]392  if (z->class) owl_free(z->class);
393  if (z->inst) owl_free(z->inst);
394  if (z->opcode) owl_free(z->opcode);
395  if (z->realm) owl_free(z->realm);
396  if (z->message) owl_free(z->message);
397  if (z->zsig) owl_free(z->zsig);
[7d4fbcd]398}
Note: See TracBrowser for help on using the repository browser.