Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / tools / source / inet / inetmsg.cxx
blob3a3a9c297ed943795328c42dd9284f2358d1522e
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 <tools/contnr.hxx>
24 #include <rtl/instance.hxx>
25 #include <comphelper/string.hxx>
26 #include <rtl/character.hxx>
28 #include <map>
30 void INetMIMEMessage::SetHeaderField_Impl (
31 const OString &rName,
32 const OUString &rValue,
33 sal_uInt32 &rnIndex)
35 SetHeaderField_Impl (
36 INetMessageHeader (rName, rValue.toUtf8()), rnIndex);
39 /* ParseDateField and local helper functions.
41 * Parses a String in (implied) GMT format into class Date and tools::Time objects.
42 * Four formats are accepted:
44 * [Wkd,] 1*2DIGIT Mon 2*4DIGIT 00:00:00 [GMT] (rfc1123)
45 * [Wkd,] 00 Mon 0000 00:00:00 [GMT]) (rfc822, rfc1123)
46 * Weekday, 00-Mon-00 00:00:00 [GMT] (rfc850, rfc1036)
47 * Wkd Mon 00 00:00:00 0000 [GMT] (ctime)
48 * 1*DIGIT (delta seconds)
51 static const sal_Char *months[12] =
53 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
54 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
57 static sal_uInt16 ParseNumber(const OString& rStr, sal_Int32& nIndex)
59 sal_Int32 n = nIndex;
60 while ((n < rStr.getLength())
61 && rtl::isAsciiDigit(static_cast<unsigned char>(rStr[n])))
62 n++;
64 OString aNum(rStr.copy(nIndex, (n - nIndex)));
65 nIndex = n;
67 return static_cast<sal_uInt16>(aNum.toInt32());
70 static sal_uInt16 ParseMonth(const OString& rStr, sal_Int32& nIndex)
72 sal_Int32 n = nIndex;
73 while ((n < rStr.getLength())
74 && rtl::isAsciiAlpha(static_cast<unsigned char>(rStr[n])))
75 n++;
77 OString aMonth(rStr.copy(nIndex, 3));
78 nIndex = n;
80 sal_uInt16 i;
81 for (i = 0; i < 12; i++)
82 if (aMonth.equalsIgnoreAsciiCase(months[i])) break;
83 return (i + 1);
86 bool INetMIMEMessage::ParseDateField (
87 const OUString& rDateFieldW, DateTime& rDateTime)
89 OString aDateField(OUStringToOString(rDateFieldW,
90 RTL_TEXTENCODING_ASCII_US));
92 if (aDateField.isEmpty()) return false;
94 if (aDateField.indexOf(':') != -1)
96 // Some DateTime format.
97 sal_Int32 nIndex = 0;
99 // Skip over <Wkd> or <Weekday>, leading and trailing space.
100 while ((nIndex < aDateField.getLength()) &&
101 (aDateField[nIndex] == ' '))
102 nIndex++;
104 while (
105 (nIndex < aDateField.getLength()) &&
106 (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField[nIndex])) ||
107 (aDateField[nIndex] == ',') ))
108 nIndex++;
110 while ((nIndex < aDateField.getLength()) &&
111 (aDateField[nIndex] == ' '))
112 nIndex++;
114 if (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField[nIndex])))
116 // Format: ctime().
117 if ((aDateField.getLength() - nIndex) < 20) return false;
119 rDateTime.SetMonth (ParseMonth (aDateField, nIndex)); nIndex++;
120 rDateTime.SetDay (ParseNumber (aDateField, nIndex)); nIndex++;
122 rDateTime.SetHour (ParseNumber (aDateField, nIndex)); nIndex++;
123 rDateTime.SetMin (ParseNumber (aDateField, nIndex)); nIndex++;
124 rDateTime.SetSec (ParseNumber (aDateField, nIndex)); nIndex++;
125 rDateTime.SetNanoSec (0);
127 sal_uInt16 nYear = ParseNumber (aDateField, nIndex);
128 if (nYear < 100) nYear += 1900;
129 rDateTime.SetYear (nYear);
131 else
133 // Format: RFC1036 or RFC1123.
134 if ((aDateField.getLength() - nIndex) < 17) return false;
136 rDateTime.SetDay (ParseNumber (aDateField, nIndex)); nIndex++;
137 rDateTime.SetMonth (ParseMonth (aDateField, nIndex)); nIndex++;
139 sal_uInt16 nYear = ParseNumber (aDateField, nIndex); nIndex++;
140 if (nYear < 100) nYear += 1900;
141 rDateTime.SetYear (nYear);
143 rDateTime.SetHour (ParseNumber (aDateField, nIndex)); nIndex++;
144 rDateTime.SetMin (ParseNumber (aDateField, nIndex)); nIndex++;
145 rDateTime.SetSec (ParseNumber (aDateField, nIndex)); nIndex++;
146 rDateTime.SetNanoSec (0);
148 const char cPossiblePlusMinus = nIndex < aDateField.getLength() ? aDateField[nIndex] : 0;
149 if (cPossiblePlusMinus == '+' || cPossiblePlusMinus == '-')
151 // Offset from GMT: "(+|-)HHMM".
152 bool bEast = (aDateField[nIndex++] == '+');
153 sal_uInt16 nOffset = ParseNumber (aDateField, nIndex);
154 if (nOffset > 0)
156 tools::Time aDiff( tools::Time::EMPTY );
157 aDiff.SetHour (nOffset / 100);
158 aDiff.SetMin (nOffset % 100);
159 aDiff.SetSec (0);
160 aDiff.SetNanoSec (0);
162 if (bEast)
163 rDateTime -= aDiff;
164 else
165 rDateTime += aDiff;
170 else if (comphelper::string::isdigitAsciiString(aDateField))
172 // Format: delta seconds.
173 tools::Time aDelta (0);
174 aDelta.SetTime (aDateField.toInt32() * 100);
176 DateTime aNow( DateTime::SYSTEM );
177 aNow += aDelta;
178 aNow.ConvertToUTC();
180 rDateTime.SetDate (aNow.GetDate());
181 rDateTime.SetTime (aNow.GetTime());
183 else
185 // Junk.
186 return false;
189 return (rDateTime.IsValidAndGregorian() &&
190 !((rDateTime.GetSec() > 59) ||
191 (rDateTime.GetMin() > 59) ||
192 (rDateTime.GetHour() > 23) ));
195 static const std::map<InetMessageMime, const char*> ImplINetMIMEMessageHeaderData =
197 { InetMessageMime::VERSION, "MIME-Version"},
198 { InetMessageMime::CONTENT_DISPOSITION, "Content-Disposition"},
199 { InetMessageMime::CONTENT_TYPE, "Content-Type"},
200 { InetMessageMime::CONTENT_TRANSFER_ENCODING, "Content-Transfer-Encoding"}
203 INetMIMEMessage::INetMIMEMessage()
204 : pParent(nullptr)
206 for (sal_uInt16 i = 0; i < static_cast<int>(InetMessageMime::NUMHDR); i++)
207 m_nMIMEIndex[static_cast<InetMessageMime>(i)] = SAL_MAX_UINT32;
210 INetMIMEMessage::~INetMIMEMessage()
214 void INetMIMEMessage::SetMIMEVersion (const OUString& rVersion)
216 SetHeaderField_Impl (
217 ImplINetMIMEMessageHeaderData.at(InetMessageMime::VERSION), rVersion,
218 m_nMIMEIndex[InetMessageMime::VERSION]);
221 void INetMIMEMessage::SetContentDisposition (const OUString& rDisposition)
223 SetHeaderField_Impl (
224 ImplINetMIMEMessageHeaderData.at(InetMessageMime::CONTENT_DISPOSITION), rDisposition,
225 m_nMIMEIndex[InetMessageMime::CONTENT_DISPOSITION]);
228 void INetMIMEMessage::SetContentType (const OUString& rType)
230 SetHeaderField_Impl (
231 ImplINetMIMEMessageHeaderData.at(InetMessageMime::CONTENT_TYPE), rType,
232 m_nMIMEIndex[InetMessageMime::CONTENT_TYPE]);
235 void INetMIMEMessage::SetContentTransferEncoding (
236 const OUString& rEncoding)
238 SetHeaderField_Impl (
239 ImplINetMIMEMessageHeaderData.at(InetMessageMime::CONTENT_TRANSFER_ENCODING), rEncoding,
240 m_nMIMEIndex[InetMessageMime::CONTENT_TRANSFER_ENCODING]);
243 OUString INetMIMEMessage::GetDefaultContentType()
245 if (pParent != nullptr)
247 OUString aParentCT (pParent->GetContentType());
248 if (aParentCT.isEmpty())
249 aParentCT = pParent->GetDefaultContentType();
251 if (aParentCT.equalsIgnoreAsciiCase("multipart/digest"))
252 return OUString("message/rfc822");
254 return OUString("text/plain; charset=us-ascii");
257 void INetMIMEMessage::EnableAttachMultipartFormDataChild()
259 // Check context.
260 if (IsContainer())
261 return;
263 // Generate a unique boundary from current time.
264 sal_Char sTail[16 + 1];
265 tools::Time aCurTime( tools::Time::SYSTEM );
266 sal_uInt64 nThis = reinterpret_cast< sal_uIntPtr >( this ); // we can be on a 64bit architecture
267 nThis = ( ( nThis >> 32 ) ^ nThis ) & SAL_MAX_UINT32;
268 sprintf (sTail, "%08X%08X",
269 static_cast< unsigned int >(aCurTime.GetTime()),
270 static_cast< unsigned int >(nThis));
271 m_aBoundary = "------------_4D48";
272 m_aBoundary += sTail;
274 // Set header fields.
275 SetMIMEVersion("1.0");
276 SetContentType(
277 OUString::fromUtf8("multipart/form-data; boundary=" + m_aBoundary));
278 SetContentTransferEncoding("7bit");
281 void INetMIMEMessage::AttachChild(std::unique_ptr<INetMIMEMessage> pChildMsg)
283 assert(IsContainer());
284 if (IsContainer())
286 pChildMsg->pParent = this;
287 aChildren.push_back( std::move(pChildMsg) );
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */