source: zwrite.c @ d9c6631

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