String MessageUtilities::GetSendersIP(boost::shared_ptr<Message> pMessage) { const String fileName = PersistentMessage::GetFileName(pMessage); AnsiString sHeader = PersistentMessage::LoadHeader(fileName); MimeHeader oHeader; oHeader.Load(sHeader, sHeader.GetLength(), true); // Locate the first Received header MimeField *pReceivedHeader = oHeader.GetField("Received"); if (pReceivedHeader == 0) return "127.0.0.1"; // Now we should try to find the IP in the received header. String sReceivedValue = pReceivedHeader->GetValue(); int iAddressStart = sReceivedValue.Find(_T("[")) +1; int iAddressEnd = sReceivedValue.Find(_T("]"), iAddressStart); int iAddressLen = iAddressEnd - iAddressStart; if (iAddressLen <= 0) return "127.0.0.1"; String sIPAddress = sReceivedValue.Mid(iAddressStart, iAddressLen); if (!StringParser::IsValidIPAddress(sIPAddress)) return "127.0.0.1"; return sIPAddress; }
void SMTPVacationMessageCreator::CreateVacationMessage(shared_ptr<const Account> recipientAccount, const String &sToAddress, const String &sVacationSubject, const String &sVacationMessage, const shared_ptr<Message> pOriginalMessage) { if (!_CanSendVacationMessage(recipientAccount->GetAddress(), sToAddress)) return; // We have already notified this user. LOG_DEBUG("Creating out-of-office message."); String sModifiedSubject = sVacationSubject; String sModifiedBody = sVacationMessage; const String originalFileName = PersistentMessage::GetFileName(recipientAccount, pOriginalMessage); if (sModifiedSubject.Find(_T("%")) >= 0 || sModifiedBody.Find(_T("%")) >= 0) { // Replace macros in the string. MimeHeader header; AnsiString sHeader = PersistentMessage::LoadHeader(originalFileName); header.Load(sHeader, sHeader.GetLength(), true); String sOldSubject; if (header.GetField("Subject")) sOldSubject = header.GetField("Subject")->GetValue(); sModifiedSubject.ReplaceNoCase(_T("%SUBJECT%"), sOldSubject); sModifiedBody.ReplaceNoCase(_T("%SUBJECT%"), sOldSubject); } if (sModifiedSubject.GetLength() == 0) { // Parse out the subject in the original // message, so that we can Re: that.. MimeHeader header; AnsiString sHeader = PersistentMessage::LoadHeader(originalFileName); header.Load(sHeader, sHeader.GetLength(), true); String sOldSubject; if (header.GetField("Subject")) sOldSubject = header.GetField("Subject")->GetValue(); sModifiedSubject = "Re: " + sOldSubject; } // Send a copy of this email. shared_ptr<Message> pMsg = shared_ptr<Message>(new Message()); pMsg->SetState(Message::Delivering); pMsg->SetFromAddress(recipientAccount->GetAddress()); const String newFileName = PersistentMessage::GetFileName(pMsg); shared_ptr<MessageData> pNewMsgData = shared_ptr<MessageData>(new MessageData()); pNewMsgData->LoadFromMessage(newFileName, pMsg); // Required headers pNewMsgData->SetReturnPath(recipientAccount->GetAddress()); pNewMsgData->GenerateMessageID(); pNewMsgData->SetSentTime(Time::GetCurrentMimeDate()); pNewMsgData->SetFieldValue("Content-Type", "text/plain; charset=\"utf-8\""); // Optional headers pNewMsgData->SetFrom(recipientAccount->GetAddress()); pNewMsgData->SetTo(sToAddress); pNewMsgData->SetSubject(sModifiedSubject); pNewMsgData->SetBody(sModifiedBody); pNewMsgData->SetAutoReplied(); pNewMsgData->IncreaseRuleLoopCount(); // Write message data pNewMsgData->Write(newFileName); // Add recipients. bool recipientOK = false; RecipientParser recipientParser; recipientParser.CreateMessageRecipientList(sToAddress, pMsg->GetRecipients(), recipientOK); // Save message if (pMsg->GetRecipients()->GetCount() > 0) PersistentMessage::SaveObject(pMsg); // Tell app to submit mail Application::Instance()->SubmitPendingEmail(); }