BrowseRefs: Context menu enhancements
[TortoiseGit.git] / src / TortoiseProc / Patch.cpp
blobbdf20e23b03887e9ce6a59fee9b0326e83ae9a59
1 #include "StdAfx.h"
2 #include "Patch.h"
3 #include "csmtp.h"
4 #include "registry.h"
5 #include "unicodeutils.h"
6 #include "hwsmtp.h"
7 #include "Windns.h"
8 #include "Git.h"
10 CPatch::CPatch()
15 CPatch::~CPatch()
21 void CPatch::ConvertToArray(CString &to,CStringArray &Array)
23 int start=0;
24 while(start>=0)
26 CString str=to.Tokenize(_T(";"),start);
27 if(!str.IsEmpty())
28 Array.Add(str);
32 int CPatch::Send(CString &pathfile,CString &TO,CString &CC,bool bAttachment)
34 CHwSMTP mail;
35 if(this->Parser(pathfile) )
36 return -1;
38 CStringArray attachments,CCArray;
39 if(bAttachment)
41 attachments.Add(pathfile);
44 //ConvertToArray(CC,CCArray);
46 CString sender;
47 sender.Format(_T("%s <%s> "),g_Git.GetUserName(),g_Git.GetUserEmail());
49 if(mail.SendSpeedEmail(this->m_Author,TO,this->m_Subject,this->m_strBody,NULL,&attachments,CC,25,sender))
50 return 0;
51 else
53 this->m_LastError=mail.GetLastErrorText();
54 return -1;
56 #if 0
57 CRegString server(REG_SMTP_SERVER);
58 CRegDWORD port(REG_SMTP_PORT,25);
59 CRegDWORD bAuth(REG_SMTP_ISAUTH);
60 CRegString user(REG_SMTP_USER);
61 CRegString password(REG_SMTP_PASSWORD);
63 mail.SetSMTPServer(CUnicodeUtils::GetUTF8(server),port);
65 AddRecipient(mail,TO,false);
66 AddRecipient(mail,CC,true);
68 if( bAttachment )
69 mail.AddAttachment(CUnicodeUtils::GetUTF8(pathfile));
71 CString name,address;
72 GetNameAddress(this->m_Author,name,address);
73 mail.SetSenderName(CUnicodeUtils::GetUTF8(name));
74 mail.SetSenderMail(CUnicodeUtils::GetUTF8(address));
76 mail.SetXPriority(XPRIORITY_NORMAL);
77 mail.SetXMailer("The Bat! (v3.02) Professional");
79 mail.SetSubject(CUnicodeUtils::GetUTF8(this->m_Subject));
81 mail.SetMessageBody((char*)&this->m_Body[0]);
83 if(bAuth)
85 mail.SetLogin(CUnicodeUtils::GetUTF8((CString&)user));
86 mail.SetPassword(CUnicodeUtils::GetUTF8((CString&)password));
89 return !mail.Send();
90 #endif
94 int CPatch::Send(CTGitPathList &list,CString &To,CString &CC, CString &subject,bool bAttachment,CString *errortext)
96 CStringArray attachments;
97 CString body;
98 for(int i=0;i<list.GetCount();i++)
100 CPatch patch;
101 patch.Parser((CString&)list[i].GetWinPathString());
102 if(bAttachment)
104 attachments.Add(list[i].GetWinPathString());
105 body+=patch.m_Subject;
106 body+=_T("\r\n");
108 }else
110 g_Git.StringAppend(&body,(BYTE*)patch.m_Body.GetBuffer(),CP_ACP,patch.m_Body.GetLength());
115 CHwSMTP mail;
117 CString sender;
118 sender.Format(_T("%s <%s> "),g_Git.GetUserName(),g_Git.GetUserEmail());
120 if(mail.SendSpeedEmail(sender,To,subject,body,NULL,&attachments,CC,25,sender))
121 return 0;
122 else
124 if(errortext)
125 *errortext=mail.GetLastErrorText();
126 return -1;
131 int CPatch::Parser(CString &pathfile)
133 CString str;
135 CFile PatchFile;
137 m_PathFile=pathfile;
138 if( ! PatchFile.Open(pathfile,CFile::modeRead) )
139 return -1;
141 int i=0;
142 #if 0
143 while(i<4)
144 { PatchFile.ReadString(str);
145 if(i==1)
146 this->m_Author=str.Right( str.GetLength() - 6 );
147 if(i==2)
148 this->m_Date = str.Right( str.GetLength() - 6 );
149 if(i==3)
150 this->m_Subject = str.Right( str.GetLength() - 8 );
152 i++;
155 LONGLONG offset=PatchFile.GetPosition();
156 #endif
157 PatchFile.Read(m_Body.GetBuffer(PatchFile.GetLength()),PatchFile.GetLength());
158 m_Body.ReleaseBuffer();
159 PatchFile.Close();
161 int start=0;
162 CStringA one;
163 one=m_Body.Tokenize("\n",start);
165 one=m_Body.Tokenize("\n",start);
166 if(one.GetLength()>6)
167 g_Git.StringAppend(&m_Author,(BYTE*)one.GetBuffer()+6,CP_ACP,one.GetLength()-6);
169 one=m_Body.Tokenize("\n",start);
170 if(one.GetLength()>6)
171 g_Git.StringAppend(&m_Date,(BYTE*)one.GetBuffer()+6,CP_ACP,one.GetLength()-6);
173 one=m_Body.Tokenize("\n",start);
174 if(one.GetLength()>8)
175 g_Git.StringAppend(&m_Subject,(BYTE*)one.GetBuffer()+8,CP_ACP,one.GetLength()-8);
177 //one=m_Body.Tokenize("\n",start);
179 g_Git.StringAppend(&m_strBody,(BYTE*)m_Body.GetBuffer()+start+1,CP_ACP,m_Body.GetLength()-start-1);
181 return 0;
184 void CPatch::GetNameAddress(CString &in, CString &name,CString &address)
186 int start,end;
187 start=in.Find(_T('<'));
188 end=in.Find(_T('>'));
190 if(start >=0 && end >=0)
192 name=in.Left(start);
193 address=in.Mid(start+1,end-start-1);
195 else
196 address=in;
198 #if 0
199 void CPatch::AddRecipient(CSmtp &mail, CString &tolist, bool isCC)
201 int pos=0;
202 while(pos>=0)
204 CString one=tolist.Tokenize(_T(";"),pos);
205 int start=one.Find(_T('<'));
206 int end = one.Find(_T('>'));
207 CStringA name;
208 CStringA address;
209 if( start>=0 && end >=0)
211 name=CUnicodeUtils::GetUTF8(one.Left(start));
212 address=CUnicodeUtils::GetUTF8(one.Mid(start+1,end-start-1));
213 if(address.IsEmpty())
214 continue;
215 if(isCC)
216 mail.AddCCRecipient(address,name);
217 else
218 mail.AddRecipient(address,name);
220 }else
222 if(one.IsEmpty())
223 continue;
224 if(isCC)
225 mail.AddCCRecipient(CUnicodeUtils::GetUTF8(one));
226 else
227 mail.AddRecipient(CUnicodeUtils::GetUTF8(one));
231 #endif