1 | #include <stdlib.h> |
---|
2 | #include <unistd.h> |
---|
3 | #include <string.h> |
---|
4 | #include <sys/socket.h> |
---|
5 | #include <netdb.h> |
---|
6 | #include <sys/types.h> |
---|
7 | #include <sys/socket.h> |
---|
8 | #include <netinet/in.h> |
---|
9 | #include <arpa/inet.h> |
---|
10 | #include <time.h> |
---|
11 | #include "owl.h" |
---|
12 | |
---|
13 | static owl_fmtext_cache fmtext_cache[OWL_FMTEXT_CACHE_SIZE]; |
---|
14 | static owl_fmtext_cache * fmtext_cache_next = fmtext_cache; |
---|
15 | |
---|
16 | void owl_message_init_fmtext_cache () |
---|
17 | { |
---|
18 | int i; |
---|
19 | for(i = 0; i < OWL_FMTEXT_CACHE_SIZE; i++) { |
---|
20 | owl_fmtext_init_null(&(fmtext_cache[i].fmtext)); |
---|
21 | fmtext_cache[i].message = NULL; |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | owl_fmtext_cache * owl_message_next_fmtext() /*noproto*/ |
---|
26 | { |
---|
27 | owl_fmtext_cache * f = fmtext_cache_next; |
---|
28 | if(fmtext_cache_next->message != NULL) { |
---|
29 | owl_message_invalidate_format(fmtext_cache_next->message); |
---|
30 | } |
---|
31 | fmtext_cache_next++; |
---|
32 | if(fmtext_cache_next - fmtext_cache == OWL_FMTEXT_CACHE_SIZE) |
---|
33 | fmtext_cache_next = fmtext_cache; |
---|
34 | return f; |
---|
35 | } |
---|
36 | |
---|
37 | void owl_message_init(owl_message *m) |
---|
38 | { |
---|
39 | m->id=owl_global_get_nextmsgid(&g); |
---|
40 | owl_message_set_direction_none(m); |
---|
41 | m->delete=0; |
---|
42 | |
---|
43 | owl_message_set_hostname(m, ""); |
---|
44 | owl_list_create(&(m->attributes)); |
---|
45 | |
---|
46 | /* save the time */ |
---|
47 | m->time=time(NULL); |
---|
48 | m->timestr=owl_strdup(ctime(&(m->time))); |
---|
49 | m->timestr[strlen(m->timestr)-1]='\0'; |
---|
50 | |
---|
51 | m->fmtext = NULL; |
---|
52 | } |
---|
53 | |
---|
54 | /* add the named attribute to the message. If an attribute with the |
---|
55 | * name already exists, replace the old value with the new value |
---|
56 | */ |
---|
57 | void owl_message_set_attribute(owl_message *m, const char *attrname, const char *attrvalue) |
---|
58 | { |
---|
59 | int i, j; |
---|
60 | owl_pair *p = NULL, *pair = NULL; |
---|
61 | |
---|
62 | /* look for an existing pair with this key, */ |
---|
63 | j=owl_list_get_size(&(m->attributes)); |
---|
64 | for (i=0; i<j; i++) { |
---|
65 | p=owl_list_get_element(&(m->attributes), i); |
---|
66 | if (!strcmp(owl_pair_get_key(p), attrname)) { |
---|
67 | owl_free(owl_pair_get_value(p)); |
---|
68 | pair = p; |
---|
69 | break; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | if(pair == NULL) { |
---|
74 | pair = owl_malloc(sizeof(owl_pair)); |
---|
75 | owl_pair_create(pair, owl_global_intern(&g, attrname), NULL); |
---|
76 | owl_list_append_element(&(m->attributes), pair); |
---|
77 | } |
---|
78 | owl_pair_set_value(pair, owl_validate_or_convert(attrvalue)); |
---|
79 | } |
---|
80 | |
---|
81 | /* return the value associated with the named attribute, or NULL if |
---|
82 | * the attribute does not exist |
---|
83 | */ |
---|
84 | const char *owl_message_get_attribute_value(const owl_message *m, const char *attrname) |
---|
85 | { |
---|
86 | int i, j; |
---|
87 | owl_pair *p; |
---|
88 | |
---|
89 | j=owl_list_get_size(&(m->attributes)); |
---|
90 | for (i=0; i<j; i++) { |
---|
91 | p=owl_list_get_element(&(m->attributes), i); |
---|
92 | if (!strcmp(owl_pair_get_key(p), attrname)) { |
---|
93 | return(owl_pair_get_value(p)); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | /* |
---|
98 | owl_function_debugmsg("No attribute %s found for message %i", |
---|
99 | attrname, |
---|
100 | owl_message_get_id(m)); |
---|
101 | */ |
---|
102 | return(NULL); |
---|
103 | } |
---|
104 | |
---|
105 | /* We cheat and indent it for now, since we really want this for |
---|
106 | * the 'info' function. Later there should just be a generic |
---|
107 | * function to indent fmtext. |
---|
108 | */ |
---|
109 | void owl_message_attributes_tofmtext(const owl_message *m, owl_fmtext *fm) { |
---|
110 | int i, j; |
---|
111 | owl_pair *p; |
---|
112 | char *buff; |
---|
113 | |
---|
114 | owl_fmtext_init_null(fm); |
---|
115 | |
---|
116 | j=owl_list_get_size(&(m->attributes)); |
---|
117 | for (i=0; i<j; i++) { |
---|
118 | p=owl_list_get_element(&(m->attributes), i); |
---|
119 | buff=owl_sprintf(" %-15.15s: %-35.35s\n", owl_pair_get_key(p), owl_pair_get_value(p)); |
---|
120 | if(buff == NULL) { |
---|
121 | buff=owl_sprintf(" %-15.15s: %-35.35s\n", owl_pair_get_key(p), "<error>"); |
---|
122 | if(buff == NULL) |
---|
123 | buff=owl_strdup(" <error>\n"); |
---|
124 | } |
---|
125 | owl_fmtext_append_normal(fm, buff); |
---|
126 | owl_free(buff); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | void owl_message_invalidate_format(owl_message *m) |
---|
131 | { |
---|
132 | if(m->fmtext) { |
---|
133 | m->fmtext->message = NULL; |
---|
134 | owl_fmtext_clear(&(m->fmtext->fmtext)); |
---|
135 | m->fmtext=NULL; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | owl_fmtext *owl_message_get_fmtext(owl_message *m) |
---|
140 | { |
---|
141 | owl_message_format(m); |
---|
142 | return(&(m->fmtext->fmtext)); |
---|
143 | } |
---|
144 | |
---|
145 | void owl_message_format(owl_message *m) |
---|
146 | { |
---|
147 | const owl_style *s; |
---|
148 | owl_view *v; |
---|
149 | |
---|
150 | if (!m->fmtext) { |
---|
151 | m->fmtext = owl_message_next_fmtext(); |
---|
152 | m->fmtext->message = m; |
---|
153 | /* for now we assume there's just the one view and use that style */ |
---|
154 | v=owl_global_get_current_view(&g); |
---|
155 | s=owl_view_get_style(v); |
---|
156 | |
---|
157 | owl_style_get_formattext(s, &(m->fmtext->fmtext), m); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void owl_message_set_class(owl_message *m, const char *class) |
---|
162 | { |
---|
163 | owl_message_set_attribute(m, "class", class); |
---|
164 | } |
---|
165 | |
---|
166 | const char *owl_message_get_class(const owl_message *m) |
---|
167 | { |
---|
168 | const char *class; |
---|
169 | |
---|
170 | class=owl_message_get_attribute_value(m, "class"); |
---|
171 | if (!class) return(""); |
---|
172 | return(class); |
---|
173 | } |
---|
174 | |
---|
175 | void owl_message_set_instance(owl_message *m, const char *inst) |
---|
176 | { |
---|
177 | owl_message_set_attribute(m, "instance", inst); |
---|
178 | } |
---|
179 | |
---|
180 | const char *owl_message_get_instance(const owl_message *m) |
---|
181 | { |
---|
182 | const char *instance; |
---|
183 | |
---|
184 | instance=owl_message_get_attribute_value(m, "instance"); |
---|
185 | if (!instance) return(""); |
---|
186 | return(instance); |
---|
187 | } |
---|
188 | |
---|
189 | void owl_message_set_sender(owl_message *m, const char *sender) |
---|
190 | { |
---|
191 | owl_message_set_attribute(m, "sender", sender); |
---|
192 | } |
---|
193 | |
---|
194 | const char *owl_message_get_sender(const owl_message *m) |
---|
195 | { |
---|
196 | const char *sender; |
---|
197 | |
---|
198 | sender=owl_message_get_attribute_value(m, "sender"); |
---|
199 | if (!sender) return(""); |
---|
200 | return(sender); |
---|
201 | } |
---|
202 | |
---|
203 | void owl_message_set_zsig(owl_message *m, const char *zsig) |
---|
204 | { |
---|
205 | owl_message_set_attribute(m, "zsig", zsig); |
---|
206 | } |
---|
207 | |
---|
208 | const char *owl_message_get_zsig(const owl_message *m) |
---|
209 | { |
---|
210 | const char *zsig; |
---|
211 | |
---|
212 | zsig=owl_message_get_attribute_value(m, "zsig"); |
---|
213 | if (!zsig) return(""); |
---|
214 | return(zsig); |
---|
215 | } |
---|
216 | |
---|
217 | void owl_message_set_recipient(owl_message *m, const char *recip) |
---|
218 | { |
---|
219 | owl_message_set_attribute(m, "recipient", recip); |
---|
220 | } |
---|
221 | |
---|
222 | const char *owl_message_get_recipient(const owl_message *m) |
---|
223 | { |
---|
224 | /* this is stupid for outgoing messages, we need to fix it. */ |
---|
225 | |
---|
226 | const char *recip; |
---|
227 | |
---|
228 | recip=owl_message_get_attribute_value(m, "recipient"); |
---|
229 | if (!recip) return(""); |
---|
230 | return(recip); |
---|
231 | } |
---|
232 | |
---|
233 | void owl_message_set_realm(owl_message *m, const char *realm) |
---|
234 | { |
---|
235 | owl_message_set_attribute(m, "realm", realm); |
---|
236 | } |
---|
237 | |
---|
238 | const char *owl_message_get_realm(const owl_message *m) |
---|
239 | { |
---|
240 | const char *realm; |
---|
241 | |
---|
242 | realm=owl_message_get_attribute_value(m, "realm"); |
---|
243 | if (!realm) return(""); |
---|
244 | return(realm); |
---|
245 | } |
---|
246 | |
---|
247 | void owl_message_set_body(owl_message *m, const char *body) |
---|
248 | { |
---|
249 | owl_message_set_attribute(m, "body", body); |
---|
250 | } |
---|
251 | |
---|
252 | const char *owl_message_get_body(const owl_message *m) |
---|
253 | { |
---|
254 | const char *body; |
---|
255 | |
---|
256 | body=owl_message_get_attribute_value(m, "body"); |
---|
257 | if (!body) return(""); |
---|
258 | return(body); |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | void owl_message_set_opcode(owl_message *m, const char *opcode) |
---|
263 | { |
---|
264 | owl_message_set_attribute(m, "opcode", opcode); |
---|
265 | } |
---|
266 | |
---|
267 | const char *owl_message_get_opcode(const owl_message *m) |
---|
268 | { |
---|
269 | const char *opcode; |
---|
270 | |
---|
271 | opcode=owl_message_get_attribute_value(m, "opcode"); |
---|
272 | if (!opcode) return(""); |
---|
273 | return(opcode); |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | void owl_message_set_islogin(owl_message *m) |
---|
278 | { |
---|
279 | owl_message_set_attribute(m, "loginout", "login"); |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | void owl_message_set_islogout(owl_message *m) |
---|
284 | { |
---|
285 | owl_message_set_attribute(m, "loginout", "logout"); |
---|
286 | } |
---|
287 | |
---|
288 | int owl_message_is_loginout(const owl_message *m) |
---|
289 | { |
---|
290 | const char *res; |
---|
291 | |
---|
292 | res=owl_message_get_attribute_value(m, "loginout"); |
---|
293 | if (!res) return(0); |
---|
294 | return(1); |
---|
295 | } |
---|
296 | |
---|
297 | int owl_message_is_login(const owl_message *m) |
---|
298 | { |
---|
299 | const char *res; |
---|
300 | |
---|
301 | res=owl_message_get_attribute_value(m, "loginout"); |
---|
302 | if (!res) return(0); |
---|
303 | if (!strcmp(res, "login")) return(1); |
---|
304 | return(0); |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | int owl_message_is_logout(const owl_message *m) |
---|
309 | { |
---|
310 | const char *res; |
---|
311 | |
---|
312 | res=owl_message_get_attribute_value(m, "loginout"); |
---|
313 | if (!res) return(0); |
---|
314 | if (!strcmp(res, "logout")) return(1); |
---|
315 | return(0); |
---|
316 | } |
---|
317 | |
---|
318 | void owl_message_set_isprivate(owl_message *m) |
---|
319 | { |
---|
320 | owl_message_set_attribute(m, "isprivate", "true"); |
---|
321 | } |
---|
322 | |
---|
323 | int owl_message_is_private(const owl_message *m) |
---|
324 | { |
---|
325 | const char *res; |
---|
326 | |
---|
327 | res=owl_message_get_attribute_value(m, "isprivate"); |
---|
328 | if (!res) return(0); |
---|
329 | return !strcmp(res, "true"); |
---|
330 | } |
---|
331 | |
---|
332 | const char *owl_message_get_timestr(const owl_message *m) |
---|
333 | { |
---|
334 | if (m->timestr) return(m->timestr); |
---|
335 | return(""); |
---|
336 | } |
---|
337 | |
---|
338 | void owl_message_set_type_admin(owl_message *m) |
---|
339 | { |
---|
340 | owl_message_set_attribute(m, "type", "admin"); |
---|
341 | } |
---|
342 | |
---|
343 | void owl_message_set_type_loopback(owl_message *m) |
---|
344 | { |
---|
345 | owl_message_set_attribute(m, "type", "loopback"); |
---|
346 | } |
---|
347 | |
---|
348 | void owl_message_set_type_zephyr(owl_message *m) |
---|
349 | { |
---|
350 | owl_message_set_attribute(m, "type", "zephyr"); |
---|
351 | } |
---|
352 | |
---|
353 | void owl_message_set_type_aim(owl_message *m) |
---|
354 | { |
---|
355 | owl_message_set_attribute(m, "type", "AIM"); |
---|
356 | } |
---|
357 | |
---|
358 | void owl_message_set_type(owl_message *m, const char* type) |
---|
359 | { |
---|
360 | owl_message_set_attribute(m, "type", type); |
---|
361 | } |
---|
362 | |
---|
363 | int owl_message_is_type(const owl_message *m, const char *type) { |
---|
364 | const char * t = owl_message_get_attribute_value(m, "type"); |
---|
365 | if(!t) return 0; |
---|
366 | return !strcasecmp(t, type); |
---|
367 | } |
---|
368 | |
---|
369 | int owl_message_is_type_admin(const owl_message *m) |
---|
370 | { |
---|
371 | return owl_message_is_type(m, "admin"); |
---|
372 | } |
---|
373 | |
---|
374 | int owl_message_is_type_zephyr(const owl_message *m) |
---|
375 | { |
---|
376 | return owl_message_is_type(m, "zephyr"); |
---|
377 | } |
---|
378 | |
---|
379 | int owl_message_is_type_aim(const owl_message *m) |
---|
380 | { |
---|
381 | return owl_message_is_type(m, "aim"); |
---|
382 | } |
---|
383 | |
---|
384 | /* XXX TODO: deprecate this */ |
---|
385 | int owl_message_is_type_jabber(const owl_message *m) |
---|
386 | { |
---|
387 | return owl_message_is_type(m, "jabber"); |
---|
388 | } |
---|
389 | |
---|
390 | int owl_message_is_type_loopback(const owl_message *m) |
---|
391 | { |
---|
392 | return owl_message_is_type(m, "loopback"); |
---|
393 | } |
---|
394 | |
---|
395 | int owl_message_is_pseudo(const owl_message *m) |
---|
396 | { |
---|
397 | if (owl_message_get_attribute_value(m, "pseudo")) return(1); |
---|
398 | return(0); |
---|
399 | } |
---|
400 | |
---|
401 | const char *owl_message_get_text(owl_message *m) |
---|
402 | { |
---|
403 | owl_message_format(m); |
---|
404 | return(owl_fmtext_get_text(&(m->fmtext->fmtext))); |
---|
405 | } |
---|
406 | |
---|
407 | void owl_message_set_direction_in(owl_message *m) |
---|
408 | { |
---|
409 | m->direction=OWL_MESSAGE_DIRECTION_IN; |
---|
410 | } |
---|
411 | |
---|
412 | void owl_message_set_direction_out(owl_message *m) |
---|
413 | { |
---|
414 | m->direction=OWL_MESSAGE_DIRECTION_OUT; |
---|
415 | } |
---|
416 | |
---|
417 | void owl_message_set_direction_none(owl_message *m) |
---|
418 | { |
---|
419 | m->direction=OWL_MESSAGE_DIRECTION_NONE; |
---|
420 | } |
---|
421 | |
---|
422 | void owl_message_set_direction(owl_message *m, int direction) |
---|
423 | { |
---|
424 | m->direction=direction; |
---|
425 | } |
---|
426 | |
---|
427 | int owl_message_is_direction_in(const owl_message *m) |
---|
428 | { |
---|
429 | if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1); |
---|
430 | return(0); |
---|
431 | } |
---|
432 | |
---|
433 | int owl_message_is_direction_out(const owl_message *m) |
---|
434 | { |
---|
435 | if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1); |
---|
436 | return(0); |
---|
437 | } |
---|
438 | |
---|
439 | int owl_message_is_direction_none(const owl_message *m) |
---|
440 | { |
---|
441 | if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1); |
---|
442 | return(0); |
---|
443 | } |
---|
444 | |
---|
445 | int owl_message_get_numlines(owl_message *m) |
---|
446 | { |
---|
447 | if (m == NULL) return(0); |
---|
448 | owl_message_format(m); |
---|
449 | return(owl_fmtext_num_lines(&(m->fmtext->fmtext))); |
---|
450 | } |
---|
451 | |
---|
452 | void owl_message_mark_delete(owl_message *m) |
---|
453 | { |
---|
454 | if (m == NULL) return; |
---|
455 | m->delete=1; |
---|
456 | } |
---|
457 | |
---|
458 | void owl_message_unmark_delete(owl_message *m) |
---|
459 | { |
---|
460 | if (m == NULL) return; |
---|
461 | m->delete=0; |
---|
462 | } |
---|
463 | |
---|
464 | const char *owl_message_get_zwriteline(const owl_message *m) |
---|
465 | { |
---|
466 | const char *z = owl_message_get_attribute_value(m, "zwriteline"); |
---|
467 | if (!z) return ""; |
---|
468 | return z; |
---|
469 | } |
---|
470 | |
---|
471 | void owl_message_set_zwriteline(owl_message *m, const char *line) |
---|
472 | { |
---|
473 | owl_message_set_attribute(m, "zwriteline", line); |
---|
474 | } |
---|
475 | |
---|
476 | int owl_message_is_delete(const owl_message *m) |
---|
477 | { |
---|
478 | if (m == NULL) return(0); |
---|
479 | if (m->delete==1) return(1); |
---|
480 | return(0); |
---|
481 | } |
---|
482 | |
---|
483 | #ifdef HAVE_LIBZEPHYR |
---|
484 | const ZNotice_t *owl_message_get_notice(const owl_message *m) |
---|
485 | { |
---|
486 | return(&(m->notice)); |
---|
487 | } |
---|
488 | #else |
---|
489 | void *owl_message_get_notice(const owl_message *m) |
---|
490 | { |
---|
491 | return(NULL); |
---|
492 | } |
---|
493 | #endif |
---|
494 | |
---|
495 | void owl_message_set_hostname(owl_message *m, const char *hostname) |
---|
496 | { |
---|
497 | m->hostname=owl_global_intern(&g, hostname); |
---|
498 | } |
---|
499 | |
---|
500 | const char *owl_message_get_hostname(const owl_message *m) |
---|
501 | { |
---|
502 | return(m->hostname); |
---|
503 | } |
---|
504 | |
---|
505 | void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int fgcolor, int bgcolor) |
---|
506 | { |
---|
507 | owl_fmtext a, b; |
---|
508 | |
---|
509 | /* this will ensure that our cached copy is up to date */ |
---|
510 | owl_message_format(m); |
---|
511 | |
---|
512 | owl_fmtext_init_null(&a); |
---|
513 | owl_fmtext_init_null(&b); |
---|
514 | |
---|
515 | owl_fmtext_truncate_lines(&(m->fmtext->fmtext), aline, bline-aline, &a); |
---|
516 | owl_fmtext_truncate_cols(&a, acol, bcol, &b); |
---|
517 | owl_fmtext_colorize(&b, fgcolor); |
---|
518 | owl_fmtext_colorizebg(&b, bgcolor); |
---|
519 | |
---|
520 | owl_fmtext_curs_waddstr(&b, win); |
---|
521 | |
---|
522 | owl_fmtext_free(&a); |
---|
523 | owl_fmtext_free(&b); |
---|
524 | } |
---|
525 | |
---|
526 | int owl_message_is_personal(const owl_message *m) |
---|
527 | { |
---|
528 | owl_filter * f = owl_global_get_filter(&g, "personal"); |
---|
529 | if(!f) { |
---|
530 | owl_function_error("personal filter is not defined"); |
---|
531 | return (0); |
---|
532 | } |
---|
533 | return owl_filter_message_match(f, m); |
---|
534 | } |
---|
535 | |
---|
536 | int owl_message_is_question(const owl_message *m) |
---|
537 | { |
---|
538 | if(!owl_message_is_type_admin(m)) return 0; |
---|
539 | if(owl_message_get_attribute_value(m, "question") != NULL) return 1; |
---|
540 | return 0; |
---|
541 | } |
---|
542 | |
---|
543 | int owl_message_is_answered(const owl_message *m) { |
---|
544 | const char *q; |
---|
545 | if(!owl_message_is_question(m)) return 0; |
---|
546 | q = owl_message_get_attribute_value(m, "question"); |
---|
547 | if(!q) return 0; |
---|
548 | return !strcmp(q, "answered"); |
---|
549 | } |
---|
550 | |
---|
551 | void owl_message_set_isanswered(owl_message *m) { |
---|
552 | owl_message_set_attribute(m, "question", "answered"); |
---|
553 | } |
---|
554 | |
---|
555 | int owl_message_is_mail(const owl_message *m) |
---|
556 | { |
---|
557 | if (owl_message_is_type_zephyr(m)) { |
---|
558 | if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) { |
---|
559 | return(1); |
---|
560 | } else { |
---|
561 | return(0); |
---|
562 | } |
---|
563 | } |
---|
564 | return(0); |
---|
565 | } |
---|
566 | |
---|
567 | /* caller must free return value. */ |
---|
568 | char *owl_message_get_cc(const owl_message *m) |
---|
569 | { |
---|
570 | const char *cur; |
---|
571 | char *out, *end; |
---|
572 | |
---|
573 | cur = owl_message_get_body(m); |
---|
574 | while (*cur && *cur==' ') cur++; |
---|
575 | if (strncasecmp(cur, "cc:", 3)) return(NULL); |
---|
576 | cur+=3; |
---|
577 | while (*cur && *cur==' ') cur++; |
---|
578 | out = owl_strdup(cur); |
---|
579 | end = strchr(out, '\n'); |
---|
580 | if (end) end[0] = '\0'; |
---|
581 | return(out); |
---|
582 | } |
---|
583 | |
---|
584 | /* caller must free return value */ |
---|
585 | char *owl_message_get_cc_without_recipient(const owl_message *m) |
---|
586 | { |
---|
587 | char *cc, *out, *end, *shortuser, *recip; |
---|
588 | const char *user; |
---|
589 | |
---|
590 | cc = owl_message_get_cc(m); |
---|
591 | if (cc == NULL) |
---|
592 | return NULL; |
---|
593 | |
---|
594 | recip = short_zuser(owl_message_get_recipient(m)); |
---|
595 | out = owl_malloc(strlen(cc)); |
---|
596 | end = out; |
---|
597 | |
---|
598 | user = strtok(cc, " "); |
---|
599 | while (user != NULL) { |
---|
600 | shortuser = short_zuser(user); |
---|
601 | if (strcasecmp(shortuser, recip) != 0) { |
---|
602 | strcpy(end, user); |
---|
603 | end[strlen(user)] = ' '; |
---|
604 | end += strlen(user) + 1; |
---|
605 | } |
---|
606 | free(shortuser); |
---|
607 | user = strtok(NULL, " "); |
---|
608 | } |
---|
609 | end[0] = '\0'; |
---|
610 | |
---|
611 | owl_free(recip); |
---|
612 | owl_free(cc); |
---|
613 | |
---|
614 | if (strlen(out) == 0) { |
---|
615 | owl_free(out); |
---|
616 | out = NULL; |
---|
617 | } |
---|
618 | |
---|
619 | return(out); |
---|
620 | } |
---|
621 | |
---|
622 | int owl_message_get_id(const owl_message *m) |
---|
623 | { |
---|
624 | return(m->id); |
---|
625 | } |
---|
626 | |
---|
627 | const char *owl_message_get_type(const owl_message *m) { |
---|
628 | const char * type = owl_message_get_attribute_value(m, "type"); |
---|
629 | if(!type) { |
---|
630 | return "generic"; |
---|
631 | } |
---|
632 | return type; |
---|
633 | } |
---|
634 | |
---|
635 | const char *owl_message_get_direction(const owl_message *m) { |
---|
636 | switch (m->direction) { |
---|
637 | case OWL_MESSAGE_DIRECTION_IN: |
---|
638 | return("in"); |
---|
639 | case OWL_MESSAGE_DIRECTION_OUT: |
---|
640 | return("out"); |
---|
641 | case OWL_MESSAGE_DIRECTION_NONE: |
---|
642 | return("none"); |
---|
643 | default: |
---|
644 | return("unknown"); |
---|
645 | } |
---|
646 | } |
---|
647 | |
---|
648 | int owl_message_parse_direction(const char *d) { |
---|
649 | if(!strcmp(d, "in")) { |
---|
650 | return OWL_MESSAGE_DIRECTION_IN; |
---|
651 | } else if(!strcmp(d, "out")) { |
---|
652 | return OWL_MESSAGE_DIRECTION_OUT; |
---|
653 | } else { |
---|
654 | return OWL_MESSAGE_DIRECTION_NONE; |
---|
655 | } |
---|
656 | } |
---|
657 | |
---|
658 | |
---|
659 | const char *owl_message_get_login(const owl_message *m) { |
---|
660 | if (owl_message_is_login(m)) { |
---|
661 | return "login"; |
---|
662 | } else if (owl_message_is_logout(m)) { |
---|
663 | return "logout"; |
---|
664 | } else { |
---|
665 | return "none"; |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | |
---|
670 | const char *owl_message_get_header(const owl_message *m) { |
---|
671 | return owl_message_get_attribute_value(m, "adminheader"); |
---|
672 | } |
---|
673 | |
---|
674 | /* return 1 if the message contains "string", 0 otherwise. This is |
---|
675 | * case insensitive because the functions it uses are |
---|
676 | */ |
---|
677 | int owl_message_search(owl_message *m, owl_regex *re) |
---|
678 | { |
---|
679 | |
---|
680 | owl_message_format(m); /* is this necessary? */ |
---|
681 | |
---|
682 | return (owl_fmtext_search(&(m->fmtext->fmtext), re)); |
---|
683 | } |
---|
684 | |
---|
685 | |
---|
686 | /* if loginout == -1 it's a logout message |
---|
687 | * 0 it's not a login/logout message |
---|
688 | * 1 it's a login message |
---|
689 | */ |
---|
690 | void owl_message_create_aim(owl_message *m, const char *sender, const char *recipient, const char *text, int direction, int loginout) |
---|
691 | { |
---|
692 | owl_message_init(m); |
---|
693 | owl_message_set_body(m, text); |
---|
694 | owl_message_set_sender(m, sender); |
---|
695 | owl_message_set_recipient(m, recipient); |
---|
696 | owl_message_set_type_aim(m); |
---|
697 | |
---|
698 | if (direction==OWL_MESSAGE_DIRECTION_IN) { |
---|
699 | owl_message_set_direction_in(m); |
---|
700 | } else if (direction==OWL_MESSAGE_DIRECTION_OUT) { |
---|
701 | owl_message_set_direction_out(m); |
---|
702 | } |
---|
703 | |
---|
704 | /* for now all messages that aren't loginout are private */ |
---|
705 | if (!loginout) { |
---|
706 | owl_message_set_isprivate(m); |
---|
707 | } |
---|
708 | |
---|
709 | if (loginout==-1) { |
---|
710 | owl_message_set_islogout(m); |
---|
711 | } else if (loginout==1) { |
---|
712 | owl_message_set_islogin(m); |
---|
713 | } |
---|
714 | } |
---|
715 | |
---|
716 | void owl_message_create_admin(owl_message *m, const char *header, const char *text) |
---|
717 | { |
---|
718 | owl_message_init(m); |
---|
719 | owl_message_set_type_admin(m); |
---|
720 | owl_message_set_body(m, text); |
---|
721 | owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */ |
---|
722 | } |
---|
723 | |
---|
724 | /* caller should set the direction */ |
---|
725 | void owl_message_create_loopback(owl_message *m, const char *text) |
---|
726 | { |
---|
727 | owl_message_init(m); |
---|
728 | owl_message_set_type_loopback(m); |
---|
729 | owl_message_set_body(m, text); |
---|
730 | owl_message_set_sender(m, "loopsender"); |
---|
731 | owl_message_set_recipient(m, "looprecip"); |
---|
732 | owl_message_set_isprivate(m); |
---|
733 | } |
---|
734 | |
---|
735 | #ifdef HAVE_LIBZEPHYR |
---|
736 | void owl_message_create_from_znotice(owl_message *m, const ZNotice_t *n) |
---|
737 | { |
---|
738 | struct hostent *hent; |
---|
739 | const char *ptr; |
---|
740 | char *tmp, *tmp2; |
---|
741 | int len; |
---|
742 | |
---|
743 | owl_message_init(m); |
---|
744 | |
---|
745 | owl_message_set_type_zephyr(m); |
---|
746 | owl_message_set_direction_in(m); |
---|
747 | |
---|
748 | /* first save the full notice */ |
---|
749 | memcpy(&(m->notice), n, sizeof(ZNotice_t)); |
---|
750 | |
---|
751 | /* a little gross, we'll replace \r's with ' ' for now */ |
---|
752 | owl_zephyr_hackaway_cr(&(m->notice)); |
---|
753 | |
---|
754 | /* save the time, we need to nuke the string saved by message_init */ |
---|
755 | if (m->timestr) owl_free(m->timestr); |
---|
756 | m->time=n->z_time.tv_sec; |
---|
757 | m->timestr=owl_strdup(ctime(&(m->time))); |
---|
758 | m->timestr[strlen(m->timestr)-1]='\0'; |
---|
759 | |
---|
760 | /* set other info */ |
---|
761 | owl_message_set_sender(m, n->z_sender); |
---|
762 | owl_message_set_class(m, n->z_class); |
---|
763 | owl_message_set_instance(m, n->z_class_inst); |
---|
764 | owl_message_set_recipient(m, n->z_recipient); |
---|
765 | if (n->z_opcode) { |
---|
766 | owl_message_set_opcode(m, n->z_opcode); |
---|
767 | } else { |
---|
768 | owl_message_set_opcode(m, ""); |
---|
769 | } |
---|
770 | owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len)); |
---|
771 | |
---|
772 | if ((ptr=strchr(n->z_recipient, '@'))!=NULL) { |
---|
773 | owl_message_set_realm(m, ptr+1); |
---|
774 | } else { |
---|
775 | owl_message_set_realm(m, owl_zephyr_get_realm()); |
---|
776 | } |
---|
777 | |
---|
778 | /* Set the "isloginout" attribute if it's a login message */ |
---|
779 | if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) { |
---|
780 | if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) { |
---|
781 | tmp=owl_zephyr_get_field(n, 1); |
---|
782 | owl_message_set_attribute(m, "loginhost", tmp); |
---|
783 | owl_free(tmp); |
---|
784 | |
---|
785 | tmp=owl_zephyr_get_field(n, 3); |
---|
786 | owl_message_set_attribute(m, "logintty", tmp); |
---|
787 | owl_free(tmp); |
---|
788 | } |
---|
789 | |
---|
790 | if (!strcasecmp(n->z_opcode, "user_login")) { |
---|
791 | owl_message_set_islogin(m); |
---|
792 | } else if (!strcasecmp(n->z_opcode, "user_logout")) { |
---|
793 | owl_message_set_islogout(m); |
---|
794 | } |
---|
795 | } |
---|
796 | |
---|
797 | |
---|
798 | /* set the "isprivate" attribute if it's a private zephyr. |
---|
799 | ``private'' means recipient is non-empty and doesn't start wit |
---|
800 | `@' */ |
---|
801 | if (*n->z_recipient && *n->z_recipient != '@') { |
---|
802 | owl_message_set_isprivate(m); |
---|
803 | } |
---|
804 | |
---|
805 | /* set the "isauto" attribute if it's an autoreply */ |
---|
806 | if (!strcasecmp(n->z_message, "Automated reply:") || |
---|
807 | !strcasecmp(n->z_opcode, "auto")) { |
---|
808 | owl_message_set_attribute(m, "isauto", ""); |
---|
809 | } |
---|
810 | |
---|
811 | /* save the hostname */ |
---|
812 | owl_function_debugmsg("About to do gethostbyaddr"); |
---|
813 | hent=gethostbyaddr(&(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET); |
---|
814 | if (hent && hent->h_name) { |
---|
815 | owl_message_set_hostname(m, hent->h_name); |
---|
816 | } else { |
---|
817 | owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr)); |
---|
818 | } |
---|
819 | |
---|
820 | /* set the body */ |
---|
821 | tmp=owl_zephyr_get_message(n, m); |
---|
822 | if (owl_global_is_newlinestrip(&g)) { |
---|
823 | tmp2=owl_util_stripnewlines(tmp); |
---|
824 | owl_message_set_body(m, tmp2); |
---|
825 | owl_free(tmp2); |
---|
826 | } else { |
---|
827 | owl_message_set_body(m, tmp); |
---|
828 | } |
---|
829 | owl_free(tmp); |
---|
830 | |
---|
831 | #ifdef OWL_ENABLE_ZCRYPT |
---|
832 | /* if zcrypt is enabled try to decrypt the message */ |
---|
833 | if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) { |
---|
834 | char *out; |
---|
835 | int ret; |
---|
836 | |
---|
837 | out=owl_malloc(strlen(owl_message_get_body(m))*16+20); |
---|
838 | ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m)); |
---|
839 | if (ret==0) { |
---|
840 | owl_message_set_body(m, out); |
---|
841 | } else { |
---|
842 | owl_free(out); |
---|
843 | } |
---|
844 | } |
---|
845 | #endif |
---|
846 | } |
---|
847 | #else |
---|
848 | void owl_message_create_from_znotice(owl_message *m, const void *n) |
---|
849 | { |
---|
850 | } |
---|
851 | #endif |
---|
852 | |
---|
853 | /* If 'direction' is '0' it is a login message, '1' is a logout message. */ |
---|
854 | void owl_message_create_pseudo_zlogin(owl_message *m, int direction, const char *user, const char *host, const char *time, const char *tty) |
---|
855 | { |
---|
856 | char *longuser; |
---|
857 | const char *ptr; |
---|
858 | |
---|
859 | #ifdef HAVE_LIBZEPHYR |
---|
860 | memset(&(m->notice), 0, sizeof(ZNotice_t)); |
---|
861 | #endif |
---|
862 | |
---|
863 | longuser=long_zuser(user); |
---|
864 | |
---|
865 | owl_message_init(m); |
---|
866 | |
---|
867 | owl_message_set_type_zephyr(m); |
---|
868 | owl_message_set_direction_in(m); |
---|
869 | |
---|
870 | owl_message_set_attribute(m, "pseudo", ""); |
---|
871 | owl_message_set_attribute(m, "loginhost", host ? host : ""); |
---|
872 | owl_message_set_attribute(m, "logintty", tty ? tty : ""); |
---|
873 | |
---|
874 | owl_message_set_sender(m, longuser); |
---|
875 | owl_message_set_class(m, "LOGIN"); |
---|
876 | owl_message_set_instance(m, longuser); |
---|
877 | owl_message_set_recipient(m, ""); |
---|
878 | if (direction==0) { |
---|
879 | owl_message_set_opcode(m, "USER_LOGIN"); |
---|
880 | owl_message_set_islogin(m); |
---|
881 | } else if (direction==1) { |
---|
882 | owl_message_set_opcode(m, "USER_LOGOUT"); |
---|
883 | owl_message_set_islogout(m); |
---|
884 | } |
---|
885 | |
---|
886 | if ((ptr=strchr(longuser, '@'))!=NULL) { |
---|
887 | owl_message_set_realm(m, ptr+1); |
---|
888 | } else { |
---|
889 | owl_message_set_realm(m, owl_zephyr_get_realm()); |
---|
890 | } |
---|
891 | |
---|
892 | owl_message_set_body(m, "<uninitialized>"); |
---|
893 | |
---|
894 | /* save the hostname */ |
---|
895 | owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : ""); |
---|
896 | owl_message_set_hostname(m, host ? host : ""); |
---|
897 | owl_free(longuser); |
---|
898 | } |
---|
899 | |
---|
900 | void owl_message_create_from_zwriteline(owl_message *m, const char *line, const char *body, const char *zsig) |
---|
901 | { |
---|
902 | owl_zwrite z; |
---|
903 | int ret; |
---|
904 | char hostbuff[5000]; |
---|
905 | |
---|
906 | owl_message_init(m); |
---|
907 | |
---|
908 | /* create a zwrite for the purpose of filling in other message fields */ |
---|
909 | owl_zwrite_create_from_line(&z, line); |
---|
910 | |
---|
911 | /* set things */ |
---|
912 | owl_message_set_direction_out(m); |
---|
913 | owl_message_set_type_zephyr(m); |
---|
914 | owl_message_set_sender(m, owl_zephyr_get_sender()); |
---|
915 | owl_message_set_class(m, owl_zwrite_get_class(&z)); |
---|
916 | owl_message_set_instance(m, owl_zwrite_get_instance(&z)); |
---|
917 | if (owl_zwrite_get_numrecips(&z)>0) { |
---|
918 | char *longzuser = long_zuser(owl_zwrite_get_recip_n(&z, 0)); |
---|
919 | owl_message_set_recipient(m, |
---|
920 | longzuser); /* only gets the first user, must fix */ |
---|
921 | owl_free(longzuser); |
---|
922 | } |
---|
923 | owl_message_set_opcode(m, owl_zwrite_get_opcode(&z)); |
---|
924 | owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */ |
---|
925 | owl_message_set_zwriteline(m, line); |
---|
926 | owl_message_set_body(m, body); |
---|
927 | owl_message_set_zsig(m, zsig); |
---|
928 | |
---|
929 | /* save the hostname */ |
---|
930 | ret=gethostname(hostbuff, MAXHOSTNAMELEN); |
---|
931 | hostbuff[MAXHOSTNAMELEN]='\0'; |
---|
932 | if (ret) { |
---|
933 | owl_message_set_hostname(m, "localhost"); |
---|
934 | } else { |
---|
935 | owl_message_set_hostname(m, hostbuff); |
---|
936 | } |
---|
937 | |
---|
938 | /* set the "isprivate" attribute if it's a private zephyr. */ |
---|
939 | if (owl_zwrite_is_personal(&z)) { |
---|
940 | owl_message_set_isprivate(m); |
---|
941 | } |
---|
942 | |
---|
943 | owl_zwrite_free(&z); |
---|
944 | } |
---|
945 | |
---|
946 | void owl_message_free(owl_message *m) |
---|
947 | { |
---|
948 | int i, j; |
---|
949 | owl_pair *p; |
---|
950 | #ifdef HAVE_LIBZEPHYR |
---|
951 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
952 | ZFreeNotice(&(m->notice)); |
---|
953 | } |
---|
954 | #endif |
---|
955 | if (m->timestr) owl_free(m->timestr); |
---|
956 | |
---|
957 | /* free all the attributes */ |
---|
958 | j=owl_list_get_size(&(m->attributes)); |
---|
959 | for (i=0; i<j; i++) { |
---|
960 | p=owl_list_get_element(&(m->attributes), i); |
---|
961 | owl_free(owl_pair_get_value(p)); |
---|
962 | owl_free(p); |
---|
963 | } |
---|
964 | |
---|
965 | owl_list_free_simple(&(m->attributes)); |
---|
966 | |
---|
967 | owl_message_invalidate_format(m); |
---|
968 | } |
---|