[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 | |
---|
[a387d12e] | 15 | static owl_fmtext_cache fmtext_cache[OWL_FMTEXT_CACHE_SIZE]; |
---|
| 16 | static owl_fmtext_cache * fmtext_cache_next = fmtext_cache; |
---|
| 17 | |
---|
| 18 | void owl_message_init_fmtext_cache () |
---|
| 19 | { |
---|
| 20 | int i; |
---|
| 21 | for(i = 0; i < OWL_FMTEXT_CACHE_SIZE; i++) { |
---|
| 22 | owl_fmtext_init_null(&(fmtext_cache[i].fmtext)); |
---|
| 23 | fmtext_cache[i].message = NULL; |
---|
| 24 | } |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | owl_fmtext_cache * owl_message_next_fmtext() /*noproto*/ |
---|
| 28 | { |
---|
[ad15610] | 29 | owl_fmtext_cache * f = fmtext_cache_next; |
---|
[a387d12e] | 30 | if(fmtext_cache_next->message != NULL) { |
---|
| 31 | owl_message_invalidate_format(fmtext_cache_next->message); |
---|
| 32 | } |
---|
| 33 | fmtext_cache_next++; |
---|
| 34 | if(fmtext_cache_next - fmtext_cache == OWL_FMTEXT_CACHE_SIZE) |
---|
| 35 | fmtext_cache_next = fmtext_cache; |
---|
| 36 | return f; |
---|
| 37 | } |
---|
| 38 | |
---|
[0ff8fb57] | 39 | void owl_message_init(owl_message *m) |
---|
| 40 | { |
---|
[7d4fbcd] | 41 | m->id=owl_global_get_nextmsgid(&g); |
---|
[4b464a4] | 42 | owl_message_set_direction_none(m); |
---|
[7d4fbcd] | 43 | m->delete=0; |
---|
[95caa16] | 44 | m->zwriteline=NULL; |
---|
[7d4fbcd] | 45 | |
---|
[2d1feac] | 46 | owl_message_set_hostname(m, ""); |
---|
[d0d65df] | 47 | owl_list_create(&(m->attributes)); |
---|
| 48 | |
---|
[7d4fbcd] | 49 | /* save the time */ |
---|
[25dd31a] | 50 | m->time=time(NULL); |
---|
| 51 | m->timestr=owl_strdup(ctime(&(m->time))); |
---|
| 52 | m->timestr[strlen(m->timestr)-1]='\0'; |
---|
| 53 | |
---|
[a387d12e] | 54 | m->fmtext = NULL; |
---|
[4b464a4] | 55 | } |
---|
| 56 | |
---|
[5a95b69] | 57 | /* add the named attribute to the message. If an attribute with the |
---|
| 58 | * name already exists, replace the old value with the new value |
---|
| 59 | */ |
---|
[0ff8fb57] | 60 | void owl_message_set_attribute(owl_message *m, char *attrname, char *attrvalue) |
---|
| 61 | { |
---|
[d0d65df] | 62 | int i, j; |
---|
[e849734] | 63 | owl_pair *p = NULL, *pair = NULL; |
---|
[d0d65df] | 64 | |
---|
[e849734] | 65 | /* look for an existing pair with this key, */ |
---|
[d0d65df] | 66 | j=owl_list_get_size(&(m->attributes)); |
---|
| 67 | for (i=0; i<j; i++) { |
---|
| 68 | p=owl_list_get_element(&(m->attributes), i); |
---|
| 69 | if (!strcmp(owl_pair_get_key(p), attrname)) { |
---|
| 70 | owl_free(owl_pair_get_value(p)); |
---|
[e849734] | 71 | pair = p; |
---|
[d0d65df] | 72 | break; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
[e849734] | 76 | if(pair == NULL) { |
---|
| 77 | pair = owl_malloc(sizeof(owl_pair)); |
---|
| 78 | owl_pair_create(pair, owl_global_intern(&g, attrname), NULL); |
---|
| 79 | owl_list_append_element(&(m->attributes), pair); |
---|
| 80 | } |
---|
| 81 | owl_pair_set_value(pair, owl_strdup(attrvalue)); |
---|
[d0d65df] | 82 | } |
---|
| 83 | |
---|
[5a95b69] | 84 | /* return the value associated with the named attribute, or NULL if |
---|
| 85 | * the attribute does not exist |
---|
| 86 | */ |
---|
[0ff8fb57] | 87 | char *owl_message_get_attribute_value(owl_message *m, char *attrname) |
---|
| 88 | { |
---|
[d0d65df] | 89 | int i, j; |
---|
| 90 | owl_pair *p; |
---|
| 91 | |
---|
| 92 | j=owl_list_get_size(&(m->attributes)); |
---|
| 93 | for (i=0; i<j; i++) { |
---|
| 94 | p=owl_list_get_element(&(m->attributes), i); |
---|
| 95 | if (!strcmp(owl_pair_get_key(p), attrname)) { |
---|
| 96 | return(owl_pair_get_value(p)); |
---|
| 97 | } |
---|
| 98 | } |
---|
[f4d0975] | 99 | |
---|
| 100 | /* |
---|
| 101 | owl_function_debugmsg("No attribute %s found for message %i", |
---|
| 102 | attrname, |
---|
| 103 | owl_message_get_id(m)); |
---|
| 104 | */ |
---|
[d0d65df] | 105 | return(NULL); |
---|
| 106 | } |
---|
| 107 | |
---|
[5789230] | 108 | /* We cheat and indent it for now, since we really want this for |
---|
| 109 | * the 'info' function. Later there should just be a generic |
---|
| 110 | * function to indent fmtext. |
---|
| 111 | */ |
---|
| 112 | void owl_message_attributes_tofmtext(owl_message *m, owl_fmtext *fm) { |
---|
| 113 | int i, j; |
---|
| 114 | owl_pair *p; |
---|
| 115 | char *buff; |
---|
| 116 | |
---|
| 117 | owl_fmtext_init_null(fm); |
---|
| 118 | |
---|
| 119 | j=owl_list_get_size(&(m->attributes)); |
---|
| 120 | for (i=0; i<j; i++) { |
---|
| 121 | p=owl_list_get_element(&(m->attributes), i); |
---|
| 122 | buff=owl_sprintf(" %-15.15s: %-35.35s\n", owl_pair_get_key(p), owl_pair_get_value(p)); |
---|
| 123 | owl_fmtext_append_normal(fm, buff); |
---|
| 124 | owl_free(buff); |
---|
| 125 | } |
---|
| 126 | } |
---|
[4b464a4] | 127 | |
---|
[bd3f232] | 128 | void owl_message_invalidate_format(owl_message *m) |
---|
| 129 | { |
---|
[a387d12e] | 130 | if(m->fmtext) { |
---|
| 131 | m->fmtext->message = NULL; |
---|
| 132 | owl_fmtext_clear(&(m->fmtext->fmtext)); |
---|
| 133 | m->fmtext=NULL; |
---|
| 134 | } |
---|
[bd3f232] | 135 | } |
---|
| 136 | |
---|
[0ff8fb57] | 137 | owl_fmtext *owl_message_get_fmtext(owl_message *m) |
---|
| 138 | { |
---|
[f14a7ee] | 139 | owl_message_format(m); |
---|
[a387d12e] | 140 | return(&(m->fmtext->fmtext)); |
---|
[f14a7ee] | 141 | } |
---|
| 142 | |
---|
| 143 | void owl_message_format(owl_message *m) |
---|
| 144 | { |
---|
[bd3f232] | 145 | owl_style *s; |
---|
[ef56a67] | 146 | owl_view *v; |
---|
[bd3f232] | 147 | |
---|
[a387d12e] | 148 | if (!m->fmtext) { |
---|
| 149 | m->fmtext = owl_message_next_fmtext(); |
---|
| 150 | m->fmtext->message = m; |
---|
[421c8ef7] | 151 | /* for now we assume there's just the one view and use that style */ |
---|
[ef56a67] | 152 | v=owl_global_get_current_view(&g); |
---|
| 153 | s=owl_view_get_style(v); |
---|
| 154 | |
---|
[a387d12e] | 155 | owl_style_get_formattext(s, &(m->fmtext->fmtext), m); |
---|
[bd3f232] | 156 | } |
---|
[4b464a4] | 157 | } |
---|
| 158 | |
---|
[0ff8fb57] | 159 | void owl_message_set_class(owl_message *m, char *class) |
---|
| 160 | { |
---|
[d0d65df] | 161 | owl_message_set_attribute(m, "class", class); |
---|
[4b464a4] | 162 | } |
---|
| 163 | |
---|
[0ff8fb57] | 164 | char *owl_message_get_class(owl_message *m) |
---|
| 165 | { |
---|
[d0d65df] | 166 | char *class; |
---|
| 167 | |
---|
| 168 | class=owl_message_get_attribute_value(m, "class"); |
---|
| 169 | if (!class) return(""); |
---|
| 170 | return(class); |
---|
[4b464a4] | 171 | } |
---|
| 172 | |
---|
[0ff8fb57] | 173 | void owl_message_set_instance(owl_message *m, char *inst) |
---|
| 174 | { |
---|
[d0d65df] | 175 | owl_message_set_attribute(m, "instance", inst); |
---|
[4b464a4] | 176 | } |
---|
| 177 | |
---|
[0ff8fb57] | 178 | char *owl_message_get_instance(owl_message *m) |
---|
| 179 | { |
---|
[d0d65df] | 180 | char *instance; |
---|
| 181 | |
---|
| 182 | instance=owl_message_get_attribute_value(m, "instance"); |
---|
| 183 | if (!instance) return(""); |
---|
| 184 | return(instance); |
---|
[4b464a4] | 185 | } |
---|
| 186 | |
---|
[0ff8fb57] | 187 | void owl_message_set_sender(owl_message *m, char *sender) |
---|
| 188 | { |
---|
[d0d65df] | 189 | owl_message_set_attribute(m, "sender", sender); |
---|
[4b464a4] | 190 | } |
---|
| 191 | |
---|
[0ff8fb57] | 192 | char *owl_message_get_sender(owl_message *m) |
---|
| 193 | { |
---|
[d0d65df] | 194 | char *sender; |
---|
| 195 | |
---|
| 196 | sender=owl_message_get_attribute_value(m, "sender"); |
---|
| 197 | if (!sender) return(""); |
---|
| 198 | return(sender); |
---|
[4b464a4] | 199 | } |
---|
| 200 | |
---|
[0ff8fb57] | 201 | void owl_message_set_zsig(owl_message *m, char *zsig) |
---|
| 202 | { |
---|
[d0d65df] | 203 | owl_message_set_attribute(m, "zsig", zsig); |
---|
[b45293f] | 204 | } |
---|
| 205 | |
---|
[0ff8fb57] | 206 | char *owl_message_get_zsig(owl_message *m) |
---|
| 207 | { |
---|
[d0d65df] | 208 | char *zsig; |
---|
| 209 | |
---|
| 210 | zsig=owl_message_get_attribute_value(m, "zsig"); |
---|
| 211 | if (!zsig) return(""); |
---|
| 212 | return(zsig); |
---|
[b45293f] | 213 | } |
---|
| 214 | |
---|
[0ff8fb57] | 215 | void owl_message_set_recipient(owl_message *m, char *recip) |
---|
| 216 | { |
---|
[d0d65df] | 217 | owl_message_set_attribute(m, "recipient", recip); |
---|
[4b464a4] | 218 | } |
---|
| 219 | |
---|
[0ff8fb57] | 220 | char *owl_message_get_recipient(owl_message *m) |
---|
| 221 | { |
---|
[4b464a4] | 222 | /* this is stupid for outgoing messages, we need to fix it. */ |
---|
[d0d65df] | 223 | |
---|
| 224 | char *recip; |
---|
[0ff8fb57] | 225 | |
---|
| 226 | recip=owl_message_get_attribute_value(m, "recipient"); |
---|
[d0d65df] | 227 | if (!recip) return(""); |
---|
| 228 | return(recip); |
---|
[4b464a4] | 229 | } |
---|
| 230 | |
---|
[0ff8fb57] | 231 | void owl_message_set_realm(owl_message *m, char *realm) |
---|
| 232 | { |
---|
[d0d65df] | 233 | owl_message_set_attribute(m, "realm", realm); |
---|
[4b464a4] | 234 | } |
---|
| 235 | |
---|
[0ff8fb57] | 236 | char *owl_message_get_realm(owl_message *m) |
---|
| 237 | { |
---|
[d0d65df] | 238 | char *realm; |
---|
| 239 | |
---|
| 240 | realm=owl_message_get_attribute_value(m, "realm"); |
---|
| 241 | if (!realm) return(""); |
---|
| 242 | return(realm); |
---|
| 243 | } |
---|
| 244 | |
---|
[0ff8fb57] | 245 | void owl_message_set_body(owl_message *m, char *body) |
---|
| 246 | { |
---|
[d0d65df] | 247 | owl_message_set_attribute(m, "body", body); |
---|
| 248 | } |
---|
| 249 | |
---|
[0ff8fb57] | 250 | char *owl_message_get_body(owl_message *m) |
---|
| 251 | { |
---|
[d0d65df] | 252 | char *body; |
---|
| 253 | |
---|
| 254 | body=owl_message_get_attribute_value(m, "body"); |
---|
| 255 | if (!body) return(""); |
---|
| 256 | return(body); |
---|
[4b464a4] | 257 | } |
---|
| 258 | |
---|
[d0d65df] | 259 | |
---|
[0ff8fb57] | 260 | void owl_message_set_opcode(owl_message *m, char *opcode) |
---|
| 261 | { |
---|
[d0d65df] | 262 | owl_message_set_attribute(m, "opcode", opcode); |
---|
[4b464a4] | 263 | } |
---|
| 264 | |
---|
[0ff8fb57] | 265 | char *owl_message_get_opcode(owl_message *m) |
---|
| 266 | { |
---|
[d0d65df] | 267 | char *opcode; |
---|
| 268 | |
---|
| 269 | opcode=owl_message_get_attribute_value(m, "opcode"); |
---|
| 270 | if (!opcode) return(""); |
---|
| 271 | return(opcode); |
---|
[4b464a4] | 272 | } |
---|
| 273 | |
---|
[5789230] | 274 | |
---|
[d559df9] | 275 | void owl_message_set_islogin(owl_message *m) |
---|
[5789230] | 276 | { |
---|
[d559df9] | 277 | owl_message_set_attribute(m, "loginout", "login"); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | void owl_message_set_islogout(owl_message *m) |
---|
| 282 | { |
---|
| 283 | owl_message_set_attribute(m, "loginout", "logout"); |
---|
[5789230] | 284 | } |
---|
| 285 | |
---|
| 286 | int owl_message_is_loginout(owl_message *m) |
---|
| 287 | { |
---|
| 288 | char *res; |
---|
| 289 | |
---|
[d559df9] | 290 | res=owl_message_get_attribute_value(m, "loginout"); |
---|
[5789230] | 291 | if (!res) return(0); |
---|
| 292 | return(1); |
---|
| 293 | } |
---|
| 294 | |
---|
[d559df9] | 295 | int owl_message_is_login(owl_message *m) |
---|
| 296 | { |
---|
| 297 | char *res; |
---|
| 298 | |
---|
| 299 | res=owl_message_get_attribute_value(m, "loginout"); |
---|
| 300 | if (!res) return(0); |
---|
| 301 | if (!strcmp(res, "login")) return(1); |
---|
| 302 | return(0); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | |
---|
| 306 | int owl_message_is_logout(owl_message *m) |
---|
| 307 | { |
---|
| 308 | char *res; |
---|
| 309 | |
---|
| 310 | res=owl_message_get_attribute_value(m, "loginout"); |
---|
| 311 | if (!res) return(0); |
---|
| 312 | if (!strcmp(res, "logout")) return(1); |
---|
| 313 | return(0); |
---|
| 314 | } |
---|
| 315 | |
---|
[5789230] | 316 | void owl_message_set_isprivate(owl_message *m) |
---|
| 317 | { |
---|
[312675c] | 318 | owl_message_set_attribute(m, "isprivate", "true"); |
---|
[5789230] | 319 | } |
---|
| 320 | |
---|
| 321 | int owl_message_is_private(owl_message *m) |
---|
| 322 | { |
---|
| 323 | char *res; |
---|
| 324 | |
---|
| 325 | res=owl_message_get_attribute_value(m, "isprivate"); |
---|
| 326 | if (!res) return(0); |
---|
| 327 | return(1); |
---|
| 328 | } |
---|
| 329 | |
---|
[0ff8fb57] | 330 | char *owl_message_get_timestr(owl_message *m) |
---|
| 331 | { |
---|
[25dd31a] | 332 | if (m->timestr) return(m->timestr); |
---|
| 333 | return(""); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | /* caller must free the return */ |
---|
| 337 | char *owl_message_get_shorttimestr(owl_message *m) |
---|
| 338 | { |
---|
| 339 | struct tm *tmstruct; |
---|
| 340 | char *out; |
---|
| 341 | |
---|
| 342 | tmstruct=localtime(&(m->time)); |
---|
| 343 | out=owl_sprintf("%2.2i:%2.2i", tmstruct->tm_hour, tmstruct->tm_min); |
---|
| 344 | if (out) return(out); |
---|
| 345 | return("??:??"); |
---|
[4b464a4] | 346 | } |
---|
| 347 | |
---|
[0ff8fb57] | 348 | void owl_message_set_type_admin(owl_message *m) |
---|
| 349 | { |
---|
[30678ae] | 350 | owl_message_set_attribute(m, "type", "admin"); |
---|
[4b464a4] | 351 | } |
---|
| 352 | |
---|
[37eab7f] | 353 | void owl_message_set_type_loopback(owl_message *m) |
---|
| 354 | { |
---|
[30678ae] | 355 | owl_message_set_attribute(m, "type", "loopback"); |
---|
[37eab7f] | 356 | } |
---|
| 357 | |
---|
[0ff8fb57] | 358 | void owl_message_set_type_zephyr(owl_message *m) |
---|
| 359 | { |
---|
[30678ae] | 360 | owl_message_set_attribute(m, "type", "zephyr"); |
---|
[4b464a4] | 361 | } |
---|
[d09e5a1] | 362 | |
---|
[0ff8fb57] | 363 | void owl_message_set_type_aim(owl_message *m) |
---|
| 364 | { |
---|
[e363375] | 365 | owl_message_set_attribute(m, "type", "AIM"); |
---|
[d09e5a1] | 366 | } |
---|
[dd16bdd] | 367 | |
---|
[30678ae] | 368 | void owl_message_set_type(owl_message *m, char* type) |
---|
[dd16bdd] | 369 | { |
---|
[30678ae] | 370 | owl_message_set_attribute(m, "type", type); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | int owl_message_is_type(owl_message *m, char *type) { |
---|
| 374 | char * t = owl_message_get_attribute_value(m, "type"); |
---|
| 375 | if(!t) return 0; |
---|
[e363375] | 376 | return !strcasecmp(t, type); |
---|
[dd16bdd] | 377 | } |
---|
[4b464a4] | 378 | |
---|
[0ff8fb57] | 379 | int owl_message_is_type_admin(owl_message *m) |
---|
| 380 | { |
---|
[30678ae] | 381 | return owl_message_is_type(m, "admin"); |
---|
[4b464a4] | 382 | } |
---|
| 383 | |
---|
[421c8ef7] | 384 | int owl_message_is_type_generic(owl_message *m) |
---|
[37eab7f] | 385 | { |
---|
[30678ae] | 386 | char * t = owl_message_get_attribute_value(m, "type"); |
---|
| 387 | return (t == NULL); |
---|
[37eab7f] | 388 | } |
---|
| 389 | |
---|
[0ff8fb57] | 390 | int owl_message_is_type_zephyr(owl_message *m) |
---|
| 391 | { |
---|
[30678ae] | 392 | return owl_message_is_type(m, "zephyr"); |
---|
[4b464a4] | 393 | } |
---|
| 394 | |
---|
[0ff8fb57] | 395 | int owl_message_is_type_aim(owl_message *m) |
---|
| 396 | { |
---|
[30678ae] | 397 | return owl_message_is_type(m, "aim"); |
---|
[d09e5a1] | 398 | } |
---|
| 399 | |
---|
[30678ae] | 400 | /* XXX TODO: deprecate this */ |
---|
[421c8ef7] | 401 | int owl_message_is_type_jabber(owl_message *m) |
---|
[5a95b69] | 402 | { |
---|
[30678ae] | 403 | return owl_message_is_type(m, "jabber"); |
---|
[421c8ef7] | 404 | } |
---|
| 405 | |
---|
| 406 | int owl_message_is_type_loopback(owl_message *m) |
---|
| 407 | { |
---|
[30678ae] | 408 | return owl_message_is_type(m, "loopback"); |
---|
[421c8ef7] | 409 | } |
---|
| 410 | |
---|
| 411 | int owl_message_is_pseudo(owl_message *m) |
---|
| 412 | { |
---|
| 413 | if (owl_message_get_attribute_value(m, "pseudo")) return(1); |
---|
[4b464a4] | 414 | return(0); |
---|
| 415 | } |
---|
| 416 | |
---|
[0ff8fb57] | 417 | char *owl_message_get_text(owl_message *m) |
---|
| 418 | { |
---|
[21f0a9d] | 419 | owl_message_format(m); |
---|
[a387d12e] | 420 | return(owl_fmtext_get_text(&(m->fmtext->fmtext))); |
---|
[4b464a4] | 421 | } |
---|
| 422 | |
---|
[0ff8fb57] | 423 | void owl_message_set_direction_in(owl_message *m) |
---|
| 424 | { |
---|
[4b464a4] | 425 | m->direction=OWL_MESSAGE_DIRECTION_IN; |
---|
| 426 | } |
---|
| 427 | |
---|
[0ff8fb57] | 428 | void owl_message_set_direction_out(owl_message *m) |
---|
| 429 | { |
---|
[4b464a4] | 430 | m->direction=OWL_MESSAGE_DIRECTION_OUT; |
---|
| 431 | } |
---|
| 432 | |
---|
[0ff8fb57] | 433 | void owl_message_set_direction_none(owl_message *m) |
---|
| 434 | { |
---|
[4b464a4] | 435 | m->direction=OWL_MESSAGE_DIRECTION_NONE; |
---|
| 436 | } |
---|
| 437 | |
---|
[dd16bdd] | 438 | void owl_message_set_direction(owl_message *m, int direction) |
---|
| 439 | { |
---|
| 440 | m->direction=direction; |
---|
| 441 | } |
---|
| 442 | |
---|
[0ff8fb57] | 443 | int owl_message_is_direction_in(owl_message *m) |
---|
| 444 | { |
---|
[4b464a4] | 445 | if (m->direction==OWL_MESSAGE_DIRECTION_IN) return(1); |
---|
| 446 | return(0); |
---|
| 447 | } |
---|
| 448 | |
---|
[0ff8fb57] | 449 | int owl_message_is_direction_out(owl_message *m) |
---|
| 450 | { |
---|
[4b464a4] | 451 | if (m->direction==OWL_MESSAGE_DIRECTION_OUT) return(1); |
---|
| 452 | return(0); |
---|
| 453 | } |
---|
| 454 | |
---|
[0ff8fb57] | 455 | int owl_message_is_direction_none(owl_message *m) |
---|
| 456 | { |
---|
[4b464a4] | 457 | if (m->direction==OWL_MESSAGE_DIRECTION_NONE) return(1); |
---|
| 458 | return(0); |
---|
| 459 | } |
---|
| 460 | |
---|
[0ff8fb57] | 461 | int owl_message_get_numlines(owl_message *m) |
---|
| 462 | { |
---|
[4b464a4] | 463 | if (m == NULL) return(0); |
---|
[f14a7ee] | 464 | owl_message_format(m); |
---|
[a387d12e] | 465 | return(owl_fmtext_num_lines(&(m->fmtext->fmtext))); |
---|
[4b464a4] | 466 | } |
---|
| 467 | |
---|
[0ff8fb57] | 468 | void owl_message_mark_delete(owl_message *m) |
---|
| 469 | { |
---|
[4b464a4] | 470 | if (m == NULL) return; |
---|
| 471 | m->delete=1; |
---|
| 472 | } |
---|
| 473 | |
---|
[0ff8fb57] | 474 | void owl_message_unmark_delete(owl_message *m) |
---|
| 475 | { |
---|
[4b464a4] | 476 | if (m == NULL) return; |
---|
| 477 | m->delete=0; |
---|
| 478 | } |
---|
| 479 | |
---|
[0ff8fb57] | 480 | char *owl_message_get_zwriteline(owl_message *m) |
---|
| 481 | { |
---|
[95caa16] | 482 | if(!m->zwriteline) |
---|
| 483 | return ""; |
---|
[4b464a4] | 484 | return(m->zwriteline); |
---|
| 485 | } |
---|
| 486 | |
---|
[0ff8fb57] | 487 | void owl_message_set_zwriteline(owl_message *m, char *line) |
---|
| 488 | { |
---|
[95caa16] | 489 | if(m->zwriteline) owl_free(m->zwriteline); |
---|
[4b464a4] | 490 | m->zwriteline=strdup(line); |
---|
| 491 | } |
---|
| 492 | |
---|
[0ff8fb57] | 493 | int owl_message_is_delete(owl_message *m) |
---|
| 494 | { |
---|
[4b464a4] | 495 | if (m == NULL) return(0); |
---|
| 496 | if (m->delete==1) return(1); |
---|
| 497 | return(0); |
---|
| 498 | } |
---|
| 499 | |
---|
[be0a79f] | 500 | #ifdef HAVE_LIBZEPHYR |
---|
[0ff8fb57] | 501 | ZNotice_t *owl_message_get_notice(owl_message *m) |
---|
| 502 | { |
---|
[4b464a4] | 503 | return(&(m->notice)); |
---|
| 504 | } |
---|
[09489b89] | 505 | #else |
---|
| 506 | void *owl_message_get_notice(owl_message *m) |
---|
| 507 | { |
---|
| 508 | return(NULL); |
---|
| 509 | } |
---|
[be0a79f] | 510 | #endif |
---|
[4b464a4] | 511 | |
---|
[8298425] | 512 | void owl_message_set_hostname(owl_message *m, char *hostname) |
---|
| 513 | { |
---|
[e849734] | 514 | m->hostname=owl_global_intern(&g, hostname); |
---|
[8298425] | 515 | } |
---|
| 516 | |
---|
[0ff8fb57] | 517 | char *owl_message_get_hostname(owl_message *m) |
---|
| 518 | { |
---|
[4b464a4] | 519 | return(m->hostname); |
---|
| 520 | } |
---|
| 521 | |
---|
[8fa9562] | 522 | void owl_message_curs_waddstr(owl_message *m, WINDOW *win, int aline, int bline, int acol, int bcol, int fgcolor, int bgcolor) |
---|
[0ff8fb57] | 523 | { |
---|
[4b464a4] | 524 | owl_fmtext a, b; |
---|
| 525 | |
---|
[bd3f232] | 526 | /* this will ensure that our cached copy is up to date */ |
---|
[f14a7ee] | 527 | owl_message_format(m); |
---|
[bd3f232] | 528 | |
---|
[af2ca19] | 529 | owl_fmtext_init_null(&a); |
---|
| 530 | owl_fmtext_init_null(&b); |
---|
| 531 | |
---|
[a387d12e] | 532 | owl_fmtext_truncate_lines(&(m->fmtext->fmtext), aline, bline-aline+1, &a); |
---|
[4b464a4] | 533 | owl_fmtext_truncate_cols(&a, acol, bcol, &b); |
---|
[8fa9562] | 534 | if (fgcolor!=OWL_COLOR_DEFAULT) { |
---|
| 535 | owl_fmtext_colorize(&b, fgcolor); |
---|
| 536 | } |
---|
| 537 | if (bgcolor!=OWL_COLOR_DEFAULT) { |
---|
| 538 | owl_fmtext_colorizebg(&b, bgcolor); |
---|
[4b464a4] | 539 | } |
---|
| 540 | |
---|
| 541 | if (owl_global_is_search_active(&g)) { |
---|
| 542 | owl_fmtext_search_and_highlight(&b, owl_global_get_search_string(&g)); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | owl_fmtext_curs_waddstr(&b, win); |
---|
| 546 | |
---|
| 547 | owl_fmtext_free(&a); |
---|
| 548 | owl_fmtext_free(&b); |
---|
| 549 | } |
---|
| 550 | |
---|
[0ff8fb57] | 551 | int owl_message_is_personal(owl_message *m) |
---|
| 552 | { |
---|
[ce74deb] | 553 | owl_filter * f = owl_global_get_filter(&g, "personal"); |
---|
| 554 | if(!f) { |
---|
| 555 | owl_function_error("personal filter is not defined"); |
---|
| 556 | return (0); |
---|
[0ff8fb57] | 557 | } |
---|
[ce74deb] | 558 | return owl_filter_message_match(f, m); |
---|
[0ff8fb57] | 559 | } |
---|
| 560 | |
---|
[f4d32cd] | 561 | int owl_message_is_question(owl_message *m) |
---|
| 562 | { |
---|
| 563 | if(!owl_message_is_type_admin(m)) return 0; |
---|
| 564 | if(owl_message_get_attribute_value(m, "question") != NULL) return 1; |
---|
| 565 | return 0; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | int owl_message_is_answered(owl_message *m) { |
---|
[ad15610] | 569 | char *q; |
---|
[f4d32cd] | 570 | if(!owl_message_is_question(m)) return 0; |
---|
[ad15610] | 571 | q = owl_message_get_attribute_value(m, "question"); |
---|
[f4d32cd] | 572 | if(!q) return 0; |
---|
| 573 | return !strcmp(q, "answered"); |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | void owl_message_set_isanswered(owl_message *m) { |
---|
| 577 | owl_message_set_attribute(m, "question", "answered"); |
---|
| 578 | } |
---|
| 579 | |
---|
[0ff8fb57] | 580 | int owl_message_is_from_me(owl_message *m) |
---|
| 581 | { |
---|
| 582 | if (owl_message_is_type_zephyr(m)) { |
---|
[09489b89] | 583 | if (!strcasecmp(owl_message_get_sender(m), owl_zephyr_get_sender())) { |
---|
[0ff8fb57] | 584 | return(1); |
---|
| 585 | } else { |
---|
| 586 | return(0); |
---|
| 587 | } |
---|
| 588 | } else if (owl_message_is_type_aim(m)) { |
---|
| 589 | if (!strcasecmp(owl_message_get_sender(m), owl_global_get_aim_screenname(&g))) { |
---|
| 590 | return(1); |
---|
| 591 | } else { |
---|
| 592 | return(0); |
---|
| 593 | } |
---|
| 594 | } else if (owl_message_is_type_admin(m)) { |
---|
| 595 | return(0); |
---|
| 596 | } |
---|
[4b464a4] | 597 | return(0); |
---|
| 598 | } |
---|
| 599 | |
---|
[0ff8fb57] | 600 | int owl_message_is_mail(owl_message *m) |
---|
| 601 | { |
---|
| 602 | if (owl_message_is_type_zephyr(m)) { |
---|
[5789230] | 603 | if (!strcasecmp(owl_message_get_class(m), "mail") && owl_message_is_private(m)) { |
---|
[0ff8fb57] | 604 | return(1); |
---|
| 605 | } else { |
---|
| 606 | return(0); |
---|
| 607 | } |
---|
[4b464a4] | 608 | } |
---|
| 609 | return(0); |
---|
| 610 | } |
---|
| 611 | |
---|
[0ff8fb57] | 612 | int owl_message_is_ping(owl_message *m) |
---|
| 613 | { |
---|
| 614 | if (owl_message_is_type_zephyr(m)) { |
---|
| 615 | if (!strcasecmp(owl_message_get_opcode(m), "ping")) { |
---|
| 616 | return(1); |
---|
| 617 | } else { |
---|
| 618 | return(0); |
---|
| 619 | } |
---|
| 620 | } |
---|
[4b464a4] | 621 | return(0); |
---|
| 622 | } |
---|
| 623 | |
---|
[0ff8fb57] | 624 | int owl_message_is_burningears(owl_message *m) |
---|
| 625 | { |
---|
[4b464a4] | 626 | /* we should add a global to cache the short zsender */ |
---|
| 627 | char sender[LINE], *ptr; |
---|
| 628 | |
---|
| 629 | /* if the message is from us or to us, it doesn't count */ |
---|
[5789230] | 630 | if (owl_message_is_from_me(m) || owl_message_is_private(m)) return(0); |
---|
[0ff8fb57] | 631 | |
---|
| 632 | if (owl_message_is_type_zephyr(m)) { |
---|
[09489b89] | 633 | strcpy(sender, owl_zephyr_get_sender()); |
---|
[0ff8fb57] | 634 | ptr=strchr(sender, '@'); |
---|
| 635 | if (ptr) *ptr='\0'; |
---|
| 636 | } else if (owl_message_is_type_aim(m)) { |
---|
| 637 | strcpy(sender, owl_global_get_aim_screenname(&g)); |
---|
| 638 | } else { |
---|
| 639 | return(0); |
---|
| 640 | } |
---|
[4b464a4] | 641 | |
---|
| 642 | if (stristr(owl_message_get_body(m), sender)) { |
---|
| 643 | return(1); |
---|
| 644 | } |
---|
| 645 | return(0); |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | /* caller must free return value. */ |
---|
[0ff8fb57] | 649 | char *owl_message_get_cc(owl_message *m) |
---|
| 650 | { |
---|
[4b464a4] | 651 | char *cur, *out, *end; |
---|
| 652 | |
---|
| 653 | cur = owl_message_get_body(m); |
---|
| 654 | while (*cur && *cur==' ') cur++; |
---|
[985f85b] | 655 | if (strncasecmp(cur, "cc:", 3)) return(NULL); |
---|
[4b464a4] | 656 | cur+=3; |
---|
| 657 | while (*cur && *cur==' ') cur++; |
---|
| 658 | out = owl_strdup(cur); |
---|
| 659 | end = strchr(out, '\n'); |
---|
| 660 | if (end) end[0] = '\0'; |
---|
| 661 | return(out); |
---|
| 662 | } |
---|
| 663 | |
---|
[48609ce] | 664 | /* caller must free return value */ |
---|
| 665 | char *owl_message_get_cc_without_recipient(owl_message *m) |
---|
| 666 | { |
---|
[9c590d4] | 667 | char *cc, *out, *end, *user, *recip; |
---|
[48609ce] | 668 | |
---|
| 669 | cc = owl_message_get_cc(m); |
---|
[9c590d4] | 670 | if (cc == NULL) |
---|
| 671 | return NULL; |
---|
| 672 | |
---|
| 673 | recip = short_zuser(owl_message_get_recipient(m)); |
---|
[48609ce] | 674 | out = owl_malloc(strlen(cc)); |
---|
| 675 | end = out; |
---|
| 676 | |
---|
| 677 | user = strtok(cc, " "); |
---|
| 678 | while (user != NULL) { |
---|
[9c590d4] | 679 | if (strcasecmp(user, recip) != 0) { |
---|
[48609ce] | 680 | strcpy(end, user); |
---|
| 681 | end[strlen(user)] = ' '; |
---|
| 682 | end += strlen(user) + 1; |
---|
| 683 | } |
---|
| 684 | user = strtok(NULL, " "); |
---|
| 685 | } |
---|
| 686 | end[0] = '\0'; |
---|
| 687 | |
---|
[9c590d4] | 688 | owl_free(recip); |
---|
[48609ce] | 689 | owl_free(cc); |
---|
[9c590d4] | 690 | |
---|
| 691 | if (strlen(out) == 0) { |
---|
| 692 | owl_free(out); |
---|
| 693 | out = NULL; |
---|
| 694 | } |
---|
| 695 | |
---|
[48609ce] | 696 | return(out); |
---|
| 697 | } |
---|
| 698 | |
---|
[0ff8fb57] | 699 | int owl_message_get_id(owl_message *m) |
---|
| 700 | { |
---|
[4b464a4] | 701 | return(m->id); |
---|
| 702 | } |
---|
[bd3f232] | 703 | |
---|
[f1e629d] | 704 | char *owl_message_get_type(owl_message *m) { |
---|
[30678ae] | 705 | char * type = owl_message_get_attribute_value(m, "type"); |
---|
| 706 | if(!type) { |
---|
| 707 | return "generic"; |
---|
[dd16bdd] | 708 | } |
---|
[30678ae] | 709 | return type; |
---|
[dd16bdd] | 710 | } |
---|
| 711 | |
---|
[f1e629d] | 712 | char *owl_message_get_direction(owl_message *m) { |
---|
| 713 | switch (m->direction) { |
---|
| 714 | case OWL_MESSAGE_DIRECTION_IN: |
---|
| 715 | return("in"); |
---|
| 716 | case OWL_MESSAGE_DIRECTION_OUT: |
---|
| 717 | return("out"); |
---|
| 718 | case OWL_MESSAGE_DIRECTION_NONE: |
---|
| 719 | return("none"); |
---|
| 720 | default: |
---|
| 721 | return("unknown"); |
---|
| 722 | } |
---|
| 723 | } |
---|
| 724 | |
---|
[dd16bdd] | 725 | int owl_message_parse_direction(char *d) { |
---|
| 726 | if(!strcmp(d, "in")) { |
---|
| 727 | return OWL_MESSAGE_DIRECTION_IN; |
---|
| 728 | } else if(!strcmp(d, "out")) { |
---|
| 729 | return OWL_MESSAGE_DIRECTION_OUT; |
---|
| 730 | } else { |
---|
| 731 | return OWL_MESSAGE_DIRECTION_NONE; |
---|
| 732 | } |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | |
---|
[f1e629d] | 736 | char *owl_message_get_login(owl_message *m) { |
---|
| 737 | if (owl_message_is_login(m)) { |
---|
| 738 | return "login"; |
---|
| 739 | } else if (owl_message_is_logout(m)) { |
---|
| 740 | return "logout"; |
---|
| 741 | } else { |
---|
| 742 | return "none"; |
---|
| 743 | } |
---|
| 744 | } |
---|
| 745 | |
---|
[dd16bdd] | 746 | |
---|
[f1e629d] | 747 | char *owl_message_get_header(owl_message *m) { |
---|
| 748 | return owl_message_get_attribute_value(m, "adminheader"); |
---|
| 749 | } |
---|
| 750 | |
---|
[bd3f232] | 751 | /* return 1 if the message contains "string", 0 otherwise. This is |
---|
| 752 | * case insensitive because the functions it uses are |
---|
| 753 | */ |
---|
[0ff8fb57] | 754 | int owl_message_search(owl_message *m, char *string) |
---|
| 755 | { |
---|
[4b464a4] | 756 | |
---|
[f14a7ee] | 757 | owl_message_format(m); /* is this necessary? */ |
---|
[bd3f232] | 758 | |
---|
[a387d12e] | 759 | return (owl_fmtext_search(&(m->fmtext->fmtext), string)); |
---|
[4b464a4] | 760 | } |
---|
| 761 | |
---|
| 762 | |
---|
[d559df9] | 763 | /* if loginout == -1 it's a logout message |
---|
| 764 | * 0 it's not a login/logout message |
---|
| 765 | * 1 it's a login message |
---|
| 766 | */ |
---|
| 767 | void owl_message_create_aim(owl_message *m, char *sender, char *recipient, char *text, int direction, int loginout) |
---|
[0ff8fb57] | 768 | { |
---|
[d09e5a1] | 769 | owl_message_init(m); |
---|
| 770 | owl_message_set_body(m, text); |
---|
| 771 | owl_message_set_sender(m, sender); |
---|
[440ce01] | 772 | owl_message_set_recipient(m, recipient); |
---|
[d09e5a1] | 773 | owl_message_set_type_aim(m); |
---|
[3abf28b] | 774 | |
---|
[d559df9] | 775 | if (direction==OWL_MESSAGE_DIRECTION_IN) { |
---|
| 776 | owl_message_set_direction_in(m); |
---|
| 777 | } else if (direction==OWL_MESSAGE_DIRECTION_OUT) { |
---|
| 778 | owl_message_set_direction_out(m); |
---|
[3abf28b] | 779 | } |
---|
| 780 | |
---|
[d559df9] | 781 | /* for now all messages that aren't loginout are private */ |
---|
| 782 | if (!loginout) { |
---|
| 783 | owl_message_set_isprivate(m); |
---|
| 784 | } |
---|
[3abf28b] | 785 | |
---|
[d559df9] | 786 | if (loginout==-1) { |
---|
| 787 | owl_message_set_islogout(m); |
---|
| 788 | } else if (loginout==1) { |
---|
| 789 | owl_message_set_islogin(m); |
---|
| 790 | } |
---|
[aa5f725] | 791 | } |
---|
| 792 | |
---|
[0ff8fb57] | 793 | void owl_message_create_admin(owl_message *m, char *header, char *text) |
---|
| 794 | { |
---|
[d0d65df] | 795 | owl_message_init(m); |
---|
[4b464a4] | 796 | owl_message_set_type_admin(m); |
---|
[d0d65df] | 797 | owl_message_set_body(m, text); |
---|
[bd3f232] | 798 | owl_message_set_attribute(m, "adminheader", header); /* just a hack for now */ |
---|
[7d4fbcd] | 799 | } |
---|
| 800 | |
---|
[37eab7f] | 801 | /* caller should set the direction */ |
---|
| 802 | void owl_message_create_loopback(owl_message *m, char *text) |
---|
| 803 | { |
---|
| 804 | owl_message_init(m); |
---|
| 805 | owl_message_set_type_loopback(m); |
---|
| 806 | owl_message_set_body(m, text); |
---|
[eec69e1] | 807 | owl_message_set_sender(m, "loopsender"); |
---|
| 808 | owl_message_set_recipient(m, "looprecip"); |
---|
[37eab7f] | 809 | owl_message_set_isprivate(m); |
---|
| 810 | } |
---|
| 811 | |
---|
[09489b89] | 812 | #ifdef HAVE_LIBZEPHYR |
---|
[0ff8fb57] | 813 | void owl_message_create_from_znotice(owl_message *m, ZNotice_t *n) |
---|
| 814 | { |
---|
[7d4fbcd] | 815 | struct hostent *hent; |
---|
[d0d65df] | 816 | char *ptr, *tmp, *tmp2; |
---|
[804ab8a] | 817 | int len; |
---|
[7d4fbcd] | 818 | |
---|
[d0d65df] | 819 | owl_message_init(m); |
---|
| 820 | |
---|
[4b464a4] | 821 | owl_message_set_type_zephyr(m); |
---|
| 822 | owl_message_set_direction_in(m); |
---|
[7d4fbcd] | 823 | |
---|
| 824 | /* first save the full notice */ |
---|
| 825 | memcpy(&(m->notice), n, sizeof(ZNotice_t)); |
---|
| 826 | |
---|
[25dd31a] | 827 | /* a little gross, we'll replace \r's with ' ' for now */ |
---|
[7d4fbcd] | 828 | owl_zephyr_hackaway_cr(&(m->notice)); |
---|
| 829 | |
---|
[25dd31a] | 830 | /* save the time, we need to nuke the string saved by message_init */ |
---|
[5a95b69] | 831 | if (m->timestr) owl_free(m->timestr); |
---|
[25dd31a] | 832 | m->time=n->z_time.tv_sec; |
---|
| 833 | m->timestr=owl_strdup(ctime(&(m->time))); |
---|
| 834 | m->timestr[strlen(m->timestr)-1]='\0'; |
---|
| 835 | |
---|
[7d4fbcd] | 836 | /* set other info */ |
---|
[d0d65df] | 837 | owl_message_set_sender(m, n->z_sender); |
---|
| 838 | owl_message_set_class(m, n->z_class); |
---|
| 839 | owl_message_set_instance(m, n->z_class_inst); |
---|
| 840 | owl_message_set_recipient(m, n->z_recipient); |
---|
[7d4fbcd] | 841 | if (n->z_opcode) { |
---|
[d0d65df] | 842 | owl_message_set_opcode(m, n->z_opcode); |
---|
[7d4fbcd] | 843 | } else { |
---|
[d0d65df] | 844 | owl_message_set_opcode(m, ""); |
---|
[7d4fbcd] | 845 | } |
---|
[804ab8a] | 846 | owl_message_set_zsig(m, owl_zephyr_get_zsig(n, &len)); |
---|
[7d4fbcd] | 847 | |
---|
| 848 | if ((ptr=strchr(n->z_recipient, '@'))!=NULL) { |
---|
[d0d65df] | 849 | owl_message_set_realm(m, ptr+1); |
---|
[7d4fbcd] | 850 | } else { |
---|
[09489b89] | 851 | owl_message_set_realm(m, owl_zephyr_get_realm()); |
---|
[7d4fbcd] | 852 | } |
---|
| 853 | |
---|
[5789230] | 854 | /* Set the "isloginout" attribute if it's a login message */ |
---|
[1d3e925] | 855 | if (!strcasecmp(n->z_class, "login") || !strcasecmp(n->z_class, OWL_WEBZEPHYR_CLASS)) { |
---|
[5a95b69] | 856 | if (!strcasecmp(n->z_opcode, "user_login") || !strcasecmp(n->z_opcode, "user_logout")) { |
---|
[b0430a6] | 857 | tmp=owl_zephyr_get_field(n, 1); |
---|
[5a95b69] | 858 | owl_message_set_attribute(m, "loginhost", tmp); |
---|
| 859 | owl_free(tmp); |
---|
| 860 | |
---|
[b0430a6] | 861 | tmp=owl_zephyr_get_field(n, 3); |
---|
[5a95b69] | 862 | owl_message_set_attribute(m, "logintty", tmp); |
---|
| 863 | owl_free(tmp); |
---|
| 864 | } |
---|
| 865 | |
---|
[d559df9] | 866 | if (!strcasecmp(n->z_opcode, "user_login")) { |
---|
| 867 | owl_message_set_islogin(m); |
---|
| 868 | } else if (!strcasecmp(n->z_opcode, "user_logout")) { |
---|
| 869 | owl_message_set_islogout(m); |
---|
| 870 | } |
---|
[5789230] | 871 | } |
---|
| 872 | |
---|
[5a95b69] | 873 | |
---|
[963542b] | 874 | /* set the "isprivate" attribute if it's a private zephyr. |
---|
[ce74deb] | 875 | ``private'' means recipient is non-empty and doesn't start wit |
---|
| 876 | `@' */ |
---|
[963542b] | 877 | if (*n->z_recipient && *n->z_recipient != '@') { |
---|
[5789230] | 878 | owl_message_set_isprivate(m); |
---|
| 879 | } |
---|
| 880 | |
---|
[9854278] | 881 | /* set the "isauto" attribute if it's an autoreply */ |
---|
| 882 | if (!strcasecmp(n->z_message, "Automated reply:") || |
---|
| 883 | !strcasecmp(n->z_opcode, "auto")) { |
---|
| 884 | owl_message_set_attribute(m, "isauto", ""); |
---|
| 885 | } |
---|
| 886 | |
---|
[7d4fbcd] | 887 | m->zwriteline=strdup(""); |
---|
| 888 | |
---|
[85d1795] | 889 | /* save the hostname */ |
---|
| 890 | owl_function_debugmsg("About to do gethostbyaddr"); |
---|
| 891 | hent=gethostbyaddr((char *) &(n->z_uid.zuid_addr), sizeof(n->z_uid.zuid_addr), AF_INET); |
---|
| 892 | if (hent && hent->h_name) { |
---|
| 893 | owl_message_set_hostname(m, hent->h_name); |
---|
| 894 | } else { |
---|
| 895 | owl_message_set_hostname(m, inet_ntoa(n->z_sender_addr)); |
---|
| 896 | } |
---|
| 897 | |
---|
[b45293f] | 898 | /* set the body */ |
---|
[85d1795] | 899 | tmp=owl_zephyr_get_message(n, m); |
---|
[7e3e00a] | 900 | if (owl_global_is_newlinestrip(&g)) { |
---|
[d0d65df] | 901 | tmp2=owl_util_stripnewlines(tmp); |
---|
| 902 | owl_message_set_body(m, tmp2); |
---|
| 903 | owl_free(tmp2); |
---|
[7e3e00a] | 904 | } else { |
---|
[d0d65df] | 905 | owl_message_set_body(m, tmp); |
---|
[7e3e00a] | 906 | } |
---|
[ecd5dc5] | 907 | owl_free(tmp); |
---|
[7d4fbcd] | 908 | |
---|
[c86a35c] | 909 | #ifdef OWL_ENABLE_ZCRYPT |
---|
[d309eb3] | 910 | /* if zcrypt is enabled try to decrypt the message */ |
---|
| 911 | if (owl_global_is_zcrypt(&g) && !strcasecmp(n->z_opcode, "crypt")) { |
---|
| 912 | char *out; |
---|
[c269e22] | 913 | int ret; |
---|
[d309eb3] | 914 | |
---|
[d0d65df] | 915 | out=owl_malloc(strlen(owl_message_get_body(m))*16+20); |
---|
[9ceee9d] | 916 | ret=owl_zcrypt_decrypt(out, owl_message_get_body(m), owl_message_get_class(m), owl_message_get_instance(m)); |
---|
[a15a84f] | 917 | if (ret==0) { |
---|
[d0d65df] | 918 | owl_message_set_body(m, out); |
---|
[a15a84f] | 919 | } else { |
---|
| 920 | owl_free(out); |
---|
| 921 | } |
---|
[d309eb3] | 922 | } |
---|
[c269e22] | 923 | #endif |
---|
[7d4fbcd] | 924 | } |
---|
[09489b89] | 925 | #else |
---|
| 926 | void owl_message_create_from_znotice(owl_message *m, void *n) |
---|
| 927 | { |
---|
| 928 | } |
---|
| 929 | #endif |
---|
[7d4fbcd] | 930 | |
---|
[5a95b69] | 931 | /* If 'direction' is '0' it is a login message, '1' is a logout message. */ |
---|
| 932 | void owl_message_create_pseudo_zlogin(owl_message *m, int direction, char *user, char *host, char *time, char *tty) |
---|
| 933 | { |
---|
| 934 | char *longuser, *ptr; |
---|
| 935 | |
---|
[ba9f236] | 936 | #ifdef HAVE_LIBZEPHYR |
---|
[5a95b69] | 937 | memset(&(m->notice), 0, sizeof(ZNotice_t)); |
---|
[ba9f236] | 938 | #endif |
---|
| 939 | |
---|
[5a95b69] | 940 | longuser=long_zuser(user); |
---|
| 941 | |
---|
| 942 | owl_message_init(m); |
---|
| 943 | |
---|
| 944 | owl_message_set_type_zephyr(m); |
---|
| 945 | owl_message_set_direction_in(m); |
---|
| 946 | |
---|
| 947 | owl_message_set_attribute(m, "pseudo", ""); |
---|
| 948 | owl_message_set_attribute(m, "loginhost", host ? host : ""); |
---|
| 949 | owl_message_set_attribute(m, "logintty", tty ? tty : ""); |
---|
| 950 | |
---|
| 951 | owl_message_set_sender(m, longuser); |
---|
| 952 | owl_message_set_class(m, "LOGIN"); |
---|
| 953 | owl_message_set_instance(m, longuser); |
---|
| 954 | owl_message_set_recipient(m, ""); |
---|
| 955 | if (direction==0) { |
---|
| 956 | owl_message_set_opcode(m, "USER_LOGIN"); |
---|
| 957 | owl_message_set_islogin(m); |
---|
| 958 | } else if (direction==1) { |
---|
| 959 | owl_message_set_opcode(m, "USER_LOGOUT"); |
---|
| 960 | owl_message_set_islogout(m); |
---|
| 961 | } |
---|
| 962 | |
---|
| 963 | if ((ptr=strchr(longuser, '@'))!=NULL) { |
---|
| 964 | owl_message_set_realm(m, ptr+1); |
---|
| 965 | } else { |
---|
| 966 | owl_message_set_realm(m, owl_zephyr_get_realm()); |
---|
| 967 | } |
---|
| 968 | |
---|
| 969 | m->zwriteline=strdup(""); |
---|
| 970 | |
---|
| 971 | owl_message_set_body(m, "<uninitialized>"); |
---|
| 972 | |
---|
| 973 | /* save the hostname */ |
---|
[2de4f20] | 974 | owl_function_debugmsg("create_pseudo_login: host is %s", host ? host : ""); |
---|
[8298425] | 975 | owl_message_set_hostname(m, host ? host : ""); |
---|
[5a95b69] | 976 | owl_free(longuser); |
---|
| 977 | } |
---|
| 978 | |
---|
[0ff8fb57] | 979 | void owl_message_create_from_zwriteline(owl_message *m, char *line, char *body, char *zsig) |
---|
| 980 | { |
---|
[b45293f] | 981 | owl_zwrite z; |
---|
| 982 | int ret; |
---|
[8298425] | 983 | char hostbuff[5000]; |
---|
[b45293f] | 984 | |
---|
[d0d65df] | 985 | owl_message_init(m); |
---|
[b45293f] | 986 | |
---|
| 987 | /* create a zwrite for the purpose of filling in other message fields */ |
---|
| 988 | owl_zwrite_create_from_line(&z, line); |
---|
| 989 | |
---|
| 990 | /* set things */ |
---|
| 991 | owl_message_set_direction_out(m); |
---|
| 992 | owl_message_set_type_zephyr(m); |
---|
[09489b89] | 993 | owl_message_set_sender(m, owl_zephyr_get_sender()); |
---|
[8fec514] | 994 | owl_message_set_class(m, owl_zwrite_get_class(&z)); |
---|
| 995 | owl_message_set_instance(m, owl_zwrite_get_instance(&z)); |
---|
[9ceee9d] | 996 | if (owl_zwrite_get_numrecips(&z)>0) { |
---|
| 997 | owl_message_set_recipient(m, |
---|
| 998 | long_zuser(owl_zwrite_get_recip_n(&z, 0))); /* only gets the first user, must fix */ |
---|
| 999 | } |
---|
[8fec514] | 1000 | owl_message_set_opcode(m, owl_zwrite_get_opcode(&z)); |
---|
[d0d65df] | 1001 | owl_message_set_realm(m, owl_zwrite_get_realm(&z)); /* also a hack, but not here */ |
---|
[b45293f] | 1002 | m->zwriteline=owl_strdup(line); |
---|
[d0d65df] | 1003 | owl_message_set_body(m, body); |
---|
[8fec514] | 1004 | owl_message_set_zsig(m, zsig); |
---|
[b45293f] | 1005 | |
---|
| 1006 | /* save the hostname */ |
---|
[8298425] | 1007 | ret=gethostname(hostbuff, MAXHOSTNAMELEN); |
---|
| 1008 | hostbuff[MAXHOSTNAMELEN]='\0'; |
---|
[b45293f] | 1009 | if (ret) { |
---|
[8298425] | 1010 | owl_message_set_hostname(m, "localhost"); |
---|
| 1011 | } else { |
---|
| 1012 | owl_message_set_hostname(m, hostbuff); |
---|
[b45293f] | 1013 | } |
---|
[312675c] | 1014 | |
---|
[ce74deb] | 1015 | /* set the "isprivate" attribute if it's a private zephyr. */ |
---|
| 1016 | if (owl_zwrite_is_personal(&z)) { |
---|
[312675c] | 1017 | owl_message_set_isprivate(m); |
---|
[ce74deb] | 1018 | } |
---|
| 1019 | |
---|
| 1020 | owl_zwrite_free(&z); |
---|
[b45293f] | 1021 | } |
---|
[7d4fbcd] | 1022 | |
---|
[0ff8fb57] | 1023 | void owl_message_pretty_zsig(owl_message *m, char *buff) |
---|
| 1024 | { |
---|
[b45293f] | 1025 | /* stick a one line version of the zsig in buff */ |
---|
[7d4fbcd] | 1026 | char *ptr; |
---|
| 1027 | |
---|
[d0d65df] | 1028 | strcpy(buff, owl_message_get_zsig(m)); |
---|
[b45293f] | 1029 | ptr=strchr(buff, '\n'); |
---|
| 1030 | if (ptr) ptr[0]='\0'; |
---|
[7d4fbcd] | 1031 | } |
---|
| 1032 | |
---|
[0ff8fb57] | 1033 | void owl_message_free(owl_message *m) |
---|
| 1034 | { |
---|
[d0d65df] | 1035 | int i, j; |
---|
| 1036 | owl_pair *p; |
---|
[09489b89] | 1037 | #ifdef HAVE_LIBZEPHYR |
---|
[4b464a4] | 1038 | if (owl_message_is_type_zephyr(m) && owl_message_is_direction_in(m)) { |
---|
[7d4fbcd] | 1039 | ZFreeNotice(&(m->notice)); |
---|
| 1040 | } |
---|
[09489b89] | 1041 | #endif |
---|
[25dd31a] | 1042 | if (m->timestr) owl_free(m->timestr); |
---|
[7d4fbcd] | 1043 | if (m->zwriteline) owl_free(m->zwriteline); |
---|
[d0d65df] | 1044 | |
---|
| 1045 | /* free all the attributes */ |
---|
| 1046 | j=owl_list_get_size(&(m->attributes)); |
---|
| 1047 | for (i=0; i<j; i++) { |
---|
| 1048 | p=owl_list_get_element(&(m->attributes), i); |
---|
| 1049 | owl_free(owl_pair_get_value(p)); |
---|
| 1050 | owl_free(p); |
---|
| 1051 | } |
---|
| 1052 | |
---|
| 1053 | owl_list_free_simple(&(m->attributes)); |
---|
[7d4fbcd] | 1054 | |
---|
[a387d12e] | 1055 | owl_message_invalidate_format(m); |
---|
[7d4fbcd] | 1056 | } |
---|