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