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