1 | #include <string.h> |
---|
2 | #include <pwd.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include "owl.h" |
---|
6 | |
---|
7 | 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 | |
---|
17 | 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 | owl_list_create(&(z->recips)); |
---|
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 (owl_list_get_size(&(z->recips))<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 | owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0])); |
---|
120 | myargv++; |
---|
121 | myargc--; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | owl_parse_delete(argv, argc); |
---|
126 | |
---|
127 | if (badargs) { |
---|
128 | return(-1); |
---|
129 | } |
---|
130 | |
---|
131 | if (z->class == NULL && |
---|
132 | z->inst == NULL && |
---|
133 | owl_list_get_size(&(z->recips))==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 | |
---|
153 | void 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 | |
---|
162 | void owl_zwrite_send_ping(const owl_zwrite *z) |
---|
163 | { |
---|
164 | int i, j; |
---|
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 | j=owl_list_get_size(&(z->recips)); |
---|
176 | for (i=0; i<j; i++) { |
---|
177 | if (strcmp(z->realm, "")) { |
---|
178 | to = g_strdup_printf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
179 | } else { |
---|
180 | to = g_strdup(owl_list_get_element(&(z->recips), i)); |
---|
181 | } |
---|
182 | send_ping(to, z->class, z->inst); |
---|
183 | g_free(to); |
---|
184 | } |
---|
185 | |
---|
186 | } |
---|
187 | |
---|
188 | /* Set the message with no post-processing*/ |
---|
189 | void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg) |
---|
190 | { |
---|
191 | if (z->message) g_free(z->message); |
---|
192 | z->message = owl_validate_utf8(msg); |
---|
193 | } |
---|
194 | |
---|
195 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
196 | { |
---|
197 | int i, j; |
---|
198 | char *toline = NULL; |
---|
199 | char *tmp = NULL, *tmp2; |
---|
200 | |
---|
201 | if (z->message) g_free(z->message); |
---|
202 | |
---|
203 | j=owl_list_get_size(&(z->recips)); |
---|
204 | if (j>0 && z->cc) { |
---|
205 | toline = g_strdup( "CC: "); |
---|
206 | for (i=0; i<j; i++) { |
---|
207 | tmp = toline; |
---|
208 | if (strcmp(z->realm, "")) { |
---|
209 | toline = g_strdup_printf( "%s%s@%s ", toline, (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
210 | } else { |
---|
211 | toline = g_strdup_printf( "%s%s ", toline, (const char *) owl_list_get_element(&(z->recips), i)); |
---|
212 | } |
---|
213 | g_free(tmp); |
---|
214 | tmp = NULL; |
---|
215 | } |
---|
216 | tmp = owl_validate_utf8(msg); |
---|
217 | tmp2 = owl_text_expand_tabs(tmp); |
---|
218 | z->message=g_strdup_printf("%s\n%s", toline, tmp2); |
---|
219 | g_free(toline); |
---|
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 | |
---|
229 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
---|
230 | { |
---|
231 | if (z->message) return(z->message); |
---|
232 | return(""); |
---|
233 | } |
---|
234 | |
---|
235 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
---|
236 | { |
---|
237 | if (z->message) return(1); |
---|
238 | return(0); |
---|
239 | } |
---|
240 | |
---|
241 | int owl_zwrite_send_message(const owl_zwrite *z) |
---|
242 | { |
---|
243 | int i, j, ret = 0; |
---|
244 | char *to = NULL; |
---|
245 | |
---|
246 | if (z->message==NULL) return(-1); |
---|
247 | |
---|
248 | j=owl_list_get_size(&(z->recips)); |
---|
249 | if (j>0) { |
---|
250 | for (i=0; i<j; i++) { |
---|
251 | if (strcmp(z->realm, "")) { |
---|
252 | to = g_strdup_printf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
253 | } else { |
---|
254 | to = g_strdup( owl_list_get_element(&(z->recips), i)); |
---|
255 | } |
---|
256 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
257 | /* Abort on the first error, to match the zwrite binary. */ |
---|
258 | if (ret != 0) |
---|
259 | break; |
---|
260 | g_free(to); |
---|
261 | to = NULL; |
---|
262 | } |
---|
263 | } else { |
---|
264 | to = g_strdup_printf( "@%s", z->realm); |
---|
265 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
266 | } |
---|
267 | g_free(to); |
---|
268 | return ret; |
---|
269 | } |
---|
270 | |
---|
271 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
272 | { |
---|
273 | owl_zwrite z; |
---|
274 | int rv; |
---|
275 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
276 | if (rv) return(rv); |
---|
277 | if (!owl_zwrite_is_message_set(&z)) { |
---|
278 | owl_zwrite_set_message(&z, msg); |
---|
279 | } |
---|
280 | owl_zwrite_populate_zsig(&z); |
---|
281 | owl_zwrite_send_message(&z); |
---|
282 | owl_zwrite_cleanup(&z); |
---|
283 | return(0); |
---|
284 | } |
---|
285 | |
---|
286 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
---|
287 | { |
---|
288 | return(z->class); |
---|
289 | } |
---|
290 | |
---|
291 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
---|
292 | { |
---|
293 | return(z->inst); |
---|
294 | } |
---|
295 | |
---|
296 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
---|
297 | { |
---|
298 | return(z->opcode); |
---|
299 | } |
---|
300 | |
---|
301 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
302 | { |
---|
303 | if (z->opcode) g_free(z->opcode); |
---|
304 | z->opcode=owl_validate_utf8(opcode); |
---|
305 | } |
---|
306 | |
---|
307 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
---|
308 | { |
---|
309 | return(z->realm); |
---|
310 | } |
---|
311 | |
---|
312 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
---|
313 | { |
---|
314 | if (z->zsig) return(z->zsig); |
---|
315 | return(""); |
---|
316 | } |
---|
317 | |
---|
318 | void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig) |
---|
319 | { |
---|
320 | if(z->zsig) g_free(z->zsig); |
---|
321 | z->zsig = g_strdup(zsig); |
---|
322 | } |
---|
323 | |
---|
324 | void owl_zwrite_get_recipstr(const owl_zwrite *z, char *buff) |
---|
325 | { |
---|
326 | int i, j; |
---|
327 | |
---|
328 | strcpy(buff, ""); |
---|
329 | j=owl_list_get_size(&(z->recips)); |
---|
330 | for (i=0; i<j; i++) { |
---|
331 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
332 | strcat(buff, " "); |
---|
333 | } |
---|
334 | buff[strlen(buff)-1]='\0'; |
---|
335 | } |
---|
336 | |
---|
337 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
---|
338 | { |
---|
339 | return(owl_list_get_size(&(z->recips))); |
---|
340 | } |
---|
341 | |
---|
342 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
---|
343 | { |
---|
344 | return(owl_list_get_element(&(z->recips), n)); |
---|
345 | } |
---|
346 | |
---|
347 | int owl_zwrite_is_personal(const owl_zwrite *z) |
---|
348 | { |
---|
349 | /* return true if at least one of the recipients is personal */ |
---|
350 | int i, j; |
---|
351 | char *foo; |
---|
352 | |
---|
353 | j=owl_list_get_size(&(z->recips)); |
---|
354 | for (i=0; i<j; i++) { |
---|
355 | foo=owl_list_get_element(&(z->recips), i); |
---|
356 | if (foo[0]!='@') return(1); |
---|
357 | } |
---|
358 | return(0); |
---|
359 | } |
---|
360 | |
---|
361 | void owl_zwrite_delete(owl_zwrite *z) |
---|
362 | { |
---|
363 | owl_zwrite_cleanup(z); |
---|
364 | g_free(z); |
---|
365 | } |
---|
366 | |
---|
367 | void owl_zwrite_cleanup(owl_zwrite *z) |
---|
368 | { |
---|
369 | owl_list_cleanup(&(z->recips), &g_free); |
---|
370 | if (z->cmd) g_free(z->cmd); |
---|
371 | if (z->zwriteline) g_free(z->zwriteline); |
---|
372 | if (z->class) g_free(z->class); |
---|
373 | if (z->inst) g_free(z->inst); |
---|
374 | if (z->opcode) g_free(z->opcode); |
---|
375 | if (z->realm) g_free(z->realm); |
---|
376 | if (z->message) g_free(z->message); |
---|
377 | if (z->zsig) g_free(z->zsig); |
---|
378 | } |
---|
379 | |
---|
380 | /* |
---|
381 | * Returns a zwrite line suitable for replying, specifically the |
---|
382 | * message field is stripped out. Result should be freed with |
---|
383 | * g_free. |
---|
384 | */ |
---|
385 | char *owl_zwrite_get_replyline(const owl_zwrite *z) |
---|
386 | { |
---|
387 | /* Match ordering in zwrite help. */ |
---|
388 | GString *buf = g_string_new(""); |
---|
389 | int i; |
---|
390 | |
---|
391 | /* Disturbingly, it is apparently possible to z->cmd to be null if |
---|
392 | * owl_zwrite_create_from_line got something starting with -. And we |
---|
393 | * can't kill it because this is exported to perl. */ |
---|
394 | owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite"); |
---|
395 | if (z->noping) { |
---|
396 | g_string_append(buf, " -n"); |
---|
397 | } |
---|
398 | if (z->cc) { |
---|
399 | g_string_append(buf, " -C"); |
---|
400 | } |
---|
401 | if (strcmp(z->class, "message")) { |
---|
402 | g_string_append(buf, " -c "); |
---|
403 | owl_string_append_quoted_arg(buf, z->class); |
---|
404 | } |
---|
405 | if (strcmp(z->inst, "personal")) { |
---|
406 | g_string_append(buf, " -i "); |
---|
407 | owl_string_append_quoted_arg(buf, z->inst); |
---|
408 | } |
---|
409 | if (z->realm && z->realm[0] != '\0') { |
---|
410 | g_string_append(buf, " -r "); |
---|
411 | owl_string_append_quoted_arg(buf, z->realm); |
---|
412 | } |
---|
413 | if (z->opcode && z->opcode[0] != '\0') { |
---|
414 | g_string_append(buf, " -O "); |
---|
415 | owl_string_append_quoted_arg(buf, z->opcode); |
---|
416 | } |
---|
417 | for (i = 0; i < owl_list_get_size(&(z->recips)); i++) { |
---|
418 | g_string_append_c(buf, ' '); |
---|
419 | owl_string_append_quoted_arg(buf, owl_list_get_element(&(z->recips), i)); |
---|
420 | } |
---|
421 | |
---|
422 | return g_string_free(buf, false); |
---|
423 | } |
---|