Fixed issue #1264: TortoiseProc might crash if commands are executed w/o a working...
[TortoiseGit.git] / src / TortoiseProc / Patch.cpp
blob1a770a4b1606f25c5be5a3e175bdaeac71f9e5d6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2011 - TortoiseGit
4 // Copyright (C) 2011 Sven Strickroth <email@cs-ware.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "StdAfx.h"
22 #include "Patch.h"
23 #include "csmtp.h"
24 #include "registry.h"
25 #include "unicodeutils.h"
26 #include "hwsmtp.h"
27 #include "mailmsg.h"
28 #include "Windns.h"
29 #include "Git.h"
31 CPatch::CPatch()
35 CPatch::~CPatch()
39 void CPatch::ConvertToArray(CString &to,CStringArray &Array)
41 int start=0;
42 while(start>=0)
44 CString str=to.Tokenize(_T(";"),start);
45 if(!str.IsEmpty())
46 Array.Add(str);
50 int CPatch::Send(CString &pathfile,CString &TO,CString &CC,bool bAttachment, bool useMAPI)
52 if(this->Parser(pathfile))
53 return -1;
55 CString body;
56 CStringArray attachments;
57 if(bAttachment)
59 attachments.Add(pathfile);
61 else
63 body = this->m_strBody;
66 CString errortext = _T("");
67 int ret = SendMail(TO, CC, this->m_Subject, body, attachments, useMAPI, &errortext);
68 this->m_LastError=errortext;
69 return ret;
72 int CPatch::SendPatchesCombined(CTGitPathList &list,CString &To,CString &CC, CString &subject,bool bAttachment, bool useMAPI,CString *errortext)
74 CStringArray attachments;
75 CString body;
76 for(int i=0;i<list.GetCount();i++)
78 CPatch patch;
79 patch.Parser((CString&)list[i].GetWinPathString());
80 if(bAttachment)
82 attachments.Add(list[i].GetWinPathString());
83 body+=patch.m_Subject;
84 body+=_T("\r\n");
86 else
88 g_Git.StringAppend(&body, (BYTE*)patch.m_Body.GetBuffer(), CP_UTF8, patch.m_Body.GetLength());
92 return SendMail(To, CC, subject, body, attachments, useMAPI, errortext);
95 int CPatch::SendMail(CString &To, CString &CC, CString &subject, CString &body, CStringArray &attachments, bool useMAPI, CString *errortext)
97 CString sender;
98 sender.Format(_T("%s <%s>"), g_Git.GetUserName(), g_Git.GetUserEmail());
100 if (useMAPI)
102 CMailMsg mapiSender;
103 BOOL bMAPIInit = mapiSender.MAPIInitialize();
104 if(!bMAPIInit)
106 if(errortext)
107 *errortext = mapiSender.GetLastErrorMsg();
108 return -1;
111 mapiSender.SetShowComposeDialog(TRUE);
112 mapiSender.SetFrom(g_Git.GetUserEmail());
113 mapiSender.SetTo(To);
114 if(!CC.IsEmpty())
115 mapiSender.AddCC(CC);
116 mapiSender.SetSubject(subject);
117 mapiSender.SetMessage(body);
118 for(int i=0; i < attachments.GetSize(); i++)
120 mapiSender.AddAttachment(attachments[i]);
123 BOOL bSend = mapiSender.Send();
124 if (bSend==TRUE)
125 return 0;
126 else
128 if(errortext)
129 *errortext = mapiSender.GetLastErrorMsg();
130 return -1;
133 else
135 CHwSMTP mail;
136 if(mail.SendSpeedEmail(sender,To,subject,body,NULL,&attachments,CC,25,sender))
137 return 0;
138 else
140 if(errortext)
141 *errortext=mail.GetLastErrorText();
142 return -1;
147 int CPatch::Parser(CString &pathfile)
149 CString str;
151 CFile PatchFile;
153 m_PathFile=pathfile;
154 if( ! PatchFile.Open(pathfile,CFile::modeRead) )
155 return -1;
157 #if 0
158 int i=0;
159 while(i<4)
161 PatchFile.ReadString(str);
162 if(i==1)
163 this->m_Author=str.Right( str.GetLength() - 6 );
164 if(i==2)
165 this->m_Date = str.Right( str.GetLength() - 6 );
166 if(i==3)
167 this->m_Subject = str.Right( str.GetLength() - 8 );
169 i++;
172 LONGLONG offset=PatchFile.GetPosition();
173 #endif
174 PatchFile.Read(m_Body.GetBuffer(PatchFile.GetLength()),PatchFile.GetLength());
175 m_Body.ReleaseBuffer();
176 PatchFile.Close();
178 int start=0;
179 CStringA one;
180 one=m_Body.Tokenize("\n",start);
182 one=m_Body.Tokenize("\n",start);
183 if(one.GetLength()>6)
184 g_Git.StringAppend(&m_Author, (BYTE*)one.GetBuffer() + 6, CP_UTF8, one.GetLength() - 6);
186 one=m_Body.Tokenize("\n",start);
187 if(one.GetLength()>6)
188 g_Git.StringAppend(&m_Date, (BYTE*)one.GetBuffer() + 6, CP_UTF8, one.GetLength() - 6);
190 one=m_Body.Tokenize("\n",start);
191 if(one.GetLength()>9)
192 g_Git.StringAppend(&m_Subject, (BYTE*)one.GetBuffer() + 9, CP_UTF8, one.GetLength() - 9);
194 //one=m_Body.Tokenize("\n",start);
196 g_Git.StringAppend(&m_strBody, (BYTE*)m_Body.GetBuffer() + start + 1, CP_UTF8, m_Body.GetLength() - start - 1);
198 return 0;
201 void CPatch::GetNameAddress(CString &in, CString &name,CString &address)
203 int start,end;
204 start=in.Find(_T('<'));
205 end=in.Find(_T('>'));
207 if(start >=0 && end >=0)
209 name=in.Left(start);
210 address=in.Mid(start+1,end-start-1);
212 else
213 address=in;