source: zwrite.c @ d6c2e03

Last change on this file since d6c2e03 was d6c2e03, checked in by David Benjamin <davidben@mit.edu>, 11 years ago
Allow building a zwrite from an argv Add new versions of owl_zwrite_create and owl_zwrite_new. Note that this means we rebuild z->zwriteline from scratch unconditionally. The only behavior change is that the user's spacing and quoting is not preserved in zwrite locktext. For instance :zwrite davidben -i "spaces yay?" now gets a locktext of ----> zwrite davidben -i 'spaces yay?' I think this isn't a big deal. It already happens for every other *write locktext, and if we kill the buff argument, it'll happen there too.
  • Property mode set to 100644
File size: 9.4 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_new(owl_zwrite, 1);
6  if (owl_zwrite_create_from_line(z, line) < 0) {
7    owl_zwrite_delete(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_new(owl_zwrite, 1);
16  if (owl_zwrite_create(z, argc, argv) < 0) {
17    owl_zwrite_delete(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, myargc;
43  const char *const *myargv;
44  char *msg = NULL;
45
46  badargs=0;
47 
48  /* start with null entries */
49  z->cmd=NULL;
50  z->realm=NULL;
51  z->class=NULL;
52  z->inst=NULL;
53  z->opcode=NULL;
54  z->zsig=NULL;
55  z->message=NULL;
56  z->cc=0;
57  z->noping=0;
58  z->recips = g_ptr_array_new();
59  z->zwriteline = owl_argv_quote(argc, argv);
60
61  myargc = argc;
62  myargv = argv;
63  if (myargc && *(myargv[0])!='-') {
64    z->cmd=g_strdup(myargv[0]);
65    myargc--;
66    myargv++;
67  }
68  while (myargc) {
69    if (!strcmp(myargv[0], "-c")) {
70      if (myargc<2) {
71        badargs=1;
72        break;
73      }
74      z->class=owl_validate_utf8(myargv[1]);
75      myargv+=2;
76      myargc-=2;
77    } else if (!strcmp(myargv[0], "-i")) {
78      if (myargc<2) {
79        badargs=1;
80        break;
81      }
82      z->inst=owl_validate_utf8(myargv[1]);
83      myargv+=2;
84      myargc-=2;
85    } else if (!strcmp(myargv[0], "-r")) {
86      if (myargc<2) {
87        badargs=1;
88        break;
89      }
90      z->realm=owl_validate_utf8(myargv[1]);
91      myargv+=2;
92      myargc-=2;
93    } else if (!strcmp(myargv[0], "-s")) {
94      if (myargc<2) {
95        badargs=1;
96        break;
97      }
98      z->zsig=owl_validate_utf8(myargv[1]);
99      myargv+=2;
100      myargc-=2;
101    } else if (!strcmp(myargv[0], "-O")) {
102      if (myargc<2) {
103        badargs=1;
104        break;
105      }
106      z->opcode=owl_validate_utf8(myargv[1]);
107      myargv+=2;
108      myargc-=2;
109    } else if (!strcmp(myargv[0], "-m")) {
110      if (myargc<2) {
111        badargs=1;
112        break;
113      }
114      /* we must already have users or a class or an instance */
115      if (z->recips->len < 1 && (!z->class) && (!z->inst)) {
116        badargs=1;
117        break;
118      }
119
120      /* Once we have -m, gobble up everything else on the line */
121      myargv++;
122      myargc--;
123      msg = g_strjoinv(" ", (char**)myargv);
124      break;
125    } else if (!strcmp(myargv[0], "-C")) {
126      z->cc=1;
127      myargv++;
128      myargc--;
129    } else if (!strcmp(myargv[0], "-n")) {
130      z->noping=1;
131      myargv++;
132      myargc--;
133    } else {
134      /* anything unattached is a recipient */
135      g_ptr_array_add(z->recips, owl_validate_utf8(myargv[0]));
136      myargv++;
137      myargc--;
138    }
139  }
140
141  if (badargs) {
142    return(-1);
143  }
144
145  if (z->class == NULL &&
146      z->inst == NULL &&
147      z->recips->len == 0) {
148    owl_function_error("You must specify a recipient for zwrite");
149    return(-1);
150  }
151
152  /* now deal with defaults */
153  if (z->class==NULL) z->class=g_strdup("message");
154  if (z->inst==NULL) z->inst=g_strdup("personal");
155  if (z->realm==NULL) z->realm=g_strdup("");
156  if (z->opcode==NULL) z->opcode=g_strdup("");
157  /* z->message is allowed to stay NULL */
158
159  if(msg) {
160    owl_zwrite_set_message(z, msg);
161    g_free(msg);
162  }
163
164  return(0);
165}
166
167void owl_zwrite_populate_zsig(owl_zwrite *z)
168{
169  /* get a zsig, if not given */
170  if (z->zsig != NULL)
171    return;
172
173  z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g));
174}
175
176void owl_zwrite_send_ping(const owl_zwrite *z)
177{
178  int i;
179  char *to;
180
181  if (z->noping) return;
182 
183  if (strcasecmp(z->class, "message")) {
184    return;
185  }
186
187  /* if there are no recipients we won't send a ping, which
188     is what we want */
189  for (i = 0; i < z->recips->len; i++) {
190    to = owl_zwrite_get_recip_n_with_realm(z, i);
191    send_ping(to, z->class, z->inst);
192    g_free(to);
193  }
194
195}
196
197/* Set the message with no post-processing*/
198void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg)
199{
200  g_free(z->message);
201  z->message = owl_validate_utf8(msg);
202}
203
204void owl_zwrite_set_message(owl_zwrite *z, const char *msg)
205{
206  int i;
207  GString *message;
208  char *tmp = NULL, *tmp2;
209
210  g_free(z->message);
211
212  if (z->recips->len > 0 && z->cc) {
213    message = g_string_new("CC: ");
214    for (i = 0; i < z->recips->len; i++) {
215      tmp = owl_zwrite_get_recip_n_with_realm(z, i);
216      g_string_append_printf(message, "%s ", tmp);
217      g_free(tmp);
218      tmp = NULL;
219    }
220    tmp = owl_validate_utf8(msg);
221    tmp2 = owl_text_expand_tabs(tmp);
222    g_string_append_printf(message, "\n%s", tmp2);
223    z->message = g_string_free(message, false);
224    g_free(tmp);
225    g_free(tmp2);
226  } else {
227    tmp=owl_validate_utf8(msg);
228    z->message=owl_text_expand_tabs(tmp);
229    g_free(tmp);
230  }
231}
232
233const char *owl_zwrite_get_message(const owl_zwrite *z)
234{
235  if (z->message) return(z->message);
236  return("");
237}
238
239int owl_zwrite_is_message_set(const owl_zwrite *z)
240{
241  if (z->message) return(1);
242  return(0);
243}
244
245int owl_zwrite_send_message(const owl_zwrite *z)
246{
247  int i, ret = 0;
248  char *to = NULL;
249
250  if (z->message==NULL) return(-1);
251
252  if (z->recips->len > 0) {
253    for (i = 0; i < z->recips->len; i++) {
254      to = owl_zwrite_get_recip_n_with_realm(z, i);
255      ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
256      /* Abort on the first error, to match the zwrite binary. */
257      if (ret != 0)
258        break;
259      g_free(to);
260      to = NULL;
261    }
262  } else {
263    to = g_strdup_printf( "@%s", z->realm);
264    ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
265  }
266  g_free(to);
267  return ret;
268}
269
270int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg)
271{
272  owl_zwrite z;
273  int rv;
274  rv=owl_zwrite_create_from_line(&z, cmd);
275  if (rv) return(rv);
276  if (!owl_zwrite_is_message_set(&z)) {
277    owl_zwrite_set_message(&z, msg);
278  }
279  owl_zwrite_populate_zsig(&z);
280  owl_zwrite_send_message(&z);
281  owl_zwrite_cleanup(&z);
282  return(0);
283}
284
285const char *owl_zwrite_get_class(const owl_zwrite *z)
286{
287  return(z->class);
288}
289
290const char *owl_zwrite_get_instance(const owl_zwrite *z)
291{
292  return(z->inst);
293}
294
295const char *owl_zwrite_get_opcode(const owl_zwrite *z)
296{
297  return(z->opcode);
298}
299
300void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode)
301{
302  g_free(z->opcode);
303  z->opcode=owl_validate_utf8(opcode);
304}
305
306const char *owl_zwrite_get_realm(const owl_zwrite *z)
307{
308  return(z->realm);
309}
310
311const char *owl_zwrite_get_zsig(const owl_zwrite *z)
312{
313  if (z->zsig) return(z->zsig);
314  return("");
315}
316
317void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig)
318{
319  g_free(z->zsig);
320  z->zsig = g_strdup(zsig);
321}
322
323int owl_zwrite_get_numrecips(const owl_zwrite *z)
324{
325  return z->recips->len;
326}
327
328const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n)
329{
330  return z->recips->pdata[n];
331}
332
333/* Caller must free the result. */
334CALLER_OWN char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n)
335{
336  if (z->realm[0]) {
337    return g_strdup_printf("%s@%s", owl_zwrite_get_recip_n(z, n), z->realm);
338  } else {
339    return g_strdup(owl_zwrite_get_recip_n(z, n));
340  }
341}
342
343int owl_zwrite_is_personal(const owl_zwrite *z)
344{
345  /* return true if at least one of the recipients is personal */
346  int i;
347  char *recip;
348
349  for (i = 0; i < z->recips->len; i++) {
350    recip = z->recips->pdata[i];
351    if (recip[0] != '@') return 1;
352  }
353  return(0);
354}
355
356void owl_zwrite_delete(owl_zwrite *z)
357{
358  owl_zwrite_cleanup(z);
359  g_free(z);
360}
361
362void owl_zwrite_cleanup(owl_zwrite *z)
363{
364  owl_ptr_array_free(z->recips, g_free);
365  g_free(z->cmd);
366  g_free(z->zwriteline);
367  g_free(z->class);
368  g_free(z->inst);
369  g_free(z->opcode);
370  g_free(z->realm);
371  g_free(z->message);
372  g_free(z->zsig);
373}
374
375/*
376 * Returns a zwrite line suitable for replying, specifically the
377 * message field is stripped out. Result should be freed with
378 * g_free.
379 *
380 * If not a CC, only the recip_index'th user will be replied to.
381 */
382CALLER_OWN char *owl_zwrite_get_replyline(const owl_zwrite *z, int recip_index)
383{
384  /* Match ordering in zwrite help. */
385  GString *buf = g_string_new("");
386  int i;
387
388  /* Disturbingly, it is apparently possible to z->cmd to be null if
389   * owl_zwrite_create_from_line got something starting with -. And we
390   * can't kill it because this is exported to perl. */
391  owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite");
392  if (z->noping) {
393    g_string_append(buf, " -n");
394  }
395  if (z->cc) {
396    g_string_append(buf, " -C");
397  }
398  if (strcmp(z->class, "message")) {
399    g_string_append(buf, " -c ");
400    owl_string_append_quoted_arg(buf, z->class);
401  }
402  if (strcmp(z->inst, "personal")) {
403    g_string_append(buf, " -i ");
404    owl_string_append_quoted_arg(buf, z->inst);
405  }
406  if (z->realm && z->realm[0] != '\0') {
407    g_string_append(buf, " -r ");
408    owl_string_append_quoted_arg(buf, z->realm);
409  }
410  if (z->opcode && z->opcode[0] != '\0') {
411    g_string_append(buf, " -O ");
412    owl_string_append_quoted_arg(buf, z->opcode);
413  }
414  if (z->cc) {
415    for (i = 0; i < z->recips->len; i++) {
416      g_string_append_c(buf, ' ');
417      owl_string_append_quoted_arg(buf, z->recips->pdata[i]);
418    }
419  } else if (recip_index < z->recips->len) {
420    g_string_append_c(buf, ' ');
421    owl_string_append_quoted_arg(buf, z->recips->pdata[recip_index]);
422  }
423
424  return g_string_free(buf, false);
425}
Note: See TracBrowser for help on using the repository browser.