source: zwrite.c @ 2640b63

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