[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 | |
---|
[15283bb] | 9 | void owl_log_outgoing_zephyr(char *to, char *text) |
---|
| 10 | { |
---|
[7d4fbcd] | 11 | FILE *file; |
---|
[e1c4636] | 12 | char filename[MAXPATHLEN], *logpath; |
---|
[e6449bc] | 13 | char *tobuff, *ptr=""; |
---|
[7d4fbcd] | 14 | |
---|
[12c35df] | 15 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return; |
---|
| 16 | |
---|
[7d4fbcd] | 17 | tobuff=owl_malloc(strlen(to)+20); |
---|
| 18 | strcpy(tobuff, to); |
---|
| 19 | |
---|
| 20 | /* chop off a local realm */ |
---|
| 21 | ptr=strchr(tobuff, '@'); |
---|
[09489b89] | 22 | if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) { |
---|
[7d4fbcd] | 23 | *ptr='\0'; |
---|
| 24 | } |
---|
| 25 | |
---|
[e1c4636] | 26 | /* expand ~ in path names */ |
---|
[e3d9c77] | 27 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
[e1c4636] | 28 | owl_global_get_homedir(&g)); |
---|
| 29 | |
---|
| 30 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
[7d4fbcd] | 31 | file=fopen(filename, "a"); |
---|
| 32 | if (!file) { |
---|
[836ea3a3] | 33 | owl_function_error("Unable to open file for outgoing logging"); |
---|
[e1c4636] | 34 | owl_free(logpath); |
---|
[7d4fbcd] | 35 | return; |
---|
| 36 | } |
---|
| 37 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 38 | if (text[strlen(text)-1]!='\n') { |
---|
| 39 | fprintf(file, "\n"); |
---|
| 40 | } |
---|
| 41 | fclose(file); |
---|
| 42 | |
---|
[e1c4636] | 43 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
| 44 | owl_free(logpath); |
---|
[7d4fbcd] | 45 | file=fopen(filename, "a"); |
---|
| 46 | if (!file) { |
---|
[836ea3a3] | 47 | owl_function_error("Unable to open file for outgoing logging"); |
---|
[7d4fbcd] | 48 | return; |
---|
| 49 | } |
---|
| 50 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 51 | if (text[strlen(text)-1]!='\n') { |
---|
| 52 | fprintf(file, "\n"); |
---|
| 53 | } |
---|
| 54 | fclose(file); |
---|
| 55 | |
---|
| 56 | owl_free(tobuff); |
---|
| 57 | } |
---|
| 58 | |
---|
[37eab7f] | 59 | void owl_log_outgoing_aim(char *to, char *text) |
---|
| 60 | { |
---|
[aac889a] | 61 | FILE *file; |
---|
| 62 | char filename[MAXPATHLEN], *logpath; |
---|
[d559df9] | 63 | char *tobuff; |
---|
[aac889a] | 64 | |
---|
[12c35df] | 65 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return; |
---|
| 66 | |
---|
[aac889a] | 67 | tobuff=owl_sprintf("aim:%s", to); |
---|
| 68 | |
---|
| 69 | /* expand ~ in path names */ |
---|
[e3d9c77] | 70 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
[aac889a] | 71 | owl_global_get_homedir(&g)); |
---|
| 72 | |
---|
| 73 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
| 74 | file=fopen(filename, "a"); |
---|
| 75 | if (!file) { |
---|
[836ea3a3] | 76 | owl_function_error("Unable to open file for outgoing logging"); |
---|
[aac889a] | 77 | owl_free(logpath); |
---|
| 78 | return; |
---|
| 79 | } |
---|
| 80 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 81 | if (text[strlen(text)-1]!='\n') { |
---|
| 82 | fprintf(file, "\n"); |
---|
| 83 | } |
---|
| 84 | fclose(file); |
---|
| 85 | |
---|
| 86 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
| 87 | owl_free(logpath); |
---|
| 88 | file=fopen(filename, "a"); |
---|
| 89 | if (!file) { |
---|
[836ea3a3] | 90 | owl_function_error("Unable to open file for outgoing logging"); |
---|
[aac889a] | 91 | return; |
---|
| 92 | } |
---|
| 93 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 94 | if (text[strlen(text)-1]!='\n') { |
---|
| 95 | fprintf(file, "\n"); |
---|
| 96 | } |
---|
| 97 | fclose(file); |
---|
| 98 | |
---|
| 99 | owl_free(tobuff); |
---|
| 100 | } |
---|
| 101 | |
---|
[37eab7f] | 102 | void owl_log_outgoing_loopback(char *text) |
---|
| 103 | { |
---|
| 104 | FILE *file; |
---|
| 105 | char filename[MAXPATHLEN], *logpath; |
---|
| 106 | char *tobuff; |
---|
| 107 | |
---|
[12c35df] | 108 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_IN) return; |
---|
| 109 | |
---|
| 110 | tobuff=owl_sprintf("loopback"); |
---|
[37eab7f] | 111 | |
---|
| 112 | /* expand ~ in path names */ |
---|
[e3d9c77] | 113 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
[37eab7f] | 114 | owl_global_get_homedir(&g)); |
---|
| 115 | |
---|
| 116 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, tobuff); |
---|
| 117 | file=fopen(filename, "a"); |
---|
| 118 | if (!file) { |
---|
| 119 | owl_function_error("Unable to open file for outgoing logging"); |
---|
| 120 | owl_free(logpath); |
---|
| 121 | return; |
---|
| 122 | } |
---|
| 123 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 124 | if (text[strlen(text)-1]!='\n') { |
---|
| 125 | fprintf(file, "\n"); |
---|
| 126 | } |
---|
| 127 | fclose(file); |
---|
| 128 | |
---|
| 129 | snprintf(filename, MAXPATHLEN, "%s/all", logpath); |
---|
| 130 | owl_free(logpath); |
---|
| 131 | file=fopen(filename, "a"); |
---|
| 132 | if (!file) { |
---|
| 133 | owl_function_error("Unable to open file for outgoing logging"); |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 137 | if (text[strlen(text)-1]!='\n') { |
---|
| 138 | fprintf(file, "\n"); |
---|
| 139 | } |
---|
| 140 | fclose(file); |
---|
| 141 | |
---|
| 142 | owl_free(tobuff); |
---|
| 143 | } |
---|
| 144 | |
---|
[15283bb] | 145 | void owl_log_incoming(owl_message *m) |
---|
| 146 | { |
---|
[7d4fbcd] | 147 | FILE *file, *allfile; |
---|
[e1c4636] | 148 | char filename[MAXPATHLEN], allfilename[MAXPATHLEN], *logpath; |
---|
[e6449bc] | 149 | char *frombuff=NULL, *from=NULL, *buff=NULL, *ptr; |
---|
[7d4fbcd] | 150 | int len, ch, i, personal; |
---|
[12c35df] | 151 | |
---|
| 152 | if (owl_global_get_loggingdirection(&g)==OWL_LOGGING_DIRECTION_OUT) return; |
---|
[d09e5a1] | 153 | |
---|
[7d4fbcd] | 154 | /* check for nolog */ |
---|
| 155 | if (!strcasecmp(owl_message_get_opcode(m), "nolog") || |
---|
| 156 | !strcasecmp(owl_message_get_instance(m), "nolog")) return; |
---|
| 157 | |
---|
[aac889a] | 158 | /* this is kind of a mess */ |
---|
| 159 | if (owl_message_is_type_zephyr(m)) { |
---|
| 160 | if (owl_message_is_personal(m)) { |
---|
| 161 | personal=1; |
---|
| 162 | if (!owl_global_is_logging(&g)) return; |
---|
| 163 | } else { |
---|
| 164 | personal=0; |
---|
| 165 | if (!owl_global_is_classlogging(&g)) return; |
---|
| 166 | } |
---|
[7d4fbcd] | 167 | } else { |
---|
[79a0e82] | 168 | if (owl_message_is_private(m) || owl_message_is_loginout(m)) { |
---|
[aac889a] | 169 | personal=1; |
---|
| 170 | if (!owl_global_is_logging(&g)) return; |
---|
| 171 | } else { |
---|
| 172 | personal=0; |
---|
| 173 | if (!owl_global_is_classlogging(&g)) return; |
---|
| 174 | } |
---|
[7d4fbcd] | 175 | } |
---|
| 176 | |
---|
[aac889a] | 177 | if (owl_message_is_type_zephyr(m)) { |
---|
| 178 | /* chop off a local realm for personal zephyr messages */ |
---|
| 179 | if (personal) { |
---|
| 180 | if (owl_message_is_type_zephyr(m)) { |
---|
| 181 | from=frombuff=owl_strdup(owl_message_get_sender(m)); |
---|
| 182 | ptr=strchr(frombuff, '@'); |
---|
[09489b89] | 183 | if (ptr && !strncmp(ptr+1, owl_zephyr_get_realm(), strlen(owl_zephyr_get_realm()))) { |
---|
[aac889a] | 184 | *ptr='\0'; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | } else { |
---|
| 188 | /* we assume zephyr for now */ |
---|
| 189 | from=frombuff=owl_strdup(owl_message_get_class(m)); |
---|
[7d4fbcd] | 190 | } |
---|
[aac889a] | 191 | } else if (owl_message_is_type_aim(m)) { |
---|
| 192 | /* we do not yet handle chat rooms */ |
---|
| 193 | from=frombuff=owl_sprintf("aim:%s", owl_message_get_sender(m)); |
---|
[37eab7f] | 194 | } else if (owl_message_is_type_loopback(m)) { |
---|
| 195 | from=frombuff=owl_strdup("loopback"); |
---|
[e6449bc] | 196 | } else { |
---|
| 197 | from=frombuff=owl_strdup("unknown"); |
---|
[7d4fbcd] | 198 | } |
---|
| 199 | |
---|
| 200 | /* check for malicious sender formats */ |
---|
| 201 | len=strlen(frombuff); |
---|
| 202 | if (len<1 || len>35) from="weird"; |
---|
| 203 | if (strchr(frombuff, '/')) from="weird"; |
---|
| 204 | |
---|
| 205 | ch=frombuff[0]; |
---|
| 206 | if (!isalnum(ch)) from="weird"; |
---|
| 207 | |
---|
| 208 | for (i=0; i<len; i++) { |
---|
[e1c4636] | 209 | if (frombuff[i]<'!' || frombuff[i]>='~') from="weird"; |
---|
[7d4fbcd] | 210 | } |
---|
| 211 | |
---|
| 212 | if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird"; |
---|
| 213 | |
---|
| 214 | if (!personal) { |
---|
| 215 | if (strcmp(from, "weird")) downstr(from); |
---|
| 216 | } |
---|
| 217 | |
---|
[e1c4636] | 218 | /* create the filename (expanding ~ in path names) */ |
---|
[7d4fbcd] | 219 | if (personal) { |
---|
[e3d9c77] | 220 | logpath = owl_text_substitute(owl_global_get_logpath(&g), "~", |
---|
[e1c4636] | 221 | owl_global_get_homedir(&g)); |
---|
| 222 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
| 223 | snprintf(allfilename, MAXPATHLEN, "%s/all", logpath); |
---|
| 224 | |
---|
[7d4fbcd] | 225 | } else { |
---|
[e3d9c77] | 226 | logpath = owl_text_substitute(owl_global_get_classlogpath(&g), "~", |
---|
[e1c4636] | 227 | owl_global_get_homedir(&g)); |
---|
| 228 | |
---|
| 229 | snprintf(filename, MAXPATHLEN, "%s/%s", logpath, from); |
---|
[7d4fbcd] | 230 | } |
---|
[e1c4636] | 231 | owl_free(logpath); |
---|
[7d4fbcd] | 232 | |
---|
| 233 | file=fopen(filename, "a"); |
---|
| 234 | if (!file) { |
---|
[836ea3a3] | 235 | owl_function_error("Unable to open file for incoming logging"); |
---|
[7d4fbcd] | 236 | return; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | allfile=NULL; |
---|
| 240 | if (personal) { |
---|
| 241 | allfile=fopen(allfilename, "a"); |
---|
| 242 | if (!allfile) { |
---|
[836ea3a3] | 243 | owl_function_error("Unable to open file for incoming logging"); |
---|
[79a0e82] | 244 | fclose(file); |
---|
[7d4fbcd] | 245 | return; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
[aac889a] | 249 | /* write to the main file */ |
---|
| 250 | if (owl_message_is_type_zephyr(m)) { |
---|
[e6449bc] | 251 | char *tmp; |
---|
| 252 | |
---|
[aac889a] | 253 | tmp=short_zuser(owl_message_get_sender(m)); |
---|
| 254 | fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
| 255 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m)); |
---|
| 256 | fprintf(file, "\n"); |
---|
| 257 | fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
| 258 | ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i); |
---|
| 259 | buff=owl_malloc(i+10); |
---|
| 260 | memcpy(buff, ptr, i); |
---|
| 261 | buff[i]='\0'; |
---|
| 262 | fprintf(file, "From: %s <%s>\n\n", buff, tmp); |
---|
[269ed34] | 263 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
[e6449bc] | 264 | owl_free(tmp); |
---|
[79a0e82] | 265 | } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) { |
---|
[aac889a] | 266 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 267 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 268 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
[79a0e82] | 269 | } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) { |
---|
| 270 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 271 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 272 | if (owl_message_is_login(m)) fprintf(file, "LOGIN\n\n"); |
---|
| 273 | if (owl_message_is_logout(m)) fprintf(file, "LOGOUT\n\n"); |
---|
[37eab7f] | 274 | } else { |
---|
| 275 | fprintf(file, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 276 | fprintf(file, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 277 | fprintf(file, "%s\n\n", owl_message_get_body(m)); |
---|
[aac889a] | 278 | } |
---|
[37eab7f] | 279 | |
---|
[7d4fbcd] | 280 | fclose(file); |
---|
| 281 | |
---|
[aac889a] | 282 | /* if it's a personal message, also write to the 'all' file */ |
---|
[7d4fbcd] | 283 | if (personal) { |
---|
[aac889a] | 284 | if (owl_message_is_type_zephyr(m)) { |
---|
[e6449bc] | 285 | char *tmp; |
---|
| 286 | |
---|
| 287 | tmp=short_zuser(owl_message_get_sender(m)); |
---|
[aac889a] | 288 | fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
| 289 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m)); |
---|
| 290 | fprintf(allfile, "\n"); |
---|
| 291 | fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
| 292 | fprintf(allfile, "From: %s <%s>\n\n", buff, tmp); |
---|
[269ed34] | 293 | fprintf(allfile, "%s\n\n", owl_message_get_body(m)); |
---|
[e6449bc] | 294 | owl_free(tmp); |
---|
[79a0e82] | 295 | } else if (owl_message_is_type_aim(m) && !owl_message_is_loginout(m)) { |
---|
| 296 | fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 297 | fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 298 | fprintf(allfile, "%s\n\n", owl_message_get_body(m)); |
---|
| 299 | } else if (owl_message_is_type_aim(m) && owl_message_is_loginout(m)) { |
---|
| 300 | fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 301 | fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 302 | if (owl_message_is_login(m)) fprintf(allfile, "LOGIN\n\n"); |
---|
| 303 | if (owl_message_is_logout(m)) fprintf(allfile, "LOGOUT\n\n"); |
---|
[37eab7f] | 304 | } else { |
---|
[eec69e1] | 305 | fprintf(allfile, "From: <%s> To: <%s>\n", owl_message_get_sender(m), owl_message_get_recipient(m)); |
---|
| 306 | fprintf(allfile, "Time: %s\n\n", owl_message_get_timestr(m)); |
---|
| 307 | fprintf(allfile, "%s\n\n", owl_message_get_body(m)); |
---|
[aac889a] | 308 | } |
---|
[723c427] | 309 | fclose(allfile); |
---|
[7d4fbcd] | 310 | } |
---|
| 311 | |
---|
[aac889a] | 312 | if (owl_message_is_type_zephyr(m)) { |
---|
| 313 | owl_free(buff); |
---|
| 314 | } |
---|
[7d4fbcd] | 315 | owl_free(frombuff); |
---|
| 316 | } |
---|