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