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