1 | #include <stdlib.h> |
---|
2 | #include <unistd.h> |
---|
3 | #include <sys/types.h> |
---|
4 | #include <sys/wait.h> |
---|
5 | #include <string.h> |
---|
6 | #include <zephyr/zephyr.h> |
---|
7 | #include <com_err.h> |
---|
8 | #include <pwd.h> |
---|
9 | #include "owl.h" |
---|
10 | |
---|
11 | static const char fileIdent[] = "$Id$"; |
---|
12 | |
---|
13 | int owl_zephyr_loadsubs(char *filename) { |
---|
14 | /* return 0 on success |
---|
15 | * -1 on file error |
---|
16 | * -2 on subscription error |
---|
17 | */ |
---|
18 | FILE *file; |
---|
19 | char *tmp, *start; |
---|
20 | char buffer[1024], subsfile[1024]; |
---|
21 | ZSubscription_t subs[3001]; |
---|
22 | int count, ret, i; |
---|
23 | |
---|
24 | ret=0; |
---|
25 | |
---|
26 | if (filename==NULL) { |
---|
27 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs"); |
---|
28 | } else { |
---|
29 | strcpy(subsfile, filename); |
---|
30 | } |
---|
31 | |
---|
32 | /* need to redo this to do chunks, not just bail after 3000 */ |
---|
33 | count=0; |
---|
34 | file=fopen(subsfile, "r"); |
---|
35 | if (file) { |
---|
36 | while ( fgets(buffer, 1024, file)!=NULL ) { |
---|
37 | if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue; |
---|
38 | |
---|
39 | if (buffer[0]=='-') { |
---|
40 | start=buffer+1; |
---|
41 | } else { |
---|
42 | start=buffer; |
---|
43 | } |
---|
44 | |
---|
45 | if (count >= 3000) break; /* also tell the user */ |
---|
46 | |
---|
47 | /* add it to the list of subs */ |
---|
48 | if ((tmp=(char *) strtok(start, ",\n\r"))==NULL) continue; |
---|
49 | subs[count].zsub_class=owl_strdup(tmp); |
---|
50 | if ((tmp=(char *) strtok(NULL, ",\n\r"))==NULL) continue; |
---|
51 | subs[count].zsub_classinst=owl_strdup(tmp); |
---|
52 | if ((tmp=(char *) strtok(NULL, " \t\n\r"))==NULL) continue; |
---|
53 | subs[count].zsub_recipient=owl_strdup(tmp); |
---|
54 | |
---|
55 | /* if it started with '-' then add it to the global punt list */ |
---|
56 | if (buffer[0]=='-') { |
---|
57 | owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0); |
---|
58 | } |
---|
59 | |
---|
60 | count++; |
---|
61 | } |
---|
62 | fclose(file); |
---|
63 | } else { |
---|
64 | count=0; |
---|
65 | ret=-1; |
---|
66 | } |
---|
67 | |
---|
68 | /* sub with defaults */ |
---|
69 | if (ZSubscribeTo(subs,count,0) != ZERR_NONE) { |
---|
70 | fprintf(stderr, "Error subbing\n"); |
---|
71 | ret=-2; |
---|
72 | } |
---|
73 | |
---|
74 | /* free stuff */ |
---|
75 | for (i=0; i<count; i++) { |
---|
76 | owl_free(subs[i].zsub_class); |
---|
77 | owl_free(subs[i].zsub_classinst); |
---|
78 | owl_free(subs[i].zsub_recipient); |
---|
79 | } |
---|
80 | |
---|
81 | return(ret); |
---|
82 | } |
---|
83 | |
---|
84 | int loadloginsubs(char *filename) { |
---|
85 | FILE *file; |
---|
86 | ZSubscription_t subs[3001]; |
---|
87 | char subsfile[1024], buffer[1024]; |
---|
88 | int count, ret, i; |
---|
89 | |
---|
90 | ret=0; |
---|
91 | |
---|
92 | if (filename==NULL) { |
---|
93 | sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone"); |
---|
94 | } else { |
---|
95 | strcpy(subsfile, filename); |
---|
96 | } |
---|
97 | |
---|
98 | /* need to redo this to do chunks, not just bag out after 3000 */ |
---|
99 | count=0; |
---|
100 | file=fopen(subsfile, "r"); |
---|
101 | if (file) { |
---|
102 | while ( fgets(buffer, 1024, file)!=NULL ) { |
---|
103 | if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue; |
---|
104 | |
---|
105 | if (count >= 3000) break; /* also tell the user */ |
---|
106 | |
---|
107 | buffer[strlen(buffer)-1]='\0'; |
---|
108 | subs[count].zsub_class="login"; |
---|
109 | subs[count].zsub_recipient="*"; |
---|
110 | if (strchr(buffer, '@')) { |
---|
111 | subs[count].zsub_classinst=owl_strdup(buffer); |
---|
112 | } else { |
---|
113 | subs[count].zsub_classinst=owl_malloc(1024); |
---|
114 | sprintf(subs[count].zsub_classinst, "%s@%s", buffer, ZGetRealm()); |
---|
115 | } |
---|
116 | |
---|
117 | count++; |
---|
118 | } |
---|
119 | fclose(file); |
---|
120 | } else { |
---|
121 | count=0; |
---|
122 | ret=-1; |
---|
123 | } |
---|
124 | |
---|
125 | /* sub with defaults */ |
---|
126 | if (ZSubscribeTo(subs,count,0) != ZERR_NONE) { |
---|
127 | fprintf(stderr, "Error subbing\n"); |
---|
128 | ret=-2; |
---|
129 | } |
---|
130 | |
---|
131 | /* free stuff */ |
---|
132 | for (i=0; i<count; i++) { |
---|
133 | owl_free(subs[i].zsub_classinst); |
---|
134 | } |
---|
135 | |
---|
136 | return(ret); |
---|
137 | } |
---|
138 | |
---|
139 | void unsuball() { |
---|
140 | int ret; |
---|
141 | |
---|
142 | ret=ZCancelSubscriptions(0); |
---|
143 | if (ret != ZERR_NONE) { |
---|
144 | com_err("owl",ret,"while unsubscribing"); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | int owl_zephyr_sub(char *class, char *inst, char *recip) { |
---|
149 | ZSubscription_t subs[5]; |
---|
150 | int ret; |
---|
151 | |
---|
152 | subs[0].zsub_class=class; |
---|
153 | subs[0].zsub_classinst=inst; |
---|
154 | subs[0].zsub_recipient=recip; |
---|
155 | |
---|
156 | if (ZSubscribeTo(subs,1,0) != ZERR_NONE) { |
---|
157 | fprintf(stderr, "Error subbing\n"); |
---|
158 | ret=-2; |
---|
159 | } |
---|
160 | return(0); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | int owl_zephyr_unsub(char *class, char *inst, char *recip) { |
---|
165 | ZSubscription_t subs[5]; |
---|
166 | int ret; |
---|
167 | |
---|
168 | subs[0].zsub_class=class; |
---|
169 | subs[0].zsub_classinst=inst; |
---|
170 | subs[0].zsub_recipient=recip; |
---|
171 | |
---|
172 | if (ZUnsubscribeTo(subs,1,0) != ZERR_NONE) { |
---|
173 | fprintf(stderr, "Error unsubbing\n"); |
---|
174 | ret=-2; |
---|
175 | } |
---|
176 | return(0); |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | void zlog_in() { |
---|
181 | int ret; |
---|
182 | |
---|
183 | ret=ZSetLocation(EXPOSE_NETVIS); |
---|
184 | /* do something on failure */ |
---|
185 | } |
---|
186 | |
---|
187 | void zlog_out() { |
---|
188 | int ret; |
---|
189 | |
---|
190 | ret=ZUnsetLocation(); |
---|
191 | /* do something on failure */ |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | char *owl_zephyr_get_field(ZNotice_t *n, int j, int *k) { |
---|
196 | /* return a pointer to the Jth field, place the length in k. If the |
---|
197 | field doesn't exist return an emtpy string */ |
---|
198 | int i, count, save; |
---|
199 | |
---|
200 | count=save=0; |
---|
201 | for (i=0; i<n->z_message_len; i++) { |
---|
202 | if (n->z_message[i]=='\0') { |
---|
203 | count++; |
---|
204 | if (count==j) { |
---|
205 | /* just found the end of the field we're looking for */ |
---|
206 | *k=i-save; |
---|
207 | return(n->z_message+save); |
---|
208 | } else { |
---|
209 | save=i+1; |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | /* catch the last field */ |
---|
214 | if (count==j-1) { |
---|
215 | *k=n->z_message_len-save; |
---|
216 | return(n->z_message+save); |
---|
217 | } |
---|
218 | |
---|
219 | *k=0; |
---|
220 | return(""); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | int owl_zephyr_get_num_fields(ZNotice_t *n) { |
---|
225 | int i, fields; |
---|
226 | |
---|
227 | fields=1; |
---|
228 | for (i=0; i<n->z_message_len; i++) { |
---|
229 | if (n->z_message[i]=='\0') fields++; |
---|
230 | } |
---|
231 | |
---|
232 | return(fields); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | char *owl_zephyr_get_message(ZNotice_t *n, int *k) { |
---|
237 | /* return a pointer to the message, place the message length in k */ |
---|
238 | if (!strcasecmp(n->z_opcode, "ping")) { |
---|
239 | *k=0; |
---|
240 | return(""); |
---|
241 | } |
---|
242 | |
---|
243 | return(owl_zephyr_get_field(n, 2, k)); |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | char *owl_zephyr_get_zsig(ZNotice_t *n, int *k) { |
---|
248 | /* return a pointer to the zsig if there is one */ |
---|
249 | |
---|
250 | if (n->z_message_len==0) { |
---|
251 | *k=0; |
---|
252 | return(""); |
---|
253 | } |
---|
254 | *k=strlen(n->z_message); |
---|
255 | return(n->z_message); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | int send_zephyr(char *opcode, char *zsig, char *class, char *instance, char *recipient, char *message) { |
---|
260 | int ret; |
---|
261 | ZNotice_t notice; |
---|
262 | char *ptr; |
---|
263 | struct passwd *pw; |
---|
264 | char zsigtmp[LINE]; |
---|
265 | char *zsigexec, *zsigowlvar, *zsigzvar; |
---|
266 | |
---|
267 | zsigexec = owl_global_get_zsig_exec(&g); |
---|
268 | zsigowlvar = owl_global_get_zsig(&g); |
---|
269 | zsigzvar = ZGetVariable("zwrite-signature"); |
---|
270 | |
---|
271 | if (zsig) { |
---|
272 | strcpy(zsigtmp, zsig); |
---|
273 | } else if (zsigowlvar && *zsigowlvar) { |
---|
274 | strncpy(zsigtmp, zsigowlvar, LINE); |
---|
275 | } else if (zsigexec && *zsigexec) { |
---|
276 | FILE *file; |
---|
277 | char buff[LINE]; |
---|
278 | strcpy(zsigtmp, ""); |
---|
279 | file=popen(zsigexec, "r"); |
---|
280 | if (!file) { |
---|
281 | if (zsigzvar && *zsigzvar) { |
---|
282 | strncpy(zsigtmp, zsigzvar, LINE); |
---|
283 | } |
---|
284 | } else { |
---|
285 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
286 | strcat(zsigtmp, buff); |
---|
287 | } |
---|
288 | pclose(file); |
---|
289 | if (zsigtmp[strlen(zsigtmp)-1]=='\n') { |
---|
290 | zsigtmp[strlen(zsigtmp)-1]='\0'; |
---|
291 | } |
---|
292 | } |
---|
293 | } else if (zsigzvar) { |
---|
294 | strncpy(zsigtmp, zsigzvar, LINE); |
---|
295 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
296 | strncpy(zsigtmp, pw->pw_gecos, LINE); |
---|
297 | ptr=strchr(zsigtmp, ','); |
---|
298 | if (ptr) { |
---|
299 | ptr[0]='\0'; |
---|
300 | } |
---|
301 | } else { |
---|
302 | strcpy(zsigtmp, ""); |
---|
303 | } |
---|
304 | |
---|
305 | memset(¬ice, 0, sizeof(notice)); |
---|
306 | |
---|
307 | notice.z_kind=ACKED; |
---|
308 | notice.z_port=0; |
---|
309 | notice.z_class=class; |
---|
310 | notice.z_class_inst=instance; |
---|
311 | if (!strcmp(recipient, "*") || !strcmp(recipient, "@")) { |
---|
312 | notice.z_recipient=""; |
---|
313 | } else { |
---|
314 | notice.z_recipient=recipient; |
---|
315 | } |
---|
316 | notice.z_default_format="Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold{$1 <$sender>}\n\n$2"; |
---|
317 | notice.z_sender=NULL; |
---|
318 | if (opcode) notice.z_opcode=opcode; |
---|
319 | |
---|
320 | notice.z_message_len=strlen(zsigtmp)+1+strlen(message); |
---|
321 | notice.z_message=owl_malloc(notice.z_message_len+10); |
---|
322 | strcpy(notice.z_message, zsigtmp); |
---|
323 | memcpy(notice.z_message+strlen(zsigtmp)+1, message, strlen(message)); |
---|
324 | |
---|
325 | /* ret=ZSendNotice(¬ice, ZAUTH); */ |
---|
326 | ret=ZSrvSendNotice(¬ice, ZAUTH, send_zephyr_helper); |
---|
327 | |
---|
328 | /* free then check the return */ |
---|
329 | owl_free(notice.z_message); |
---|
330 | ZFreeNotice(¬ice); |
---|
331 | if (ret!=ZERR_NONE) { |
---|
332 | owl_function_makemsg("Error sending zephyr"); |
---|
333 | return(ret); |
---|
334 | } |
---|
335 | |
---|
336 | return(0); |
---|
337 | } |
---|
338 | |
---|
339 | Code_t send_zephyr_helper(ZNotice_t *notice, char *buf, int len, int wait) { |
---|
340 | return(ZSendPacket(buf, len, 0)); |
---|
341 | } |
---|
342 | |
---|
343 | void send_ping(char *to) { |
---|
344 | send_zephyr("PING", "", "MESSAGE", "PERSONAL", to, ""); |
---|
345 | } |
---|
346 | |
---|
347 | void owl_zephyr_handle_ack(ZNotice_t *retnotice) { |
---|
348 | char buff[LINE]; |
---|
349 | char *tmp; |
---|
350 | |
---|
351 | /* if it's an HMACK ignore it */ |
---|
352 | if (retnotice->z_kind == HMACK) return; |
---|
353 | |
---|
354 | if (retnotice->z_kind == SERVNAK) { |
---|
355 | owl_function_makemsg("Authorization failure sending zephyr"); |
---|
356 | } else if ((retnotice->z_kind != SERVACK) || !retnotice->z_message_len) { |
---|
357 | owl_function_makemsg("Detected server failure while receiving acknowledgement"); |
---|
358 | } else if (!strcmp(retnotice->z_message, ZSRVACK_SENT)) { |
---|
359 | if (!strcasecmp(retnotice->z_opcode, "ping")) { |
---|
360 | return; |
---|
361 | } else if (!strcasecmp(retnotice->z_class, "message") && |
---|
362 | !strcasecmp(retnotice->z_class_inst, "personal")) { |
---|
363 | tmp=pretty_sender(retnotice->z_recipient); |
---|
364 | sprintf(buff, "Message sent to %s.", tmp); |
---|
365 | free(tmp); |
---|
366 | } else { |
---|
367 | sprintf(buff, "Message sent to -c %s -i %s\n", retnotice->z_class, retnotice->z_class_inst); |
---|
368 | } |
---|
369 | owl_function_makemsg(buff); |
---|
370 | } else if (!strcmp(retnotice->z_message, ZSRVACK_NOTSENT)) { |
---|
371 | if (strcasecmp(retnotice->z_class, "message")) { |
---|
372 | sprintf(buff, "Not logged in or not subscribing to class %s, instance %s", |
---|
373 | retnotice->z_class, retnotice->z_class_inst); |
---|
374 | owl_function_makemsg(buff); |
---|
375 | } else { |
---|
376 | owl_function_makemsg("Not logged in or subscribing to messages."); |
---|
377 | } |
---|
378 | } else { |
---|
379 | char buff[1024]; |
---|
380 | sprintf(buff, "Internal error on ack (%s)", retnotice->z_message); |
---|
381 | owl_function_makemsg(buff); |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | int owl_zephyr_notice_is_ack(ZNotice_t *n) { |
---|
386 | if (n->z_kind == SERVNAK || n->z_kind == SERVACK || n->z_kind == HMACK) { |
---|
387 | if (!strcasecmp(n->z_class, LOGIN_CLASS)) return(0); |
---|
388 | return(1); |
---|
389 | } |
---|
390 | return(0); |
---|
391 | } |
---|
392 | |
---|
393 | void owl_zephyr_zaway(owl_message *m) { |
---|
394 | char *tmpbuff; |
---|
395 | |
---|
396 | /* bail if it doesn't look like a message we should reply to */ |
---|
397 | if (strcasecmp(owl_message_get_class(m), "message")) return; |
---|
398 | if (strcasecmp(owl_message_get_recipient(m), ZGetSender())) return; |
---|
399 | if (!strcasecmp(owl_message_get_sender(m), "")) return; |
---|
400 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) return; |
---|
401 | if (!strcasecmp(owl_message_get_opcode(m), "auto")) return; |
---|
402 | if (!strcasecmp(owl_message_get_notice(m)->z_message, "Automated reply:")) return; |
---|
403 | if (!strcasecmp(owl_message_get_sender(m), ZGetSender())) return; |
---|
404 | /* add one to not send to ourselves */ |
---|
405 | |
---|
406 | send_zephyr("", |
---|
407 | "Automated reply:", |
---|
408 | owl_message_get_class(m), |
---|
409 | owl_message_get_instance(m), |
---|
410 | owl_message_get_sender(m), |
---|
411 | owl_global_get_zaway_msg(&g)); |
---|
412 | |
---|
413 | /* display the message as an admin message in the receive window */ |
---|
414 | tmpbuff=owl_malloc(strlen(owl_global_get_zaway_msg(&g))+LINE); |
---|
415 | sprintf(tmpbuff, "Message sent to %s", owl_message_get_sender(m)); |
---|
416 | owl_function_adminmsg(tmpbuff, owl_global_get_zaway_msg(&g)); |
---|
417 | owl_free(tmpbuff); |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | void owl_zephyr_hackaway_cr(ZNotice_t *n) { |
---|
422 | /* replace \r's with ' '. Gross-ish */ |
---|
423 | int i; |
---|
424 | |
---|
425 | for (i=0; i<n->z_message_len; i++) { |
---|
426 | if (n->z_message[i]=='\r') { |
---|
427 | n->z_message[i]=' '; |
---|
428 | } |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | void owl_zephyr_zlocate(char *user, char *out, int auth) { |
---|
433 | int ret, numlocs; |
---|
434 | int one = 1; |
---|
435 | ZLocations_t locations; |
---|
436 | char *myuser; |
---|
437 | |
---|
438 | strcpy(out, ""); |
---|
439 | |
---|
440 | ret=ZLocateUser(user,&numlocs,auth?ZAUTH:ZNOAUTH); |
---|
441 | if (ret != ZERR_NONE) { |
---|
442 | owl_function_makemsg("Error locating user"); |
---|
443 | } |
---|
444 | |
---|
445 | if (numlocs==0) { |
---|
446 | strcpy(out, "Hidden or not logged-in\n"); |
---|
447 | return; |
---|
448 | } |
---|
449 | |
---|
450 | for (;numlocs;numlocs--) { |
---|
451 | ZGetLocations(&locations,&one); |
---|
452 | myuser=pretty_sender(user); |
---|
453 | sprintf(out, "%s%s: %s\t%s\t%s\n", out, myuser, locations.host, locations.tty, locations.time); |
---|
454 | owl_free(myuser); |
---|
455 | } |
---|
456 | } |
---|