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