source: zwrite.c

Last change on this file was 1958790, checked in by Anders Kaseorg <andersk@mit.edu>, 7 years ago
zephyr_zwrite: Use owl_function_zwrite This adds outgoing personals to the message list, and supports zcrypt, all for the low low price of −4 LoC. (I’m not sure why anyone would use this interface, though, given the existence of BarnOwl::zwrite('andersk', '-m', 'message').) Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 9.3 KB
Line 
1#include "owl.h"
2
3CALLER_OWN owl_zwrite *owl_zwrite_new_from_line(const char *line)
4{
5  owl_zwrite *z = g_slice_new(owl_zwrite);
6  if (owl_zwrite_create_from_line(z, line) != 0) {
7    g_slice_free(owl_zwrite, z);
8    return NULL;
9  }
10  return z;
11}
12
13CALLER_OWN owl_zwrite *owl_zwrite_new(int argc, const char *const *argv)
14{
15  owl_zwrite *z = g_slice_new(owl_zwrite);
16  if (owl_zwrite_create(z, argc, argv) != 0) {
17    g_slice_free(owl_zwrite, z);
18    return NULL;
19  }
20  return z;
21}
22
23G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create_from_line(owl_zwrite *z, const char *line)
24{
25  int argc;
26  char **argv;
27  int ret;
28
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;
44 
45  /* start with null entries */
46  z->cmd=NULL;
47  z->realm=NULL;
48  z->class=NULL;
49  z->inst=NULL;
50  z->opcode=NULL;
51  z->zsig=NULL;
52  z->message=NULL;
53  z->cc=0;
54  z->noping=0;
55  z->recips = g_ptr_array_new();
56  z->zwriteline = owl_argv_quote(argc, argv);
57
58  if (argc && *(argv[0])!='-') {
59    z->cmd=g_strdup(argv[0]);
60    argc--;
61    argv++;
62  }
63  while (argc) {
64    if (!strcmp(argv[0], "-c")) {
65      if (argc<2) {
66        badargs=1;
67        break;
68      }
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) {
74        badargs=1;
75        break;
76      }
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) {
82        badargs=1;
83        break;
84      }
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) {
90        badargs=1;
91        break;
92      }
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) {
98        badargs=1;
99        break;
100      }
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) {
106        badargs=1;
107        break;
108      }
109      /* we must already have users or a class or an instance */
110      if (z->recips->len < 1 && (!z->class) && (!z->inst)) {
111        badargs=1;
112        break;
113      }
114
115      /* Once we have -m, gobble up everything else on the line */
116      argv++;
117      argc--;
118      msg = g_strjoinv(" ", (char**)argv);
119      break;
120    } else if (!strcmp(argv[0], "-C")) {
121      z->cc=1;
122      argv++;
123      argc--;
124    } else if (!strcmp(argv[0], "-n")) {
125      z->noping=1;
126      argv++;
127      argc--;
128    } else {
129      /* anything unattached is a recipient */
130      g_ptr_array_add(z->recips, owl_validate_utf8(argv[0]));
131      argv++;
132      argc--;
133    }
134  }
135
136  if (badargs) {
137    owl_zwrite_cleanup(z);
138    return(-1);
139  }
140
141  if (z->class == NULL &&
142      z->inst == NULL &&
143      z->recips->len == 0) {
144    owl_function_error("You must specify a recipient for zwrite");
145    owl_zwrite_cleanup(z);
146    return(-1);
147  }
148
149  /* now deal with defaults */
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("");
154  /* z->message is allowed to stay NULL */
155
156  if(msg) {
157    owl_zwrite_set_message(z, msg);
158    g_free(msg);
159  }
160
161  return(0);
162}
163
164void owl_zwrite_populate_zsig(owl_zwrite *z)
165{
166  /* get a zsig, if not given */
167  if (z->zsig != NULL)
168    return;
169
170  z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g));
171}
172
173void owl_zwrite_send_ping(const owl_zwrite *z)
174{
175  int i;
176  char *to;
177
178  if (z->noping) return;
179 
180  if (strcasecmp(z->class, "message")) {
181    return;
182  }
183
184  for (i = 0; i < z->recips->len; i++) {
185    to = owl_zwrite_get_recip_n_with_realm(z, i);
186    if (owl_zwrite_recip_is_personal(to))
187      send_ping(to, z->class, z->inst);
188    g_free(to);
189  }
190
191}
192
193/* Set the message with no post-processing*/
194void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg)
195{
196  g_free(z->message);
197  z->message = owl_validate_utf8(msg);
198}
199
200void owl_zwrite_set_message(owl_zwrite *z, const char *msg)
201{
202  int i;
203  GString *message;
204  char *tmp = NULL, *tmp2;
205
206  g_free(z->message);
207
208  if (z->cc && owl_zwrite_is_personal(z)) {
209    message = g_string_new("CC: ");
210    for (i = 0; i < z->recips->len; i++) {
211      tmp = owl_zwrite_get_recip_n_with_realm(z, i);
212      g_string_append_printf(message, "%s ", tmp);
213      g_free(tmp);
214      tmp = NULL;
215    }
216    tmp = owl_validate_utf8(msg);
217    tmp2 = owl_text_expand_tabs(tmp);
218    g_string_append_printf(message, "\n%s", tmp2);
219    z->message = g_string_free(message, false);
220    g_free(tmp);
221    g_free(tmp2);
222  } else {
223    tmp=owl_validate_utf8(msg);
224    z->message=owl_text_expand_tabs(tmp);
225    g_free(tmp);
226  }
227}
228
229const char *owl_zwrite_get_message(const owl_zwrite *z)
230{
231  if (z->message) return(z->message);
232  return("");
233}
234
235int owl_zwrite_is_message_set(const owl_zwrite *z)
236{
237  if (z->message) return(1);
238  return(0);
239}
240
241int owl_zwrite_send_message(const owl_zwrite *z)
242{
243  int i, ret = 0;
244  char *to = NULL;
245
246  if (z->message==NULL) return(-1);
247
248  if (z->recips->len > 0) {
249    for (i = 0; i < z->recips->len; i++) {
250      to = owl_zwrite_get_recip_n_with_realm(z, i);
251      ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
252      /* Abort on the first error, to match the zwrite binary. */
253      if (ret != 0)
254        break;
255      g_free(to);
256      to = NULL;
257    }
258  } else {
259    to = g_strdup_printf( "@%s", z->realm);
260    ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
261  }
262  g_free(to);
263  return ret;
264}
265
266int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg)
267{
268  owl_zwrite z;
269  int rv;
270  rv = owl_zwrite_create_from_line(&z, cmd);
271  if (rv != 0) return rv;
272  owl_function_zwrite(&z, msg);
273  owl_zwrite_cleanup(&z);
274  return(0);
275}
276
277const char *owl_zwrite_get_class(const owl_zwrite *z)
278{
279  return(z->class);
280}
281
282const char *owl_zwrite_get_instance(const owl_zwrite *z)
283{
284  return(z->inst);
285}
286
287const char *owl_zwrite_get_opcode(const owl_zwrite *z)
288{
289  return(z->opcode);
290}
291
292void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode)
293{
294  g_free(z->opcode);
295  z->opcode=owl_validate_utf8(opcode);
296}
297
298const char *owl_zwrite_get_realm(const owl_zwrite *z)
299{
300  return(z->realm);
301}
302
303const char *owl_zwrite_get_zsig(const owl_zwrite *z)
304{
305  if (z->zsig) return(z->zsig);
306  return("");
307}
308
309void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig)
310{
311  g_free(z->zsig);
312  z->zsig = g_strdup(zsig);
313}
314
315int owl_zwrite_get_numrecips(const owl_zwrite *z)
316{
317  return z->recips->len;
318}
319
320const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n)
321{
322  return z->recips->pdata[n];
323}
324
325/* Caller must free the result. */
326CALLER_OWN char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n)
327{
328  if (z->realm[0]) {
329    return g_strdup_printf("%s@%s", owl_zwrite_get_recip_n(z, n), z->realm);
330  } else {
331    return g_strdup(owl_zwrite_get_recip_n(z, n));
332  }
333}
334
335bool owl_zwrite_recip_is_personal(const char *recipient)
336{
337  return recipient[0] && recipient[0] != '@';
338}
339
340bool owl_zwrite_is_personal(const owl_zwrite *z)
341{
342  /* return true if at least one of the recipients is personal */
343  int i;
344
345  for (i = 0; i < z->recips->len; i++)
346    if (owl_zwrite_recip_is_personal(z->recips->pdata[i]))
347      return true;
348  return false;
349}
350
351void owl_zwrite_delete(owl_zwrite *z)
352{
353  owl_zwrite_cleanup(z);
354  g_slice_free(owl_zwrite, z);
355}
356
357void owl_zwrite_cleanup(owl_zwrite *z)
358{
359  owl_ptr_array_free(z->recips, g_free);
360  g_free(z->cmd);
361  g_free(z->zwriteline);
362  g_free(z->class);
363  g_free(z->inst);
364  g_free(z->opcode);
365  g_free(z->realm);
366  g_free(z->message);
367  g_free(z->zsig);
368}
369
370/*
371 * Returns a zwrite line suitable for replying, specifically the
372 * message field is stripped out. Result should be freed with
373 * g_free.
374 *
375 * If not a CC, only the recip_index'th user will be replied to.
376 */
377CALLER_OWN char *owl_zwrite_get_replyline(const owl_zwrite *z, int recip_index)
378{
379  /* Match ordering in zwrite help. */
380  GString *buf = g_string_new("");
381  int i;
382
383  /* Disturbingly, it is apparently possible to z->cmd to be null if
384   * owl_zwrite_create_from_line got something starting with -. And we
385   * can't kill it because this is exported to perl. */
386  owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite");
387  if (z->noping) {
388    g_string_append(buf, " -n");
389  }
390  if (z->cc) {
391    g_string_append(buf, " -C");
392  }
393  if (strcmp(z->class, "message")) {
394    g_string_append(buf, " -c ");
395    owl_string_append_quoted_arg(buf, z->class);
396  }
397  if (strcmp(z->inst, "personal")) {
398    g_string_append(buf, " -i ");
399    owl_string_append_quoted_arg(buf, z->inst);
400  }
401  if (z->realm && z->realm[0] != '\0') {
402    g_string_append(buf, " -r ");
403    owl_string_append_quoted_arg(buf, z->realm);
404  }
405  if (z->opcode && z->opcode[0] != '\0') {
406    g_string_append(buf, " -O ");
407    owl_string_append_quoted_arg(buf, z->opcode);
408  }
409  if (z->cc) {
410    for (i = 0; i < z->recips->len; i++) {
411      g_string_append_c(buf, ' ');
412      owl_string_append_quoted_arg(buf, z->recips->pdata[i]);
413    }
414  } else if (recip_index < z->recips->len) {
415    g_string_append_c(buf, ' ');
416    owl_string_append_quoted_arg(buf, z->recips->pdata[recip_index]);
417  }
418
419  return g_string_free(buf, false);
420}
Note: See TracBrowser for help on using the repository browser.