1/*
2 * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved.
3 * Copyright 2004-2017, Haiku, Inc. All rights reserved.
4 * Distributed under the terms of the MIT License.
5 */
6#ifndef _MAIL_COMPONENT_H
7#define _MAIL_COMPONENT_H
8
9
10#include <UTF8.h>
11#include <Message.h>
12#include <String.h>
13
14#include <mail_encoding.h>
15
16
17class BMimeType;
18
19extern const char* kHeaderCharsetString;
20extern const char* kHeaderEncodingString;
21// Special field names in the headers which specify the character set (int32)
22// and encoding (int8) to use when converting the headers from UTF-8 to the
23// output e-mail format (rfc2047).  For use with SetHeaderField when you pass
24// it a structured header in a BMessage.
25
26
27enum component_type {
28	B_MAIL_PLAIN_TEXT_BODY = 0,
29	B_MAIL_SIMPLE_ATTACHMENT,
30	B_MAIL_ATTRIBUTED_ATTACHMENT,
31	B_MAIL_MULTIPART_CONTAINER
32};
33
34
35class BMailComponent {
36public:
37								BMailComponent(
38									uint32 defaultCharSet = B_MAIL_NULL_CONVERSION);
39	virtual						~BMailComponent();
40
41			uint32				ComponentType();
42			BMailComponent*		WhatIsThis();
43			bool				IsAttachment();
44
45			void				SetHeaderField(const char *key,
46									const char *value,
47									uint32 charset = B_MAIL_NULL_CONVERSION,
48									mail_encoding encoding = null_encoding,
49									bool replace_existing = true);
50			void				SetHeaderField(const char *key,
51									BMessage *structured_header,
52									bool replace_existing = true);
53
54			const char* 		HeaderAt(int32 index) const;
55			const char*			HeaderField(const char *key,
56									int32 index = 0) const;
57			status_t			HeaderField(const char *key,
58									BMessage *structured_header,
59									int32 index = 0) const;
60
61			status_t			RemoveHeader(const char *key);
62
63	virtual status_t 			GetDecodedData(BPositionIO *data);
64	virtual status_t 			SetDecodedData(BPositionIO *data);
65
66	virtual status_t 			SetToRFC822(BPositionIO *data, size_t length,
67									bool parse_now = false);
68	virtual status_t 			RenderToRFC822(BPositionIO *render_to);
69
70	virtual status_t 			MIMEType(BMimeType *mime);
71
72protected:
73		uint32 _charSetForTextDecoding;
74			// This is the character set to be used for decoding text
75			// components, or if it is B_MAIL_NULL_CONVERSION then the character
76			// set will be determined automatically.  Since we can't use a
77			// global variable (different messages might have different values
78			// of this), and since sub-components can't find their parents,
79			// this is passed down during construction to some (just Component,
80			// Container, Message, MIME, Text) child components and ends up
81			// being used in the text components.
82
83private:
84		virtual void _ReservedComponent1();
85		virtual void _ReservedComponent2();
86		virtual void _ReservedComponent3();
87		virtual void _ReservedComponent4();
88		virtual void _ReservedComponent5();
89
90		BMessage headers;
91
92		uint32 _reserved[5];
93};
94
95
96class BTextMailComponent : public BMailComponent {
97	public:
98		BTextMailComponent(const char *text = NULL, uint32 defaultCharSet = B_MAIL_NULL_CONVERSION);
99		virtual ~BTextMailComponent();
100
101		void SetEncoding(mail_encoding encoding, int32 charset);
102			// encoding: you should always use quoted_printable, base64 is strongly not
103			//		recommended, see rfc 2047 for the reasons why
104			// charset: use Conversion flavor constants from UTF8.h
105
106		void SetText(const char *text);
107		void AppendText(const char *text);
108
109		const char *Text();
110		BString *BStringText();
111
112		void Quote(const char *message = NULL,
113				   const char *quote_style = "> ");
114
115		virtual status_t GetDecodedData(BPositionIO *data);
116		virtual status_t SetDecodedData(BPositionIO *data);
117
118		virtual status_t SetToRFC822(BPositionIO *data, size_t length, bool parse_now = false);
119		virtual status_t RenderToRFC822(BPositionIO *render_to);
120
121	private:
122		virtual void _ReservedText1();
123		virtual void _ReservedText2();
124
125		BString text;
126		BString decoded;
127
128		mail_encoding encoding;
129		uint32 charset; // This character set is used for encoding, not decoding.
130
131		status_t ParseRaw();
132		BPositionIO *raw_data;
133		size_t raw_length;
134		off_t raw_offset;
135
136		uint32 _reserved[5];
137};
138
139#endif // _MAIL_COMPONENT_H
140