1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #ifndef _TOOLS_INETMSG_HXX 24 #define _TOOLS_INETMSG_HXX 25 26 #include "tools/toolsdllapi.h" 27 #include <sal/types.h> 28 29 #ifndef _RTL_TEXTENC_H_ 30 #include <rtl/textenc.h> 31 #endif 32 33 #ifndef _TOOLS_INETMIME_HXX 34 #include <tools/inetmime.hxx> 35 #endif 36 #include <tools/list.hxx> 37 #include <tools/stream.hxx> 38 #include <tools/string.hxx> 39 40 class DateTime; 41 42 /*======================================================================= 43 * 44 * INetMessageHeader Interface. 45 * 46 *=====================================================================*/ 47 class INetMessageHeader 48 { 49 ByteString m_aName; 50 ByteString m_aValue; 51 52 public: INetMessageHeader(void)53 INetMessageHeader (void) 54 {} 55 INetMessageHeader(const ByteString & rName,const ByteString & rValue)56 INetMessageHeader ( 57 const ByteString& rName, const ByteString& rValue) 58 : m_aName (rName), m_aValue (rValue) 59 {} 60 INetMessageHeader(const INetMessageHeader & rHdr)61 INetMessageHeader ( 62 const INetMessageHeader& rHdr) 63 : m_aName (rHdr.m_aName), m_aValue (rHdr.m_aValue) 64 {} 65 ~INetMessageHeader(void)66 ~INetMessageHeader (void) 67 {} 68 operator =(const INetMessageHeader & rHdr)69 INetMessageHeader& operator= (const INetMessageHeader& rHdr) 70 { 71 m_aName = rHdr.m_aName; 72 m_aValue = rHdr.m_aValue; 73 return *this; 74 } 75 GetName(void) const76 const ByteString& GetName (void) const { return m_aName; } GetValue(void) const77 const ByteString& GetValue (void) const { return m_aValue; } 78 operator <<(SvStream & rStrm,const INetMessageHeader & rHdr)79 friend SvStream& operator<< ( 80 SvStream& rStrm, const INetMessageHeader& rHdr) 81 { 82 #ifdef ENABLE_BYTESTRING_STREAM_OPERATORS 83 rStrm << rHdr.m_aName; 84 rStrm << rHdr.m_aValue; 85 #else 86 rStrm.WriteByteString (rHdr.m_aName); 87 rStrm.WriteByteString (rHdr.m_aValue); 88 #endif 89 return rStrm; 90 } 91 operator >>(SvStream & rStrm,INetMessageHeader & rHdr)92 friend SvStream& operator>> ( 93 SvStream& rStrm, INetMessageHeader& rHdr) 94 { 95 #ifdef ENABLE_BYTESTRING_STREAM_OPERATORS 96 rStrm >> rHdr.m_aName; 97 rStrm >> rHdr.m_aValue; 98 #else 99 rStrm.ReadByteString (rHdr.m_aName); 100 rStrm.ReadByteString (rHdr.m_aValue); 101 #endif 102 return rStrm; 103 } 104 }; 105 106 /*======================================================================= 107 * 108 * INetMessage Interface. 109 * 110 *=====================================================================*/ 111 class INetMessage 112 { 113 List m_aHeaderList; 114 115 sal_uIntPtr m_nDocSize; 116 UniString m_aDocName; 117 SvLockBytesRef m_xDocLB; 118 119 void ListCleanup_Impl (void); 120 void ListCopy (const INetMessage& rMsg); 121 122 protected: GetHeaderName_Impl(sal_uIntPtr nIndex,rtl_TextEncoding eEncoding) const123 UniString GetHeaderName_Impl ( 124 sal_uIntPtr nIndex, rtl_TextEncoding eEncoding) const 125 { 126 INetMessageHeader *p = 127 (INetMessageHeader*)(m_aHeaderList.GetObject(nIndex)); 128 if (p) 129 return UniString(p->GetName(), eEncoding); 130 else 131 return UniString(); 132 } 133 GetHeaderValue_Impl(sal_uIntPtr nIndex,INetMIME::HeaderFieldType eType) const134 UniString GetHeaderValue_Impl ( 135 sal_uIntPtr nIndex, INetMIME::HeaderFieldType eType) const 136 { 137 INetMessageHeader *p = 138 (INetMessageHeader*)(m_aHeaderList.GetObject(nIndex)); 139 if (p) 140 return INetMIME::decodeHeaderFieldBody (eType, p->GetValue()); 141 else 142 return UniString(); 143 } 144 SetHeaderField_Impl(const INetMessageHeader & rHeader,sal_uIntPtr & rnIndex)145 void SetHeaderField_Impl ( 146 const INetMessageHeader &rHeader, sal_uIntPtr &rnIndex) 147 { 148 INetMessageHeader *p = new INetMessageHeader (rHeader); 149 if (m_aHeaderList.Count() <= rnIndex) 150 { 151 m_aHeaderList.Insert (p, LIST_APPEND); 152 rnIndex = m_aHeaderList.Count() - 1; 153 } 154 else 155 { 156 p = (INetMessageHeader*)(m_aHeaderList.Replace(p, rnIndex)); 157 delete p; 158 } 159 } 160 161 void SetHeaderField_Impl ( 162 INetMIME::HeaderFieldType eType, 163 const ByteString &rName, 164 const UniString &rValue, 165 sal_uIntPtr &rnIndex); 166 167 virtual SvStream& operator<< (SvStream& rStrm) const; 168 virtual SvStream& operator>> (SvStream& rStrm); 169 170 public: INetMessage(void)171 INetMessage (void) : m_nDocSize(0) {} 172 virtual ~INetMessage (void); 173 INetMessage(const INetMessage & rMsg)174 INetMessage (const INetMessage& rMsg) 175 : m_nDocSize (rMsg.m_nDocSize), 176 m_aDocName (rMsg.m_aDocName), 177 m_xDocLB (rMsg.m_xDocLB) 178 { 179 ListCopy (rMsg); 180 } 181 operator =(const INetMessage & rMsg)182 INetMessage& operator= (const INetMessage& rMsg) 183 { 184 m_nDocSize = rMsg.m_nDocSize; 185 m_aDocName = rMsg.m_aDocName; 186 m_xDocLB = rMsg.m_xDocLB; 187 ListCopy (rMsg); 188 return *this; 189 } 190 GetHeaderCount(void) const191 sal_uIntPtr GetHeaderCount (void) const { return m_aHeaderList.Count(); } 192 GetHeaderName(sal_uIntPtr nIndex) const193 UniString GetHeaderName (sal_uIntPtr nIndex) const 194 { 195 return GetHeaderName_Impl (nIndex, RTL_TEXTENCODING_ASCII_US); 196 } 197 GetHeaderValue(sal_uIntPtr nIndex) const198 UniString GetHeaderValue (sal_uIntPtr nIndex) const 199 { 200 return GetHeaderValue_Impl (nIndex, INetMIME::HEADER_FIELD_TEXT); 201 } 202 GetHeaderField(sal_uIntPtr nIndex) const203 INetMessageHeader GetHeaderField (sal_uIntPtr nIndex) const 204 { 205 INetMessageHeader *p = 206 (INetMessageHeader*)(m_aHeaderList.GetObject(nIndex)); 207 if (p) 208 return INetMessageHeader(*p); 209 else 210 return INetMessageHeader(); 211 } 212 213 sal_uIntPtr SetHeaderField ( 214 const UniString& rName, 215 const UniString& rValue, 216 sal_uIntPtr nIndex = LIST_APPEND); 217 218 virtual sal_uIntPtr SetHeaderField ( 219 const INetMessageHeader &rField, sal_uIntPtr nIndex = LIST_APPEND); 220 GetDocumentSize(void) const221 sal_uIntPtr GetDocumentSize (void) const { return m_nDocSize; } SetDocumentSize(sal_uIntPtr nSize)222 void SetDocumentSize (sal_uIntPtr nSize) { m_nDocSize = nSize; } 223 GetDocumentName(void) const224 const UniString& GetDocumentName (void) const { return m_aDocName; } SetDocumentName(const UniString & rName)225 void SetDocumentName (const UniString& rName) { m_aDocName = rName; } 226 GetDocumentLB(void) const227 SvLockBytes* GetDocumentLB (void) const { return m_xDocLB; } SetDocumentLB(SvLockBytes * pDocLB)228 void SetDocumentLB (SvLockBytes *pDocLB) { m_xDocLB = pDocLB; } 229 operator <<(SvStream & rStrm,const INetMessage & rMsg)230 friend SvStream& operator<< ( 231 SvStream& rStrm, const INetMessage& rMsg) 232 { 233 return rMsg.operator<< (rStrm); 234 } 235 operator >>(SvStream & rStrm,INetMessage & rMsg)236 friend SvStream& operator>> ( 237 SvStream& rStrm, INetMessage& rMsg) 238 { 239 return rMsg.operator>> (rStrm); 240 } 241 }; 242 243 /*======================================================================= 244 * 245 * INetMessageHeaderIterator Interface. 246 * 247 *=====================================================================*/ 248 class INetMessageHeaderIterator 249 { 250 sal_uIntPtr nValueCount; 251 List aValueList; 252 UniString aEmptyString; 253 254 public: 255 INetMessageHeaderIterator ( 256 const INetMessage& rMsg, const UniString& rHdrName); 257 virtual ~INetMessageHeaderIterator (void); 258 GetValueCount(void) const259 sal_uIntPtr GetValueCount (void) const { return nValueCount; } GetValue(sal_uIntPtr nIndex) const260 const UniString& GetValue (sal_uIntPtr nIndex) const 261 { 262 if (nIndex < nValueCount) 263 { 264 return *((UniString*)(aValueList.GetObject(nIndex))); 265 } 266 else 267 { 268 return aEmptyString; 269 } 270 } 271 }; 272 273 /*======================================================================= 274 * 275 * INetRFC822Message Interface. 276 * 277 *=====================================================================*/ 278 #define INETMSG_RFC822_BCC 0 279 #define INETMSG_RFC822_CC 1 280 #define INETMSG_RFC822_COMMENTS 2 281 #define INETMSG_RFC822_DATE 3 282 #define INETMSG_RFC822_FROM 4 283 #define INETMSG_RFC822_IN_REPLY_TO 5 284 #define INETMSG_RFC822_KEYWORDS 6 285 #define INETMSG_RFC822_MESSAGE_ID 7 286 #define INETMSG_RFC822_REFERENCES 8 287 #define INETMSG_RFC822_REPLY_TO 9 288 #define INETMSG_RFC822_RETURN_PATH 10 289 #define INETMSG_RFC822_SENDER 11 290 #define INETMSG_RFC822_SUBJECT 12 291 #define INETMSG_RFC822_TO 13 292 293 #define INETMSG_RFC822_X_MAILER 14 294 #define INETMSG_RFC822_RETURN_RECEIPT_TO 15 295 296 #define INETMSG_RFC822_NUMHDR 16 297 298 class TOOLS_DLLPUBLIC INetRFC822Message : public INetMessage 299 { 300 sal_uIntPtr m_nIndex[INETMSG_RFC822_NUMHDR]; 301 302 protected: 303 virtual SvStream& operator<< (SvStream& rStrm) const; 304 virtual SvStream& operator>> (SvStream& rStrm); 305 306 public: 307 INetRFC822Message (void); 308 INetRFC822Message (const INetRFC822Message& rMsg); 309 virtual ~INetRFC822Message (void); 310 311 INetRFC822Message& operator= (const INetRFC822Message& rMsg); 312 313 static sal_Bool GenerateDateField ( 314 const DateTime& rDateTime, UniString& rDateField); 315 static sal_Bool ParseDateField ( 316 const UniString& rDateField, DateTime& rDateTime); 317 318 using INetMessage::SetHeaderField; 319 virtual sal_uIntPtr SetHeaderField ( 320 const INetMessageHeader &rHeader, sal_uIntPtr nIndex = LIST_APPEND); 321 322 /** Header fields. 323 */ 324 void SetBCC (const UniString& rBCC); GetBCC(void) const325 UniString GetBCC (void) const 326 { 327 return GetHeaderValue_Impl ( 328 m_nIndex[INETMSG_RFC822_BCC], 329 INetMIME::HEADER_FIELD_ADDRESS); 330 } 331 332 void SetCC (const UniString& rCC); GetCC(void) const333 UniString GetCC (void) const 334 { 335 return GetHeaderValue_Impl ( 336 m_nIndex[INETMSG_RFC822_CC], 337 INetMIME::HEADER_FIELD_ADDRESS); 338 } 339 340 void SetComments (const UniString& rComments); GetComments(void) const341 UniString GetComments (void) const 342 { 343 return GetHeaderValue_Impl ( 344 m_nIndex[INETMSG_RFC822_COMMENTS], 345 INetMIME::HEADER_FIELD_TEXT); 346 } 347 348 void SetDate (const UniString& rDate); GetDate(void) const349 UniString GetDate (void) const 350 { 351 return GetHeaderValue_Impl ( 352 m_nIndex[INETMSG_RFC822_DATE], 353 INetMIME::HEADER_FIELD_STRUCTURED); 354 } 355 356 void SetFrom (const UniString& rFrom); GetFrom(void) const357 UniString GetFrom (void) const 358 { 359 return GetHeaderValue_Impl ( 360 m_nIndex[INETMSG_RFC822_FROM], 361 INetMIME::HEADER_FIELD_ADDRESS); 362 } 363 364 void SetInReplyTo (const UniString& rInReplyTo); GetInReplyTo(void) const365 UniString GetInReplyTo (void) const 366 { 367 return GetHeaderValue_Impl ( 368 m_nIndex[INETMSG_RFC822_IN_REPLY_TO], 369 INetMIME::HEADER_FIELD_ADDRESS); // ??? MESSAGE_ID ??? 370 } 371 372 void SetKeywords (const UniString& rKeywords); GetKeywords(void) const373 UniString GetKeywords (void) const 374 { 375 return GetHeaderValue_Impl ( 376 m_nIndex[INETMSG_RFC822_KEYWORDS], 377 INetMIME::HEADER_FIELD_PHRASE); 378 } 379 380 void SetMessageID (const UniString& rMessageID); GetMessageID(void) const381 UniString GetMessageID (void) const 382 { 383 return GetHeaderValue_Impl ( 384 m_nIndex[INETMSG_RFC822_MESSAGE_ID], 385 INetMIME::HEADER_FIELD_MESSAGE_ID); 386 } 387 388 void SetReferences (const UniString& rReferences); GetReferences(void) const389 UniString GetReferences (void) const 390 { 391 return GetHeaderValue_Impl ( 392 m_nIndex[INETMSG_RFC822_REFERENCES], 393 INetMIME::HEADER_FIELD_ADDRESS); 394 } 395 396 void SetReplyTo (const UniString& rReplyTo); GetReplyTo(void) const397 UniString GetReplyTo (void) const 398 { 399 return GetHeaderValue_Impl ( 400 m_nIndex[INETMSG_RFC822_REPLY_TO], 401 INetMIME::HEADER_FIELD_ADDRESS); 402 } 403 404 void SetReturnPath (const UniString& rReturnPath); GetReturnPath(void) const405 UniString GetReturnPath (void) const 406 { 407 return GetHeaderValue_Impl ( 408 m_nIndex[INETMSG_RFC822_RETURN_PATH], 409 INetMIME::HEADER_FIELD_ADDRESS); 410 } 411 412 void SetReturnReceiptTo (const UniString& rReturnReceiptTo); GetReturnReceiptTo(void) const413 UniString GetReturnReceiptTo (void) const 414 { 415 return GetHeaderValue_Impl ( 416 m_nIndex[INETMSG_RFC822_RETURN_RECEIPT_TO], 417 INetMIME::HEADER_FIELD_ADDRESS); 418 } 419 420 void SetSender (const UniString& rSender); GetSender(void) const421 UniString GetSender (void) const 422 { 423 return GetHeaderValue_Impl ( 424 m_nIndex[INETMSG_RFC822_SENDER], 425 INetMIME::HEADER_FIELD_ADDRESS); 426 } 427 428 void SetSubject (const UniString& rSubject); GetSubject(void) const429 UniString GetSubject (void) const 430 { 431 return GetHeaderValue_Impl ( 432 m_nIndex[INETMSG_RFC822_SUBJECT], 433 INetMIME::HEADER_FIELD_TEXT); 434 } 435 436 void SetTo (const UniString& rTo); GetTo(void) const437 UniString GetTo (void) const 438 { 439 return GetHeaderValue_Impl ( 440 m_nIndex[INETMSG_RFC822_TO], 441 INetMIME::HEADER_FIELD_TEXT); 442 } 443 444 void SetXMailer (const UniString& rXMailer); GetXMailer(void) const445 UniString GetXMailer (void) const 446 { 447 return GetHeaderValue_Impl ( 448 m_nIndex[INETMSG_RFC822_X_MAILER], 449 INetMIME::HEADER_FIELD_TEXT); 450 } 451 452 /** Stream operators. 453 */ operator <<(SvStream & rStrm,const INetRFC822Message & rMsg)454 friend SvStream& operator<< ( 455 SvStream& rStrm, const INetRFC822Message& rMsg) 456 { 457 return rMsg.operator<< (rStrm); 458 } 459 operator >>(SvStream & rStrm,INetRFC822Message & rMsg)460 friend SvStream& operator>> ( 461 SvStream& rStrm, INetRFC822Message& rMsg) 462 { 463 return rMsg.operator>> (rStrm); 464 } 465 }; 466 467 /*======================================================================= 468 * 469 * INetMIMEMessage Interface. 470 * 471 *=====================================================================*/ 472 #define INETMSG_MIME_VERSION 0 473 #define INETMSG_MIME_CONTENT_DESCRIPTION 1 474 #define INETMSG_MIME_CONTENT_DISPOSITION 2 475 #define INETMSG_MIME_CONTENT_ID 3 476 #define INETMSG_MIME_CONTENT_TYPE 4 477 #define INETMSG_MIME_CONTENT_TRANSFER_ENCODING 5 478 479 #define INETMSG_MIME_NUMHDR 6 480 481 enum INetMessageContainerType 482 { 483 INETMSG_MESSAGE_RFC822, 484 INETMSG_MULTIPART_MIXED, 485 INETMSG_MULTIPART_ALTERNATIVE, 486 INETMSG_MULTIPART_DIGEST, 487 INETMSG_MULTIPART_PARALLEL, 488 INETMSG_MULTIPART_RELATED, 489 INETMSG_MULTIPART_FORM_DATA 490 }; 491 492 class TOOLS_DLLPUBLIC INetMIMEMessage : public INetRFC822Message 493 { 494 sal_uIntPtr m_nIndex[INETMSG_MIME_NUMHDR]; 495 496 INetMIMEMessage *pParent; 497 sal_uIntPtr nNumChildren; 498 List aChildren; 499 ByteString m_aBoundary; 500 sal_Bool bHeaderParsed; 501 502 friend class INetMIMEMessageStream; 503 SetChildCount(sal_uIntPtr nCount)504 void SetChildCount (sal_uIntPtr nCount) { nNumChildren = nCount; } GetMultipartBoundary(void) const505 const ByteString& GetMultipartBoundary (void) const { return m_aBoundary; } SetMultipartBoundary(const ByteString & rBnd)506 void SetMultipartBoundary (const ByteString& rBnd) { m_aBoundary = rBnd; } 507 508 void CleanupImp (void); 509 void CopyImp (const INetMIMEMessage& rMsg); SetHeaderParsed()510 void SetHeaderParsed() { bHeaderParsed = sal_True; } 511 512 protected: 513 virtual SvStream& operator<< (SvStream& rStrm) const; 514 virtual SvStream& operator>> (SvStream& rStrm); 515 516 public: 517 INetMIMEMessage (void); 518 INetMIMEMessage (const INetMIMEMessage& rMsg); 519 virtual ~INetMIMEMessage (void); 520 521 INetMIMEMessage& operator= (const INetMIMEMessage& rMsg); 522 HeaderParsed() const523 sal_Bool HeaderParsed() const { return bHeaderParsed; } 524 525 virtual INetMIMEMessage* CreateMessage ( 526 const INetMIMEMessage& rMsg) const; 527 528 using INetRFC822Message::SetHeaderField; 529 virtual sal_uIntPtr SetHeaderField ( 530 const INetMessageHeader &rHeader, sal_uIntPtr nIndex = LIST_APPEND); 531 532 /** Header fields. 533 */ 534 void SetMIMEVersion (const UniString& rVersion); GetMIMEVersion(void) const535 UniString GetMIMEVersion (void) const 536 { 537 return GetHeaderValue (m_nIndex[INETMSG_MIME_VERSION]); 538 } 539 540 void SetContentDescription (const UniString& rDescription); GetContentDescription(void) const541 UniString GetContentDescription (void) const 542 { 543 return GetHeaderValue (m_nIndex[INETMSG_MIME_CONTENT_DESCRIPTION]); 544 } 545 546 void SetContentDisposition (const UniString& rDisposition); GetContentDisposition(void) const547 UniString GetContentDisposition (void) const 548 { 549 return GetHeaderValue (m_nIndex[INETMSG_MIME_CONTENT_DISPOSITION]); 550 } 551 552 void SetContentID (const UniString& rID); GetContentID(void) const553 UniString GetContentID (void) const 554 { 555 return GetHeaderValue (m_nIndex[INETMSG_MIME_CONTENT_ID]); 556 } 557 558 void SetContentType (const UniString& rType); GetContentType(void) const559 UniString GetContentType (void) const 560 { 561 return GetHeaderValue (m_nIndex[INETMSG_MIME_CONTENT_TYPE]); 562 } 563 564 void SetContentTransferEncoding (const UniString& rEncoding); GetContentTransferEncoding(void) const565 UniString GetContentTransferEncoding (void) const 566 { 567 return GetHeaderValue (m_nIndex[INETMSG_MIME_CONTENT_TRANSFER_ENCODING]); 568 } 569 570 virtual void GetDefaultContentType (UniString& rContentType); 571 572 /** Message container methods. 573 */ IsContainer(void) const574 sal_Bool IsContainer (void) const 575 { 576 return (IsMessage() || IsMultipart()); 577 } IsMessage(void) const578 sal_Bool IsMessage (void) const 579 { 580 UniString aType (GetContentType()); 581 return (aType.CompareIgnoreCaseToAscii("message/", 8) == 0); 582 } IsMultipart(void) const583 sal_Bool IsMultipart (void) const 584 { 585 UniString aType (GetContentType()); 586 return (aType.CompareIgnoreCaseToAscii("multipart/", 10) == 0); 587 } 588 GetChildCount(void) const589 sal_uIntPtr GetChildCount (void) const { return nNumChildren; } GetChild(sal_uIntPtr nIndex) const590 INetMIMEMessage* GetChild (sal_uIntPtr nIndex) const 591 { 592 return ((INetMIMEMessage *)(aChildren.GetObject (nIndex))); 593 } GetParent(void) const594 INetMIMEMessage* GetParent (void) const { return pParent; } 595 596 sal_Bool EnableAttachChild ( 597 INetMessageContainerType eType = INETMSG_MULTIPART_MIXED); 598 sal_Bool AttachChild ( 599 INetMIMEMessage& rChildMsg, sal_Bool bOwner = sal_True); 600 sal_Bool DetachChild ( 601 sal_uIntPtr nIndex, INetMIMEMessage& rChildMsg) const; 602 603 /** Stream operators. 604 */ operator <<(SvStream & rStrm,const INetMIMEMessage & rMsg)605 friend SvStream& operator<< ( 606 SvStream& rStrm, const INetMIMEMessage& rMsg) 607 { 608 return rMsg.operator<< (rStrm); 609 } 610 operator >>(SvStream & rStrm,INetMIMEMessage & rMsg)611 friend SvStream& operator>> ( 612 SvStream& rStrm, INetMIMEMessage& rMsg) 613 { 614 return rMsg.operator>> (rStrm); 615 } 616 }; 617 618 #endif /* !_TOOLS_INETMSG_HXX */ 619 620