/***********************************************************************/ /* */ /* mail_archive.c saves mails and removes unnecessary headers */ /* */ /* by Boris Lutz (lutz@.n-r-g.com) 01.04.1999 */ /* */ /***********************************************************************/ /* This application is still in development. The parameters are hard */ /* coded. However I will add suport for an external configuration file */ /* soon. */ /* Don't change the buffer size! */ #define BUFFER_SIZE 3072 #define NUMOFHDRS 3 /* Change this when you add or remove mail files and adresses */ #define NUMOFACCNTS 2 #include #include #include #include #include #include #include void get_alowedheaders(char *input_buf, long in_len, char *output_buf, long *out_len, char *mail_file); int main() { FILE *MailFile; char *mail_buff, *temp_buff, file_name[256]; long bytes_read, temp_len, return_status = 0; mail_buff = (char*) malloc(sizeof(char) * BUFFER_SIZE); temp_buff = (char*) malloc(sizeof(char) * BUFFER_SIZE); bytes_read = fread(mail_buff, sizeof(char), BUFFER_SIZE, stdin); if(bytes_read == 0) { perror("error reading stdin\n"); exit(-39); } get_alowedheaders(mail_buff, bytes_read, temp_buff, &temp_len, file_name); printf("Filename: %s\n", file_name); MailFile = fopen(file_name, "r+"); if(MailFile == (FILE*) NULL) { perror("can not open mailfile\n"); exit(-37); } return_status = fseek(MailFile, 0L, SEEK_END); if(return_status != 0) { perror("can not set r/w pos of mailfile\n"); exit(-35); } fprintf(MailFile, "\n"); return_status = fwrite(temp_buff, sizeof(char), temp_len, MailFile); if(return_status != temp_len) { perror("can not write mailfile\n"); exit(-33); } printf("wrote header\n"); while(!feof(stdin)) { bytes_read = fread(mail_buff, sizeof(char), BUFFER_SIZE, stdin); if(bytes_read == 0) { perror("error reading stdin\n"); exit(-39); } return_status = fwrite(mail_buff, sizeof(char), bytes_read, MailFile); if(return_status != bytes_read) { perror("can not write mailfile\n"); exit(-31); } printf("wrote body\n"); } fprintf(MailFile, "\n------------------------------------------------------------------------------\n"); fclose(MailFile); free(mail_buff); free(temp_buff); return 0; } void get_alowedheaders(char *input_buf, long in_len, char *output_buf, long *out_len, char *mail_file) { long i, j, k, offset, offind; char Alowed_Hdrs[NUMOFHDRS][256] = {"From: ", "Date: ", "Subject: "}, Addr_found = 0, /* put your mailaddresses and mail files here */ Known_Froms[NUMOFACCNTS][256] = {"mailaddress1", "mailaddress2"}; Mail_Dest[NUMOFACCNTS][256] = {"Mail_file1", "Mail_file2"}; *out_len = 0; /* Set your path for saving the mail files here */ strcpy(mail_file, "/home/username/Mail/"); for(i = 0 ; i < in_len; i++) { if(i > 0 && input_buf[i-1] == '\n' && input_buf[i] == '\n') { printf("End of header\n"); memcpy(output_buf + *out_len, input_buf + i, in_len - i); (*out_len) += in_len - i; assert( (*out_len) <= in_len); if(Addr_found == 0) { printf("Unknown sender, using default Mail-File\n"); strcat(mail_file, "Div_Mails"); } return; } for(j = 0; j < NUMOFHDRS; j++) { if( (i + strlen(Alowed_Hdrs[j]) ) < in_len && ( i == 0 || input_buf[i-1] == '\n') && memcmp(Alowed_Hdrs[j], input_buf + i, strlen(Alowed_Hdrs[j]) ) == 0) { printf("Match found\n"); offset = i; while(offset < in_len && input_buf[offset] != '\n') { output_buf[(*out_len)++] = input_buf[offset++]; } output_buf[(*out_len)++] = '\n'; if(j == 0) { for(offind = i; offind < offset && Addr_found == 0; offind++) { for(k = 0; k < NUMOFACCNTS; k++) { if(offind >= (offset - strlen(Known_Froms[k])) ) continue; if(memcmp(Known_Froms[k], input_buf + offind, strlen(Known_Froms[k]) ) == 0) { strcat(mail_file, Mail_Dest[k]); Addr_found = 1; break; } } } } i = offset; } } } printf("SHOULD NOT GET HERE!!! BUFFER_SIZE must be large enough to read header in one peace otherwise it won't work as expected!\n\n"); assert( (*out_len) <= in_len); if(Addr_found == 0) { printf("Unknown sender, using default Mail-File\n"); strcat(mail_file, "Div_Mails"); } return; }