Bump version to 6.4-15
[LibreOffice.git] / tools / source / inet / inetmsg.cxx
blob918746eeaea59f0c15f0101d1cb199abed302203
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <tools/datetime.hxx>
22 #include <tools/inetmsg.hxx>
23 #include <comphelper/string.hxx>
24 #include <rtl/character.hxx>
26 #include <map>
28 void INetMIMEMessage::SetHeaderField_Impl (
29 const OString &rName,
30 const OUString &rValue,
31 sal_uInt32 &rnIndex)
33 SetHeaderField_Impl (
34 INetMessageHeader (rName, rValue.toUtf8()), rnIndex);
37 /* ParseDateField and local helper functions.
39 * Parses a String in (implied) GMT format into class Date and tools::Time objects.
40 * Four formats are accepted:
42 * [Wkd,] 1*2DIGIT Mon 2*4DIGIT 00:00:00 [GMT] (rfc1123)
43 * [Wkd,] 00 Mon 0000 00:00:00 [GMT]) (rfc822, rfc1123)
44 * Weekday, 00-Mon-00 00:00:00 [GMT] (rfc850, rfc1036)
45 * Wkd Mon 00 00:00:00 0000 [GMT] (ctime)
46 * 1*DIGIT (delta seconds)
49 static const sal_Char *months[12] =
51 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
52 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
55 static sal_uInt16 ParseNumber(const OString& rStr, sal_Int32& nIndex)
57 sal_Int32 n = nIndex;
58 while ((n < rStr.getLength())
59 && rtl::isAsciiDigit(static_cast<unsigned char>(rStr[n])))
60 n++;
62 OString aNum(rStr.copy(nIndex, (n - nIndex)));
63 nIndex = n;
65 return static_cast<sal_uInt16>(aNum.toInt32());
68 static sal_uInt16 ParseMonth(const OString& rStr, sal_Int32& nIndex)
70 sal_Int32 n = nIndex;
71 while ((n < rStr.getLength())
72 && rtl::isAsciiAlpha(static_cast<unsigned char>(rStr[n])))
73 n++;
75 OString aMonth(rStr.copy(nIndex, 3));
76 nIndex = n;
78 sal_uInt16 i;
79 for (i = 0; i < 12; i++)
80 if (aMonth.equalsIgnoreAsciiCase(months[i])) break;
81 return (i + 1);
84 bool INetMIMEMessage::ParseDateField (
85 const OUString& rDateFieldW, DateTime& rDateTime)
87 OString aDateField(OUStringToOString(rDateFieldW,
88 RTL_TEXTENCODING_ASCII_US));
90 if (aDateField.isEmpty()) return false;
92 if (aDateField.indexOf(':') != -1)
94 // Some DateTime format.
95 sal_Int32 nIndex = 0;
97 // Skip over <Wkd> or <Weekday>, leading and trailing space.
98 while ((nIndex < aDateField.getLength()) &&
99 (aDateField[nIndex] == ' '))
100 nIndex++;
102 while (
103 (nIndex < aDateField.getLength()) &&
104 (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField[nIndex])) ||
105 (aDateField[nIndex] == ',') ))
106 nIndex++;
108 while ((nIndex < aDateField.getLength()) &&
109 (aDateField[nIndex] == ' '))
110 nIndex++;
112 if (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField[nIndex])))
114 // Format: ctime().
115 if ((aDateField.getLength() - nIndex) < 20) return false;
117 rDateTime.SetMonth (ParseMonth (aDateField, nIndex)); nIndex++;
118 rDateTime.SetDay (ParseNumber (aDateField, nIndex)); nIndex++;
120 rDateTime.SetHour (ParseNumber (aDateField, nIndex)); nIndex++;
121 rDateTime.SetMin (ParseNumber (aDateField, nIndex)); nIndex++;
122 rDateTime.SetSec (ParseNumber (aDateField, nIndex)); nIndex++;
123 rDateTime.SetNanoSec (0);
125 sal_uInt16 nYear = ParseNumber (aDateField, nIndex);
126 if (nYear < 100) nYear += 1900;
127 rDateTime.SetYear (nYear);
129 else
131 // Format: RFC1036 or RFC1123.
132 if ((aDateField.getLength() - nIndex) < 17) return false;
134 rDateTime.SetDay (ParseNumber (aDateField, nIndex)); nIndex++;
135 rDateTime.SetMonth (ParseMonth (aDateField, nIndex)); nIndex++;
137 sal_uInt16 nYear = ParseNumber (aDateField, nIndex); nIndex++;
138 if (nYear < 100) nYear += 1900;
139 rDateTime.SetYear (nYear);
141 rDateTime.SetHour (ParseNumber (aDateField, nIndex)); nIndex++;
142 rDateTime.SetMin (ParseNumber (aDateField, nIndex)); nIndex++;
143 rDateTime.SetSec (ParseNumber (aDateField, nIndex)); nIndex++;
144 rDateTime.SetNanoSec (0);
146 const char cPossiblePlusMinus = nIndex < aDateField.getLength() ? aDateField[nIndex] : 0;
147 if (cPossiblePlusMinus == '+' || cPossiblePlusMinus == '-')
149 // Offset from GMT: "(+|-)HHMM".
150 bool bEast = (aDateField[nIndex++] == '+');
151 sal_uInt16 nOffset = ParseNumber (aDateField, nIndex);
152 if (nOffset > 0)
154 tools::Time aDiff( tools::Time::EMPTY );
155 aDiff.SetHour (nOffset / 100);
156 aDiff.SetMin (nOffset % 100);
157 aDiff.SetSec (0);
158 aDiff.SetNanoSec (0);
160 if (bEast)
161 rDateTime -= aDiff;
162 else
163 rDateTime += aDiff;
168 else if (comphelper::string::isdigitAsciiString(aDateField))
170 // Format: delta seconds.
171 tools::Time aDelta (0);
172 aDelta.SetTime (aDateField.toInt32() * 100);
174 DateTime aNow( DateTime::SYSTEM );
175 aNow += aDelta;
176 aNow.ConvertToUTC();
178 rDateTime.SetDate (aNow.GetDate());
179 rDateTime.SetTime (aNow.GetTime());
181 else
183 // Junk.
184 return false;
187 return (rDateTime.IsValidAndGregorian() &&
188 !((rDateTime.GetSec() > 59) ||
189 (rDateTime.GetMin() > 59) ||
190 (rDateTime.GetHour() > 23) ));
193 static const std::map<InetMessageMime, const char*> ImplINetMIMEMessageHeaderData =
195 { InetMessageMime::VERSION, "MIME-Version"},
196 { InetMessageMime::CONTENT_DISPOSITION, "Content-Disposition"},
197 { InetMessageMime::CONTENT_TYPE, "Content-Type"},
198 { InetMessageMime::CONTENT_TRANSFER_ENCODING, "Content-Transfer-Encoding"}
201 INetMIMEMessage::INetMIMEMessage()
202 : pParent(nullptr)
204 for (sal_uInt16 i = 0; i < static_cast<int>(InetMessageMime::NUMHDR); i++)
205 m_nMIMEIndex[static_cast<InetMessageMime>(i)] = SAL_MAX_UINT32;
208 INetMIMEMessage::~INetMIMEMessage()
212 void INetMIMEMessage::SetMIMEVersion (const OUString& rVersion)
214 SetHeaderField_Impl (
215 ImplINetMIMEMessageHeaderData.at(InetMessageMime::VERSION), rVersion,
216 m_nMIMEIndex[InetMessageMime::VERSION]);
219 void INetMIMEMessage::SetContentDisposition (const OUString& rDisposition)
221 SetHeaderField_Impl (
222 ImplINetMIMEMessageHeaderData.at(InetMessageMime::CONTENT_DISPOSITION), rDisposition,
223 m_nMIMEIndex[InetMessageMime::CONTENT_DISPOSITION]);
226 void INetMIMEMessage::SetContentType (const OUString& rType)
228 SetHeaderField_Impl (
229 ImplINetMIMEMessageHeaderData.at(InetMessageMime::CONTENT_TYPE), rType,
230 m_nMIMEIndex[InetMessageMime::CONTENT_TYPE]);
233 void INetMIMEMessage::SetContentTransferEncoding (
234 const OUString& rEncoding)
236 SetHeaderField_Impl (
237 ImplINetMIMEMessageHeaderData.at(InetMessageMime::CONTENT_TRANSFER_ENCODING), rEncoding,
238 m_nMIMEIndex[InetMessageMime::CONTENT_TRANSFER_ENCODING]);
241 OUString INetMIMEMessage::GetDefaultContentType()
243 if (pParent != nullptr)
245 OUString aParentCT (pParent->GetContentType());
246 if (aParentCT.isEmpty())
247 aParentCT = pParent->GetDefaultContentType();
249 if (aParentCT.equalsIgnoreAsciiCase("multipart/digest"))
250 return "message/rfc822";
252 return "text/plain; charset=us-ascii";
255 void INetMIMEMessage::EnableAttachMultipartFormDataChild()
257 // Check context.
258 if (IsContainer())
259 return;
261 // Generate a unique boundary from current time.
262 sal_Char sTail[16 + 1];
263 tools::Time aCurTime( tools::Time::SYSTEM );
264 sal_uInt64 nThis = reinterpret_cast< sal_uIntPtr >( this ); // we can be on a 64bit architecture
265 nThis = ( ( nThis >> 32 ) ^ nThis ) & SAL_MAX_UINT32;
266 sprintf (sTail, "%08X%08X",
267 static_cast< unsigned int >(aCurTime.GetTime()),
268 static_cast< unsigned int >(nThis));
269 m_aBoundary = "------------_4D48";
270 m_aBoundary += sTail;
272 // Set header fields.
273 SetMIMEVersion("1.0");
274 SetContentType(
275 OUString::fromUtf8("multipart/form-data; boundary=" + m_aBoundary));
276 SetContentTransferEncoding("7bit");
279 void INetMIMEMessage::AttachChild(std::unique_ptr<INetMIMEMessage> pChildMsg)
281 assert(IsContainer());
282 if (IsContainer())
284 pChildMsg->pParent = this;
285 aChildren.push_back( std::move(pChildMsg) );
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */