1 | #include "owl.h" |
---|
2 | |
---|
3 | CALLER_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 | g_free(z); |
---|
8 | return NULL; |
---|
9 | } |
---|
10 | return z; |
---|
11 | } |
---|
12 | |
---|
13 | CALLER_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 | g_free(z); |
---|
18 | return NULL; |
---|
19 | } |
---|
20 | return z; |
---|
21 | } |
---|
22 | |
---|
23 | G_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 | |
---|
40 | G_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 | |
---|
164 | void 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 | |
---|
173 | void 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 | /* if there are no recipients we won't send a ping, which |
---|
185 | is what we want */ |
---|
186 | for (i = 0; i < z->recips->len; i++) { |
---|
187 | to = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
188 | send_ping(to, z->class, z->inst); |
---|
189 | g_free(to); |
---|
190 | } |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | /* Set the message with no post-processing*/ |
---|
195 | void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg) |
---|
196 | { |
---|
197 | g_free(z->message); |
---|
198 | z->message = owl_validate_utf8(msg); |
---|
199 | } |
---|
200 | |
---|
201 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
202 | { |
---|
203 | int i; |
---|
204 | GString *message; |
---|
205 | char *tmp = NULL, *tmp2; |
---|
206 | |
---|
207 | g_free(z->message); |
---|
208 | |
---|
209 | if (z->recips->len > 0 && z->cc) { |
---|
210 | message = g_string_new("CC: "); |
---|
211 | for (i = 0; i < z->recips->len; i++) { |
---|
212 | tmp = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
213 | g_string_append_printf(message, "%s ", tmp); |
---|
214 | g_free(tmp); |
---|
215 | tmp = NULL; |
---|
216 | } |
---|
217 | tmp = owl_validate_utf8(msg); |
---|
218 | tmp2 = owl_text_expand_tabs(tmp); |
---|
219 | g_string_append_printf(message, "\n%s", tmp2); |
---|
220 | z->message = g_string_free(message, false); |
---|
221 | g_free(tmp); |
---|
222 | g_free(tmp2); |
---|
223 | } else { |
---|
224 | tmp=owl_validate_utf8(msg); |
---|
225 | z->message=owl_text_expand_tabs(tmp); |
---|
226 | g_free(tmp); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
---|
231 | { |
---|
232 | if (z->message) return(z->message); |
---|
233 | return(""); |
---|
234 | } |
---|
235 | |
---|
236 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
---|
237 | { |
---|
238 | if (z->message) return(1); |
---|
239 | return(0); |
---|
240 | } |
---|
241 | |
---|
242 | int owl_zwrite_send_message(const owl_zwrite *z) |
---|
243 | { |
---|
244 | int i, ret = 0; |
---|
245 | char *to = NULL; |
---|
246 | |
---|
247 | if (z->message==NULL) return(-1); |
---|
248 | |
---|
249 | if (z->recips->len > 0) { |
---|
250 | for (i = 0; i < z->recips->len; i++) { |
---|
251 | to = owl_zwrite_get_recip_n_with_realm(z, i); |
---|
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; |
---|
256 | g_free(to); |
---|
257 | to = NULL; |
---|
258 | } |
---|
259 | } else { |
---|
260 | to = g_strdup_printf( "@%s", z->realm); |
---|
261 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
262 | } |
---|
263 | g_free(to); |
---|
264 | return ret; |
---|
265 | } |
---|
266 | |
---|
267 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
268 | { |
---|
269 | owl_zwrite z; |
---|
270 | int rv; |
---|
271 | rv = owl_zwrite_create_from_line(&z, cmd); |
---|
272 | if (rv != 0) return rv; |
---|
273 | if (!owl_zwrite_is_message_set(&z)) { |
---|
274 | owl_zwrite_set_message(&z, msg); |
---|
275 | } |
---|
276 | owl_zwrite_populate_zsig(&z); |
---|
277 | owl_zwrite_send_message(&z); |
---|
278 | owl_zwrite_cleanup(&z); |
---|
279 | return(0); |
---|
280 | } |
---|
281 | |
---|
282 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
---|
283 | { |
---|
284 | return(z->class); |
---|
285 | } |
---|
286 | |
---|
287 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
---|
288 | { |
---|
289 | return(z->inst); |
---|
290 | } |
---|
291 | |
---|
292 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
---|
293 | { |
---|
294 | return(z->opcode); |
---|
295 | } |
---|
296 | |
---|
297 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
298 | { |
---|
299 | g_free(z->opcode); |
---|
300 | z->opcode=owl_validate_utf8(opcode); |
---|
301 | } |
---|
302 | |
---|
303 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
---|
304 | { |
---|
305 | return(z->realm); |
---|
306 | } |
---|
307 | |
---|
308 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
---|
309 | { |
---|
310 | if (z->zsig) return(z->zsig); |
---|
311 | return(""); |
---|
312 | } |
---|
313 | |
---|
314 | void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig) |
---|
315 | { |
---|
316 | g_free(z->zsig); |
---|
317 | z->zsig = g_strdup(zsig); |
---|
318 | } |
---|
319 | |
---|
320 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
---|
321 | { |
---|
322 | return z->recips->len; |
---|
323 | } |
---|
324 | |
---|
325 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
---|
326 | { |
---|
327 | return z->recips->pdata[n]; |
---|
328 | } |
---|
329 | |
---|
330 | /* Caller must free the result. */ |
---|
331 | CALLER_OWN char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n) |
---|
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 | |
---|
340 | bool owl_zwrite_recip_is_personal(const char *recipient) |
---|
341 | { |
---|
342 | return recipient[0] && recipient[0] != '@'; |
---|
343 | } |
---|
344 | |
---|
345 | bool owl_zwrite_is_personal(const owl_zwrite *z) |
---|
346 | { |
---|
347 | /* return true if at least one of the recipients is personal */ |
---|
348 | int i; |
---|
349 | |
---|
350 | for (i = 0; i < z->recips->len; i++) |
---|
351 | if (owl_zwrite_recip_is_personal(z->recips->pdata[i])) |
---|
352 | return true; |
---|
353 | return false; |
---|
354 | } |
---|
355 | |
---|
356 | void owl_zwrite_delete(owl_zwrite *z) |
---|
357 | { |
---|
358 | owl_zwrite_cleanup(z); |
---|
359 | g_free(z); |
---|
360 | } |
---|
361 | |
---|
362 | void 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 | */ |
---|
382 | CALLER_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 | } |
---|