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 = owl_malloc(sizeof *z); |
---|
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, i, len; |
---|
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 = owl_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=owl_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 | len = 0; |
---|
108 | for (i=0;i<myargc;i++) { |
---|
109 | len += strlen(myargv[i]) + 1; |
---|
110 | } |
---|
111 | len++; /* NULL after the last trailing ' ' in the loop below. */ |
---|
112 | msg = owl_malloc(len); |
---|
113 | msg[0] = '\0'; |
---|
114 | while (myargc) { |
---|
115 | strcat(msg, myargv[0]); |
---|
116 | strcat(msg, " "); |
---|
117 | myargc--; |
---|
118 | myargv++; |
---|
119 | } |
---|
120 | msg[strlen(msg)-1] = '\0'; |
---|
121 | break; |
---|
122 | } else if (!strcmp(myargv[0], "-C")) { |
---|
123 | z->cc=1; |
---|
124 | myargv++; |
---|
125 | myargc--; |
---|
126 | } else if (!strcmp(myargv[0], "-n")) { |
---|
127 | z->noping=1; |
---|
128 | myargv++; |
---|
129 | myargc--; |
---|
130 | } else { |
---|
131 | /* anything unattached is a recipient */ |
---|
132 | owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0])); |
---|
133 | myargv++; |
---|
134 | myargc--; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | owl_parse_delete(argv, argc); |
---|
139 | |
---|
140 | if (badargs) { |
---|
141 | return(-1); |
---|
142 | } |
---|
143 | |
---|
144 | if (z->class == NULL && |
---|
145 | z->inst == NULL && |
---|
146 | owl_list_get_size(&(z->recips))==0) { |
---|
147 | owl_function_error("You must specify a recipient for zwrite"); |
---|
148 | return(-1); |
---|
149 | } |
---|
150 | |
---|
151 | /* now deal with defaults */ |
---|
152 | if (z->class==NULL) z->class=owl_strdup("message"); |
---|
153 | if (z->inst==NULL) z->inst=owl_strdup("personal"); |
---|
154 | if (z->realm==NULL) z->realm=owl_strdup(""); |
---|
155 | if (z->opcode==NULL) z->opcode=owl_strdup(""); |
---|
156 | /* z->message is allowed to stay NULL */ |
---|
157 | |
---|
158 | if(msg) { |
---|
159 | owl_zwrite_set_message(z, msg); |
---|
160 | owl_free(msg); |
---|
161 | } |
---|
162 | |
---|
163 | return(0); |
---|
164 | } |
---|
165 | |
---|
166 | void owl_zwrite_populate_zsig(owl_zwrite *z) |
---|
167 | { |
---|
168 | /* get a zsig, if not given */ |
---|
169 | if (z->zsig != NULL) |
---|
170 | return; |
---|
171 | |
---|
172 | z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g)); |
---|
173 | } |
---|
174 | |
---|
175 | void owl_zwrite_send_ping(const owl_zwrite *z) |
---|
176 | { |
---|
177 | int i, j; |
---|
178 | char *to; |
---|
179 | |
---|
180 | if (z->noping) return; |
---|
181 | |
---|
182 | if (strcasecmp(z->class, "message")) { |
---|
183 | return; |
---|
184 | } |
---|
185 | |
---|
186 | /* if there are no recipients we won't send a ping, which |
---|
187 | is what we want */ |
---|
188 | j=owl_list_get_size(&(z->recips)); |
---|
189 | for (i=0; i<j; i++) { |
---|
190 | if (strcmp(z->realm, "")) { |
---|
191 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
192 | } else { |
---|
193 | to = owl_strdup(owl_list_get_element(&(z->recips), i)); |
---|
194 | } |
---|
195 | send_ping(to, z->class, z->inst); |
---|
196 | owl_free(to); |
---|
197 | } |
---|
198 | |
---|
199 | } |
---|
200 | |
---|
201 | /* Set the message with no post-processing*/ |
---|
202 | void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg) |
---|
203 | { |
---|
204 | if (z->message) owl_free(z->message); |
---|
205 | z->message = owl_validate_utf8(msg); |
---|
206 | } |
---|
207 | |
---|
208 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
209 | { |
---|
210 | int i, j; |
---|
211 | char *toline = NULL; |
---|
212 | char *tmp = NULL, *tmp2; |
---|
213 | |
---|
214 | if (z->message) owl_free(z->message); |
---|
215 | |
---|
216 | j=owl_list_get_size(&(z->recips)); |
---|
217 | if (j>0 && z->cc) { |
---|
218 | toline = owl_strdup( "CC: "); |
---|
219 | for (i=0; i<j; i++) { |
---|
220 | tmp = toline; |
---|
221 | if (strcmp(z->realm, "")) { |
---|
222 | toline = owl_sprintf( "%s%s@%s ", toline, (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
223 | } else { |
---|
224 | toline = owl_sprintf( "%s%s ", toline, (const char *) owl_list_get_element(&(z->recips), i)); |
---|
225 | } |
---|
226 | owl_free(tmp); |
---|
227 | tmp = NULL; |
---|
228 | } |
---|
229 | tmp = owl_validate_utf8(msg); |
---|
230 | tmp2 = owl_text_expand_tabs(tmp); |
---|
231 | z->message=owl_sprintf("%s\n%s", toline, tmp2); |
---|
232 | owl_free(toline); |
---|
233 | owl_free(tmp); |
---|
234 | owl_free(tmp2); |
---|
235 | } else { |
---|
236 | tmp=owl_validate_utf8(msg); |
---|
237 | z->message=owl_text_expand_tabs(tmp); |
---|
238 | owl_free(tmp); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
---|
243 | { |
---|
244 | if (z->message) return(z->message); |
---|
245 | return(""); |
---|
246 | } |
---|
247 | |
---|
248 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
---|
249 | { |
---|
250 | if (z->message) return(1); |
---|
251 | return(0); |
---|
252 | } |
---|
253 | |
---|
254 | int owl_zwrite_send_message(const owl_zwrite *z) |
---|
255 | { |
---|
256 | int i, j, ret = 0; |
---|
257 | char *to = NULL; |
---|
258 | |
---|
259 | if (z->message==NULL) return(-1); |
---|
260 | |
---|
261 | j=owl_list_get_size(&(z->recips)); |
---|
262 | if (j>0) { |
---|
263 | for (i=0; i<j; i++) { |
---|
264 | if (strcmp(z->realm, "")) { |
---|
265 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
266 | } else { |
---|
267 | to = owl_strdup( owl_list_get_element(&(z->recips), i)); |
---|
268 | } |
---|
269 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
270 | /* Abort on the first error, to match the zwrite binary. */ |
---|
271 | if (ret != 0) |
---|
272 | break; |
---|
273 | owl_free(to); |
---|
274 | to = NULL; |
---|
275 | } |
---|
276 | } else { |
---|
277 | to = owl_sprintf( "@%s", z->realm); |
---|
278 | ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
279 | } |
---|
280 | owl_free(to); |
---|
281 | return ret; |
---|
282 | } |
---|
283 | |
---|
284 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
285 | { |
---|
286 | owl_zwrite z; |
---|
287 | int rv; |
---|
288 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
289 | if (rv) return(rv); |
---|
290 | if (!owl_zwrite_is_message_set(&z)) { |
---|
291 | owl_zwrite_set_message(&z, msg); |
---|
292 | } |
---|
293 | owl_zwrite_populate_zsig(&z); |
---|
294 | owl_zwrite_send_message(&z); |
---|
295 | owl_zwrite_cleanup(&z); |
---|
296 | return(0); |
---|
297 | } |
---|
298 | |
---|
299 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
---|
300 | { |
---|
301 | return(z->class); |
---|
302 | } |
---|
303 | |
---|
304 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
---|
305 | { |
---|
306 | return(z->inst); |
---|
307 | } |
---|
308 | |
---|
309 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
---|
310 | { |
---|
311 | return(z->opcode); |
---|
312 | } |
---|
313 | |
---|
314 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
315 | { |
---|
316 | if (z->opcode) owl_free(z->opcode); |
---|
317 | z->opcode=owl_validate_utf8(opcode); |
---|
318 | } |
---|
319 | |
---|
320 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
---|
321 | { |
---|
322 | return(z->realm); |
---|
323 | } |
---|
324 | |
---|
325 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
---|
326 | { |
---|
327 | if (z->zsig) return(z->zsig); |
---|
328 | return(""); |
---|
329 | } |
---|
330 | |
---|
331 | void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig) |
---|
332 | { |
---|
333 | if(z->zsig) owl_free(z->zsig); |
---|
334 | z->zsig = owl_strdup(zsig); |
---|
335 | } |
---|
336 | |
---|
337 | void owl_zwrite_get_recipstr(const owl_zwrite *z, char *buff) |
---|
338 | { |
---|
339 | int i, j; |
---|
340 | |
---|
341 | strcpy(buff, ""); |
---|
342 | j=owl_list_get_size(&(z->recips)); |
---|
343 | for (i=0; i<j; i++) { |
---|
344 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
345 | strcat(buff, " "); |
---|
346 | } |
---|
347 | buff[strlen(buff)-1]='\0'; |
---|
348 | } |
---|
349 | |
---|
350 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
---|
351 | { |
---|
352 | return(owl_list_get_size(&(z->recips))); |
---|
353 | } |
---|
354 | |
---|
355 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
---|
356 | { |
---|
357 | return(owl_list_get_element(&(z->recips), n)); |
---|
358 | } |
---|
359 | |
---|
360 | int owl_zwrite_is_personal(const owl_zwrite *z) |
---|
361 | { |
---|
362 | /* return true if at least one of the recipients is personal */ |
---|
363 | int i, j; |
---|
364 | char *foo; |
---|
365 | |
---|
366 | j=owl_list_get_size(&(z->recips)); |
---|
367 | for (i=0; i<j; i++) { |
---|
368 | foo=owl_list_get_element(&(z->recips), i); |
---|
369 | if (foo[0]!='@') return(1); |
---|
370 | } |
---|
371 | return(0); |
---|
372 | } |
---|
373 | |
---|
374 | void owl_zwrite_delete(owl_zwrite *z) |
---|
375 | { |
---|
376 | owl_zwrite_cleanup(z); |
---|
377 | owl_free(z); |
---|
378 | } |
---|
379 | |
---|
380 | void owl_zwrite_cleanup(owl_zwrite *z) |
---|
381 | { |
---|
382 | owl_list_cleanup(&(z->recips), &owl_free); |
---|
383 | if (z->zwriteline) owl_free(z->zwriteline); |
---|
384 | if (z->class) owl_free(z->class); |
---|
385 | if (z->inst) owl_free(z->inst); |
---|
386 | if (z->opcode) owl_free(z->opcode); |
---|
387 | if (z->realm) owl_free(z->realm); |
---|
388 | if (z->message) owl_free(z->message); |
---|
389 | if (z->zsig) owl_free(z->zsig); |
---|
390 | } |
---|