OSDN Git Service

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