[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 | |
---|
[7d4fbcd] | 9 | void owl_log_outgoing(char *to, char *text) { |
---|
| 10 | FILE *file; |
---|
| 11 | char filename[MAXPATHLEN]; |
---|
| 12 | char *tobuff, *ptr; |
---|
| 13 | |
---|
| 14 | tobuff=owl_malloc(strlen(to)+20); |
---|
| 15 | strcpy(tobuff, to); |
---|
| 16 | |
---|
| 17 | /* chop off a local realm */ |
---|
| 18 | ptr=strchr(tobuff, '@'); |
---|
| 19 | if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) { |
---|
| 20 | *ptr='\0'; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | sprintf(filename, "%s/zlog/people/%s", owl_global_get_homedir(&g), tobuff); |
---|
| 24 | file=fopen(filename, "a"); |
---|
| 25 | if (!file) { |
---|
| 26 | owl_function_makemsg("Unable to open file for outgoing logging"); |
---|
| 27 | return; |
---|
| 28 | } |
---|
| 29 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 30 | if (text[strlen(text)-1]!='\n') { |
---|
| 31 | fprintf(file, "\n"); |
---|
| 32 | } |
---|
| 33 | fclose(file); |
---|
| 34 | |
---|
| 35 | sprintf(filename, "%s/zlog/people/all", owl_global_get_homedir(&g)); |
---|
| 36 | file=fopen(filename, "a"); |
---|
| 37 | if (!file) { |
---|
| 38 | owl_function_makemsg("Unable to open file for outgoing logging"); |
---|
| 39 | return; |
---|
| 40 | } |
---|
| 41 | fprintf(file, "OUTGOING (owl): %s\n%s\n", tobuff, text); |
---|
| 42 | if (text[strlen(text)-1]!='\n') { |
---|
| 43 | fprintf(file, "\n"); |
---|
| 44 | } |
---|
| 45 | fclose(file); |
---|
| 46 | |
---|
| 47 | owl_free(tobuff); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | void owl_log_incoming(owl_message *m) { |
---|
| 51 | FILE *file, *allfile; |
---|
| 52 | char filename[MAXPATHLEN], allfilename[MAXPATHLEN]; |
---|
| 53 | char *frombuff, *ptr, *from, *buff, *tmp; |
---|
| 54 | int len, ch, i, personal; |
---|
| 55 | |
---|
| 56 | /* check for nolog */ |
---|
| 57 | if (!strcasecmp(owl_message_get_opcode(m), "nolog") || |
---|
| 58 | !strcasecmp(owl_message_get_instance(m), "nolog")) return; |
---|
| 59 | |
---|
| 60 | if (owl_message_is_personal(m)) { |
---|
| 61 | personal=1; |
---|
| 62 | if (!owl_global_is_logging(&g)) return; |
---|
| 63 | } else { |
---|
| 64 | personal=0; |
---|
| 65 | if (!owl_global_is_classlogging(&g)) return; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | if (personal) { |
---|
| 69 | from=frombuff=owl_strdup(owl_message_get_sender(m)); |
---|
| 70 | /* chop off a local realm */ |
---|
| 71 | ptr=strchr(frombuff, '@'); |
---|
| 72 | if (ptr && !strncmp(ptr+1, ZGetRealm(), strlen(ZGetRealm()))) { |
---|
| 73 | *ptr='\0'; |
---|
| 74 | } |
---|
| 75 | } else { |
---|
| 76 | from=frombuff=owl_strdup(owl_message_get_class(m)); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /* check for malicious sender formats */ |
---|
| 80 | len=strlen(frombuff); |
---|
| 81 | if (len<1 || len>35) from="weird"; |
---|
| 82 | if (strchr(frombuff, '/')) from="weird"; |
---|
| 83 | |
---|
| 84 | ch=frombuff[0]; |
---|
| 85 | if (!isalnum(ch)) from="weird"; |
---|
| 86 | |
---|
| 87 | for (i=0; i<len; i++) { |
---|
| 88 | if (frombuff[i]<'!' || frombuff[i]>'~') from="weird"; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | if (!strcmp(frombuff, ".") || !strcasecmp(frombuff, "..")) from="weird"; |
---|
| 92 | |
---|
| 93 | if (!personal) { |
---|
| 94 | if (strcmp(from, "weird")) downstr(from); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /* create the filename */ |
---|
| 98 | if (personal) { |
---|
| 99 | sprintf(filename, "%s/%s", owl_global_get_logpath(&g), from); |
---|
| 100 | sprintf(allfilename, "%s/all", owl_global_get_logpath(&g)); |
---|
| 101 | } else { |
---|
| 102 | sprintf(filename, "%s/%s", owl_global_get_classlogpath(&g), from); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | file=fopen(filename, "a"); |
---|
| 106 | if (!file) { |
---|
| 107 | owl_function_makemsg("Unable to open file for incoming logging"); |
---|
| 108 | return; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | allfile=NULL; |
---|
| 112 | if (personal) { |
---|
| 113 | allfile=fopen(allfilename, "a"); |
---|
| 114 | if (!allfile) { |
---|
| 115 | owl_function_makemsg("Unable to open file for incoming logging"); |
---|
| 116 | return; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | tmp=pretty_sender(owl_message_get_sender(m)); |
---|
| 121 | |
---|
| 122 | fprintf(file, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
| 123 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(file, " Opcode: %s", owl_message_get_opcode(m)); |
---|
| 124 | fprintf(file, "\n"); |
---|
| 125 | fprintf(file, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
| 126 | ptr=owl_zephyr_get_zsig(owl_message_get_notice(m), &i); |
---|
| 127 | buff=owl_malloc(i+10); |
---|
| 128 | memcpy(buff, ptr, i); |
---|
| 129 | buff[i]='\0'; |
---|
| 130 | fprintf(file, "From: %s <%s>\n\n", buff, tmp); |
---|
| 131 | fprintf(file, "%s\n", owl_message_get_body(m)); |
---|
| 132 | fclose(file); |
---|
| 133 | |
---|
| 134 | if (personal) { |
---|
| 135 | fprintf(allfile, "Class: %s Instance: %s", owl_message_get_class(m), owl_message_get_instance(m)); |
---|
| 136 | if (strcmp(owl_message_get_opcode(m), "")) fprintf(allfile, " Opcode: %s", owl_message_get_opcode(m)); |
---|
| 137 | fprintf(allfile, "\n"); |
---|
| 138 | fprintf(allfile, "Time: %s Host: %s\n", owl_message_get_timestr(m), owl_message_get_hostname(m)); |
---|
| 139 | fprintf(allfile, "From: %s <%s>\n\n", buff, tmp); |
---|
| 140 | fprintf(allfile, "%s\n", owl_message_get_body(m)); |
---|
| 141 | fclose(allfile); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | owl_free(tmp); |
---|
| 145 | owl_free(buff); |
---|
| 146 | owl_free(frombuff); |
---|
| 147 | } |
---|