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