Bool HandleMail(char *ptr, long len) { long index; WORD num_recipients, i; char message[MAXMESSAGE + MAX_SUBJECT + 200]; char* msg = message; char sender[MAXUSERNAME + 1]; char recipients[MAX_RECIPIENTS][MAXUSERNAME + 1]; ID resource_id; long msg_time; char *start = ptr; Extract(&ptr, &index, 4); len -= 4; len = ExtractString(&ptr, len, sender, MAXUSERNAME); if (len == -1) return False; Extract(&ptr, &msg_time, SIZE_TIME); Extract(&ptr, &num_recipients, SIZE_NUM_RECIPIENTS); len -= SIZE_TIME + SIZE_NUM_RECIPIENTS; // If somehow mail message is screwed up, delete it if (num_recipients > MAX_RECIPIENTS) { RequestDeleteMail(index); return False; } /* If no recipients, then there is no more mail */ if (num_recipients == 0) { MailNewMessage(0, sender, 0, NULL, NULL, 0); return True; } for (i=0; i < num_recipients; i++) { len = ExtractString(&ptr, len, recipients[i], MAXUSERNAME); if (len == -1) return False; } Extract(&ptr, &resource_id, SIZE_ID); len -= SIZE_ID; /* Remove format string id # & other ids from length */ if (!CheckServerMessage(&msg, &ptr, len, resource_id)) return False; MailNewMessage(index, sender, num_recipients, recipients, msg, msg_time); return True; }
/* * MailNewMessage: We've received a new message from the server. Format it * and save it. If read mail dialog is up, update the dialog's message list. */ void MailNewMessage(int server_index, char *sender, int num_recipients, char recipients[MAX_RECIPIENTS][MAXUSERNAME + 1], char *message, long msg_time) { int index, num_msgs, msgnum; char new_msg[MAXMAIL * 2 + 200 + MAX_SUBJECT + MAXUSERNAME * MAX_RECIPIENTS]; char *subject = NULL, *ptr = NULL, *subject_str; int i, num; char filename[FILENAME_MAX + MAX_PATH]; char date[MAXDATE]; MailHeader header; Bool subject_found = True; if (recipients == NULL) { /* No new mail */ if (hReadMailDlg != NULL) SendMessage(hReadMailDlg, BK_NONEWMAIL, 0, 0); return; } // Format message into new_msg /* Add To: and From: fields to message */ sprintf(new_msg, "%s%s\r\n%s", GetString(hInst, IDS_FROM), sender, GetString(hInst, IDS_TO)); /* Add recipients' names */ num = 0; for (i = 0; i < num_recipients; i++) { if (num++ > 0) strcat(new_msg, ", "); strcat(new_msg, recipients[i]); } strcat(new_msg, "\r\n"); /* Take subject field out of main part of message */ subject_str = GetString(hInst, IDS_SUBJECT_ENGLISH); if (strncmp(message, subject_str, strlen(subject_str))) { subject_str = GetString(hInst, IDS_SUBJECT_GERMAN); if (strncmp(message, subject_str, strlen(subject_str))) { subject_found = False; subject = ""; } } if (subject_found) { /* Skip "Subject: " leader */ subject = message + strlen(subject_str); /* Skip subject line; have to deal with \n (from users) or \r\n (from kod resources) */ ptr = strchr(subject, '\n'); if (ptr != NULL) { if (ptr != subject && *(ptr - 1) == '\r') *(ptr - 1) = 0; *ptr = 0; message = ptr + 1; } } /* Add "Subject:" field to message */ strcat(new_msg, subject_str); strcat(new_msg, subject); strcat(new_msg, "\r\n"); /* Add "Date:" field to message */ date[0] = 0; if (DateFromSeconds(msg_time, date) == True) { strcat(new_msg, GetString(hInst, IDS_DATE)); strcat(new_msg, date); } strcat(new_msg, "\r\n-------------\r\n"); // Add the message, replacing single '\n' with '\r\n' char newline_msg[MAXMAIL * 2]; char *newline_ptr = newline_msg; while (message[0] != '\0') { // if we already have \r\n, don't make it \r\r\n if (message[0] == '\r' && message[1] == '\n') { newline_ptr[0] = message[0]; message++; newline_ptr++; } else if (message[0] == '\n') { newline_ptr[0] = '\r'; newline_ptr++; } newline_ptr[0] = message[0]; newline_ptr++; message++; } newline_ptr[0] = 0; strcat(new_msg, newline_msg); // Save message /* Set number of message to one more than current highest message */ num_msgs = ListBox_GetCount(hMsgList); if (num_msgs == 0) msgnum = 1; else msgnum = ListBox_GetItemData(hMsgList, num_msgs - 1) + 1; /* If we save message ok, tell server that it can delete message */ if (MailSaveMessage(new_msg, msgnum) == True) RequestDeleteMail(server_index); sprintf(filename, "%s\\%04d.msg", MAIL_DIR, msgnum); // Store filename in main part of list box; message number in itemdata field index = ListBox_AddString(hMsgList, filename); ListBox_SetItemData(hMsgList, index, msgnum); if (hReadMailDlg != NULL && MailParseMessageHeader(msgnum, filename, &header)) SendMessage(hReadMailDlg, BK_NEWMAIL, msgnum, (LPARAM) &header); }