[7d4fbcd] | 1 | #include <zephyr/zephyr.h> |
---|
| 2 | #include <stdlib.h> |
---|
[b45293f] | 3 | #include <unistd.h> |
---|
[7d4fbcd] | 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 | |
---|
[1aee7d9] | 14 | static const char fileIdent[] = "$Id$"; |
---|
| 15 | |
---|
[4b464a4] | 16 | void owl_message_init_raw(owl_message *m) { |
---|
[7d4fbcd] | 17 | time_t t; |
---|
| 18 | |
---|
| 19 | m->id=owl_global_get_nextmsgid(&g); |
---|
[4b464a4] | 20 | m->type=OWL_MESSAGE_TYPE_GENERIC; |
---|
| 21 | owl_message_set_direction_none(m); |
---|
[7d4fbcd] | 22 | m->delete=0; |
---|
[4b464a4] | 23 | m->sender=owl_strdup(""); |
---|
[7d4fbcd] | 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(""); |
---|
[b45293f] | 29 | m->zsig=owl_strdup(""); |
---|
[7d4fbcd] | 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'; |
---|
[4b464a4] | 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 | |
---|
[b45293f] | 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 | |
---|
[4b464a4] | 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 | |
---|
[af2ca19] | 218 | owl_fmtext_init_null(&a); |
---|
| 219 | owl_fmtext_init_null(&b); |
---|
| 220 | |
---|
[4b464a4] | 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); |
---|
[a2641cd9] | 240 | if (!strcasecmp(owl_message_get_recipient(m), ZGetSender()) || |
---|
| 241 | !strcasecmp(owl_message_get_sender(m), ZGetSender())) { |
---|
[4b464a4] | 242 | return(1); |
---|
| 243 | } |
---|
| 244 | return(0); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | int owl_message_is_private(owl_message *m) { |
---|
[a2641cd9] | 248 | if (!strcasecmp(owl_message_get_recipient(m), ZGetSender())) return(1); |
---|
[4b464a4] | 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); |
---|
[7d4fbcd] | 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 | |
---|
[b45293f] | 360 | void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n) { |
---|
[7d4fbcd] | 361 | struct hostent *hent; |
---|
[a15a84f] | 362 | int k, ret; |
---|
[7e3e00a] | 363 | char *ptr, *tmp; |
---|
[7d4fbcd] | 364 | |
---|
| 365 | m->id=owl_global_get_nextmsgid(&g); |
---|
[4b464a4] | 366 | owl_message_set_type_zephyr(m); |
---|
| 367 | owl_message_set_direction_in(m); |
---|
[7d4fbcd] | 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 | } |
---|
[b45293f] | 387 | m->zsig=owl_strdup(n->z_message); |
---|
[7d4fbcd] | 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 | |
---|
[b45293f] | 397 | /* set the body */ |
---|
[7d4fbcd] | 398 | ptr=owl_zephyr_get_message(n, &k); |
---|
[7e3e00a] | 399 | tmp=owl_malloc(k+10); |
---|
| 400 | memcpy(tmp, ptr, k); |
---|
| 401 | tmp[k]='\0'; |
---|
| 402 | if (owl_global_is_newlinestrip(&g)) { |
---|
| 403 | m->body=owl_util_stripnewlines(tmp); |
---|
| 404 | owl_free(tmp); |
---|
| 405 | } else { |
---|
| 406 | m->body=tmp; |
---|
| 407 | } |
---|
[7d4fbcd] | 408 | |
---|
[d309eb3] | 409 | /* if zcrypt is enabled try to decrypt the message */ |
---|
| 410 | if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) { |
---|
| 411 | char *out; |
---|
| 412 | |
---|
| 413 | out=owl_malloc(strlen(m->body)*16+20); |
---|
[a15a84f] | 414 | ret=zcrypt_decrypt(out, m->body, m->class, m->inst); |
---|
| 415 | if (ret==0) { |
---|
| 416 | owl_free(m->body); |
---|
| 417 | m->body=out; |
---|
| 418 | } else { |
---|
| 419 | owl_free(out); |
---|
| 420 | } |
---|
[d309eb3] | 421 | } |
---|
| 422 | |
---|
[7d4fbcd] | 423 | /* save the hostname */ |
---|
[3a2daac] | 424 | owl_function_debugmsg("About to do gethostbyaddr"); |
---|
[7d4fbcd] | 425 | hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET); |
---|
| 426 | if (hent && hent->h_name) { |
---|
| 427 | strcpy(m->hostname, hent->h_name); |
---|
| 428 | } else { |
---|
| 429 | strcpy(m->hostname, inet_ntoa(n->z_sender_addr)); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /* save the time */ |
---|
| 433 | m->time=owl_strdup(ctime((time_t *) &n->z_time.tv_sec)); |
---|
| 434 | m->time[strlen(m->time)-1]='\0'; |
---|
| 435 | |
---|
| 436 | /* create the formatted message */ |
---|
| 437 | if (owl_global_is_config_format(&g)) { |
---|
| 438 | _owl_message_make_text_from_config(m); |
---|
| 439 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
| 440 | _owl_message_make_text_from_notice_standard(m); |
---|
| 441 | } else { |
---|
| 442 | _owl_message_make_text_from_notice_simple(m); |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | } |
---|
| 446 | |
---|
[b45293f] | 447 | void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig) { |
---|
| 448 | owl_zwrite z; |
---|
| 449 | int ret; |
---|
| 450 | |
---|
| 451 | owl_message_init_raw(m); |
---|
| 452 | |
---|
| 453 | /* create a zwrite for the purpose of filling in other message fields */ |
---|
| 454 | owl_zwrite_create_from_line(&z, line); |
---|
| 455 | |
---|
| 456 | /* set things */ |
---|
| 457 | owl_message_set_direction_out(m); |
---|
| 458 | owl_message_set_type_zephyr(m); |
---|
[8fec514] | 459 | owl_message_set_sender(m, ZGetSender()); |
---|
| 460 | owl_message_set_class(m, owl_zwrite_get_class(&z)); |
---|
| 461 | owl_message_set_instance(m, owl_zwrite_get_instance(&z)); |
---|
[b45293f] | 462 | m->recip=long_zuser(owl_zwrite_get_recip_n(&z, 0)); /* only gets the first user, must fix */ |
---|
[8fec514] | 463 | owl_message_set_opcode(m, owl_zwrite_get_opcode(&z)); |
---|
[b45293f] | 464 | m->realm=owl_strdup(owl_zwrite_get_realm(&z)); /* also a hack, but not here */ |
---|
| 465 | m->zwriteline=owl_strdup(line); |
---|
| 466 | m->body=owl_strdup(body); |
---|
[8fec514] | 467 | owl_message_set_zsig(m, zsig); |
---|
[b45293f] | 468 | |
---|
| 469 | /* save the hostname */ |
---|
| 470 | ret=gethostname(m->hostname, MAXHOSTNAMELEN); |
---|
| 471 | if (ret) { |
---|
| 472 | strcpy(m->hostname, "localhost"); |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | /* create the formatted message */ |
---|
| 476 | if (owl_global_is_config_format(&g)) { |
---|
| 477 | _owl_message_make_text_from_config(m); |
---|
| 478 | } else if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
| 479 | _owl_message_make_text_from_zwriteline_standard(m); |
---|
| 480 | } else { |
---|
| 481 | _owl_message_make_text_from_zwriteline_simple(m); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | owl_zwrite_free(&z); |
---|
| 485 | } |
---|
[7d4fbcd] | 486 | |
---|
| 487 | void _owl_message_make_text_from_config(owl_message *m) { |
---|
| 488 | char *body, *indent; |
---|
| 489 | |
---|
| 490 | owl_fmtext_init_null(&(m->fmtext)); |
---|
| 491 | |
---|
| 492 | /* get body from the config */ |
---|
| 493 | body=owl_config_getmsg(m, 1); |
---|
| 494 | |
---|
| 495 | /* indent */ |
---|
| 496 | indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_TAB+10); |
---|
| 497 | owl_text_indent(indent, body, OWL_TAB); |
---|
| 498 | |
---|
| 499 | /* fmtext_append. This needs to change */ |
---|
| 500 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
| 501 | |
---|
| 502 | owl_free(indent); |
---|
| 503 | owl_free(body); |
---|
| 504 | } |
---|
| 505 | |
---|
[b45293f] | 506 | void _owl_message_make_text_from_zwriteline_standard(owl_message *m) { |
---|
[be97670] | 507 | char *indent, *text, *zsigbuff, *foo; |
---|
[b45293f] | 508 | |
---|
| 509 | text=owl_message_get_body(m); |
---|
| 510 | |
---|
| 511 | indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10); |
---|
| 512 | owl_text_indent(indent, text, OWL_MSGTAB); |
---|
| 513 | owl_fmtext_init_null(&(m->fmtext)); |
---|
| 514 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
| 515 | owl_fmtext_append_normal(&(m->fmtext), "Zephyr sent to "); |
---|
[be97670] | 516 | foo=short_zuser(owl_message_get_recipient(m)); |
---|
| 517 | owl_fmtext_append_normal(&(m->fmtext), foo); |
---|
| 518 | owl_free(foo); |
---|
[b45293f] | 519 | owl_fmtext_append_normal(&(m->fmtext), " (Zsig: "); |
---|
| 520 | |
---|
[8fec514] | 521 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
[b45293f] | 522 | owl_message_pretty_zsig(m, zsigbuff); |
---|
| 523 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
| 524 | owl_free(zsigbuff); |
---|
| 525 | |
---|
| 526 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
| 527 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 528 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
| 529 | if (text[strlen(text)-1]!='\n') { |
---|
| 530 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | owl_free(indent); |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | void _owl_message_make_text_from_zwriteline_simple(owl_message *m) { |
---|
[b12c219] | 537 | char *indent, *text, *zsigbuff, *foo; |
---|
| 538 | |
---|
| 539 | text=owl_message_get_body(m); |
---|
| 540 | |
---|
| 541 | indent=owl_malloc(strlen(text)+owl_text_num_lines(text)*OWL_MSGTAB+10); |
---|
| 542 | owl_text_indent(indent, text, OWL_MSGTAB); |
---|
| 543 | owl_fmtext_init_null(&(m->fmtext)); |
---|
| 544 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
| 545 | owl_fmtext_append_normal(&(m->fmtext), "To: "); |
---|
| 546 | foo=short_zuser(owl_message_get_recipient(m)); |
---|
| 547 | owl_fmtext_append_normal(&(m->fmtext), foo); |
---|
| 548 | owl_free(foo); |
---|
| 549 | owl_fmtext_append_normal(&(m->fmtext), " (Zsig: "); |
---|
| 550 | |
---|
[a3ba748] | 551 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
[b12c219] | 552 | owl_message_pretty_zsig(m, zsigbuff); |
---|
| 553 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
| 554 | owl_free(zsigbuff); |
---|
| 555 | |
---|
| 556 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
| 557 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 558 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
| 559 | if (text[strlen(text)-1]!='\n') { |
---|
| 560 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | owl_free(indent); |
---|
[b45293f] | 564 | } |
---|
| 565 | |
---|
[7d4fbcd] | 566 | void _owl_message_make_text_from_notice_standard(owl_message *m) { |
---|
[b45293f] | 567 | char *body, *indent, *ptr, *zsigbuff, frombuff[LINE]; |
---|
[7d4fbcd] | 568 | ZNotice_t *n; |
---|
| 569 | |
---|
| 570 | n=&(m->notice); |
---|
[d309eb3] | 571 | |
---|
| 572 | /* get the body */ |
---|
| 573 | body=owl_malloc(strlen(m->body)+30); |
---|
| 574 | strcpy(body, m->body); |
---|
[7d4fbcd] | 575 | |
---|
| 576 | /* add a newline if we need to */ |
---|
| 577 | if (body[0]!='\0' && body[strlen(body)-1]!='\n') { |
---|
| 578 | strcat(body, "\n"); |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | /* do the indenting into indent */ |
---|
| 582 | indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10); |
---|
| 583 | owl_text_indent(indent, body, OWL_MSGTAB); |
---|
| 584 | |
---|
| 585 | /* edit the from addr for printing */ |
---|
| 586 | strcpy(frombuff, m->sender); |
---|
| 587 | ptr=strchr(frombuff, '@'); |
---|
| 588 | if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) { |
---|
| 589 | *ptr='\0'; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | /* set the message for printing */ |
---|
| 593 | owl_fmtext_init_null(&(m->fmtext)); |
---|
| 594 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
| 595 | |
---|
[5a35ae8e] | 596 | if (!strcasecmp(n->z_opcode, "ping") && owl_message_is_private(m)) { |
---|
[7d4fbcd] | 597 | owl_fmtext_append_bold(&(m->fmtext), "PING"); |
---|
| 598 | owl_fmtext_append_normal(&(m->fmtext), " from "); |
---|
| 599 | owl_fmtext_append_bold(&(m->fmtext), frombuff); |
---|
| 600 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 601 | } else if (!strcasecmp(n->z_class, "login")) { |
---|
| 602 | char *ptr, host[LINE], tty[LINE]; |
---|
| 603 | int len; |
---|
| 604 | |
---|
| 605 | ptr=owl_zephyr_get_field(n, 1, &len); |
---|
| 606 | strncpy(host, ptr, len); |
---|
| 607 | host[len]='\0'; |
---|
| 608 | ptr=owl_zephyr_get_field(n, 3, &len); |
---|
| 609 | strncpy(tty, ptr, len); |
---|
| 610 | tty[len]='\0'; |
---|
| 611 | |
---|
| 612 | if (!strcasecmp(n->z_opcode, "user_login")) { |
---|
| 613 | owl_fmtext_append_bold(&(m->fmtext), "LOGIN"); |
---|
| 614 | } else if (!strcasecmp(n->z_opcode, "user_logout")) { |
---|
| 615 | owl_fmtext_append_bold(&(m->fmtext), "LOGOUT"); |
---|
| 616 | } |
---|
| 617 | owl_fmtext_append_normal(&(m->fmtext), " for "); |
---|
[4b464a4] | 618 | ptr=short_zuser(n->z_class_inst); |
---|
[7d4fbcd] | 619 | owl_fmtext_append_bold(&(m->fmtext), ptr); |
---|
| 620 | owl_free(ptr); |
---|
| 621 | owl_fmtext_append_normal(&(m->fmtext), " at "); |
---|
| 622 | owl_fmtext_append_normal(&(m->fmtext), host); |
---|
| 623 | owl_fmtext_append_normal(&(m->fmtext), " "); |
---|
| 624 | owl_fmtext_append_normal(&(m->fmtext), tty); |
---|
| 625 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 626 | } else { |
---|
| 627 | owl_fmtext_append_normal(&(m->fmtext), m->class); |
---|
| 628 | owl_fmtext_append_normal(&(m->fmtext), " / "); |
---|
| 629 | owl_fmtext_append_normal(&(m->fmtext), m->inst); |
---|
| 630 | owl_fmtext_append_normal(&(m->fmtext), " / "); |
---|
| 631 | owl_fmtext_append_bold(&(m->fmtext), frombuff); |
---|
| 632 | if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) { |
---|
| 633 | owl_fmtext_append_normal(&(m->fmtext), " {"); |
---|
| 634 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m)); |
---|
| 635 | owl_fmtext_append_normal(&(m->fmtext), "} "); |
---|
| 636 | } |
---|
| 637 | if (n->z_opcode[0]!='\0') { |
---|
| 638 | owl_fmtext_append_normal(&(m->fmtext), " ["); |
---|
| 639 | owl_fmtext_append_normal(&(m->fmtext), n->z_opcode); |
---|
| 640 | owl_fmtext_append_normal(&(m->fmtext), "] "); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | /* stick on the zsig */ |
---|
[a3ba748] | 644 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
[b45293f] | 645 | owl_message_pretty_zsig(m, zsigbuff); |
---|
| 646 | owl_fmtext_append_normal(&(m->fmtext), " ("); |
---|
| 647 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
| 648 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
[7d4fbcd] | 649 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
[b45293f] | 650 | owl_free(zsigbuff); |
---|
| 651 | |
---|
| 652 | /* then the indented message */ |
---|
[7d4fbcd] | 653 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
| 654 | |
---|
| 655 | /* make personal messages bold for smaat users */ |
---|
| 656 | if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
| 657 | if (owl_message_is_personal(m)) { |
---|
| 658 | owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD); |
---|
| 659 | } |
---|
| 660 | } |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | owl_free(body); |
---|
| 664 | owl_free(indent); |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | void _owl_message_make_text_from_notice_simple(owl_message *m) { |
---|
[b45293f] | 668 | char *body, *indent, *ptr, *zsigbuff, frombuff[LINE]; |
---|
[7d4fbcd] | 669 | ZNotice_t *n; |
---|
| 670 | |
---|
| 671 | n=&(m->notice); |
---|
[d309eb3] | 672 | |
---|
| 673 | /* get the body */ |
---|
| 674 | body=owl_malloc(strlen(m->body)+30); |
---|
| 675 | strcpy(body, m->body); |
---|
[7d4fbcd] | 676 | |
---|
| 677 | /* add a newline if we need to */ |
---|
| 678 | if (body[0]!='\0' && body[strlen(body)-1]!='\n') { |
---|
| 679 | strcat(body, "\n"); |
---|
| 680 | } |
---|
| 681 | |
---|
| 682 | /* do the indenting into indent */ |
---|
| 683 | indent=owl_malloc(strlen(body)+owl_text_num_lines(body)*OWL_MSGTAB+10); |
---|
| 684 | owl_text_indent(indent, body, OWL_MSGTAB); |
---|
| 685 | |
---|
| 686 | /* edit the from addr for printing */ |
---|
| 687 | strcpy(frombuff, m->sender); |
---|
| 688 | ptr=strchr(frombuff, '@'); |
---|
| 689 | if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) { |
---|
| 690 | *ptr='\0'; |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | /* set the message for printing */ |
---|
| 694 | owl_fmtext_init_null(&(m->fmtext)); |
---|
| 695 | owl_fmtext_append_normal(&(m->fmtext), OWL_TABSTR); |
---|
| 696 | |
---|
| 697 | if (!strcasecmp(n->z_opcode, "ping")) { |
---|
| 698 | owl_fmtext_append_bold(&(m->fmtext), "PING"); |
---|
| 699 | owl_fmtext_append_normal(&(m->fmtext), " from "); |
---|
| 700 | owl_fmtext_append_bold(&(m->fmtext), frombuff); |
---|
| 701 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 702 | } else if (!strcasecmp(n->z_class, "login")) { |
---|
| 703 | char *ptr, host[LINE], tty[LINE]; |
---|
| 704 | int len; |
---|
| 705 | |
---|
| 706 | ptr=owl_zephyr_get_field(n, 1, &len); |
---|
| 707 | strncpy(host, ptr, len); |
---|
| 708 | host[len]='\0'; |
---|
| 709 | ptr=owl_zephyr_get_field(n, 3, &len); |
---|
| 710 | strncpy(tty, ptr, len); |
---|
| 711 | tty[len]='\0'; |
---|
| 712 | |
---|
| 713 | if (!strcasecmp(n->z_opcode, "user_login")) { |
---|
| 714 | owl_fmtext_append_bold(&(m->fmtext), "LOGIN"); |
---|
| 715 | } else if (!strcasecmp(n->z_opcode, "user_logout")) { |
---|
| 716 | owl_fmtext_append_bold(&(m->fmtext), "LOGOUT"); |
---|
| 717 | } |
---|
| 718 | owl_fmtext_append_normal(&(m->fmtext), " for "); |
---|
[4b464a4] | 719 | ptr=short_zuser(n->z_class_inst); |
---|
[7d4fbcd] | 720 | owl_fmtext_append_bold(&(m->fmtext), ptr); |
---|
| 721 | owl_free(ptr); |
---|
| 722 | owl_fmtext_append_normal(&(m->fmtext), " at "); |
---|
| 723 | owl_fmtext_append_normal(&(m->fmtext), host); |
---|
| 724 | owl_fmtext_append_normal(&(m->fmtext), " "); |
---|
| 725 | owl_fmtext_append_normal(&(m->fmtext), tty); |
---|
| 726 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
| 727 | } else { |
---|
| 728 | owl_fmtext_append_normal(&(m->fmtext), "From: "); |
---|
| 729 | if (strcasecmp(m->class, "message")) { |
---|
| 730 | owl_fmtext_append_normal(&(m->fmtext), "Class "); |
---|
| 731 | owl_fmtext_append_normal(&(m->fmtext), m->class); |
---|
| 732 | owl_fmtext_append_normal(&(m->fmtext), " / Instance "); |
---|
| 733 | owl_fmtext_append_normal(&(m->fmtext), m->inst); |
---|
| 734 | owl_fmtext_append_normal(&(m->fmtext), " / "); |
---|
| 735 | } |
---|
| 736 | owl_fmtext_append_normal(&(m->fmtext), frombuff); |
---|
| 737 | if (strcasecmp(owl_message_get_realm(m), ZGetRealm())) { |
---|
| 738 | owl_fmtext_append_normal(&(m->fmtext), " {"); |
---|
| 739 | owl_fmtext_append_normal(&(m->fmtext), owl_message_get_realm(m)); |
---|
| 740 | owl_fmtext_append_normal(&(m->fmtext), "} "); |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | /* stick on the zsig */ |
---|
[a3ba748] | 744 | zsigbuff=owl_malloc(strlen(owl_message_get_zsig(m))+30); |
---|
[b45293f] | 745 | owl_message_pretty_zsig(m, zsigbuff); |
---|
| 746 | owl_fmtext_append_normal(&(m->fmtext), " ("); |
---|
| 747 | owl_fmtext_append_ztext(&(m->fmtext), zsigbuff); |
---|
| 748 | owl_fmtext_append_normal(&(m->fmtext), ")"); |
---|
[7d4fbcd] | 749 | owl_fmtext_append_normal(&(m->fmtext), "\n"); |
---|
[b45293f] | 750 | owl_free(zsigbuff); |
---|
| 751 | |
---|
| 752 | /* then the indented message */ |
---|
[7d4fbcd] | 753 | owl_fmtext_append_ztext(&(m->fmtext), indent); |
---|
| 754 | |
---|
| 755 | /* make personal messages bold for smaat users */ |
---|
| 756 | if (owl_global_is_userclue(&g, OWL_USERCLUE_CLASSES)) { |
---|
| 757 | if (owl_message_is_personal(m)) { |
---|
| 758 | owl_fmtext_addattr((&m->fmtext), OWL_FMTEXT_ATTR_BOLD); |
---|
| 759 | } |
---|
| 760 | } |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | owl_free(body); |
---|
| 764 | owl_free(indent); |
---|
| 765 | } |
---|
| 766 | |
---|
[b45293f] | 767 | void owl_message_pretty_zsig(owl_message *m, char *buff) { |
---|
| 768 | /* stick a one line version of the zsig in buff */ |
---|
[7d4fbcd] | 769 | char *ptr; |
---|
| 770 | |
---|
[b45293f] | 771 | strcpy(buff, m->zsig); |
---|
| 772 | ptr=strchr(buff, '\n'); |
---|
| 773 | if (ptr) ptr[0]='\0'; |
---|
[7d4fbcd] | 774 | } |
---|
| 775 | |
---|
| 776 | void owl_message_free(owl_message *m) { |
---|
[4b464a4] | 777 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
[7d4fbcd] | 778 | ZFreeNotice(&(m->notice)); |
---|
| 779 | } |
---|
| 780 | if (m->sender) owl_free(m->sender); |
---|
| 781 | if (m->recip) owl_free(m->recip); |
---|
| 782 | if (m->class) owl_free(m->class); |
---|
| 783 | if (m->inst) owl_free(m->inst); |
---|
| 784 | if (m->opcode) owl_free(m->opcode); |
---|
| 785 | if (m->time) owl_free(m->time); |
---|
| 786 | if (m->realm) owl_free(m->realm); |
---|
| 787 | if (m->body) owl_free(m->body); |
---|
| 788 | if (m->zwriteline) owl_free(m->zwriteline); |
---|
| 789 | |
---|
| 790 | owl_fmtext_free(&(m->fmtext)); |
---|
| 791 | } |
---|
| 792 | |
---|