[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 for nolog */ |
---|
| 54 | if (!strcasecmp(owl_message_get_opcode(m), "nolog") || !strcasecmp(owl_message_get_instance(m), "nolog")) return(0); |
---|
| 55 | |
---|
| 56 | /* check direction */ |
---|
| 57 | if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) && owl_message_is_direction_out(m)) { |
---|
| 58 | return(0); |
---|
| 59 | } |
---|
| 60 | if ((owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) && owl_message_is_direction_in(m)) { |
---|
| 61 | return(0); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | if (owl_message_is_type_zephyr(m)) { |
---|
| 65 | if (owl_message_is_personal(m) && !owl_global_is_logging(&g)) return(0); |
---|
| 66 | if (!owl_message_is_personal(m) && !owl_global_is_classlogging(&g)) return(0); |
---|
| 67 | } else { |
---|
| 68 | if (owl_message_is_private(m) || owl_message_is_loginout(m)) { |
---|
| 69 | if (!owl_global_is_logging(&g)) return(0); |
---|
| 70 | } else { |
---|
| 71 | if (!owl_global_is_classlogging(&g)) return(0); |
---|
| 72 | } |
---|
[7d4fbcd] | 73 | } |
---|
[15b34fd] | 74 | return(1); |
---|
| 75 | } |
---|
| 76 | |
---|
[42947f1] | 77 | void owl_log_zephyr(owl_message *m, FILE *file) { |
---|
| 78 | char *tmp; |
---|
| 79 | tmp=short_zuser(owl_message_get_sender(m)); |
---|
| 80 | fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
| 81 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m)); |
---|
| 82 | fprintf(file, "\n"); |
---|
| 83 | fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
| 84 | fprintf(file, "From: %s <%s>\n\n", owl_message_get_zsig(m), tmp); |
---|
| 85 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
| 86 | owl_free(tmp); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void owl_log_aim(owl_message *m, FILE *file) { |
---|
| 90 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 91 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 92 | if (owl_message_is_login(m)) |
---|
| 93 | fprintf(file, "LOGIN\n\n"); |
---|
| 94 | else if (owl_message_is_logout(m)) |
---|
| 95 | fprintf(file, "LOGOUT\n\n"); |
---|
| 96 | else |
---|
| 97 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void owl_log_jabber(owl_message *m, FILE *file) { |
---|
| 101 | fprintf(file, "From: <%s> To: <%s>\n",owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 102 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 103 | fprintf(file, "%s\n\n",owl_message_get_body(m)); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void owl_log_generic(owl_message *m, FILE *file) { |
---|
| 107 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 108 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 109 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void owl_log_append(owl_message *m, char *filename) { |
---|
| 113 | FILE *file; |
---|
| 114 | file=fopen(filename, "a"); |
---|
| 115 | if (!file) { |
---|
| 116 | owl_function_error("Unable to open file for logging"); |
---|
| 117 | return; |
---|
| 118 | } |
---|
| 119 | if (owl_message_is_type_zephyr(m)) { |
---|
| 120 | owl_log_zephyr(m, file); |
---|
| 121 | } else if (owl_message_is_type_jabber(m)) { |
---|
| 122 | owl_log_jabber(m, file); |
---|
| 123 | } else if (owl_message_is_type_aim(m)) { |
---|
| 124 | owl_log_aim(m, file); |
---|
| 125 | } else { |
---|
| 126 | owl_log_generic(m, file); |
---|
| 127 | } |
---|
| 128 | fclose(file); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void owl_log_outgoing(owl_message *m) |
---|
[15b34fd] | 132 | { |
---|
| 133 | char filename[MAXPATHLEN], *logpath; |
---|
[42947f1] | 134 | char *to; |
---|
[15b34fd] | 135 | |
---|
[42947f1] | 136 | /* Figure out what path to log to */ |
---|
| 137 | if (owl_message_is_type_zephyr(m)) { |
---|
| 138 | to = short_zuser(owl_message_get_recipient(m)); |
---|
| 139 | } else if (owl_message_is_type_jabber(m)) { |
---|
| 140 | to = owl_sprintf("jabber:%s", owl_message_get_recipient(m)); |
---|
| 141 | } else if (owl_message_is_type_aim(m)) { |
---|
| 142 | char *temp; |
---|
| 143 | temp = owl_aim_normalize_screenname(owl_message_get_recipient(m)); |
---|
| 144 | downstr(temp); |
---|
| 145 | to = owl_sprintf("aim:%s", temp); |
---|
| 146 | owl_free(temp); |
---|
| 147 | } else { |
---|
| 148 | to = owl_sprintf("loopback"); |
---|
| 149 | } |
---|
[7d4fbcd] | 150 | |
---|
[e1c4636] | 151 | /* expand ~ in path names */ |
---|
[42947f1] | 152 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[15b34fd] | 153 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, to); |
---|
[42947f1] | 154 | owl_free(to); |
---|
| 155 | |
---|
| 156 | owl_log_append(m, filename); |
---|
[7d4fbcd] | 157 | |
---|
[e1c4636] | 158 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
| 159 | owl_free(logpath); |
---|
[7d4fbcd] | 160 | |
---|
[42947f1] | 161 | owl_log_append(m, filename); |
---|
[7d4fbcd] | 162 | } |
---|
| 163 | |
---|
[42947f1] | 164 | |
---|
[2b86d14] | 165 | void owl_log_outgoing_zephyr_error(char *to, char *text) |
---|
| 166 | { |
---|
| 167 | FILE *file; |
---|
| 168 | char filename[MAXPATHLEN], *logpath; |
---|
[180cd15] | 169 | char *tobuff, *zwriteline; |
---|
| 170 | owl_message *m; |
---|
| 171 | |
---|
| 172 | /* create a present message so we can pass it to |
---|
| 173 | * owl_log_shouldlog_message() |
---|
| 174 | */ |
---|
| 175 | zwriteline=owl_sprintf("zwrite %s", to); |
---|
| 176 | m=owl_function_make_outgoing_zephyr(text, zwriteline, ""); |
---|
| 177 | owl_free(zwriteline); |
---|
| 178 | if (!owl_log_shouldlog_message(m)) { |
---|
| 179 | owl_message_free(m); |
---|
| 180 | return; |
---|
| 181 | } |
---|
| 182 | owl_message_free(m); |
---|
[2b86d14] | 183 | |
---|
[180cd15] | 184 | /* chop off a local realm */ |
---|
[15b34fd] | 185 | tobuff=short_zuser(to); |
---|
[2b86d14] | 186 | |
---|
| 187 | /* expand ~ in path names */ |
---|
[15b34fd] | 188 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[2b86d14] | 189 | |
---|
| 190 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
| 191 | file=fopen(filename, "a"); |
---|
| 192 | if (!file) { |
---|
| 193 | owl_function_error("Unable to open file for outgoing logging"); |
---|
| 194 | owl_free(logpath); |
---|
[15b34fd] | 195 | owl_free(tobuff); |
---|
[2b86d14] | 196 | return; |
---|
| 197 | } |
---|
| 198 | fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text); |
---|
| 199 | if (text[strlen(text)-1]!='\n') { |
---|
| 200 | fprintf(file, "\n"); |
---|
| 201 | } |
---|
| 202 | fclose(file); |
---|
| 203 | |
---|
| 204 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
| 205 | owl_free(logpath); |
---|
| 206 | file=fopen(filename, "a"); |
---|
| 207 | if (!file) { |
---|
| 208 | owl_function_error("Unable to open file for outgoing logging"); |
---|
[15b34fd] | 209 | owl_free(tobuff); |
---|
[2b86d14] | 210 | return; |
---|
| 211 | } |
---|
| 212 | fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text); |
---|
| 213 | if (text[strlen(text)-1]!='\n') { |
---|
| 214 | fprintf(file, "\n"); |
---|
| 215 | } |
---|
| 216 | fclose(file); |
---|
| 217 | |
---|
| 218 | owl_free(tobuff); |
---|
| 219 | } |
---|
| 220 | |
---|
[15283bb] | 221 | void owl_log_incoming(owl_message *m) |
---|
| 222 | { |
---|
[e1c4636] | 223 | char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath; |
---|
[42947f1] | 224 | char *frombuff=NULL, *from=NULL; |
---|
[7d4fbcd] | 225 | int len, ch, i, personal; |
---|
[12c35df] | 226 | |
---|
[15b34fd] | 227 | /* figure out if it's a "personal" message or not */ |
---|
[aac889a] | 228 | if (owl_message_is_type_zephyr(m)) { |
---|
| 229 | if (owl_message_is_personal(m)) { |
---|
[42947f1] | 230 | personal = 1; |
---|
[aac889a] | 231 | } else { |
---|
[42947f1] | 232 | personal = 0; |
---|
[aac889a] | 233 | } |
---|
[2182be3] | 234 | } else if (owl_message_is_type_jabber(m)) { |
---|
[42947f1] | 235 | /* This needs to be fixed to handle groupchat */ |
---|
| 236 | char* msgtype = owl_message_get_attribute_value(m,"jtype"); |
---|
| 237 | if (msgtype && !strcmp(msgtype,"groupchat")) { |
---|
| 238 | personal = 0; |
---|
| 239 | } else { |
---|
| 240 | personal = 1; |
---|
| 241 | } |
---|
[7d4fbcd] | 242 | } else { |
---|
[79a0e82] | 243 | if (owl_message_is_private(m) || owl_message_is_loginout(m)) { |
---|
[42947f1] | 244 | personal = 1; |
---|
[aac889a] | 245 | } else { |
---|
[42947f1] | 246 | personal = 0; |
---|
[aac889a] | 247 | } |
---|
[7d4fbcd] | 248 | } |
---|
| 249 | |
---|
[2182be3] | 250 | |
---|
[aac889a] | 251 | if (owl_message_is_type_zephyr(m)) { |
---|
| 252 | if (personal) { |
---|
[3066d23] | 253 | from=frombuff=short_zuser(owl_message_get_sender(m)); |
---|
[aac889a] | 254 | } else { |
---|
| 255 | from=frombuff=owl_strdup(owl_message_get_class(m)); |
---|
[7d4fbcd] | 256 | } |
---|
[aac889a] | 257 | } else if (owl_message_is_type_aim(m)) { |
---|
| 258 | /* we do not yet handle chat rooms */ |
---|
[421c286f] | 259 | char *normalto; |
---|
| 260 | normalto=owl_aim_normalize_screenname(owl_message_get_sender(m)); |
---|
| 261 | downstr(normalto); |
---|
| 262 | from=frombuff=owl_sprintf("aim:%s", normalto); |
---|
| 263 | owl_free(normalto); |
---|
[37eab7f] | 264 | } else if (owl_message_is_type_loopback(m)) { |
---|
| 265 | from=frombuff=owl_strdup("loopback"); |
---|
[2182be3] | 266 | } else if (owl_message_is_type_jabber(m)) { |
---|
[5c30091] | 267 | if (personal) { |
---|
| 268 | from=frombuff=owl_sprintf("jabber:%s",owl_message_get_sender(m)); |
---|
| 269 | } else { |
---|
| 270 | from=frombuff=owl_sprintf("jabber:%s",owl_message_get_recipient(m)); |
---|
| 271 | } |
---|
[e6449bc] | 272 | } else { |
---|
| 273 | from=frombuff=owl_strdup("unknown"); |
---|
[7d4fbcd] | 274 | } |
---|
| 275 | |
---|
| 276 | /* check for malicious sender formats */ |
---|
| 277 | len=strlen(frombuff); |
---|
| 278 | if (len<1 || len>35) from="weird"; |
---|
| 279 | if (strchr(frombuff, '/')) from="weird"; |
---|
| 280 | |
---|
| 281 | ch=frombuff[0]; |
---|
| 282 | if (!isalnum(ch)) from="weird"; |
---|
| 283 | |
---|
| 284 | for (i=0; i<len; i++) { |
---|
[e1c4636] | 285 | if (frombuff[i]<'!' || frombuff[i]>='~') from="weird"; |
---|
[7d4fbcd] | 286 | } |
---|
| 287 | |
---|
| 288 | if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird"; |
---|
| 289 | |
---|
| 290 | if (!personal) { |
---|
| 291 | if (strcmp(from, "weird")) downstr(from); |
---|
| 292 | } |
---|
| 293 | |
---|
[e1c4636] | 294 | /* create the filename (expanding ~ in path names) */ |
---|
[7d4fbcd] | 295 | if (personal) { |
---|
[15b34fd] | 296 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[e1c4636] | 297 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
| 298 | snprintf(allfilename, MAXPATHLEN, "%s/all", logpath); |
---|
| 299 | |
---|
[7d4fbcd] | 300 | } else { |
---|
[15b34fd] | 301 | logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", owl_global_get_homedir(&g)); |
---|
[e1c4636] | 302 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
[7d4fbcd] | 303 | } |
---|
[e1c4636] | 304 | owl_free(logpath); |
---|
[42947f1] | 305 | owl_free(frombuff); |
---|
[37eab7f] | 306 | |
---|
[42947f1] | 307 | owl_log_append(m, filename); |
---|
[7d4fbcd] | 308 | |
---|
[42947f1] | 309 | if (personal) |
---|
| 310 | owl_log_append(m, allfilename); |
---|
[7d4fbcd] | 311 | |
---|
| 312 | } |
---|