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