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