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