source: zwrite.c @ 8135737

release-1.9
Last change on this file since 8135737 was ef4074b, checked in by David Benjamin <davidben@mit.edu>, 10 years ago
Cleanup in owl_zwrite_create on error cases Before, the caller was required to cleanup, even on error. This is kind of weird. In particular, owl_zwrite_create_and_send_from_line forgot to so there was a small leak in there. Also with 6329cd5136a27361dedf94bb92e20fa2d327e43c it's now possible to error from owl_zwrite_create_from_line before the owl_zwrite is initialized, which means that owl_zwrite_new_from_line on misquoted strings explodes. Oops. Although I don't think this is actually possible to trigger now. Also be consistent about whether rv != 0 or rv < 0 is the error condition. Barring any actual arguments one or another, I'm going to arbitrary declare it's the former since that function doesn't really have a useful number to return.
  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2
[d953ede]3CALLER_OWN owl_zwrite *owl_zwrite_new_from_line(const char *line)
[987cf3f]4{
[96828e4]5  owl_zwrite *z = g_new(owl_zwrite, 1);
[ef4074b]6  if (owl_zwrite_create_from_line(z, line) != 0) {
7    g_free(z);
[987cf3f]8    return NULL;
9  }
10  return z;
11}
12
[6329cd5]13CALLER_OWN owl_zwrite *owl_zwrite_new(int argc, const char *const *argv)
14{
15  owl_zwrite *z = g_new(owl_zwrite, 1);
[ef4074b]16  if (owl_zwrite_create(z, argc, argv) != 0) {
17    g_free(z);
[6329cd5]18    return NULL;
19  }
20  return z;
21}
22
[d427f08]23G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create_from_line(owl_zwrite *z, const char *line)
[ce7db4d]24{
[6329cd5]25  int argc;
[65b2173]26  char **argv;
[6329cd5]27  int ret;
[7d4fbcd]28
[6329cd5]29  /* parse the command line for options */
30  argv = owl_parseline(line, &argc);
31  if (argc < 0) {
32    owl_function_error("Unbalanced quotes in zwrite");
33    return -1;
34  }
35  ret = owl_zwrite_create(z, argc, strs(argv));
36  g_strfreev(argv);
37  return ret;
38}
39
40G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create(owl_zwrite *z, int argc, const char *const *argv)
41{
42  int badargs = 0;
43  char *msg = NULL;
[7d4fbcd]44 
[69f89c7]45  /* start with null entries */
[987cf3f]46  z->cmd=NULL;
[ce7db4d]47  z->realm=NULL;
48  z->class=NULL;
49  z->inst=NULL;
50  z->opcode=NULL;
51  z->zsig=NULL;
52  z->message=NULL;
[7d4fbcd]53  z->cc=0;
54  z->noping=0;
[12294d2]55  z->recips = g_ptr_array_new();
[6329cd5]56  z->zwriteline = owl_argv_quote(argc, argv);
[7d4fbcd]57
[6329cd5]58  if (argc && *(argv[0])!='-') {
59    z->cmd=g_strdup(argv[0]);
60    argc--;
61    argv++;
[e1c4636]62  }
[6329cd5]63  while (argc) {
64    if (!strcmp(argv[0], "-c")) {
65      if (argc<2) {
[7d4fbcd]66        badargs=1;
67        break;
68      }
[6329cd5]69      z->class=owl_validate_utf8(argv[1]);
70      argv+=2;
71      argc-=2;
72    } else if (!strcmp(argv[0], "-i")) {
73      if (argc<2) {
[7d4fbcd]74        badargs=1;
75        break;
76      }
[6329cd5]77      z->inst=owl_validate_utf8(argv[1]);
78      argv+=2;
79      argc-=2;
80    } else if (!strcmp(argv[0], "-r")) {
81      if (argc<2) {
[7d4fbcd]82        badargs=1;
83        break;
84      }
[6329cd5]85      z->realm=owl_validate_utf8(argv[1]);
86      argv+=2;
87      argc-=2;
88    } else if (!strcmp(argv[0], "-s")) {
89      if (argc<2) {
[ce7db4d]90        badargs=1;
91        break;
92      }
[6329cd5]93      z->zsig=owl_validate_utf8(argv[1]);
94      argv+=2;
95      argc-=2;
96    } else if (!strcmp(argv[0], "-O")) {
97      if (argc<2) {
[7d4fbcd]98        badargs=1;
99        break;
100      }
[6329cd5]101      z->opcode=owl_validate_utf8(argv[1]);
102      argv+=2;
103      argc-=2;
104    } else if (!strcmp(argv[0], "-m")) {
105      if (argc<2) {
[ce7db4d]106        badargs=1;
107        break;
108      }
[69f89c7]109      /* we must already have users or a class or an instance */
[12294d2]110      if (z->recips->len < 1 && (!z->class) && (!z->inst)) {
[ce7db4d]111        badargs=1;
112        break;
113      }
114
115      /* Once we have -m, gobble up everything else on the line */
[6329cd5]116      argv++;
117      argc--;
118      msg = g_strjoinv(" ", (char**)argv);
[ce7db4d]119      break;
[6329cd5]120    } else if (!strcmp(argv[0], "-C")) {
[7d4fbcd]121      z->cc=1;
[6329cd5]122      argv++;
123      argc--;
124    } else if (!strcmp(argv[0], "-n")) {
[7d4fbcd]125      z->noping=1;
[6329cd5]126      argv++;
127      argc--;
[7d4fbcd]128    } else {
129      /* anything unattached is a recipient */
[6329cd5]130      g_ptr_array_add(z->recips, owl_validate_utf8(argv[0]));
131      argv++;
132      argc--;
[7d4fbcd]133    }
134  }
135
136  if (badargs) {
[ef4074b]137    owl_zwrite_cleanup(z);
[7d4fbcd]138    return(-1);
139  }
140
[1fe100c]141  if (z->class == NULL &&
142      z->inst == NULL &&
[12294d2]143      z->recips->len == 0) {
[1fe100c]144    owl_function_error("You must specify a recipient for zwrite");
[ef4074b]145    owl_zwrite_cleanup(z);
[1fe100c]146    return(-1);
147  }
148
[ce7db4d]149  /* now deal with defaults */
[d4927a7]150  if (z->class==NULL) z->class=g_strdup("message");
151  if (z->inst==NULL) z->inst=g_strdup("personal");
152  if (z->realm==NULL) z->realm=g_strdup("");
153  if (z->opcode==NULL) z->opcode=g_strdup("");
[ce7db4d]154  /* z->message is allowed to stay NULL */
[a52d13a]155
156  if(msg) {
157    owl_zwrite_set_message(z, msg);
[ddbbcffa]158    g_free(msg);
[a52d13a]159  }
160
[3f3ee61]161  return(0);
162}
163
164void owl_zwrite_populate_zsig(owl_zwrite *z)
165{
[ce7db4d]166  /* get a zsig, if not given */
[de3f641]167  if (z->zsig != NULL)
168    return;
[ce7db4d]169
[de3f641]170  z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g));
[7d4fbcd]171}
172
[a352029b]173void owl_zwrite_send_ping(const owl_zwrite *z)
[ce7db4d]174{
[12294d2]175  int i;
[44a61ac]176  char *to;
[7d4fbcd]177
178  if (z->noping) return;
179 
[3ef779b]180  if (strcasecmp(z->class, "message")) {
[7d4fbcd]181    return;
182  }
183
184  /* if there are no recipients we won't send a ping, which
185     is what we want */
[12294d2]186  for (i = 0; i < z->recips->len; i++) {
[3f52e14]187    to = owl_zwrite_get_recip_n_with_realm(z, i);
[3ef779b]188    send_ping(to, z->class, z->inst);
[ddbbcffa]189    g_free(to);
[7d4fbcd]190  }
191
192}
193
[7bfc613]194/* Set the message with no post-processing*/
195void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg)
196{
[3b8a563]197  g_free(z->message);
[7bfc613]198  z->message = owl_validate_utf8(msg);
199}
200
[e19eb97]201void owl_zwrite_set_message(owl_zwrite *z, const char *msg)
[ce7db4d]202{
[12294d2]203  int i;
[3f52e14]204  GString *message;
[a52d13a]205  char *tmp = NULL, *tmp2;
[db2dd3d]206
[3b8a563]207  g_free(z->message);
[db2dd3d]208
[12294d2]209  if (z->recips->len > 0 && z->cc) {
[3f52e14]210    message = g_string_new("CC: ");
[12294d2]211    for (i = 0; i < z->recips->len; i++) {
[3f52e14]212      tmp = owl_zwrite_get_recip_n_with_realm(z, i);
213      g_string_append_printf(message, "%s ", tmp);
[ddbbcffa]214      g_free(tmp);
[3538bc8]215      tmp = NULL;
[db2dd3d]216    }
[4b17a6c]217    tmp = owl_validate_utf8(msg);
[a52d13a]218    tmp2 = owl_text_expand_tabs(tmp);
[3f52e14]219    g_string_append_printf(message, "\n%s", tmp2);
220    z->message = g_string_free(message, false);
[ddbbcffa]221    g_free(tmp);
222    g_free(tmp2);
[db2dd3d]223  } else {
[a52d13a]224    tmp=owl_validate_utf8(msg);
225    z->message=owl_text_expand_tabs(tmp);
[ddbbcffa]226    g_free(tmp);
[db2dd3d]227  }
[ce7db4d]228}
229
[a352029b]230const char *owl_zwrite_get_message(const owl_zwrite *z)
[ce7db4d]231{
232  if (z->message) return(z->message);
233  return("");
234}
235
[a352029b]236int owl_zwrite_is_message_set(const owl_zwrite *z)
[ce7db4d]237{
238  if (z->message) return(1);
239  return(0);
240}
241
[a352029b]242int owl_zwrite_send_message(const owl_zwrite *z)
[ce7db4d]243{
[12294d2]244  int i, ret = 0;
[823671c]245  char *to = NULL;
[7d4fbcd]246
[ce7db4d]247  if (z->message==NULL) return(-1);
248
[12294d2]249  if (z->recips->len > 0) {
250    for (i = 0; i < z->recips->len; i++) {
[3f52e14]251      to = owl_zwrite_get_recip_n_with_realm(z, i);
[0743696]252      ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
253      /* Abort on the first error, to match the zwrite binary. */
254      if (ret != 0)
255        break;
[ddbbcffa]256      g_free(to);
[823671c]257      to = NULL;
[7d4fbcd]258    }
259  } else {
[3472845]260    to = g_strdup_printf( "@%s", z->realm);
[0743696]261    ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
[ce7db4d]262  }
[ddbbcffa]263  g_free(to);
[0743696]264  return ret;
[ce7db4d]265}
266
[e19eb97]267int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg)
[ce7db4d]268{
269  owl_zwrite z;
270  int rv;
[ef4074b]271  rv = owl_zwrite_create_from_line(&z, cmd);
272  if (rv != 0) return rv;
[ce7db4d]273  if (!owl_zwrite_is_message_set(&z)) {
274    owl_zwrite_set_message(&z, msg);
[7d4fbcd]275  }
[3f3ee61]276  owl_zwrite_populate_zsig(&z);
[ce7db4d]277  owl_zwrite_send_message(&z);
[c230bc1]278  owl_zwrite_cleanup(&z);
[ce7db4d]279  return(0);
[7d4fbcd]280}
281
[a352029b]282const char *owl_zwrite_get_class(const owl_zwrite *z)
[ce7db4d]283{
[7d4fbcd]284  return(z->class);
285}
286
[a352029b]287const char *owl_zwrite_get_instance(const owl_zwrite *z)
[ce7db4d]288{
[7d4fbcd]289  return(z->inst);
290}
291
[a352029b]292const char *owl_zwrite_get_opcode(const owl_zwrite *z)
[ce7db4d]293{
[4b464a4]294  return(z->opcode);
295}
296
[e19eb97]297void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode)
[9ceee9d]298{
[3b8a563]299  g_free(z->opcode);
[4b17a6c]300  z->opcode=owl_validate_utf8(opcode);
[9ceee9d]301}
302
[a352029b]303const char *owl_zwrite_get_realm(const owl_zwrite *z)
[ce7db4d]304{
[7d4fbcd]305  return(z->realm);
306}
307
[a352029b]308const char *owl_zwrite_get_zsig(const owl_zwrite *z)
[ce7db4d]309{
310  if (z->zsig) return(z->zsig);
311  return("");
[56330ff]312}
313
[24ccc01]314void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig)
315{
[3b8a563]316  g_free(z->zsig);
[d4927a7]317  z->zsig = g_strdup(zsig);
[24ccc01]318}
319
[a352029b]320int owl_zwrite_get_numrecips(const owl_zwrite *z)
[ce7db4d]321{
[12294d2]322  return z->recips->len;
[7d4fbcd]323}
324
[a352029b]325const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n)
[ce7db4d]326{
[12294d2]327  return z->recips->pdata[n];
[7d4fbcd]328}
329
[3f52e14]330/* Caller must free the result. */
[6829afc]331CALLER_OWN char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n)
[3f52e14]332{
333  if (z->realm[0]) {
334    return g_strdup_printf("%s@%s", owl_zwrite_get_recip_n(z, n), z->realm);
335  } else {
336    return g_strdup(owl_zwrite_get_recip_n(z, n));
337  }
338}
339
[a352029b]340int owl_zwrite_is_personal(const owl_zwrite *z)
[ce7db4d]341{
[7d4fbcd]342  /* return true if at least one of the recipients is personal */
[12294d2]343  int i;
344  char *recip;
[7d4fbcd]345
[12294d2]346  for (i = 0; i < z->recips->len; i++) {
347    recip = z->recips->pdata[i];
348    if (recip[0] != '@') return 1;
[7d4fbcd]349  }
350  return(0);
351}
[3f3ee61]352
[987cf3f]353void owl_zwrite_delete(owl_zwrite *z)
354{
355  owl_zwrite_cleanup(z);
[ddbbcffa]356  g_free(z);
[987cf3f]357}
358
[c230bc1]359void owl_zwrite_cleanup(owl_zwrite *z)
[ce7db4d]360{
[3cdd6d2]361  owl_ptr_array_free(z->recips, g_free);
[3b8a563]362  g_free(z->cmd);
363  g_free(z->zwriteline);
364  g_free(z->class);
365  g_free(z->inst);
366  g_free(z->opcode);
367  g_free(z->realm);
368  g_free(z->message);
369  g_free(z->zsig);
[7d4fbcd]370}
[719119de]371
372/*
373 * Returns a zwrite line suitable for replying, specifically the
374 * message field is stripped out. Result should be freed with
[ddbbcffa]375 * g_free.
[a5b5d00]376 *
377 * If not a CC, only the recip_index'th user will be replied to.
[719119de]378 */
[6829afc]379CALLER_OWN char *owl_zwrite_get_replyline(const owl_zwrite *z, int recip_index)
[719119de]380{
381  /* Match ordering in zwrite help. */
382  GString *buf = g_string_new("");
383  int i;
384
385  /* Disturbingly, it is apparently possible to z->cmd to be null if
386   * owl_zwrite_create_from_line got something starting with -. And we
387   * can't kill it because this is exported to perl. */
388  owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite");
389  if (z->noping) {
390    g_string_append(buf, " -n");
391  }
392  if (z->cc) {
393    g_string_append(buf, " -C");
394  }
395  if (strcmp(z->class, "message")) {
396    g_string_append(buf, " -c ");
397    owl_string_append_quoted_arg(buf, z->class);
398  }
399  if (strcmp(z->inst, "personal")) {
400    g_string_append(buf, " -i ");
401    owl_string_append_quoted_arg(buf, z->inst);
402  }
403  if (z->realm && z->realm[0] != '\0') {
404    g_string_append(buf, " -r ");
405    owl_string_append_quoted_arg(buf, z->realm);
406  }
407  if (z->opcode && z->opcode[0] != '\0') {
408    g_string_append(buf, " -O ");
409    owl_string_append_quoted_arg(buf, z->opcode);
410  }
[a5b5d00]411  if (z->cc) {
[12294d2]412    for (i = 0; i < z->recips->len; i++) {
[a5b5d00]413      g_string_append_c(buf, ' ');
[12294d2]414      owl_string_append_quoted_arg(buf, z->recips->pdata[i]);
[a5b5d00]415    }
[12294d2]416  } else if (recip_index < z->recips->len) {
[719119de]417    g_string_append_c(buf, ' ');
[12294d2]418    owl_string_append_quoted_arg(buf, z->recips->pdata[recip_index]);
[719119de]419  }
420
421  return g_string_free(buf, false);
422}
Note: See TracBrowser for help on using the repository browser.