Fixed some unused variables warnings
[TortoiseGit.git] / src / TortoiseProc / Patch.cpp
blob1447a3ef5157005957d98fd3b0087b1bbfc515c6
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 "mailmsg.h"
8 #include "Windns.h"
9 #include "Git.h"
11 CPatch::CPatch()
16 CPatch::~CPatch()
22 void CPatch::ConvertToArray(CString &to,CStringArray &Array)
24 int start=0;
25 while(start>=0)
27 CString str=to.Tokenize(_T(";"),start);
28 if(!str.IsEmpty())
29 Array.Add(str);
33 int CPatch::Send(CString &pathfile,CString &TO,CString &CC,bool bAttachment, bool useMAPI)
35 if(this->Parser(pathfile) )
36 return -1;
38 CStringArray attachments;
39 if(bAttachment)
41 attachments.Add(pathfile);
44 CString sender;
45 sender.Format(_T("%s <%s> "),g_Git.GetUserName(),g_Git.GetUserEmail());
47 if (useMAPI)
49 CMailMsg mapiSender;
50 BOOL bMAPIInit = mapiSender.MAPIInitialize();
51 if(!bMAPIInit)
53 m_LastError = mapiSender.GetLastErrorMsg();
54 return -1;
57 mapiSender.SetShowComposeDialog(TRUE);
58 mapiSender.SetFrom(g_Git.GetUserEmail());
59 mapiSender.SetTo(TO);
60 mapiSender.SetSubject(m_Subject);
61 mapiSender.SetMessage(m_strBody);
62 if(bAttachment)
63 mapiSender.AddAttachment(pathfile);
65 BOOL bSend = mapiSender.Send();
66 if (bSend==TRUE)
67 return 0;
68 else
70 m_LastError = mapiSender.GetLastErrorMsg();
71 return -1;
74 else
76 CHwSMTP mail;
77 if(mail.SendSpeedEmail(this->m_Author,TO,this->m_Subject,this->m_strBody,NULL,&attachments,CC,25,sender))
78 return 0;
79 else
81 this->m_LastError=mail.GetLastErrorText();
82 return -1;
85 #if 0
86 CRegString server(REG_SMTP_SERVER);
87 CRegDWORD port(REG_SMTP_PORT,25);
88 CRegDWORD bAuth(REG_SMTP_ISAUTH);
89 CRegString user(REG_SMTP_USER);
90 CRegString password(REG_SMTP_PASSWORD);
92 mail.SetSMTPServer(CUnicodeUtils::GetUTF8(server),port);
94 AddRecipient(mail,TO,false);
95 AddRecipient(mail,CC,true);
97 if( bAttachment )
98 mail.AddAttachment(CUnicodeUtils::GetUTF8(pathfile));
100 CString name,address;
101 GetNameAddress(this->m_Author,name,address);
102 mail.SetSenderName(CUnicodeUtils::GetUTF8(name));
103 mail.SetSenderMail(CUnicodeUtils::GetUTF8(address));
105 mail.SetXPriority(XPRIORITY_NORMAL);
106 mail.SetXMailer("The Bat! (v3.02) Professional");
108 mail.SetSubject(CUnicodeUtils::GetUTF8(this->m_Subject));
110 mail.SetMessageBody((char*)&this->m_Body[0]);
112 if(bAuth)
114 mail.SetLogin(CUnicodeUtils::GetUTF8((CString&)user));
115 mail.SetPassword(CUnicodeUtils::GetUTF8((CString&)password));
118 return !mail.Send();
119 #endif
123 int CPatch::Send(CTGitPathList &list,CString &To,CString &CC, CString &subject,bool bAttachment, bool useMAPI,CString *errortext)
125 CStringArray attachments;
126 CString body;
127 for(int i=0;i<list.GetCount();i++)
129 CPatch patch;
130 patch.Parser((CString&)list[i].GetWinPathString());
131 if(bAttachment)
133 attachments.Add(list[i].GetWinPathString());
134 body+=patch.m_Subject;
135 body+=_T("\r\n");
137 else
139 g_Git.StringAppend(&body,(BYTE*)patch.m_Body.GetBuffer(),CP_ACP,patch.m_Body.GetLength());
144 CString sender;
145 sender.Format(_T("%s <%s> "),g_Git.GetUserName(),g_Git.GetUserEmail());
147 if (useMAPI)
149 CMailMsg mapiSender;
150 BOOL bMAPIInit = mapiSender.MAPIInitialize();
151 if(!bMAPIInit)
153 if(errortext)
154 *errortext = mapiSender.GetLastErrorMsg();
155 return -1;
158 mapiSender.SetShowComposeDialog(TRUE);
159 mapiSender.SetFrom(g_Git.GetUserEmail());
160 mapiSender.SetTo(To);
161 mapiSender.SetSubject(subject);
162 mapiSender.SetMessage(body);
163 for(int i=0; i < attachments.GetSize(); i++)
165 mapiSender.AddAttachment(attachments[i]);
168 BOOL bSend = mapiSender.Send();
169 if (bSend==TRUE)
170 return 0;
171 else
173 if(errortext)
174 *errortext = mapiSender.GetLastErrorMsg();
175 return -1;
178 else
180 CHwSMTP mail;
181 if(mail.SendSpeedEmail(sender,To,subject,body,NULL,&attachments,CC,25,sender))
182 return 0;
183 else
185 if(errortext)
186 *errortext=mail.GetLastErrorText();
187 return -1;
192 int CPatch::Parser(CString &pathfile)
194 CString str;
196 CFile PatchFile;
198 m_PathFile=pathfile;
199 if( ! PatchFile.Open(pathfile,CFile::modeRead) )
200 return -1;
202 #if 0
203 int i=0;
204 while(i<4)
205 { PatchFile.ReadString(str);
206 if(i==1)
207 this->m_Author=str.Right( str.GetLength() - 6 );
208 if(i==2)
209 this->m_Date = str.Right( str.GetLength() - 6 );
210 if(i==3)
211 this->m_Subject = str.Right( str.GetLength() - 8 );
213 i++;
216 LONGLONG offset=PatchFile.GetPosition();
217 #endif
218 PatchFile.Read(m_Body.GetBuffer(PatchFile.GetLength()),PatchFile.GetLength());
219 m_Body.ReleaseBuffer();
220 PatchFile.Close();
222 int start=0;
223 CStringA one;
224 one=m_Body.Tokenize("\n",start);
226 one=m_Body.Tokenize("\n",start);
227 if(one.GetLength()>6)
228 g_Git.StringAppend(&m_Author,(BYTE*)one.GetBuffer()+6,CP_ACP,one.GetLength()-6);
230 one=m_Body.Tokenize("\n",start);
231 if(one.GetLength()>6)
232 g_Git.StringAppend(&m_Date,(BYTE*)one.GetBuffer()+6,CP_ACP,one.GetLength()-6);
234 one=m_Body.Tokenize("\n",start);
235 if(one.GetLength()>8)
236 g_Git.StringAppend(&m_Subject,(BYTE*)one.GetBuffer()+8,CP_ACP,one.GetLength()-8);
238 //one=m_Body.Tokenize("\n",start);
240 g_Git.StringAppend(&m_strBody,(BYTE*)m_Body.GetBuffer()+start+1,CP_ACP,m_Body.GetLength()-start-1);
242 return 0;
245 void CPatch::GetNameAddress(CString &in, CString &name,CString &address)
247 int start,end;
248 start=in.Find(_T('<'));
249 end=in.Find(_T('>'));
251 if(start >=0 && end >=0)
253 name=in.Left(start);
254 address=in.Mid(start+1,end-start-1);
256 else
257 address=in;
259 #if 0
260 void CPatch::AddRecipient(CSmtp &mail, CString &tolist, bool isCC)
262 int pos=0;
263 while(pos>=0)
265 CString one=tolist.Tokenize(_T(";"),pos);
266 int start=one.Find(_T('<'));
267 int end = one.Find(_T('>'));
268 CStringA name;
269 CStringA address;
270 if( start>=0 && end >=0)
272 name=CUnicodeUtils::GetUTF8(one.Left(start));
273 address=CUnicodeUtils::GetUTF8(one.Mid(start+1,end-start-1));
274 if(address.IsEmpty())
275 continue;
276 if(isCC)
277 mail.AddCCRecipient(address,name);
278 else
279 mail.AddRecipient(address,name);
281 }else
283 if(one.IsEmpty())
284 continue;
285 if(isCC)
286 mail.AddCCRecipient(CUnicodeUtils::GetUTF8(one));
287 else
288 mail.AddRecipient(CUnicodeUtils::GetUTF8(one));
292 #endif