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