1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
25 #include <o3tl/safeint.hxx>
26 #include <o3tl/sprintf.hxx>
27 #include <o3tl/string_view.hxx>
31 void INetMIMEMessage::SetHeaderField_Impl (
33 const OUString
&rValue
,
37 INetMessageHeader (rName
, rValue
.toUtf8()), rnIndex
);
40 /* ParseDateField and local helper functions.
42 * Parses a String in (implied) GMT format into class Date and tools::Time objects.
43 * Four formats are accepted:
45 * [Wkd,] 1*2DIGIT Mon 2*4DIGIT 00:00:00 [GMT] (rfc1123)
46 * [Wkd,] 00 Mon 0000 00:00:00 [GMT]) (rfc822, rfc1123)
47 * Weekday, 00-Mon-00 00:00:00 [GMT] (rfc850, rfc1036)
48 * Wkd Mon 00 00:00:00 0000 [GMT] (ctime)
49 * 1*DIGIT (delta seconds)
52 static const char *months
[12] =
54 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
55 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
58 static sal_uInt16
ParseNumber(std::string_view rStr
, size_t& nIndex
)
61 while ((n
< rStr
.size())
62 && rtl::isAsciiDigit(static_cast<unsigned char>(rStr
[n
])))
65 std::string_view
aNum(rStr
.substr(nIndex
, (n
- nIndex
)));
68 return static_cast<sal_uInt16
>(o3tl::toInt32(aNum
));
71 static sal_uInt16
ParseMonth(std::string_view rStr
, size_t& nIndex
)
74 while ((n
< rStr
.size())
75 && rtl::isAsciiAlpha(static_cast<unsigned char>(rStr
[n
])))
78 std::string_view
aMonth(rStr
.substr(nIndex
, 3));
82 for (i
= 0; i
< 12; i
++)
83 if (o3tl::equalsIgnoreAsciiCase(aMonth
, months
[i
])) break;
87 bool INetMIMEMessage::ParseDateField (
88 std::u16string_view rDateFieldW
, DateTime
& rDateTime
)
90 OString
aDateField(OUStringToOString(rDateFieldW
,
91 RTL_TEXTENCODING_ASCII_US
));
93 if (aDateField
.isEmpty()) return false;
95 if (aDateField
.indexOf(':') != -1)
97 // Some DateTime format.
100 // Skip over <Wkd> or <Weekday>, leading and trailing space.
101 while ((nIndex
< o3tl::make_unsigned(aDateField
.getLength())) &&
102 (aDateField
[nIndex
] == ' '))
106 (nIndex
< o3tl::make_unsigned(aDateField
.getLength())) &&
107 (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField
[nIndex
])) ||
108 (aDateField
[nIndex
] == ',') ))
111 while ((nIndex
< o3tl::make_unsigned(aDateField
.getLength())) &&
112 (aDateField
[nIndex
] == ' '))
115 if (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField
[nIndex
])))
118 if ((aDateField
.getLength() - nIndex
) < 20) return false;
120 rDateTime
.SetMonth (ParseMonth (aDateField
, nIndex
)); nIndex
++;
121 rDateTime
.SetDay (ParseNumber (aDateField
, nIndex
)); nIndex
++;
123 rDateTime
.SetHour (ParseNumber (aDateField
, nIndex
)); nIndex
++;
124 rDateTime
.SetMin (ParseNumber (aDateField
, nIndex
)); nIndex
++;
125 rDateTime
.SetSec (ParseNumber (aDateField
, nIndex
)); nIndex
++;
126 rDateTime
.SetNanoSec (0);
128 sal_uInt16 nYear
= ParseNumber (aDateField
, nIndex
);
129 if (nYear
< 100) nYear
+= 1900;
130 rDateTime
.SetYear (nYear
);
134 // Format: RFC1036 or RFC1123.
135 if ((aDateField
.getLength() - nIndex
) < 17) return false;
137 rDateTime
.SetDay (ParseNumber (aDateField
, nIndex
)); nIndex
++;
138 rDateTime
.SetMonth (ParseMonth (aDateField
, nIndex
)); nIndex
++;
140 sal_uInt16 nYear
= ParseNumber (aDateField
, nIndex
); nIndex
++;
141 if (nYear
< 100) nYear
+= 1900;
142 rDateTime
.SetYear (nYear
);
144 rDateTime
.SetHour (ParseNumber (aDateField
, nIndex
)); nIndex
++;
145 rDateTime
.SetMin (ParseNumber (aDateField
, nIndex
)); nIndex
++;
146 rDateTime
.SetSec (ParseNumber (aDateField
, nIndex
)); nIndex
++;
147 rDateTime
.SetNanoSec (0);
149 const char cPossiblePlusMinus
= nIndex
< o3tl::make_unsigned(aDateField
.getLength()) ? aDateField
[nIndex
] : 0;
150 if (cPossiblePlusMinus
== '+' || cPossiblePlusMinus
== '-')
152 // Offset from GMT: "(+|-)HHMM".
153 bool bEast
= (aDateField
[nIndex
++] == '+');
154 sal_uInt16 nOffset
= ParseNumber (aDateField
, nIndex
);
157 tools::Time
aDiff( tools::Time::EMPTY
);
158 aDiff
.SetHour (nOffset
/ 100);
159 aDiff
.SetMin (nOffset
% 100);
161 aDiff
.SetNanoSec (0);
171 else if (comphelper::string::isdigitAsciiString(aDateField
))
173 // Format: delta seconds.
174 tools::Time
aDelta (0);
175 aDelta
.SetTime (aDateField
.toInt32() * 100);
177 DateTime
aNow( DateTime::SYSTEM
);
181 rDateTime
.SetDate (aNow
.GetDate());
182 rDateTime
.SetTime (aNow
.GetTime());
190 return (rDateTime
.IsValidAndGregorian() &&
191 !((rDateTime
.GetSec() > 59) ||
192 (rDateTime
.GetMin() > 59) ||
193 (rDateTime
.GetHour() > 23) ));
196 const std::map
<InetMessageMime
, const char*> ImplINetMIMEMessageHeaderData
=
198 { InetMessageMime::VERSION
, "MIME-Version"},
199 { InetMessageMime::CONTENT_DISPOSITION
, "Content-Disposition"},
200 { InetMessageMime::CONTENT_TYPE
, "Content-Type"},
201 { InetMessageMime::CONTENT_TRANSFER_ENCODING
, "Content-Transfer-Encoding"}
204 INetMIMEMessage::INetMIMEMessage()
207 for (sal_uInt16 i
= 0; i
< static_cast<int>(InetMessageMime::NUMHDR
); i
++)
208 m_nMIMEIndex
[static_cast<InetMessageMime
>(i
)] = SAL_MAX_UINT32
;
211 INetMIMEMessage::~INetMIMEMessage()
215 void INetMIMEMessage::SetMIMEVersion (const OUString
& rVersion
)
217 SetHeaderField_Impl (
218 ImplINetMIMEMessageHeaderData
.at(InetMessageMime::VERSION
), rVersion
,
219 m_nMIMEIndex
[InetMessageMime::VERSION
]);
222 void INetMIMEMessage::SetContentDisposition (const OUString
& rDisposition
)
224 SetHeaderField_Impl (
225 ImplINetMIMEMessageHeaderData
.at(InetMessageMime::CONTENT_DISPOSITION
), rDisposition
,
226 m_nMIMEIndex
[InetMessageMime::CONTENT_DISPOSITION
]);
229 void INetMIMEMessage::SetContentType (const OUString
& rType
)
231 SetHeaderField_Impl (
232 ImplINetMIMEMessageHeaderData
.at(InetMessageMime::CONTENT_TYPE
), rType
,
233 m_nMIMEIndex
[InetMessageMime::CONTENT_TYPE
]);
236 void INetMIMEMessage::SetContentTransferEncoding (
237 const OUString
& rEncoding
)
239 SetHeaderField_Impl (
240 ImplINetMIMEMessageHeaderData
.at(InetMessageMime::CONTENT_TRANSFER_ENCODING
), rEncoding
,
241 m_nMIMEIndex
[InetMessageMime::CONTENT_TRANSFER_ENCODING
]);
244 OUString
INetMIMEMessage::GetDefaultContentType()
246 if (pParent
!= nullptr)
248 OUString
aParentCT (pParent
->GetContentType());
249 if (aParentCT
.isEmpty())
250 aParentCT
= pParent
->GetDefaultContentType();
252 if (aParentCT
.equalsIgnoreAsciiCase("multipart/digest"))
253 return "message/rfc822";
255 return "text/plain; charset=us-ascii";
258 void INetMIMEMessage::EnableAttachMultipartFormDataChild()
264 // Generate a unique boundary from current time.
266 tools::Time
aCurTime( tools::Time::SYSTEM
);
267 sal_uInt64 nThis
= reinterpret_cast< sal_uIntPtr
>( this ); // we can be on a 64bit architecture
268 nThis
= ( ( nThis
>> 32 ) ^ nThis
) & SAL_MAX_UINT32
;
269 o3tl::sprintf (sTail
, "%08X%08X",
270 static_cast< unsigned int >(aCurTime
.GetTime()),
271 static_cast< unsigned int >(nThis
));
272 m_aBoundary
= "------------_4D48"_ostr
;
273 m_aBoundary
+= sTail
;
275 // Set header fields.
276 SetMIMEVersion("1.0");
278 "multipart/form-data; boundary=" + OUString::fromUtf8(m_aBoundary
));
279 SetContentTransferEncoding("7bit");
282 void INetMIMEMessage::AttachChild(std::unique_ptr
<INetMIMEMessage
> pChildMsg
)
284 assert(IsContainer());
287 pChildMsg
->pParent
= this;
288 aChildren
.push_back( std::move(pChildMsg
) );
292 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */