source: zwrite.c @ 8f5afcf

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