source: zwrite.c @ 5118b32

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