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