xref: /haiku/src/kits/mail/b_mail_message.cpp (revision 56430ad8002b8fd1ac69b590e9cc130de6d9e852)
1 /* BMailMessage - compatibility wrapper to our mail message class
2 **
3 ** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4 */
5 
6 
7 //------This entire document is a horrible, horrible hack. I apologize.
8 #include <Entry.h>
9 
10 class _EXPORT BMailMessage;
11 
12 #include <E-mail.h>
13 
14 #include <MailAttachment.h>
15 #include <MailMessage.h>
16 
17 #include <stdio.h>
18 #include <strings.h>
19 
20 struct CharsetConversionEntry
21 {
22 	const char *charset;
23 	uint32 flavor;
24 };
25 
26 extern const CharsetConversionEntry mail_charsets[];
27 
28 
29 BMailMessage::BMailMessage(void)
30 	:	fFields((BList *)(new BEmailMessage()))
31 {
32 }
33 
34 BMailMessage::~BMailMessage(void)
35 {
36 	delete ((BEmailMessage *)(fFields));
37 }
38 
39 status_t BMailMessage::AddContent(const char *text, int32 length,
40 	uint32 encoding, bool /*clobber*/)
41 {
42 	BTextMailComponent *comp = new BTextMailComponent;
43 	BMemoryIO io(text,length);
44 	comp->SetDecodedData(&io);
45 
46 	comp->SetEncoding(quoted_printable,encoding);
47 
48 	//if (clobber)
49 	((BEmailMessage *)(fFields))->AddComponent(comp);
50 
51 	return B_OK;
52 }
53 
54 status_t BMailMessage::AddContent(const char *text, int32 length,
55 	const char *encoding, bool /*clobber*/)
56 {
57 	BTextMailComponent *comp = new BTextMailComponent();
58 	BMemoryIO io(text,length);
59 	comp->SetDecodedData(&io);
60 
61 	uint32 encode = B_ISO1_CONVERSION;
62 	//-----I'm assuming that encoding is one of the RFC charsets
63 	//-----there are no docs. Am I right?
64 	if (encoding != NULL) {
65 		for (int32 i = 0; mail_charsets[i].charset != NULL; i++) {
66 			if (strcasecmp(encoding,mail_charsets[i].charset) == 0) {
67 				encode = mail_charsets[i].flavor;
68 				break;
69 			}
70 		}
71 	}
72 
73 	comp->SetEncoding(quoted_printable,encode);
74 
75 	//if (clobber)
76 	((BEmailMessage *)(fFields))->AddComponent(comp);
77 
78 	return B_OK;
79 }
80 
81 status_t BMailMessage::AddEnclosure(entry_ref *ref, bool /*clobber*/)
82 {
83 	((BEmailMessage *)(fFields))->Attach(ref);
84 	return B_OK;
85 }
86 
87 status_t BMailMessage::AddEnclosure(const char *path, bool /*clobber*/)
88 {
89 	BEntry entry(path);
90 	status_t status;
91 	if ((status = entry.InitCheck()) < B_OK)
92 		return status;
93 
94 	entry_ref ref;
95 	if ((status = entry.GetRef(&ref)) < B_OK)
96 		return status;
97 
98 	((BEmailMessage *)(fFields))->Attach(&ref);
99 	return B_OK;
100 }
101 
102 status_t BMailMessage::AddEnclosure(const char *MIME_type, void *data, int32 len,
103 	bool /*clobber*/)
104 {
105 	BSimpleMailAttachment *attach = new BSimpleMailAttachment;
106 	attach->SetDecodedData(data,len);
107 	attach->SetHeaderField("Content-Type",MIME_type);
108 
109 	((BEmailMessage *)(fFields))->AddComponent(attach);
110 	return B_OK;
111 }
112 
113 status_t BMailMessage::AddHeaderField(uint32 /*encoding*/, const char *field_name, const char *str,
114 	bool /*clobber*/)
115 {
116 	//printf("First AddHeaderField. Args are %s%s\n",field_name,str);
117 
118 	BString string = field_name;
119 	string.Truncate(string.Length() - 2); //----BMailMessage includes the ": "
120 	((BEmailMessage *)(fFields))->SetHeaderField(string.String(),str);
121 	return B_OK;
122 }
123 
124 status_t BMailMessage::AddHeaderField(const char *field_name, const char *str,
125 	bool /*clobber*/)
126 {
127 	//printf("Second AddHeaderField. Args are %s%s\n",field_name,str);
128 	BString string = field_name;
129 	string.Truncate(string.Length() - 2); //----BMailMessage includes the ": "
130 	((BEmailMessage *)(fFields))->SetHeaderField(string.String(),str);
131 	return B_OK;
132 }
133 
134 status_t BMailMessage::Send(bool send_now,
135 	 bool /*remove_when_I_have_completed_sending_this_message_to_your_preferred_SMTP_server*/)
136 {
137  	return ((BEmailMessage *)(fFields))->Send(send_now);
138 }
139 
140