OSDN Git Service

Send Attachment successfully
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / Patch.cpp
index c5622c4..7bebda9 100644 (file)
@@ -1,5 +1,11 @@
 #include "StdAfx.h"\r
 #include "Patch.h"\r
+#include "csmtp.h"\r
+#include "registry.h"\r
+#include "unicodeutils.h"\r
+#include "hwsmtp.h"\r
+#include "Windns.h"\r
+#include "Git.h"\r
 \r
 CPatch::CPatch()\r
 {\r
@@ -12,6 +18,98 @@ CPatch::~CPatch()
 \r
 }\r
 \r
+void CPatch::ConvertToArray(CString &to,CStringArray &Array)\r
+{\r
+       int start=0;\r
+       while(start>=0)\r
+       {\r
+               CString str=to.Tokenize(_T(";"),start);\r
+               if(!str.IsEmpty())\r
+                       Array.Add(str);\r
+       }\r
+}\r
+\r
+int CPatch::Send(CString &pathfile,CString &TO,CString &CC,bool bAttachment)\r
+{\r
+       CSmtp mail;\r
+       \r
+       if(mail.GetLastError() != CSMTP_NO_ERROR )\r
+       {\r
+               return -1;\r
+       }\r
+       \r
+       if(this->Parser(pathfile)       )\r
+               return -1;\r
+\r
+       int at=TO.Find(_T('@'));\r
+       int start =0;\r
+       TO = TO.Tokenize(_T(";"),start);\r
+\r
+       CString server=TO.Mid(at+1);\r
+\r
+       PDNS_RECORD pDnsRecord; \r
+\r
+       DnsQuery(server, DNS_TYPE_MX,DNS_QUERY_BYPASS_CACHE,\r
+                                           NULL,                   //Contains DNS server IP address.\r
+                        &pDnsRecord,                //Resource record that contains the response.\r
+                        NULL); \r
+\r
+       CString name,address;\r
+       GetNameAddress(this->m_Author,name,address);\r
+\r
+\r
+       CStringArray attchments,CCArray;\r
+       if(bAttachment)\r
+       {\r
+               attchments.Add(pathfile);\r
+       }\r
+       \r
+       ConvertToArray(CC,CCArray);\r
+\r
+       SendEmail(FALSE,pDnsRecord->Data.MX.pNameExchange,\r
+               NULL,NULL,FALSE,address,TO,this->m_Author,TO,this->m_Subject,m_strBody,0,&attchments,&CCArray);\r
+                                       \r
+       DnsRecordListFree(pDnsRecord,DnsFreeRecordList);\r
+\r
+#if 0\r
+       CRegString server(REG_SMTP_SERVER);\r
+       CRegDWORD  port(REG_SMTP_PORT,25);\r
+       CRegDWORD  bAuth(REG_SMTP_ISAUTH);\r
+       CRegString  user(REG_SMTP_USER);\r
+       CRegString  password(REG_SMTP_PASSWORD);\r
+\r
+       mail.SetSMTPServer(CUnicodeUtils::GetUTF8(server),port);\r
+\r
+       AddRecipient(mail,TO,false);\r
+       AddRecipient(mail,CC,true);\r
+\r
+       if( bAttachment )\r
+               mail.AddAttachment(CUnicodeUtils::GetUTF8(pathfile));\r
+\r
+       CString name,address;\r
+       GetNameAddress(this->m_Author,name,address);\r
+       mail.SetSenderName(CUnicodeUtils::GetUTF8(name));\r
+       mail.SetSenderMail(CUnicodeUtils::GetUTF8(address));\r
+\r
+       mail.SetXPriority(XPRIORITY_NORMAL);\r
+       mail.SetXMailer("The Bat! (v3.02) Professional");\r
+\r
+       mail.SetSubject(CUnicodeUtils::GetUTF8(this->m_Subject));\r
+\r
+       mail.SetMessageBody((char*)&this->m_Body[0]);\r
+\r
+       if(bAuth)\r
+       {\r
+               mail.SetLogin(CUnicodeUtils::GetUTF8((CString&)user));\r
+               mail.SetPassword(CUnicodeUtils::GetUTF8((CString&)password));\r
+       }\r
+\r
+       return !mail.Send();\r
+#endif\r
+\r
+\r
+}\r
+\r
 int CPatch::Parser(CString &pathfile)\r
 {\r
        CString str;\r
@@ -36,7 +134,61 @@ int CPatch::Parser(CString &pathfile)
                i++;            \r
        }\r
 \r
+       m_Body.resize(PatchFile.GetLength() - PatchFile.GetPosition());\r
+       PatchFile.Read(&m_Body.at(0),PatchFile.GetLength() - PatchFile.GetPosition());\r
+       m_Body.push_back(0);\r
 \r
        PatchFile.Close();\r
 \r
+       g_Git.StringAppend(&m_strBody,&m_Body[0],CP_ACP);\r
+       \r
+\r
+}\r
+\r
+void CPatch::GetNameAddress(CString &in, CString &name,CString &address)\r
+{\r
+       int start,end;\r
+       start=in.Find(_T('<'));\r
+       end=in.Find(_T('>'));\r
+\r
+       if(start >=0 && end >=0)\r
+       {\r
+               name=in.Left(start);\r
+               address=in.Mid(start+1,end-start-1);\r
+       }\r
+       else\r
+               address=in;\r
+}\r
+\r
+void CPatch::AddRecipient(CSmtp &mail, CString &tolist, bool isCC)\r
+{\r
+       int pos=0;\r
+       while(pos>=0)\r
+       {\r
+               CString one=tolist.Tokenize(_T(";"),pos);\r
+               int start=one.Find(_T('<'));\r
+               int end = one.Find(_T('>'));\r
+               CStringA name;\r
+               CStringA address;\r
+               if( start>=0 && end >=0)\r
+               {\r
+                       name=CUnicodeUtils::GetUTF8(one.Left(start));\r
+                       address=CUnicodeUtils::GetUTF8(one.Mid(start+1,end-start-1));\r
+                       if(address.IsEmpty())\r
+                               continue;\r
+                       if(isCC)\r
+                               mail.AddCCRecipient(address,name);\r
+                       else\r
+                               mail.AddRecipient(address,name);\r
+\r
+               }else\r
+               {\r
+                       if(one.IsEmpty())\r
+                               continue;\r
+                       if(isCC)\r
+                               mail.AddCCRecipient(CUnicodeUtils::GetUTF8(one));\r
+                       else\r
+                               mail.AddRecipient(CUnicodeUtils::GetUTF8(one));\r
+               }\r
+       }\r
 }
\ No newline at end of file