1 | #include <string.h> |
---|
2 | #include <pwd.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <unistd.h> |
---|
5 | #include "owl.h" |
---|
6 | |
---|
7 | int owl_zwrite_create_from_line(owl_zwrite *z, const char *line) |
---|
8 | { |
---|
9 | int argc, badargs, myargc, i, len; |
---|
10 | char **argv; |
---|
11 | const char *const *myargv; |
---|
12 | char *msg = NULL; |
---|
13 | |
---|
14 | badargs=0; |
---|
15 | |
---|
16 | /* start with null entries */ |
---|
17 | z->realm=NULL; |
---|
18 | z->class=NULL; |
---|
19 | z->inst=NULL; |
---|
20 | z->opcode=NULL; |
---|
21 | z->zsig=NULL; |
---|
22 | z->message=NULL; |
---|
23 | z->cc=0; |
---|
24 | z->noping=0; |
---|
25 | owl_list_create(&(z->recips)); |
---|
26 | |
---|
27 | /* parse the command line for options */ |
---|
28 | argv=owl_parseline(line, &argc); |
---|
29 | myargv=strs(argv); |
---|
30 | if (argc<0) { |
---|
31 | owl_function_error("Unbalanced quotes in zwrite"); |
---|
32 | return(-1); |
---|
33 | } |
---|
34 | myargc=argc; |
---|
35 | if (myargc && *(myargv[0])!='-') { |
---|
36 | myargc--; |
---|
37 | myargv++; |
---|
38 | } |
---|
39 | while (myargc) { |
---|
40 | if (!strcmp(myargv[0], "-c")) { |
---|
41 | if (myargc<2) { |
---|
42 | badargs=1; |
---|
43 | break; |
---|
44 | } |
---|
45 | z->class=owl_validate_utf8(myargv[1]); |
---|
46 | myargv+=2; |
---|
47 | myargc-=2; |
---|
48 | } else if (!strcmp(myargv[0], "-i")) { |
---|
49 | if (myargc<2) { |
---|
50 | badargs=1; |
---|
51 | break; |
---|
52 | } |
---|
53 | z->inst=owl_validate_utf8(myargv[1]); |
---|
54 | myargv+=2; |
---|
55 | myargc-=2; |
---|
56 | } else if (!strcmp(myargv[0], "-r")) { |
---|
57 | if (myargc<2) { |
---|
58 | badargs=1; |
---|
59 | break; |
---|
60 | } |
---|
61 | z->realm=owl_validate_utf8(myargv[1]); |
---|
62 | myargv+=2; |
---|
63 | myargc-=2; |
---|
64 | } else if (!strcmp(myargv[0], "-s")) { |
---|
65 | if (myargc<2) { |
---|
66 | badargs=1; |
---|
67 | break; |
---|
68 | } |
---|
69 | z->zsig=owl_validate_utf8(myargv[1]); |
---|
70 | myargv+=2; |
---|
71 | myargc-=2; |
---|
72 | } else if (!strcmp(myargv[0], "-O")) { |
---|
73 | if (myargc<2) { |
---|
74 | badargs=1; |
---|
75 | break; |
---|
76 | } |
---|
77 | z->opcode=owl_validate_utf8(myargv[1]); |
---|
78 | myargv+=2; |
---|
79 | myargc-=2; |
---|
80 | } else if (!strcmp(myargv[0], "-m")) { |
---|
81 | if (myargc<2) { |
---|
82 | badargs=1; |
---|
83 | break; |
---|
84 | } |
---|
85 | /* we must already have users or a class or an instance */ |
---|
86 | if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) { |
---|
87 | badargs=1; |
---|
88 | break; |
---|
89 | } |
---|
90 | |
---|
91 | /* Once we have -m, gobble up everything else on the line */ |
---|
92 | myargv++; |
---|
93 | myargc--; |
---|
94 | len = 0; |
---|
95 | for (i=0;i<myargc;i++) { |
---|
96 | len += strlen(myargv[i-1]) + 1; |
---|
97 | } |
---|
98 | msg = owl_malloc(len); |
---|
99 | msg[0] = '\0'; |
---|
100 | while (myargc) { |
---|
101 | strcat(msg, myargv[0]); |
---|
102 | strcat(msg, " "); |
---|
103 | myargc--; |
---|
104 | myargv++; |
---|
105 | } |
---|
106 | msg[strlen(msg)-1] = '\0'; |
---|
107 | break; |
---|
108 | } else if (!strcmp(myargv[0], "-C")) { |
---|
109 | z->cc=1; |
---|
110 | myargv++; |
---|
111 | myargc--; |
---|
112 | } else if (!strcmp(myargv[0], "-n")) { |
---|
113 | z->noping=1; |
---|
114 | myargv++; |
---|
115 | myargc--; |
---|
116 | } else { |
---|
117 | /* anything unattached is a recipient */ |
---|
118 | owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0])); |
---|
119 | myargv++; |
---|
120 | myargc--; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | owl_parsefree(argv, argc); |
---|
125 | |
---|
126 | if (badargs) { |
---|
127 | return(-1); |
---|
128 | } |
---|
129 | |
---|
130 | if (z->class == NULL && |
---|
131 | z->inst == NULL && |
---|
132 | owl_list_get_size(&(z->recips))==0) { |
---|
133 | owl_function_error("You must specify a recipient for zwrite"); |
---|
134 | return(-1); |
---|
135 | } |
---|
136 | |
---|
137 | /* now deal with defaults */ |
---|
138 | if (z->class==NULL) z->class=owl_strdup("message"); |
---|
139 | if (z->inst==NULL) z->inst=owl_strdup("personal"); |
---|
140 | if (z->realm==NULL) z->realm=owl_strdup(""); |
---|
141 | if (z->opcode==NULL) z->opcode=owl_strdup(""); |
---|
142 | /* z->message is allowed to stay NULL */ |
---|
143 | |
---|
144 | if(msg) { |
---|
145 | owl_zwrite_set_message(z, msg); |
---|
146 | owl_free(msg); |
---|
147 | } |
---|
148 | |
---|
149 | return(0); |
---|
150 | } |
---|
151 | |
---|
152 | void owl_zwrite_populate_zsig(owl_zwrite *z) |
---|
153 | { |
---|
154 | const char *zsigproc, *zsigowlvar, *zsigzvar; |
---|
155 | char *ptr; |
---|
156 | struct passwd *pw; |
---|
157 | |
---|
158 | /* get a zsig, if not given */ |
---|
159 | if (z->zsig==NULL) { |
---|
160 | zsigproc = owl_global_get_zsigproc(&g); |
---|
161 | zsigowlvar = owl_global_get_zsig(&g); |
---|
162 | zsigzvar = owl_zephyr_get_variable("zwrite-signature"); |
---|
163 | |
---|
164 | if (zsigowlvar && *zsigowlvar) { |
---|
165 | z->zsig=owl_validate_utf8(zsigowlvar); |
---|
166 | } else if (zsigproc && *zsigproc) { |
---|
167 | FILE *file; |
---|
168 | char buff[LINE], *openline; |
---|
169 | |
---|
170 | /* simple hack for now to nuke stderr */ |
---|
171 | openline=owl_malloc(strlen(zsigproc)+40); |
---|
172 | strcpy(openline, zsigproc); |
---|
173 | #if !(OWL_STDERR_REDIR) |
---|
174 | strcat(openline, " 2> /dev/null"); |
---|
175 | #endif |
---|
176 | file=popen(openline, "r"); |
---|
177 | owl_free(openline); |
---|
178 | if (!file) { |
---|
179 | if (zsigzvar && *zsigzvar) { |
---|
180 | z->zsig=owl_validate_utf8(zsigzvar); |
---|
181 | } |
---|
182 | } else { |
---|
183 | z->zsig=owl_malloc(LINE*5); |
---|
184 | strcpy(z->zsig, ""); |
---|
185 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
186 | strcat(z->zsig, buff); |
---|
187 | } |
---|
188 | pclose(file); |
---|
189 | if (z->zsig[strlen(z->zsig)-1]=='\n') { |
---|
190 | z->zsig[strlen(z->zsig)-1]='\0'; |
---|
191 | } |
---|
192 | } |
---|
193 | } else if (zsigzvar) { |
---|
194 | z->zsig=owl_validate_utf8(zsigzvar); |
---|
195 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
196 | z->zsig=owl_validate_utf8(pw->pw_gecos); |
---|
197 | ptr=strchr(z->zsig, ','); |
---|
198 | if (ptr) { |
---|
199 | ptr[0]='\0'; |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | void owl_zwrite_send_ping(owl_zwrite *z) |
---|
206 | { |
---|
207 | int i, j; |
---|
208 | char *to; |
---|
209 | |
---|
210 | if (z->noping) return; |
---|
211 | |
---|
212 | if (strcasecmp(z->class, "message")) { |
---|
213 | return; |
---|
214 | } |
---|
215 | |
---|
216 | /* if there are no recipients we won't send a ping, which |
---|
217 | is what we want */ |
---|
218 | j=owl_list_get_size(&(z->recips)); |
---|
219 | for (i=0; i<j; i++) { |
---|
220 | if (strcmp(z->realm, "")) { |
---|
221 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
222 | } else { |
---|
223 | to = owl_strdup(owl_list_get_element(&(z->recips), i)); |
---|
224 | } |
---|
225 | send_ping(to, z->class, z->inst); |
---|
226 | owl_free(to); |
---|
227 | } |
---|
228 | |
---|
229 | } |
---|
230 | |
---|
231 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
232 | { |
---|
233 | int i, j; |
---|
234 | char *toline = NULL; |
---|
235 | char *tmp = NULL, *tmp2; |
---|
236 | |
---|
237 | if (z->message) owl_free(z->message); |
---|
238 | |
---|
239 | j=owl_list_get_size(&(z->recips)); |
---|
240 | if (j>0 && z->cc) { |
---|
241 | toline = owl_strdup( "CC: "); |
---|
242 | for (i=0; i<j; i++) { |
---|
243 | tmp = toline; |
---|
244 | if (strcmp(z->realm, "")) { |
---|
245 | toline = owl_sprintf( "%s%s@%s ", toline, (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
246 | } else { |
---|
247 | toline = owl_sprintf( "%s%s ", toline, (const char *) owl_list_get_element(&(z->recips), i)); |
---|
248 | } |
---|
249 | owl_free(tmp); |
---|
250 | tmp = NULL; |
---|
251 | } |
---|
252 | tmp = owl_validate_utf8(msg); |
---|
253 | tmp2 = owl_text_expand_tabs(tmp); |
---|
254 | z->message=owl_sprintf("%s\n%s", toline, tmp2); |
---|
255 | owl_free(toline); |
---|
256 | owl_free(tmp); |
---|
257 | owl_free(tmp2); |
---|
258 | } else { |
---|
259 | tmp=owl_validate_utf8(msg); |
---|
260 | z->message=owl_text_expand_tabs(tmp); |
---|
261 | owl_free(tmp); |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | const char *owl_zwrite_get_message(owl_zwrite *z) |
---|
266 | { |
---|
267 | if (z->message) return(z->message); |
---|
268 | return(""); |
---|
269 | } |
---|
270 | |
---|
271 | int owl_zwrite_is_message_set(owl_zwrite *z) |
---|
272 | { |
---|
273 | if (z->message) return(1); |
---|
274 | return(0); |
---|
275 | } |
---|
276 | |
---|
277 | int owl_zwrite_send_message(owl_zwrite *z) |
---|
278 | { |
---|
279 | int i, j; |
---|
280 | char *to = NULL; |
---|
281 | |
---|
282 | if (z->message==NULL) return(-1); |
---|
283 | |
---|
284 | j=owl_list_get_size(&(z->recips)); |
---|
285 | if (j>0) { |
---|
286 | for (i=0; i<j; i++) { |
---|
287 | if (strcmp(z->realm, "")) { |
---|
288 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
289 | } else { |
---|
290 | to = owl_strdup( owl_list_get_element(&(z->recips), i)); |
---|
291 | } |
---|
292 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
293 | owl_free(to); |
---|
294 | to = NULL; |
---|
295 | } |
---|
296 | } else { |
---|
297 | to = owl_sprintf( "@%s", z->realm); |
---|
298 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
299 | } |
---|
300 | owl_free(to); |
---|
301 | return(0); |
---|
302 | } |
---|
303 | |
---|
304 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
305 | { |
---|
306 | owl_zwrite z; |
---|
307 | int rv; |
---|
308 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
309 | if (rv) return(rv); |
---|
310 | if (!owl_zwrite_is_message_set(&z)) { |
---|
311 | owl_zwrite_set_message(&z, msg); |
---|
312 | } |
---|
313 | owl_zwrite_populate_zsig(&z); |
---|
314 | owl_zwrite_send_message(&z); |
---|
315 | owl_zwrite_free(&z); |
---|
316 | return(0); |
---|
317 | } |
---|
318 | |
---|
319 | const char *owl_zwrite_get_class(owl_zwrite *z) |
---|
320 | { |
---|
321 | return(z->class); |
---|
322 | } |
---|
323 | |
---|
324 | const char *owl_zwrite_get_instance(owl_zwrite *z) |
---|
325 | { |
---|
326 | return(z->inst); |
---|
327 | } |
---|
328 | |
---|
329 | const char *owl_zwrite_get_opcode(owl_zwrite *z) |
---|
330 | { |
---|
331 | return(z->opcode); |
---|
332 | } |
---|
333 | |
---|
334 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
335 | { |
---|
336 | if (z->opcode) owl_free(z->opcode); |
---|
337 | z->opcode=owl_validate_utf8(opcode); |
---|
338 | } |
---|
339 | |
---|
340 | const char *owl_zwrite_get_realm(owl_zwrite *z) |
---|
341 | { |
---|
342 | return(z->realm); |
---|
343 | } |
---|
344 | |
---|
345 | const char *owl_zwrite_get_zsig(owl_zwrite *z) |
---|
346 | { |
---|
347 | if (z->zsig) return(z->zsig); |
---|
348 | return(""); |
---|
349 | } |
---|
350 | |
---|
351 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) |
---|
352 | { |
---|
353 | int i, j; |
---|
354 | |
---|
355 | strcpy(buff, ""); |
---|
356 | j=owl_list_get_size(&(z->recips)); |
---|
357 | for (i=0; i<j; i++) { |
---|
358 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
359 | strcat(buff, " "); |
---|
360 | } |
---|
361 | buff[strlen(buff)-1]='\0'; |
---|
362 | } |
---|
363 | |
---|
364 | int owl_zwrite_get_numrecips(owl_zwrite *z) |
---|
365 | { |
---|
366 | return(owl_list_get_size(&(z->recips))); |
---|
367 | } |
---|
368 | |
---|
369 | const char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) |
---|
370 | { |
---|
371 | return(owl_list_get_element(&(z->recips), n)); |
---|
372 | } |
---|
373 | |
---|
374 | int owl_zwrite_is_personal(owl_zwrite *z) |
---|
375 | { |
---|
376 | /* return true if at least one of the recipients is personal */ |
---|
377 | int i, j; |
---|
378 | char *foo; |
---|
379 | |
---|
380 | j=owl_list_get_size(&(z->recips)); |
---|
381 | for (i=0; i<j; i++) { |
---|
382 | foo=owl_list_get_element(&(z->recips), i); |
---|
383 | if (foo[0]!='@') return(1); |
---|
384 | } |
---|
385 | return(0); |
---|
386 | } |
---|
387 | |
---|
388 | void owl_zwrite_free(owl_zwrite *z) |
---|
389 | { |
---|
390 | owl_list_free_all(&(z->recips), &owl_free); |
---|
391 | if (z->class) owl_free(z->class); |
---|
392 | if (z->inst) owl_free(z->inst); |
---|
393 | if (z->opcode) owl_free(z->opcode); |
---|
394 | if (z->realm) owl_free(z->realm); |
---|
395 | if (z->message) owl_free(z->message); |
---|
396 | if (z->zsig) owl_free(z->zsig); |
---|
397 | } |
---|