summaryrefslogtreecommitdiffstats
path: root/src/mail/mail.vala
blob: 63e936254ace8afb55d65b93f69b5800dc88c6f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* Copyright 2013, Sebastian Reichel <sre@ring0.de>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

[DBus (name = "io.mainframe.shopsystem.Mail")]
public class MailImplementation {
	private GMime.Message m;
	private GMime.Part? main_text = null;
	private GMime.Part? main_html = null;
	private GMime.Part[] attachments;

	private GMime.FilterUnix2Dos filter_unix2dos;
	private GMime.FilterSmtpData filter_smtp;

	private string[] recipients;
	private string? reversepath;

	public MailContact from { set {
		reversepath = value.email;
		m.add_mailbox(GMime.AddressType.SENDER, value.name, value.email);
		m.add_mailbox(GMime.AddressType.FROM, value.name, value.email);
	}}

	public string subject {
		owned get {
			var result = m.get_subject();
			return (result == null) ? "" : result;
		}
		set {
			m.set_subject(value, "utf-8");
		}
	}

	public string message_id {
		owned get {
			var result = m.get_message_id();
			return (result == null) ? "" : result;
		}
		set {
			m.set_message_id(value);
		}
	}

	public string reply_to {
		owned get {
			var result = m.get_reply_to().to_string(new GMime.FormatOptions(), true);
			return (result == null) ? "" : result;
		}
		set {
			m.add_mailbox(GMime.AddressType.REPLY_TO, "", value);
		}
	}

	public MailDate date {
		owned get {
			MailDate result = {};
			var tmp = m.get_date();
			result.timezone = tmp.get_timezone_abbreviation();
			result.date = tmp.to_unix();
			return result;
		}
		set {
			var timezone = new TimeZone(value.timezone);
			var date = new DateTime.from_unix_utc((int64) value.date).to_timezone(timezone);
			m.set_date(date);
		}
	}

	public MailImplementation() {
		m = new GMime.Message(true);
		m.set_header("X-Mailer", "KtT Shopsystem", "utf-8");
		attachments = new GMime.Part[0];
		filter_smtp = new GMime.FilterSmtpData();
		filter_unix2dos = new GMime.FilterUnix2Dos(true);
		recipients = new string[0];
	}

#if 0
	public void set_from(MailContact contact) {
		string sender = contact.name + " " + "<" + contact.email + ">";
		m.set_sender(sender);
	}

	public void set_subject(string subject) {
		m.set_subject(subject);
	}

	public void set_date(uint64 date, int tz_offset) {
		m.set_date((ulong) date, tz_offset);
	}
#endif

	public void add_recipient(MailContact contact, GMime.AddressType type) {
		m.add_mailbox(type, contact.name, contact.email);
		recipients += contact.email;
	}

	public void set_main_part(string text, MessageType type) {
		GMime.DataWrapper content = new GMime.DataWrapper.with_stream(
			new GMime.StreamMem.with_buffer(text.data),
			GMime.ContentEncoding.DEFAULT);

		GMime.Part? part = new GMime.Part();
		part.set_content(content);

		switch(type) {
			case MessageType.HTML:
				part.set_content_type(GMime.ContentType.parse(new GMime.ParserOptions(), "text/html; charset=utf-8"));
				part.set_content_encoding(part.get_best_content_encoding(GMime.EncodingConstraint.7BIT));
				main_html = part;
				break;
			case MessageType.PLAIN:
			default:
				part.set_content_type(GMime.ContentType.parse(new GMime.ParserOptions(), "text/plain; charset=utf-8; format=flowed"));
				part.set_content_encoding(part.get_best_content_encoding(GMime.EncodingConstraint.7BIT));
				main_text = part;
				break;
		}
	}

	public void add_attachment(string filename, string content_type, uint8[] data) {
		GMime.Part part = new GMime.Part();

		GMime.DataWrapper content = new GMime.DataWrapper.with_stream(
			new GMime.StreamMem.with_buffer(data),
			GMime.ContentEncoding.BINARY);

		/* configure part */
		part.set_disposition("attachment");
		part.set_filename(filename);
		part.set_content_type(GMime.ContentType.parse(new GMime.ParserOptions(), content_type));
		part.set_content(content);
		part.set_content_encoding(part.get_best_content_encoding(GMime.EncodingConstraint.7BIT));

		attachments += part;
	}

	private GMime.Object? generate_main() {
		if(main_text != null && main_html != null) {
			var result = new GMime.Multipart.with_subtype("alternative");
			result.add(main_text);
			result.add(main_html);
			return result;
		} else if(main_text != null) {
			return main_text;
		} else if(main_html != null) {
			return main_html;
		}

		return null;
	}

	private GMime.Object? generate_attachments() {
		if(attachments.length == 1) {
			return attachments[0];
		} else if(attachments.length > 1) {
			var multipart = new GMime.Multipart.with_subtype("mixed");
			foreach(var attachment in attachments)
				multipart.add(attachment);
			return multipart;
		}

		return null;
	}

	private void update_mime_part() {
		GMime.Object? main = generate_main();
		GMime.Object? attachments = generate_attachments();
		GMime.Object? mime_message = null;

		if(main != null && attachments != null) {
			var multipart = new GMime.Multipart.with_subtype("mixed");
			multipart.add(main);
			multipart.add(attachments);
			mime_message = multipart;
		} else if(main != null) {
			mime_message = main;
		} else if(attachments != null) {
			mime_message = attachments;
		}

		m.set_mime_part(mime_message);
	}

	[DBus (visible = false)]
	public string generate() {
		update_mime_part();
		string result = m.to_string(new GMime.FormatOptions());
		uint8[] crlfdata;
		uint8[] smtpdata;
		size_t prespace;
		filter_unix2dos.filter(result.data, 0, out crlfdata, out prespace);
		filter_smtp.filter(crlfdata, 0, out smtpdata, out prespace);
		return (string) smtpdata;
	}

	[DBus (visible = false)]
	public unowned string[] get_recipients() {
		return recipients;
	}

	[DBus (visible = false)]
	public string get_reverse_path() {
		return reversepath;
	}
}