/* scan_file is called once during boot-up. It scans through the mail file and indexes all entries currently in the mail file. */ int scan_file(void) { FILE *mail_file; header_block_type next_block; int total_messages = 0, block_num = 0; char buf[100]; if (!(mail_file = fopen(MAIL_FILE, "r"))) { log("Mail file non-existant... creating new file."); mail_file = fopen(MAIL_FILE, "w"); fclose(mail_file); return 1; } while (fread(&next_block, sizeof(header_block_type), 1, mail_file)) { if (next_block.block_type == HEADER_BLOCK) { index_mail(next_block.header_data.to, block_num * BLOCK_SIZE); total_messages++; } else if (next_block.block_type == DELETED_BLOCK) push_free_list(block_num * BLOCK_SIZE); block_num++; } file_end_pos = ftell(mail_file); fclose(mail_file); sprintf(buf, " %ld bytes read.", file_end_pos); log(buf); if (file_end_pos % BLOCK_SIZE) { log("SYSERR: Error booting mail system -- Mail file corrupt!"); log("SYSERR: Mail disabled!"); return 0; } sprintf(buf, " Mail file read -- %d messages.", total_messages); log(buf); return 1; } /* end of scan_file */
void add_mail(char file_name[]) { FILE *fptr = fopen(file_name, "r"); int date=0,id=0; static char from[NAME_SIZE],to[NAME_SIZE]; if(fptr != NULL) { if(fgets(input_buffer, BUFFER_SIZE, fptr)) { //From: strcpy(from, &input_buffer[6]); } if(fgets(input_buffer, BUFFER_SIZE, fptr)) { //Date: date = get_time(&input_buffer[6]); } if(fgets(input_buffer, BUFFER_SIZE, fptr)) { //Message-ID: id = get_int(&input_buffer[12]); } if(fgets(input_buffer, BUFFER_SIZE, fptr)) { //Subject: index_keywords(id, &input_buffer[9]); } if(fgets(input_buffer, BUFFER_SIZE, fptr)) { //To: strcpy(to, &input_buffer[4]); } from[strlen(from)-1] = STREND; to[strlen(to)-1] = STREND; index_name(id, from, to); index_date(id, date); index_mail(id); if(fgets(input_buffer, BUFFER_SIZE, fptr)) { //Content: while(fgets(input_buffer, BUFFER_SIZE, fptr)) { index_keywords(id, input_buffer); } } p("Mail "); put_int(id); p(" added, you have "); put_int(mail_set.size()); p(" mails"); puts(""); fclose(fptr); } else { p("No file: "); p(file_name); p("!!!!\n"); return; } }
void store_mail(long to, long from, char *message_pointer) { header_block_type header; data_block_type data; long last_address, target_address; char *msg_txt = message_pointer; int bytes_written = 0; int total_length = strlen(message_pointer); int i; if (to == -1) { // "all" - unchanged from mailall.lord.txt for (i = 0; i <= top_of_p_table; i++) store_mail(player_table[i].id, from, message_pointer); return; } assert(sizeof(header_block_type) == sizeof(data_block_type)); assert(sizeof(header_block_type) == BLOCK_SIZE); if (from < 0 || to < 0 || !*message_pointer) { log("SYSERR: Mail system -- non-fatal error #5."); return; } memset((char *) &header, 0, sizeof(header)); /* clear the record */ header.block_type = HEADER_BLOCK; header.header_data.next_block = LAST_BLOCK; header.header_data.from = from; header.header_data.to = to; header.header_data.mail_time = time(0); strncpy(header.txt, msg_txt, HEADER_BLOCK_DATASIZE); header.txt[HEADER_BLOCK_DATASIZE] = '\0'; target_address = pop_free_list(); /* find next free block */ index_mail(to, target_address); /* add it to mail index in memory */ write_to_file(&header, BLOCK_SIZE, target_address); if (strlen(msg_txt) <= HEADER_BLOCK_DATASIZE) return; /* that was the whole message */ bytes_written = HEADER_BLOCK_DATASIZE; msg_txt += HEADER_BLOCK_DATASIZE; /* move pointer to next bit of text */ /* * find the next block address, then rewrite the header to reflect where * the next block is. */ last_address = target_address; target_address = pop_free_list(); header.header_data.next_block = target_address; write_to_file(&header, BLOCK_SIZE, last_address); /* now write the current data block */ memset((char *) &data, 0, sizeof(data)); /* clear the record */ data.block_type = LAST_BLOCK; strncpy(data.txt, msg_txt, DATA_BLOCK_DATASIZE); data.txt[DATA_BLOCK_DATASIZE] = '\0'; write_to_file(&data, BLOCK_SIZE, target_address); bytes_written += strlen(data.txt); msg_txt += strlen(data.txt); /* * if, after 1 header block and 1 data block there is STILL part of the * message left to write to the file, keep writing the new data blocks and * rewriting the old data blocks to reflect where the next block is. Yes, * this is kind of a hack, but if the block size is big enough it won't * matter anyway. Hopefully, MUD players won't pour their life stories out * into the Mud Mail System anyway. * * Note that the block_type data field in data blocks is either a number >=0, * meaning a link to the next block, or LAST_BLOCK flag (-2) meaning the * last block in the current message. This works much like DOS' FAT. */ while (bytes_written < total_length) { last_address = target_address; target_address = pop_free_list(); /* rewrite the previous block to link it to the next */ data.block_type = target_address; write_to_file(&data, BLOCK_SIZE, last_address); /* now write the next block, assuming it's the last. */ data.block_type = LAST_BLOCK; strncpy(data.txt, msg_txt, DATA_BLOCK_DATASIZE); data.txt[DATA_BLOCK_DATASIZE] = '\0'; write_to_file(&data, BLOCK_SIZE, target_address); bytes_written += strlen(data.txt); msg_txt += strlen(data.txt); } } /* store mail */