[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <ctype.h> |
---|
| 5 | #include <sys/param.h> |
---|
| 6 | |
---|
[1aee7d9] | 7 | static const char fileIdent[] = "$Id$"; |
---|
| 8 | |
---|
[15b34fd] | 9 | /* This is now the one function that should be called to log a |
---|
| 10 | * message. It will do all the work necessary by calling the other |
---|
| 11 | * functions in this file as necessary. |
---|
| 12 | */ |
---|
| 13 | void owl_log_message(owl_message *m) { |
---|
| 14 | owl_function_debugmsg("owl_log_message: entering"); |
---|
| 15 | |
---|
[3c7d086a] | 16 | if (m == NULL) { |
---|
| 17 | owl_function_debugmsg("owl_log_message: passed null message"); |
---|
| 18 | return; |
---|
| 19 | } |
---|
| 20 | |
---|
[15b34fd] | 21 | /* should we be logging this message? */ |
---|
| 22 | if (!owl_log_shouldlog_message(m)) { |
---|
| 23 | owl_function_debugmsg("owl_log_message: not logging message"); |
---|
| 24 | return; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | /* handle incmoing messages */ |
---|
| 28 | if (owl_message_is_direction_in(m)) { |
---|
| 29 | owl_log_incoming(m); |
---|
| 30 | owl_function_debugmsg("owl_log_message: leaving"); |
---|
| 31 | return; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /* handle outgoing messages */ |
---|
[42947f1] | 35 | owl_log_outgoing(m); |
---|
| 36 | |
---|
[15b34fd] | 37 | owl_function_debugmsg("owl_log_message: leaving"); |
---|
| 38 | } |
---|
[7d4fbcd] | 39 | |
---|
[15b34fd] | 40 | /* Return 1 if we should log the given message, otherwise return 0 */ |
---|
| 41 | int owl_log_shouldlog_message(owl_message *m) { |
---|
| 42 | owl_filter *f; |
---|
[12c35df] | 43 | |
---|
[15b34fd] | 44 | /* If there's a logfilter and this message matches it, log */ |
---|
| 45 | f=owl_global_get_filter(&g, owl_global_get_logfilter(&g)); |
---|
| 46 | if (f && owl_filter_message_match(f, m)) return(1); |
---|
[7d4fbcd] | 47 | |
---|
[15b34fd] | 48 | /* otherwise we do things based on the logging variables */ |
---|
| 49 | |
---|
| 50 | /* skip login/logout messages if appropriate */ |
---|
| 51 | if (!owl_global_is_loglogins(&g) && owl_message_is_loginout(m)) return(0); |
---|
| 52 | |
---|
| 53 | /* check direction */ |
---|
| 54 | if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) { |
---|
| 55 | return(0); |
---|
| 56 | } |
---|
| 57 | if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) { |
---|
| 58 | return(0); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | if (owl_message_is_type_zephyr(m)) { |
---|
| 62 | if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0); |
---|
| 63 | if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0); |
---|
| 64 | } else { |
---|
| 65 | if (owl_message_is_private(m) || owl_message_is_loginout(m)) { |
---|
| 66 | if (!owl_global_is_logging(&g)) return(0); |
---|
| 67 | } else { |
---|
| 68 | if (!owl_global_is_classlogging(&g)) return(0); |
---|
| 69 | } |
---|
[7d4fbcd] | 70 | } |
---|
[15b34fd] | 71 | return(1); |
---|
| 72 | } |
---|
| 73 | |
---|
[42947f1] | 74 | void owl_log_zephyr(owl_message *m, FILE *file) { |
---|
| 75 | char *tmp; |
---|
| 76 | tmp=short_zuser(owl_message_get_sender(m)); |
---|
| 77 | fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
| 78 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m)); |
---|
| 79 | fprintf(file, "\n"); |
---|
| 80 | fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
| 81 | fprintf(file, "From: %s <%s>\n\n", owl_message_get_zsig(m), tmp); |
---|
| 82 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
| 83 | owl_free(tmp); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void owl_log_aim(owl_message *m, FILE *file) { |
---|
| 87 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 88 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 89 | if (owl_message_is_login(m)) |
---|
| 90 | fprintf(file, "LOGIN\n\n"); |
---|
| 91 | else if (owl_message_is_logout(m)) |
---|
| 92 | fprintf(file, "LOGOUT\n\n"); |
---|
| 93 | else |
---|
| 94 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void owl_log_jabber(owl_message *m, FILE *file) { |
---|
| 98 | fprintf(file, "From: <%s> To: <%s>\n",owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 99 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 100 | fprintf(file, "%s\n\n",owl_message_get_body(m)); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void owl_log_generic(owl_message *m, FILE *file) { |
---|
| 104 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 105 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 106 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | void owl_log_append(owl_message *m, char *filename) { |
---|
| 110 | FILE *file; |
---|
| 111 | file=fopen(filename, "a"); |
---|
| 112 | if (!file) { |
---|
| 113 | owl_function_error("Unable to open file for logging"); |
---|
| 114 | return; |
---|
| 115 | } |
---|
| 116 | if (owl_message_is_type_zephyr(m)) { |
---|
| 117 | owl_log_zephyr(m, file); |
---|
| 118 | } else if (owl_message_is_type_jabber(m)) { |
---|
| 119 | owl_log_jabber(m, file); |
---|
| 120 | } else if (owl_message_is_type_aim(m)) { |
---|
| 121 | owl_log_aim(m, file); |
---|
| 122 | } else { |
---|
| 123 | owl_log_generic(m, file); |
---|
| 124 | } |
---|
| 125 | fclose(file); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void owl_log_outgoing(owl_message *m) |
---|
[15b34fd] | 129 | { |
---|
| 130 | char filename[MAXPATHLEN], *logpath; |
---|
[9c590d4] | 131 | char *to, *temp; |
---|
| 132 | |
---|
| 133 | /* expand ~ in path names */ |
---|
| 134 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[15b34fd] | 135 | |
---|
[42947f1] | 136 | /* Figure out what path to log to */ |
---|
| 137 | if (owl_message_is_type_zephyr(m)) { |
---|
[af1920fd] | 138 | /* If this has CC's, do all but the "recipient" which we'll do below */ |
---|
[9c590d4] | 139 | to = owl_message_get_cc_without_recipient(m); |
---|
| 140 | if (to != NULL) { |
---|
| 141 | temp = strtok(to, " "); |
---|
| 142 | while (temp != NULL) { |
---|
| 143 | temp = short_zuser(temp); |
---|
| 144 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp); |
---|
| 145 | owl_log_append(m, filename); |
---|
| 146 | temp = strtok(NULL, " "); |
---|
| 147 | } |
---|
| 148 | owl_free(to); |
---|
| 149 | } |
---|
| 150 | to = short_zuser(owl_message_get_recipient(m)); |
---|
[42947f1] | 151 | } else if (owl_message_is_type_jabber(m)) { |
---|
[9c590d4] | 152 | to = owl_sprintf("jabber:%s", owl_message_get_recipient(m)); |
---|
[d8671a1] | 153 | owl_text_tr(to, '/', '_'); |
---|
[42947f1] | 154 | } else if (owl_message_is_type_aim(m)) { |
---|
[28ee32b] | 155 | char *temp2; |
---|
[9c590d4] | 156 | temp = owl_aim_normalize_screenname(owl_message_get_recipient(m)); |
---|
[28ee32b] | 157 | temp2 = g_utf8_strdown(temp,-1); |
---|
| 158 | to = owl_sprintf("aim:%s", temp2); |
---|
| 159 | owl_free(temp2); |
---|
[9c590d4] | 160 | owl_free(temp); |
---|
[42947f1] | 161 | } else { |
---|
[9c590d4] | 162 | to = owl_sprintf("loopback"); |
---|
[42947f1] | 163 | } |
---|
[7d4fbcd] | 164 | |
---|
[15b34fd] | 165 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to); |
---|
[42947f1] | 166 | owl_log_append(m, filename); |
---|
[9c590d4] | 167 | owl_free(to); |
---|
[7d4fbcd] | 168 | |
---|
[e1c4636] | 169 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
[42947f1] | 170 | owl_log_append(m, filename); |
---|
[9c590d4] | 171 | owl_free(logpath); |
---|
[7d4fbcd] | 172 | } |
---|
| 173 | |
---|
[42947f1] | 174 | |
---|
[2b86d14] | 175 | void owl_log_outgoing_zephyr_error(char *to, char *text) |
---|
| 176 | { |
---|
| 177 | FILE *file; |
---|
| 178 | char filename[MAXPATHLEN], *logpath; |
---|
[180cd15] | 179 | char *tobuff, *zwriteline; |
---|
| 180 | owl_message *m; |
---|
| 181 | |
---|
| 182 | /* create a present message so we can pass it to |
---|
| 183 | * owl_log_shouldlog_message() |
---|
| 184 | */ |
---|
| 185 | zwriteline=owl_sprintf("zwrite %s", to); |
---|
| 186 | m=owl_function_make_outgoing_zephyr(text, zwriteline, ""); |
---|
| 187 | owl_free(zwriteline); |
---|
| 188 | if (!owl_log_shouldlog_message(m)) { |
---|
| 189 | owl_message_free(m); |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | owl_message_free(m); |
---|
[2b86d14] | 193 | |
---|
[180cd15] | 194 | /* chop off a local realm */ |
---|
[15b34fd] | 195 | tobuff=short_zuser(to); |
---|
[2b86d14] | 196 | |
---|
| 197 | /* expand ~ in path names */ |
---|
[15b34fd] | 198 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[2b86d14] | 199 | |
---|
| 200 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
| 201 | file=fopen(filename, "a"); |
---|
| 202 | if (!file) { |
---|
| 203 | owl_function_error("Unable to open file for outgoing logging"); |
---|
| 204 | owl_free(logpath); |
---|
[15b34fd] | 205 | owl_free(tobuff); |
---|
[2b86d14] | 206 | return; |
---|
| 207 | } |
---|
| 208 | fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text); |
---|
| 209 | if (text[strlen(text)-1]!='\n') { |
---|
| 210 | fprintf(file, "\n"); |
---|
| 211 | } |
---|
| 212 | fclose(file); |
---|
| 213 | |
---|
| 214 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
| 215 | owl_free(logpath); |
---|
| 216 | file=fopen(filename, "a"); |
---|
| 217 | if (!file) { |
---|
| 218 | owl_function_error("Unable to open file for outgoing logging"); |
---|
[15b34fd] | 219 | owl_free(tobuff); |
---|
[2b86d14] | 220 | return; |
---|
| 221 | } |
---|
| 222 | fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text); |
---|
| 223 | if (text[strlen(text)-1]!='\n') { |
---|
| 224 | fprintf(file, "\n"); |
---|
| 225 | } |
---|
| 226 | fclose(file); |
---|
| 227 | |
---|
| 228 | owl_free(tobuff); |
---|
| 229 | } |
---|
| 230 | |
---|
[15283bb] | 231 | void owl_log_incoming(owl_message *m) |
---|
| 232 | { |
---|
[e1c4636] | 233 | char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath; |
---|
[42947f1] | 234 | char *frombuff=NULL, *from=NULL; |
---|
[7d4fbcd] | 235 | int len, ch, i, personal; |
---|
[12c35df] | 236 | |
---|
[15b34fd] | 237 | /* figure out if it's a "personal" message or not */ |
---|
[aac889a] | 238 | if (owl_message_is_type_zephyr(m)) { |
---|
| 239 | if (owl_message_is_personal(m)) { |
---|
[42947f1] | 240 | personal = 1; |
---|
[aac889a] | 241 | } else { |
---|
[42947f1] | 242 | personal = 0; |
---|
[aac889a] | 243 | } |
---|
[2182be3] | 244 | } else if (owl_message_is_type_jabber(m)) { |
---|
[42947f1] | 245 | /* This needs to be fixed to handle groupchat */ |
---|
| 246 | char* msgtype = owl_message_get_attribute_value(m,"jtype"); |
---|
| 247 | if (msgtype && !strcmp(msgtype,"groupchat")) { |
---|
| 248 | personal = 0; |
---|
| 249 | } else { |
---|
| 250 | personal = 1; |
---|
| 251 | } |
---|
[7d4fbcd] | 252 | } else { |
---|
[79a0e82] | 253 | if (owl_message_is_private(m) || owl_message_is_loginout(m)) { |
---|
[42947f1] | 254 | personal = 1; |
---|
[aac889a] | 255 | } else { |
---|
[42947f1] | 256 | personal = 0; |
---|
[aac889a] | 257 | } |
---|
[7d4fbcd] | 258 | } |
---|
| 259 | |
---|
[2182be3] | 260 | |
---|
[aac889a] | 261 | if (owl_message_is_type_zephyr(m)) { |
---|
| 262 | if (personal) { |
---|
[3066d23] | 263 | from=frombuff=short_zuser(owl_message_get_sender(m)); |
---|
[aac889a] | 264 | } else { |
---|
| 265 | from=frombuff=owl_strdup(owl_message_get_class(m)); |
---|
[7d4fbcd] | 266 | } |
---|
[aac889a] | 267 | } else if (owl_message_is_type_aim(m)) { |
---|
| 268 | /* we do not yet handle chat rooms */ |
---|
[28ee32b] | 269 | char *normalto, *temp; |
---|
| 270 | temp = owl_aim_normalize_screenname(owl_message_get_sender(m)); |
---|
| 271 | normalto = g_utf8_strdown(temp, -1); |
---|
[421c286f] | 272 | from=frombuff=owl_sprintf("aim:%s", normalto); |
---|
| 273 | owl_free(normalto); |
---|
[28ee32b] | 274 | owl_free(temp); |
---|
[37eab7f] | 275 | } else if (owl_message_is_type_loopback(m)) { |
---|
| 276 | from=frombuff=owl_strdup("loopback"); |
---|
[2182be3] | 277 | } else if (owl_message_is_type_jabber(m)) { |
---|
[5c30091] | 278 | if (personal) { |
---|
| 279 | from=frombuff=owl_sprintf("jabber:%s",owl_message_get_sender(m)); |
---|
| 280 | } else { |
---|
| 281 | from=frombuff=owl_sprintf("jabber:%s",owl_message_get_recipient(m)); |
---|
| 282 | } |
---|
[e6449bc] | 283 | } else { |
---|
| 284 | from=frombuff=owl_strdup("unknown"); |
---|
[7d4fbcd] | 285 | } |
---|
| 286 | |
---|
| 287 | /* check for malicious sender formats */ |
---|
| 288 | len=strlen(frombuff); |
---|
| 289 | if (len<1 || len>35) from="weird"; |
---|
| 290 | if (strchr(frombuff, '/')) from="weird"; |
---|
| 291 | |
---|
| 292 | ch=frombuff[0]; |
---|
[28ee32b] | 293 | if (!g_ascii_isalnum(ch)) from="weird"; |
---|
[7d4fbcd] | 294 | |
---|
| 295 | for (i=0; i<len; i++) { |
---|
[e1c4636] | 296 | if (frombuff[i]<'!' || frombuff[i]>='~') from="weird"; |
---|
[7d4fbcd] | 297 | } |
---|
| 298 | |
---|
| 299 | if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird"; |
---|
| 300 | |
---|
| 301 | if (!personal) { |
---|
[28ee32b] | 302 | if (strcmp(from, "weird")) { |
---|
| 303 | char* temp = g_utf8_strdown(frombuff, -1); |
---|
| 304 | if (temp) { |
---|
| 305 | owl_free(frombuff); |
---|
| 306 | from = frombuff = temp; |
---|
| 307 | } |
---|
| 308 | } |
---|
[7d4fbcd] | 309 | } |
---|
| 310 | |
---|
[e1c4636] | 311 | /* create the filename (expanding ~ in path names) */ |
---|
[7d4fbcd] | 312 | if (personal) { |
---|
[15b34fd] | 313 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[e1c4636] | 314 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
| 315 | snprintf(allfilename, MAXPATHLEN, "%s/all", logpath); |
---|
[d0961fe] | 316 | owl_log_append(m, allfilename); |
---|
[e1c4636] | 317 | |
---|
[7d4fbcd] | 318 | } else { |
---|
[15b34fd] | 319 | logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[e1c4636] | 320 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
[7d4fbcd] | 321 | } |
---|
[37eab7f] | 322 | |
---|
[42947f1] | 323 | owl_log_append(m, filename); |
---|
[7d4fbcd] | 324 | |
---|
[d0961fe] | 325 | if (personal && owl_message_is_type_zephyr(m)) { |
---|
[af1920fd] | 326 | /* We want to log to all of the CC'd people who were not us, or |
---|
| 327 | * the sender, as well. |
---|
| 328 | */ |
---|
[d0961fe] | 329 | char *cc, *temp; |
---|
| 330 | cc = owl_message_get_cc_without_recipient(m); |
---|
| 331 | if (cc != NULL) { |
---|
| 332 | temp = strtok(cc, " "); |
---|
| 333 | while (temp != NULL) { |
---|
| 334 | temp = short_zuser(temp); |
---|
| 335 | if (strcasecmp(temp, frombuff) != 0) { |
---|
| 336 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, temp); |
---|
| 337 | owl_log_append(m, filename); |
---|
| 338 | } |
---|
| 339 | temp = strtok(NULL, " "); |
---|
| 340 | } |
---|
| 341 | owl_free(cc); |
---|
| 342 | } |
---|
| 343 | } |
---|
[7d4fbcd] | 344 | |
---|
[d0961fe] | 345 | owl_free(frombuff); |
---|
| 346 | owl_free(logpath); |
---|
[7d4fbcd] | 347 | } |
---|