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_and_send_from_line(char *cmd, char *msg) { |
---|
10 | owl_zwrite z; |
---|
11 | int rv; |
---|
12 | rv = owl_zwrite_create_from_line(&z, cmd); |
---|
13 | if (rv) return(rv); |
---|
14 | owl_zwrite_send_message(&z, msg); |
---|
15 | owl_zwrite_free(&z); |
---|
16 | return(0); |
---|
17 | } |
---|
18 | |
---|
19 | int owl_zwrite_create_from_line(owl_zwrite *z, char *line) { |
---|
20 | int argc, badargs, myargc; |
---|
21 | char **argv, **myargv; |
---|
22 | char *zsigproc, *zsigowlvar, *zsigzvar, *ptr; |
---|
23 | struct passwd *pw; |
---|
24 | |
---|
25 | badargs=0; |
---|
26 | |
---|
27 | /* set the defaults */ |
---|
28 | strcpy(z->realm, ""); |
---|
29 | strcpy(z->class, "message"); |
---|
30 | strcpy(z->inst, "personal"); |
---|
31 | strcpy(z->opcode, ""); |
---|
32 | z->zsig=owl_strdup(""); |
---|
33 | z->cc=0; |
---|
34 | z->noping=0; |
---|
35 | owl_list_create(&(z->recips)); |
---|
36 | |
---|
37 | /* parse the command line for options */ |
---|
38 | argv=myargv=owl_parseline(line, &argc); |
---|
39 | if (argc<0) { |
---|
40 | owl_function_makemsg("Unbalanced quotes"); |
---|
41 | return(-1); |
---|
42 | } |
---|
43 | myargc=argc; |
---|
44 | if (myargc && *(myargv[0])!='-') { |
---|
45 | myargc--; |
---|
46 | myargv++; |
---|
47 | } |
---|
48 | while (myargc) { |
---|
49 | if (!strcmp(myargv[0], "-c")) { |
---|
50 | if (myargc<2) { |
---|
51 | badargs=1; |
---|
52 | break; |
---|
53 | } |
---|
54 | strcpy(z->class, myargv[1]); |
---|
55 | myargv+=2; |
---|
56 | myargc-=2; |
---|
57 | } else if (!strcmp(myargv[0], "-i")) { |
---|
58 | if (myargc<2) { |
---|
59 | badargs=1; |
---|
60 | break; |
---|
61 | } |
---|
62 | strcpy(z->inst, myargv[1]); |
---|
63 | myargv+=2; |
---|
64 | myargc-=2; |
---|
65 | } else if (!strcmp(myargv[0], "-r")) { |
---|
66 | if (myargc<2) { |
---|
67 | badargs=1; |
---|
68 | break; |
---|
69 | } |
---|
70 | strcpy(z->realm, 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 | strcpy(z->opcode, myargv[1]); |
---|
79 | myargv+=2; |
---|
80 | myargc-=2; |
---|
81 | } else if (!strcmp(myargv[0], "-C")) { |
---|
82 | z->cc=1; |
---|
83 | myargv++; |
---|
84 | myargc--; |
---|
85 | } else if (!strcmp(myargv[0], "-n")) { |
---|
86 | z->noping=1; |
---|
87 | myargv++; |
---|
88 | myargc--; |
---|
89 | } else { |
---|
90 | /* anything unattached is a recipient */ |
---|
91 | owl_list_append_element(&(z->recips), strdup(myargv[0])); |
---|
92 | myargv++; |
---|
93 | myargc--; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | owl_parsefree(argv, argc); |
---|
98 | |
---|
99 | if (badargs) { |
---|
100 | return(-1); |
---|
101 | } |
---|
102 | |
---|
103 | if (!strcasecmp(z->class, "message") && |
---|
104 | !strcasecmp(z->inst, "personal") && |
---|
105 | owl_list_get_size(&(z->recips))==0) { |
---|
106 | /* do the makemsg somewhere else */ |
---|
107 | owl_function_makemsg("You must specify a recipient"); |
---|
108 | return(-1); |
---|
109 | } |
---|
110 | |
---|
111 | /* set a zsig */ |
---|
112 | zsigproc = owl_global_get_zsigproc(&g); |
---|
113 | zsigowlvar = owl_global_get_zsig(&g); |
---|
114 | zsigzvar = ZGetVariable("zwrite-signature"); |
---|
115 | |
---|
116 | if (zsigowlvar && *zsigowlvar) { |
---|
117 | owl_free(z->zsig); |
---|
118 | z->zsig=strdup(zsigowlvar); |
---|
119 | } else if (zsigproc && *zsigproc) { |
---|
120 | FILE *file; |
---|
121 | char buff[LINE], *openline; |
---|
122 | |
---|
123 | /* simple hack for now to nuke stderr */ |
---|
124 | openline=owl_malloc(strlen(zsigproc)+40); |
---|
125 | strcpy(openline, zsigproc); |
---|
126 | strcat(openline, " 2> /dev/null"); |
---|
127 | file=popen(openline, "r"); |
---|
128 | owl_free(openline); |
---|
129 | if (!file) { |
---|
130 | if (zsigzvar && *zsigzvar) { |
---|
131 | owl_free(z->zsig); |
---|
132 | z->zsig=owl_strdup(zsigzvar); |
---|
133 | } |
---|
134 | } else { |
---|
135 | owl_free(z->zsig); |
---|
136 | z->zsig=owl_malloc(LINE); |
---|
137 | strcpy(z->zsig, ""); |
---|
138 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
139 | strcat(z->zsig, buff); |
---|
140 | } |
---|
141 | pclose(file); |
---|
142 | if (z->zsig[strlen(z->zsig)-1]=='\n') { |
---|
143 | z->zsig[strlen(z->zsig)-1]='\0'; |
---|
144 | } |
---|
145 | } |
---|
146 | } else if (zsigzvar) { |
---|
147 | owl_free(z->zsig); |
---|
148 | z->zsig=owl_strdup(zsigzvar); |
---|
149 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
150 | owl_free(z->zsig); |
---|
151 | z->zsig=strdup(pw->pw_gecos); |
---|
152 | ptr=strchr(z->zsig, ','); |
---|
153 | if (ptr) { |
---|
154 | ptr[0]='\0'; |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | return(0); |
---|
159 | } |
---|
160 | |
---|
161 | void owl_zwrite_send_ping(owl_zwrite *z) { |
---|
162 | int i, j; |
---|
163 | char to[LINE]; |
---|
164 | |
---|
165 | if (z->noping) return; |
---|
166 | |
---|
167 | if (strcasecmp(z->class, "message") || |
---|
168 | strcasecmp(z->inst, "personal")) { |
---|
169 | return; |
---|
170 | } |
---|
171 | |
---|
172 | /* if there are no recipients we won't send a ping, which |
---|
173 | is what we want */ |
---|
174 | j=owl_list_get_size(&(z->recips)); |
---|
175 | for (i=0; i<j; i++) { |
---|
176 | if (strcmp(z->realm, "")) { |
---|
177 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
178 | } else { |
---|
179 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
180 | } |
---|
181 | send_ping(to); |
---|
182 | } |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | void owl_zwrite_send_message(owl_zwrite *z, char *msg) { |
---|
187 | int i, j; |
---|
188 | char to[LINE]; |
---|
189 | |
---|
190 | j=owl_list_get_size(&(z->recips)); |
---|
191 | if (j>0) { |
---|
192 | char *tmpmsg=NULL; |
---|
193 | char toline[LINE]; |
---|
194 | |
---|
195 | if (z->cc) { |
---|
196 | strcpy(toline, "CC: "); |
---|
197 | for (i=0; i<j; i++) { |
---|
198 | if (strcmp(z->realm, "")) { |
---|
199 | sprintf(toline, "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
200 | } else { |
---|
201 | sprintf(toline, "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
202 | } |
---|
203 | } |
---|
204 | tmpmsg=owl_malloc(strlen(msg)+strlen(toline)+30); |
---|
205 | sprintf(tmpmsg, "%s\n%s", toline, msg); |
---|
206 | } |
---|
207 | |
---|
208 | for (i=0; i<j; i++) { |
---|
209 | if (strcmp(z->realm, "")) { |
---|
210 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
211 | } else { |
---|
212 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
213 | } |
---|
214 | if (z->cc) { |
---|
215 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, tmpmsg); |
---|
216 | } else { |
---|
217 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, msg); |
---|
218 | } |
---|
219 | } |
---|
220 | if (z->cc) { |
---|
221 | owl_free(tmpmsg); |
---|
222 | } |
---|
223 | } else { |
---|
224 | sprintf(to, "@%s", z->realm); |
---|
225 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, msg); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | char *owl_zwrite_get_class(owl_zwrite *z) { |
---|
230 | return(z->class); |
---|
231 | } |
---|
232 | |
---|
233 | char *owl_zwrite_get_instance(owl_zwrite *z) { |
---|
234 | return(z->inst); |
---|
235 | } |
---|
236 | |
---|
237 | char *owl_zwrite_get_opcode(owl_zwrite *z) { |
---|
238 | return(z->opcode); |
---|
239 | } |
---|
240 | |
---|
241 | char *owl_zwrite_get_realm(owl_zwrite *z) { |
---|
242 | return(z->realm); |
---|
243 | } |
---|
244 | |
---|
245 | char *owl_zwrite_get_zsig(owl_zwrite *z) { |
---|
246 | return(z->zsig); |
---|
247 | } |
---|
248 | |
---|
249 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) { |
---|
250 | int i, j; |
---|
251 | |
---|
252 | strcpy(buff, ""); |
---|
253 | j=owl_list_get_size(&(z->recips)); |
---|
254 | for (i=0; i<j; i++) { |
---|
255 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
256 | strcat(buff, " "); |
---|
257 | } |
---|
258 | buff[strlen(buff)-1]='\0'; |
---|
259 | } |
---|
260 | |
---|
261 | int owl_zwrite_get_numrecips(owl_zwrite *z) { |
---|
262 | return(owl_list_get_size(&(z->recips))); |
---|
263 | } |
---|
264 | |
---|
265 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) { |
---|
266 | return(owl_list_get_element(&(z->recips), n)); |
---|
267 | } |
---|
268 | |
---|
269 | int owl_zwrite_is_personal(owl_zwrite *z) { |
---|
270 | /* return true if at least one of the recipients is personal */ |
---|
271 | int i, j; |
---|
272 | char *foo; |
---|
273 | |
---|
274 | j=owl_list_get_size(&(z->recips)); |
---|
275 | for (i=0; i<j; i++) { |
---|
276 | foo=owl_list_get_element(&(z->recips), i); |
---|
277 | if (foo[0]!='@') return(1); |
---|
278 | } |
---|
279 | return(0); |
---|
280 | } |
---|
281 | |
---|
282 | void owl_zwrite_free(owl_zwrite *z) { |
---|
283 | owl_list_free_all(&(z->recips), &owl_free); |
---|
284 | owl_free(z->zsig); |
---|
285 | } |
---|