OSDN Git Service

Send Attachment successfully
[tortoisegit/TortoiseGitJp.git] / src / TortoiseProc / Patch.cpp
1 #include "StdAfx.h"\r
2 #include "Patch.h"\r
3 #include "csmtp.h"\r
4 #include "registry.h"\r
5 #include "unicodeutils.h"\r
6 #include "hwsmtp.h"\r
7 #include "Windns.h"\r
8 #include "Git.h"\r
9 \r
10 CPatch::CPatch()\r
11 {\r
12         \r
13 }\r
14 \r
15 CPatch::~CPatch()\r
16 {\r
17         \r
18 \r
19 }\r
20 \r
21 void CPatch::ConvertToArray(CString &to,CStringArray &Array)\r
22 {\r
23         int start=0;\r
24         while(start>=0)\r
25         {\r
26                 CString str=to.Tokenize(_T(";"),start);\r
27                 if(!str.IsEmpty())\r
28                         Array.Add(str);\r
29         }\r
30 }\r
31 \r
32 int CPatch::Send(CString &pathfile,CString &TO,CString &CC,bool bAttachment)\r
33 {\r
34         CSmtp mail;\r
35         \r
36         if(mail.GetLastError() != CSMTP_NO_ERROR )\r
37         {\r
38                 return -1;\r
39         }\r
40         \r
41         if(this->Parser(pathfile)       )\r
42                 return -1;\r
43 \r
44         int at=TO.Find(_T('@'));\r
45         int start =0;\r
46         TO = TO.Tokenize(_T(";"),start);\r
47 \r
48         CString server=TO.Mid(at+1);\r
49 \r
50         PDNS_RECORD pDnsRecord; \r
51 \r
52         DnsQuery(server, DNS_TYPE_MX,DNS_QUERY_BYPASS_CACHE,\r
53                                             NULL,                   //Contains DNS server IP address.\r
54                         &pDnsRecord,                //Resource record that contains the response.\r
55                         NULL); \r
56 \r
57         CString name,address;\r
58         GetNameAddress(this->m_Author,name,address);\r
59 \r
60 \r
61         CStringArray attchments,CCArray;\r
62         if(bAttachment)\r
63         {\r
64                 attchments.Add(pathfile);\r
65         }\r
66         \r
67         ConvertToArray(CC,CCArray);\r
68 \r
69         SendEmail(FALSE,pDnsRecord->Data.MX.pNameExchange,\r
70                 NULL,NULL,FALSE,address,TO,this->m_Author,TO,this->m_Subject,m_strBody,0,&attchments,&CCArray);\r
71                                         \r
72         DnsRecordListFree(pDnsRecord,DnsFreeRecordList);\r
73 \r
74 #if 0\r
75         CRegString server(REG_SMTP_SERVER);\r
76         CRegDWORD  port(REG_SMTP_PORT,25);\r
77         CRegDWORD  bAuth(REG_SMTP_ISAUTH);\r
78         CRegString  user(REG_SMTP_USER);\r
79         CRegString  password(REG_SMTP_PASSWORD);\r
80 \r
81         mail.SetSMTPServer(CUnicodeUtils::GetUTF8(server),port);\r
82 \r
83         AddRecipient(mail,TO,false);\r
84         AddRecipient(mail,CC,true);\r
85 \r
86         if( bAttachment )\r
87                 mail.AddAttachment(CUnicodeUtils::GetUTF8(pathfile));\r
88 \r
89         CString name,address;\r
90         GetNameAddress(this->m_Author,name,address);\r
91         mail.SetSenderName(CUnicodeUtils::GetUTF8(name));\r
92         mail.SetSenderMail(CUnicodeUtils::GetUTF8(address));\r
93 \r
94         mail.SetXPriority(XPRIORITY_NORMAL);\r
95         mail.SetXMailer("The Bat! (v3.02) Professional");\r
96 \r
97         mail.SetSubject(CUnicodeUtils::GetUTF8(this->m_Subject));\r
98 \r
99         mail.SetMessageBody((char*)&this->m_Body[0]);\r
100 \r
101         if(bAuth)\r
102         {\r
103                 mail.SetLogin(CUnicodeUtils::GetUTF8((CString&)user));\r
104                 mail.SetPassword(CUnicodeUtils::GetUTF8((CString&)password));\r
105         }\r
106 \r
107         return !mail.Send();\r
108 #endif\r
109 \r
110 \r
111 }\r
112 \r
113 int CPatch::Parser(CString &pathfile)\r
114 {\r
115         CString str;\r
116 \r
117         CStdioFile PatchFile;\r
118 \r
119         m_PathFile=pathfile;\r
120         if( ! PatchFile.Open(pathfile,CFile::modeRead) )\r
121                 return -1;\r
122         \r
123         int i=0;\r
124         while(PatchFile.ReadString(str))\r
125         {\r
126                 if(i==1)\r
127                         this->m_Author=str.Right( str.GetLength() - 6 );\r
128                 if(i==2)\r
129                         this->m_Date = str.Right( str.GetLength() - 6 );\r
130                 if(i==3)\r
131                         this->m_Subject = str.Right( str.GetLength() - 8 );\r
132                 if(i==4)\r
133                         break;\r
134                 i++;            \r
135         }\r
136 \r
137         m_Body.resize(PatchFile.GetLength() - PatchFile.GetPosition());\r
138         PatchFile.Read(&m_Body.at(0),PatchFile.GetLength() - PatchFile.GetPosition());\r
139         m_Body.push_back(0);\r
140 \r
141         PatchFile.Close();\r
142 \r
143         g_Git.StringAppend(&m_strBody,&m_Body[0],CP_ACP);\r
144         \r
145 \r
146 }\r
147 \r
148 void CPatch::GetNameAddress(CString &in, CString &name,CString &address)\r
149 {\r
150         int start,end;\r
151         start=in.Find(_T('<'));\r
152         end=in.Find(_T('>'));\r
153 \r
154         if(start >=0 && end >=0)\r
155         {\r
156                 name=in.Left(start);\r
157                 address=in.Mid(start+1,end-start-1);\r
158         }\r
159         else\r
160                 address=in;\r
161 }\r
162 \r
163 void CPatch::AddRecipient(CSmtp &mail, CString &tolist, bool isCC)\r
164 {\r
165         int pos=0;\r
166         while(pos>=0)\r
167         {\r
168                 CString one=tolist.Tokenize(_T(";"),pos);\r
169                 int start=one.Find(_T('<'));\r
170                 int end = one.Find(_T('>'));\r
171                 CStringA name;\r
172                 CStringA address;\r
173                 if( start>=0 && end >=0)\r
174                 {\r
175                         name=CUnicodeUtils::GetUTF8(one.Left(start));\r
176                         address=CUnicodeUtils::GetUTF8(one.Mid(start+1,end-start-1));\r
177                         if(address.IsEmpty())\r
178                                 continue;\r
179                         if(isCC)\r
180                                 mail.AddCCRecipient(address,name);\r
181                         else\r
182                                 mail.AddRecipient(address,name);\r
183 \r
184                 }else\r
185                 {\r
186                         if(one.IsEmpty())\r
187                                 continue;\r
188                         if(isCC)\r
189                                 mail.AddCCRecipient(CUnicodeUtils::GetUTF8(one));\r
190                         else\r
191                                 mail.AddRecipient(CUnicodeUtils::GetUTF8(one));\r
192                 }\r
193         }\r
194 }