1 | #include <zephyr/zephyr.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <unistd.h> |
---|
4 | #include <string.h> |
---|
5 | #include <sys/socket.h> |
---|
6 | #include <netdb.h> |
---|
7 | #include <sys/types.h> |
---|
8 | #include <sys/socket.h> |
---|
9 | #include <netinet/in.h> |
---|
10 | #include <arpa/inet.h> |
---|
11 | #include <time.h> |
---|
12 | #include "owl.h" |
---|
13 | |
---|
14 | static const char fileIdent[] = "$Id$"; |
---|
15 | |
---|
16 | void owl_message_init(owl_message *m) { |
---|
17 | time_t t; |
---|
18 | |
---|
19 | m->id=owl_global_get_nextmsgid(&g); |
---|
20 | m->type=OWL_MESSAGE_TYPE_GENERIC; |
---|
21 | owl_message_set_direction_none(m); |
---|
22 | m->delete=0; |
---|
23 | strcpy(m->hostname, ""); |
---|
24 | m->zwriteline=strdup(""); |
---|
25 | |
---|
26 | owl_list_create(&(m->attributes)); |
---|
27 | |
---|
28 | /* save the time */ |
---|
29 | t=time(NULL); |
---|
30 | m->time=owl_strdup(ctime(&t)); |
---|
31 | m->time[strlen(m->time)-1]='\0'; |
---|
32 | } |
---|
33 | |
---|
34 | void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue) { |
---|
35 | /* add the named attribute to the message. If an attribute with the |
---|
36 | name already exists, replace the old value with the new value */ |
---|
37 | |
---|
38 | int i, j; |
---|
39 | owl_pair *p; |
---|
40 | |
---|
41 | /* look for an existing pair with this key, and nuke the entry if |
---|
42 | found */ |
---|
43 | j=owl_list_get_size(&(m->attributes)); |
---|
44 | for (i=0; i<j; i++) { |
---|
45 | p=owl_list_get_element(&(m->attributes), i); |
---|
46 | if (!strcmp(owl_pair_get_key(p), attrname)) { |
---|
47 | owl_free(owl_pair_get_key(p)); |
---|
48 | owl_free(owl_pair_get_value(p)); |
---|
49 | owl_free(p); |
---|
50 | owl_list_remove_element(&(m->attributes), i); |
---|
51 | break; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | p=owl_malloc(sizeof(owl_pair)); |
---|
56 | owl_pair_create(p, owl_strdup(attrname), owl_strdup(attrvalue)); |
---|
57 | owl_list_append_element(&(m->attributes), p); |
---|
58 | } |
---|
59 | |
---|
60 | char *owl_message_get_attribute_value(owl_message *m, char *attrname) { |
---|
61 | /* return the value associated with the named attribute, or NULL if |
---|
62 | the attribute does not exist */ |
---|
63 | int i, j; |
---|
64 | owl_pair *p; |
---|
65 | |
---|
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 (!strcmp(owl_pair_get_key(p), attrname)) { |
---|
70 | return(owl_pair_get_value(p)); |
---|
71 | } |
---|
72 | } |
---|
73 | owl_function_debugmsg("No attribute %s found", attrname); |
---|
74 | return(NULL); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | owl_fmtext *owl_message_get_fmtext(owl_message *m) { |
---|
79 | return(&(m->fmtext)); |
---|
80 | } |
---|
81 | |
---|
82 | void owl_message_set_class(owl_message *m, char *class) { |
---|
83 | owl_message_set_attribute(m, "class", class); |
---|
84 | } |
---|
85 | |
---|
86 | char *owl_message_get_class(owl_message *m) { |
---|
87 | char *class; |
---|
88 | |
---|
89 | class=owl_message_get_attribute_value(m, "class"); |
---|
90 | if (!class) return(""); |
---|
91 | return(class); |
---|
92 | } |
---|
93 | |
---|
94 | void owl_message_set_instance(owl_message *m, char *inst) { |
---|
95 | owl_message_set_attribute(m, "instance", inst); |
---|
96 | } |
---|
97 | |
---|
98 | char *owl_message_get_instance(owl_message *m) { |
---|
99 | char *instance; |
---|
100 | |
---|
101 | instance=owl_message_get_attribute_value(m, "instance"); |
---|
102 | if (!instance) return(""); |
---|
103 | return(instance); |
---|
104 | } |
---|
105 | |
---|
106 | void owl_message_set_sender(owl_message *m, char *sender) { |
---|
107 | owl_message_set_attribute(m, "sender", sender); |
---|
108 | } |
---|
109 | |
---|
110 | char *owl_message_get_sender(owl_message *m) { |
---|
111 | char *sender; |
---|
112 | |
---|
113 | sender=owl_message_get_attribute_value(m, "sender"); |
---|
114 | if (!sender) return(""); |
---|
115 | return(sender); |
---|
116 | } |
---|
117 | |
---|
118 | void owl_message_set_zsig(owl_message *m, char *zsig) { |
---|
119 | owl_message_set_attribute(m, "zsig", zsig); |
---|
120 | } |
---|
121 | |
---|
122 | char *owl_message_get_zsig(owl_message *m) { |
---|
123 | char *zsig; |
---|
124 | |
---|
125 | zsig=owl_message_get_attribute_value(m, "zsig"); |
---|
126 | if (!zsig) return(""); |
---|
127 | return(zsig); |
---|
128 | } |
---|
129 | |
---|
130 | void owl_message_set_recipient(owl_message *m, char *recip) { |
---|
131 | owl_message_set_attribute(m, "recipient", recip); |
---|
132 | } |
---|
133 | |
---|
134 | char *owl_message_get_recipient(owl_message *m) { |
---|
135 | /* this is stupid for outgoing messages, we need to fix it. */ |
---|
136 | |
---|
137 | char *recip; |
---|
138 | |
---|
139 | if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) { |
---|
140 | recip=owl_message_get_attribute_value(m, "recipient"); |
---|
141 | } else if (owl_message_is_direction_out(m)) { |
---|
142 | recip=m->zwriteline; |
---|
143 | } else { |
---|
144 | recip=owl_message_get_attribute_value(m, "recipient"); |
---|
145 | } |
---|
146 | if (!recip) return(""); |
---|
147 | return(recip); |
---|
148 | } |
---|
149 | |
---|
150 | void owl_message_set_realm(owl_message *m, char *realm) { |
---|
151 | owl_message_set_attribute(m, "realm", realm); |
---|
152 | } |
---|
153 | |
---|
154 | char *owl_message_get_realm(owl_message *m) { |
---|
155 | char *realm; |
---|
156 | |
---|
157 | realm=owl_message_get_attribute_value(m, "realm"); |
---|
158 | if (!realm) return(""); |
---|
159 | return(realm); |
---|
160 | } |
---|
161 | |
---|
162 | void owl_message_set_body(owl_message *m, char *body) { |
---|
163 | owl_message_set_attribute(m, "body", body); |
---|
164 | } |
---|
165 | |
---|
166 | char *owl_message_get_body(owl_message *m) { |
---|
167 | char *body; |
---|
168 | |
---|
169 | body=owl_message_get_attribute_value(m, "body"); |
---|
170 | if (!body) return(""); |
---|
171 | return(body); |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | void owl_message_set_opcode(owl_message *m, char *opcode) { |
---|
176 | owl_message_set_attribute(m, "opcode", opcode); |
---|
177 | } |
---|
178 | |
---|
179 | char *owl_message_get_opcode(owl_message *m) { |
---|
180 | char *opcode; |
---|
181 | |
---|
182 | opcode=owl_message_get_attribute_value(m, "opcode"); |
---|
183 | if (!opcode) return(""); |
---|
184 | return(opcode); |
---|
185 | } |
---|
186 | |
---|
187 | char *owl_message_get_timestr(owl_message *m) { |
---|
188 | return(m->time); |
---|
189 | } |
---|
190 | |
---|
191 | void owl_message_set_type_admin(owl_message *m) { |
---|
192 | m->type=OWL_MESSAGE_TYPE_ADMIN; |
---|
193 | } |
---|
194 | |
---|
195 | void owl_message_set_type_zephyr(owl_message *m) { |
---|
196 | m->type=OWL_MESSAGE_TYPE_ZEPHYR; |
---|
197 | } |
---|
198 | |
---|
199 | int owl_message_is_type_admin(owl_message *m) { |
---|
200 | if (m->type==OWL_MESSAGE_TYPE_ADMIN) return(1); |
---|
201 | return(0); |
---|
202 | } |
---|
203 | |
---|
204 | int owl_message_is_type_zephyr(owl_message *m) { |
---|
205 | if (m->type==OWL_MESSAGE_TYPE_ZEPHYR) return(1); |
---|
206 | return(0); |
---|
207 | } |
---|
208 | |
---|
209 | int owl_message_is_type_generic(owl_message *m) { |
---|
210 | if (m->type==OWL_MESSAGE_TYPE_GENERIC) return(1); |
---|
211 | return(0); |
---|
212 | } |
---|
213 | |
---|
214 | char *owl_message_get_text(owl_message *m) { |
---|
215 | return(owl_fmtext_get_text(&(m->fmtext))); |
---|
216 | } |
---|
217 | |
---|
218 | void owl_message_set_direction_in(owl_message *m) { |
---|
219 | m->direction=OWL_MESSAGE_DIRECTION_IN; |
---|
220 | } |
---|
221 | |
---|
222 | void owl_message_set_direction_out(owl_message *m) { |
---|
223 | m->direction=OWL_MESSAGE_DIRECTION_OUT; |
---|
224 | } |
---|
225 | |
---|
226 | void owl_message_set_direction_none(owl_message *m) { |
---|
227 | m->direction=OWL_MESSAGE_DIRECTION_NONE; |
---|
228 | } |
---|
229 | |
---|
230 | int owl_message_is_direction_in(owl_message *m) { |
---|
231 | if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1); |
---|
232 | return(0); |
---|
233 | } |
---|
234 | |
---|
235 | int owl_message_is_direction_out(owl_message *m) { |
---|
236 | if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1); |
---|
237 | return(0); |
---|
238 | } |
---|
239 | |
---|
240 | int owl_message_is_direction_none(owl_message *m) { |
---|
241 | if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1); |
---|
242 | return(0); |
---|
243 | } |
---|
244 | |
---|
245 | int owl_message_get_numlines(owl_message *m) { |
---|
246 | if (m == NULL) return(0); |
---|
247 | return(owl_fmtext_num_lines(&(m->fmtext))); |
---|
248 | } |
---|
249 | |
---|
250 | void owl_message_mark_delete(owl_message *m) { |
---|
251 | if (m == NULL) return; |
---|
252 | m->delete=1; |
---|
253 | } |
---|
254 | |
---|
255 | void owl_message_unmark_delete(owl_message *m) { |
---|
256 | if (m == NULL) return; |
---|
257 | m->delete=0; |
---|
258 | } |
---|
259 | |
---|
260 | char *owl_message_get_zwriteline(owl_message *m) { |
---|
261 | return(m->zwriteline); |
---|
262 | } |
---|
263 | |
---|
264 | void owl_message_set_zwriteline(owl_message *m, char *line) { |
---|
265 | m->zwriteline=strdup(line); |
---|
266 | } |
---|
267 | |
---|
268 | int owl_message_is_delete(owl_message *m) { |
---|
269 | if (m == NULL) return(0); |
---|
270 | if (m->delete==1) return(1); |
---|
271 | return(0); |
---|
272 | } |
---|
273 | |
---|
274 | ZNotice_t *owl_message_get_notice(owl_message *m) { |
---|
275 | return(&(m->notice)); |
---|
276 | } |
---|
277 | |
---|
278 | char *owl_message_get_hostname(owl_message *m) { |
---|
279 | return(m->hostname); |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int color) { |
---|
284 | owl_fmtext a, b; |
---|
285 | |
---|
286 | owl_fmtext_init_null(&a); |
---|
287 | owl_fmtext_init_null(&b); |
---|
288 | |
---|
289 | owl_fmtext_truncate_lines(&(m->fmtext), aline, bline-aline+1, &a); |
---|
290 | owl_fmtext_truncate_cols(&a, acol, bcol, &b); |
---|
291 | if (color!=OWL_COLOR_DEFAULT) { |
---|
292 | owl_fmtext_colorize(&b, color); |
---|
293 | } |
---|
294 | |
---|
295 | if (owl_global_is_search_active(&g)) { |
---|
296 | owl_fmtext_search_and_highlight(&b, owl_global_get_search_string(&g)); |
---|
297 | } |
---|
298 | |
---|
299 | owl_fmtext_curs_waddstr(&b, win); |
---|
300 | |
---|
301 | owl_fmtext_free(&a); |
---|
302 | owl_fmtext_free(&b); |
---|
303 | } |
---|
304 | |
---|
305 | int owl_message_is_personal(owl_message *m) { |
---|
306 | if (strcasecmp(owl_message_get_class(m), "message")) return(0); |
---|
307 | if (strcasecmp(owl_message_get_instance(m), "personal")) return(0); |
---|
308 | if (!strcasecmp(owl_message_get_recipient(m), ZGetSender()) || |
---|
309 | !strcasecmp(owl_message_get_sender(m), ZGetSender())) { |
---|
310 | return(1); |
---|
311 | } |
---|
312 | return(0); |
---|
313 | } |
---|
314 | |
---|
315 | int owl_message_is_private(owl_message *m) { |
---|
316 | if (!strcasecmp(owl_message_get_recipient(m), ZGetSender())) return(1); |
---|
317 | return(0); |
---|
318 | } |
---|
319 | |
---|
320 | int owl_message_is_mail(owl_message *m) { |
---|
321 | if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) { |
---|
322 | return(1); |
---|
323 | } |
---|
324 | return(0); |
---|
325 | } |
---|
326 | |
---|
327 | int owl_message_is_ping(owl_message *m) { |
---|
328 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) return(1); |
---|
329 | return(0); |
---|
330 | } |
---|
331 | |
---|
332 | int owl_message_is_login(owl_message *m) { |
---|
333 | if (!strcasecmp(owl_message_get_class(m), "login")) return(1); |
---|
334 | return(0); |
---|
335 | /* is this good enough? */ |
---|
336 | } |
---|
337 | |
---|
338 | int owl_message_is_burningears(owl_message *m) { |
---|
339 | /* we should add a global to cache the short zsender */ |
---|
340 | char sender[LINE], *ptr; |
---|
341 | |
---|
342 | /* if the message is from us or to us, it doesn't count */ |
---|
343 | if (!strcasecmp(ZGetSender(), owl_message_get_sender(m))) return(0); |
---|
344 | if (!strcasecmp(ZGetSender(), owl_message_get_recipient(m))) return(0); |
---|
345 | |
---|
346 | strcpy(sender, ZGetSender()); |
---|
347 | ptr=strchr(sender, '@'); |
---|
348 | if (ptr) *ptr='\0'; |
---|
349 | |
---|
350 | if (stristr(owl_message_get_body(m), sender)) { |
---|
351 | return(1); |
---|
352 | } |
---|
353 | return(0); |
---|
354 | } |
---|
355 | |
---|
356 | /* caller must free return value. */ |
---|
357 | char *owl_message_get_cc(owl_message *m) { |
---|
358 | char *cur, *out, *end; |
---|
359 | |
---|
360 | cur = owl_message_get_body(m); |
---|
361 | while (*cur && *cur==' ') cur++; |
---|
362 | if (strncasecmp(cur, "cc:", 3)) return(NULL); |
---|
363 | cur+=3; |
---|
364 | while (*cur && *cur==' ') cur++; |
---|
365 | out = owl_strdup(cur); |
---|
366 | end = strchr(out, '\n'); |
---|
367 | if (end) end[0] = '\0'; |
---|
368 | return(out); |
---|
369 | } |
---|
370 | |
---|
371 | int owl_message_get_id(owl_message *m) { |
---|
372 | return(m->id); |
---|
373 | } |
---|
374 | |
---|
375 | int owl_message_search(owl_message *m, char *string) { |
---|
376 | /* return 1 if the message contains "string", 0 otherwise. This is |
---|
377 | * case insensitive because the functions it uses are */ |
---|
378 | |
---|
379 | return (owl_fmtext_search(&(m->fmtext), string)); |
---|
380 | } |
---|
381 | |
---|
382 | void owl_message_create(owl_message *m, char *header, char *text) { |
---|
383 | char *indent; |
---|
384 | |
---|
385 | owl_message_init(m); |
---|
386 | |
---|
387 | owl_message_set_body(m, text); |
---|
388 | |
---|
389 | indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10); |
---|
390 | owl_text_indent(indent, text, OWL_MSGTAB); |
---|
391 | owl_fmtext_init_null(&(m->fmtext)); |
---|
392 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
393 | owl_fmtext_append_ztext(&(m->fmtext), header); |
---|
394 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
395 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
396 | if (text[strlen(text)-1]!='\n') { |
---|
397 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
398 | } |
---|
399 | |
---|
400 | owl_free(indent); |
---|
401 | } |
---|
402 | |
---|
403 | void owl_message_create_admin(owl_message *m, char *header, char *text) { |
---|
404 | char *indent; |
---|
405 | |
---|
406 | owl_message_init(m); |
---|
407 | owl_message_set_type_admin(m); |
---|
408 | |
---|
409 | owl_message_set_body(m, text); |
---|
410 | |
---|
411 | /* do something to make it clear the notice shouldn't be used for now */ |
---|
412 | |
---|
413 | indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10); |
---|
414 | owl_text_indent(indent, text, OWL_MSGTAB); |
---|
415 | owl_fmtext_init_null(&(m->fmtext)); |
---|
416 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
417 | owl_fmtext_append_bold(&(m->fmtext), "OWL ADMIN "); |
---|
418 | owl_fmtext_append_ztext(&(m->fmtext), header); |
---|
419 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
420 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
421 | if (text[strlen(text)-1]!='\n') { |
---|
422 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
423 | } |
---|
424 | |
---|
425 | owl_free(indent); |
---|
426 | } |
---|
427 | |
---|
428 | void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n) { |
---|
429 | struct hostent *hent; |
---|
430 | int k, ret; |
---|
431 | char *ptr, *tmp, *tmp2; |
---|
432 | |
---|
433 | owl_message_init(m); |
---|
434 | |
---|
435 | owl_message_set_type_zephyr(m); |
---|
436 | owl_message_set_direction_in(m); |
---|
437 | |
---|
438 | /* first save the full notice */ |
---|
439 | memcpy(&(m->notice), n, sizeof(ZNotice_t)); |
---|
440 | |
---|
441 | /* a little gross, we'll reaplace \r's with ' ' for now */ |
---|
442 | owl_zephyr_hackaway_cr(&(m->notice)); |
---|
443 | |
---|
444 | m->delete=0; |
---|
445 | |
---|
446 | /* set other info */ |
---|
447 | owl_message_set_sender(m, n->z_sender); |
---|
448 | owl_message_set_class(m, n->z_class); |
---|
449 | owl_message_set_instance(m, n->z_class_inst); |
---|
450 | owl_message_set_recipient(m, n->z_recipient); |
---|
451 | if (n->z_opcode) { |
---|
452 | owl_message_set_opcode(m, n->z_opcode); |
---|
453 | } else { |
---|
454 | owl_message_set_opcode(m, ""); |
---|
455 | } |
---|
456 | owl_message_set_zsig(m, n->z_message); |
---|
457 | |
---|
458 | if ((ptr=strchr(n->z_recipient, '@'))!=NULL) { |
---|
459 | owl_message_set_realm(m, ptr+1); |
---|
460 | } else { |
---|
461 | owl_message_set_realm(m, ZGetRealm()); |
---|
462 | } |
---|
463 | |
---|
464 | m->zwriteline=strdup(""); |
---|
465 | |
---|
466 | /* set the body */ |
---|
467 | ptr=owl_zephyr_get_message(n, &k); |
---|
468 | tmp=owl_malloc(k+10); |
---|
469 | memcpy(tmp, ptr, k); |
---|
470 | tmp[k]='\0'; |
---|
471 | if (owl_global_is_newlinestrip(&g)) { |
---|
472 | tmp2=owl_util_stripnewlines(tmp); |
---|
473 | owl_message_set_body(m, tmp2); |
---|
474 | owl_free(tmp); |
---|
475 | owl_free(tmp2); |
---|
476 | } else { |
---|
477 | owl_message_set_body(m, tmp); |
---|
478 | } |
---|
479 | |
---|
480 | /* if zcrypt is enabled try to decrypt the message */ |
---|
481 | if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) { |
---|
482 | char *out; |
---|
483 | |
---|
484 | out=owl_malloc(strlen(owl_message_get_body(m))*16+20); |
---|
485 | ret=zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m)); |
---|
486 | if (ret==0) { |
---|
487 | owl_message_set_body(m, out); |
---|
488 | } else { |
---|
489 | owl_free(out); |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | /* save the hostname */ |
---|
494 | owl_function_debugmsg("About to do gethostbyaddr"); |
---|
495 | hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET); |
---|
496 | if (hent && hent->h_name) { |
---|
497 | strcpy(m->hostname, hent->h_name); |
---|
498 | } else { |
---|
499 | strcpy(m->hostname, inet_ntoa(n->z_sender_addr)); |
---|
500 | } |
---|
501 | |
---|
502 | /* save the time */ |
---|
503 | m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec)); |
---|
504 | m->time[strlen(m->time)-1]='\0'; |
---|
505 | |
---|
506 | /* create the formatted message */ |
---|
507 | if (owl_global_is_config_format(&g)) { |
---|
508 | _owl_message_make_text_from_config(m); |
---|
509 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
510 | _owl_message_make_text_from_notice_standard(m); |
---|
511 | } else { |
---|
512 | _owl_message_make_text_from_notice_simple(m); |
---|
513 | } |
---|
514 | |
---|
515 | } |
---|
516 | |
---|
517 | void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig) { |
---|
518 | owl_zwrite z; |
---|
519 | int ret; |
---|
520 | |
---|
521 | owl_message_init(m); |
---|
522 | |
---|
523 | /* create a zwrite for the purpose of filling in other message fields */ |
---|
524 | owl_zwrite_create_from_line(&z, line); |
---|
525 | |
---|
526 | /* set things */ |
---|
527 | owl_message_set_direction_out(m); |
---|
528 | owl_message_set_type_zephyr(m); |
---|
529 | owl_message_set_sender(m, ZGetSender()); |
---|
530 | owl_message_set_class(m, owl_zwrite_get_class(&z)); |
---|
531 | owl_message_set_instance(m, owl_zwrite_get_instance(&z)); |
---|
532 | owl_message_set_recipient(m, |
---|
533 | long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */ |
---|
534 | owl_message_set_opcode(m, owl_zwrite_get_opcode(&z)); |
---|
535 | owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */ |
---|
536 | m->zwriteline=owl_strdup(line); |
---|
537 | owl_message_set_body(m, body); |
---|
538 | owl_message_set_zsig(m, zsig); |
---|
539 | |
---|
540 | /* save the hostname */ |
---|
541 | ret=gethostname(m->hostname, MAXHOSTNAMELEN); |
---|
542 | if (ret) { |
---|
543 | strcpy(m->hostname, "localhost"); |
---|
544 | } |
---|
545 | |
---|
546 | /* create the formatted message */ |
---|
547 | if (owl_global_is_config_format(&g)) { |
---|
548 | _owl_message_make_text_from_config(m); |
---|
549 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
550 | _owl_message_make_text_from_zwriteline_standard(m); |
---|
551 | } else { |
---|
552 | _owl_message_make_text_from_zwriteline_simple(m); |
---|
553 | } |
---|
554 | |
---|
555 | owl_zwrite_free(&z); |
---|
556 | } |
---|
557 | |
---|
558 | void _owl_message_make_text_from_config(owl_message *m) { |
---|
559 | char *body, *indent; |
---|
560 | |
---|
561 | owl_fmtext_init_null(&(m->fmtext)); |
---|
562 | |
---|
563 | /* get body from the config */ |
---|
564 | body=owl_config_getmsg(m, 1); |
---|
565 | |
---|
566 | /* indent */ |
---|
567 | indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_TAB+10); |
---|
568 | owl_text_indent(indent, body, OWL_TAB); |
---|
569 | |
---|
570 | /* fmtext_append. This needs to change */ |
---|
571 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
572 | |
---|
573 | owl_free(indent); |
---|
574 | owl_free(body); |
---|
575 | } |
---|
576 | |
---|
577 | void _owl_message_make_text_from_zwriteline_standard(owl_message *m) { |
---|
578 | char *indent, *text, *zsigbuff, *foo; |
---|
579 | |
---|
580 | text=owl_message_get_body(m); |
---|
581 | |
---|
582 | indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10); |
---|
583 | owl_text_indent(indent, text, OWL_MSGTAB); |
---|
584 | owl_fmtext_init_null(&(m->fmtext)); |
---|
585 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
586 | owl_fmtext_append_normal(&(m->fmtext), "Zephyr sent to "); |
---|
587 | foo=short_zuser(owl_message_get_recipient(m)); |
---|
588 | owl_fmtext_append_normal(&(m->fmtext), foo); |
---|
589 | owl_free(foo); |
---|
590 | owl_fmtext_append_normal(&(m->fmtext), " (Zsig: "); |
---|
591 | |
---|
592 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
593 | owl_message_pretty_zsig(m, zsigbuff); |
---|
594 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
595 | owl_free(zsigbuff); |
---|
596 | |
---|
597 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
598 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
599 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
600 | if (text[strlen(text)-1]!='\n') { |
---|
601 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
602 | } |
---|
603 | |
---|
604 | owl_free(indent); |
---|
605 | } |
---|
606 | |
---|
607 | void _owl_message_make_text_from_zwriteline_simple(owl_message *m) { |
---|
608 | char *indent, *text, *zsigbuff, *foo; |
---|
609 | |
---|
610 | text=owl_message_get_body(m); |
---|
611 | |
---|
612 | indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10); |
---|
613 | owl_text_indent(indent, text, OWL_MSGTAB); |
---|
614 | owl_fmtext_init_null(&(m->fmtext)); |
---|
615 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
616 | owl_fmtext_append_normal(&(m->fmtext), "To: "); |
---|
617 | foo=short_zuser(owl_message_get_recipient(m)); |
---|
618 | owl_fmtext_append_normal(&(m->fmtext), foo); |
---|
619 | owl_free(foo); |
---|
620 | owl_fmtext_append_normal(&(m->fmtext), " (Zsig: "); |
---|
621 | |
---|
622 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
623 | owl_message_pretty_zsig(m, zsigbuff); |
---|
624 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
625 | owl_free(zsigbuff); |
---|
626 | |
---|
627 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
628 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
629 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
630 | if (text[strlen(text)-1]!='\n') { |
---|
631 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
632 | } |
---|
633 | |
---|
634 | owl_free(indent); |
---|
635 | } |
---|
636 | |
---|
637 | void _owl_message_make_text_from_notice_standard(owl_message *m) { |
---|
638 | char *body, *indent, *ptr, *zsigbuff, frombuff[LINE]; |
---|
639 | ZNotice_t *n; |
---|
640 | |
---|
641 | n=&(m->notice); |
---|
642 | |
---|
643 | /* get the body */ |
---|
644 | body=owl_malloc(strlen(owl_message_get_body(m))+30); |
---|
645 | strcpy(body, owl_message_get_body(m)); |
---|
646 | |
---|
647 | /* add a newline if we need to */ |
---|
648 | if (body[0]!='\0' && body[strlen(body)-1]!='\n') { |
---|
649 | strcat(body, "\n"); |
---|
650 | } |
---|
651 | |
---|
652 | /* do the indenting into indent */ |
---|
653 | indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10); |
---|
654 | owl_text_indent(indent, body, OWL_MSGTAB); |
---|
655 | |
---|
656 | /* edit the from addr for printing */ |
---|
657 | strcpy(frombuff, owl_message_get_sender(m)); |
---|
658 | ptr=strchr(frombuff, '@'); |
---|
659 | if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) { |
---|
660 | *ptr='\0'; |
---|
661 | } |
---|
662 | |
---|
663 | /* set the message for printing */ |
---|
664 | owl_fmtext_init_null(&(m->fmtext)); |
---|
665 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
666 | |
---|
667 | if (!strcasecmp(owl_message_get_opcode(m), "ping") && owl_message_is_private(m)) { |
---|
668 | owl_fmtext_append_bold(&(m->fmtext), "PING"); |
---|
669 | owl_fmtext_append_normal(&(m->fmtext), " from "); |
---|
670 | owl_fmtext_append_bold(&(m->fmtext), frombuff); |
---|
671 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
672 | } else if (!strcasecmp(n->z_class, "login")) { |
---|
673 | char *ptr, host[LINE], tty[LINE]; |
---|
674 | int len; |
---|
675 | |
---|
676 | ptr=owl_zephyr_get_field(n, 1, &len); |
---|
677 | strncpy(host, ptr, len); |
---|
678 | host[len]='\0'; |
---|
679 | ptr=owl_zephyr_get_field(n, 3, &len); |
---|
680 | strncpy(tty, ptr, len); |
---|
681 | tty[len]='\0'; |
---|
682 | |
---|
683 | if (!strcasecmp(n->z_opcode, "user_login")) { |
---|
684 | owl_fmtext_append_bold(&(m->fmtext), "LOGIN"); |
---|
685 | } else if (!strcasecmp(n->z_opcode, "user_logout")) { |
---|
686 | owl_fmtext_append_bold(&(m->fmtext), "LOGOUT"); |
---|
687 | } |
---|
688 | owl_fmtext_append_normal(&(m->fmtext), " for "); |
---|
689 | ptr=short_zuser(n->z_class_inst); |
---|
690 | owl_fmtext_append_bold(&(m->fmtext), ptr); |
---|
691 | owl_free(ptr); |
---|
692 | owl_fmtext_append_normal(&(m->fmtext), " at "); |
---|
693 | owl_fmtext_append_normal(&(m->fmtext), host); |
---|
694 | owl_fmtext_append_normal(&(m->fmtext), " "); |
---|
695 | owl_fmtext_append_normal(&(m->fmtext), tty); |
---|
696 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
697 | } else { |
---|
698 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m)); |
---|
699 | owl_fmtext_append_normal(&(m->fmtext), " / "); |
---|
700 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m)); |
---|
701 | owl_fmtext_append_normal(&(m->fmtext), " / "); |
---|
702 | owl_fmtext_append_bold(&(m->fmtext), frombuff); |
---|
703 | if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) { |
---|
704 | owl_fmtext_append_normal(&(m->fmtext), " {"); |
---|
705 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m)); |
---|
706 | owl_fmtext_append_normal(&(m->fmtext), "} "); |
---|
707 | } |
---|
708 | if (n->z_opcode[0]!='\0') { |
---|
709 | owl_fmtext_append_normal(&(m->fmtext), " ["); |
---|
710 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_opcode(m)); |
---|
711 | owl_fmtext_append_normal(&(m->fmtext), "] "); |
---|
712 | } |
---|
713 | |
---|
714 | /* stick on the zsig */ |
---|
715 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
716 | owl_message_pretty_zsig(m, zsigbuff); |
---|
717 | owl_fmtext_append_normal(&(m->fmtext), " ("); |
---|
718 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
719 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
720 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
721 | owl_free(zsigbuff); |
---|
722 | |
---|
723 | /* then the indented message */ |
---|
724 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
725 | |
---|
726 | /* make personal messages bold for smaat users */ |
---|
727 | if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
728 | if (owl_message_is_personal(m)) { |
---|
729 | owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD); |
---|
730 | } |
---|
731 | } |
---|
732 | } |
---|
733 | |
---|
734 | owl_free(body); |
---|
735 | owl_free(indent); |
---|
736 | } |
---|
737 | |
---|
738 | void _owl_message_make_text_from_notice_simple(owl_message *m) { |
---|
739 | char *body, *indent, *ptr, *zsigbuff, frombuff[LINE]; |
---|
740 | ZNotice_t *n; |
---|
741 | |
---|
742 | n=&(m->notice); |
---|
743 | |
---|
744 | /* get the body */ |
---|
745 | body=owl_malloc(strlen(owl_message_get_body(m)+30)); |
---|
746 | strcpy(body, owl_message_get_body(m)); |
---|
747 | |
---|
748 | /* add a newline if we need to */ |
---|
749 | if (body[0]!='\0' && body[strlen(body)-1]!='\n') { |
---|
750 | strcat(body, "\n"); |
---|
751 | } |
---|
752 | |
---|
753 | /* do the indenting into indent */ |
---|
754 | indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10); |
---|
755 | owl_text_indent(indent, body, OWL_MSGTAB); |
---|
756 | |
---|
757 | /* edit the from addr for printing */ |
---|
758 | strcpy(frombuff, owl_message_get_sender(m)); |
---|
759 | ptr=strchr(frombuff, '@'); |
---|
760 | if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) { |
---|
761 | *ptr='\0'; |
---|
762 | } |
---|
763 | |
---|
764 | /* set the message for printing */ |
---|
765 | owl_fmtext_init_null(&(m->fmtext)); |
---|
766 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
767 | |
---|
768 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) { |
---|
769 | owl_fmtext_append_bold(&(m->fmtext), "PING"); |
---|
770 | owl_fmtext_append_normal(&(m->fmtext), " from "); |
---|
771 | owl_fmtext_append_bold(&(m->fmtext), frombuff); |
---|
772 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
773 | } else if (!strcasecmp(owl_message_get_class(m), "login")) { |
---|
774 | char *ptr, host[LINE], tty[LINE]; |
---|
775 | int len; |
---|
776 | |
---|
777 | ptr=owl_zephyr_get_field(n, 1, &len); |
---|
778 | strncpy(host, ptr, len); |
---|
779 | host[len]='\0'; |
---|
780 | ptr=owl_zephyr_get_field(n, 3, &len); |
---|
781 | strncpy(tty, ptr, len); |
---|
782 | tty[len]='\0'; |
---|
783 | |
---|
784 | if (!strcasecmp(owl_message_get_opcode(m), "user_login")) { |
---|
785 | owl_fmtext_append_bold(&(m->fmtext), "LOGIN"); |
---|
786 | } else if (!strcasecmp(owl_message_get_opcode(m), "user_logout")) { |
---|
787 | owl_fmtext_append_bold(&(m->fmtext), "LOGOUT"); |
---|
788 | } |
---|
789 | owl_fmtext_append_normal(&(m->fmtext), " for "); |
---|
790 | ptr=short_zuser(owl_message_get_instance(m)); |
---|
791 | owl_fmtext_append_bold(&(m->fmtext), ptr); |
---|
792 | owl_free(ptr); |
---|
793 | owl_fmtext_append_normal(&(m->fmtext), " at "); |
---|
794 | owl_fmtext_append_normal(&(m->fmtext), host); |
---|
795 | owl_fmtext_append_normal(&(m->fmtext), " "); |
---|
796 | owl_fmtext_append_normal(&(m->fmtext), tty); |
---|
797 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
798 | } else { |
---|
799 | owl_fmtext_append_normal(&(m->fmtext), "From: "); |
---|
800 | if (strcasecmp(owl_message_get_class(m), "message")) { |
---|
801 | owl_fmtext_append_normal(&(m->fmtext), "Class "); |
---|
802 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_class(m)); |
---|
803 | owl_fmtext_append_normal(&(m->fmtext), " / Instance "); |
---|
804 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_instance(m)); |
---|
805 | owl_fmtext_append_normal(&(m->fmtext), " / "); |
---|
806 | } |
---|
807 | owl_fmtext_append_normal(&(m->fmtext), frombuff); |
---|
808 | if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) { |
---|
809 | owl_fmtext_append_normal(&(m->fmtext), " {"); |
---|
810 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m)); |
---|
811 | owl_fmtext_append_normal(&(m->fmtext), "} "); |
---|
812 | } |
---|
813 | |
---|
814 | /* stick on the zsig */ |
---|
815 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
816 | owl_message_pretty_zsig(m, zsigbuff); |
---|
817 | owl_fmtext_append_normal(&(m->fmtext), " ("); |
---|
818 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
819 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
820 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
821 | owl_free(zsigbuff); |
---|
822 | |
---|
823 | /* then the indented message */ |
---|
824 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
825 | |
---|
826 | /* make personal messages bold for smaat users */ |
---|
827 | if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
828 | if (owl_message_is_personal(m)) { |
---|
829 | owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD); |
---|
830 | } |
---|
831 | } |
---|
832 | } |
---|
833 | |
---|
834 | owl_free(body); |
---|
835 | owl_free(indent); |
---|
836 | } |
---|
837 | |
---|
838 | void owl_message_pretty_zsig(owl_message *m, char *buff) { |
---|
839 | /* stick a one line version of the zsig in buff */ |
---|
840 | char *ptr; |
---|
841 | |
---|
842 | strcpy(buff, owl_message_get_zsig(m)); |
---|
843 | ptr=strchr(buff, '\n'); |
---|
844 | if (ptr) ptr[0]='\0'; |
---|
845 | } |
---|
846 | |
---|
847 | void owl_message_free(owl_message *m) { |
---|
848 | int i, j; |
---|
849 | owl_pair *p; |
---|
850 | |
---|
851 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
852 | ZFreeNotice(&(m->notice)); |
---|
853 | } |
---|
854 | if (m->time) owl_free(m->time); |
---|
855 | if (m->zwriteline) owl_free(m->zwriteline); |
---|
856 | |
---|
857 | /* free all the attributes */ |
---|
858 | j=owl_list_get_size(&(m->attributes)); |
---|
859 | for (i=0; i<j; i++) { |
---|
860 | p=owl_list_get_element(&(m->attributes), i); |
---|
861 | owl_free(owl_pair_get_key(p)); |
---|
862 | owl_free(owl_pair_get_value(p)); |
---|
863 | owl_free(p); |
---|
864 | } |
---|
865 | |
---|
866 | owl_list_free_simple(&(m->attributes)); |
---|
867 | |
---|
868 | owl_fmtext_free(&(m->fmtext)); |
---|
869 | } |
---|