forked from forwardemail/forwardemail.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
219 lines (192 loc) · 5.77 KB
/
test.js
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
218
219
/**
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: BUSL-1.1
*/
// eslint-disable-next-line import/no-unassigned-import
require('#config/env');
// eslint-disable-next-line import/no-unassigned-import
require('#config/mongoose');
// const { randomUUID } = require('node:crypto');
const Redis = require('ioredis');
const dayjs = require('dayjs-with-plugins');
const ms = require('ms');
const SQLite = require('./sqlite-server');
const IMAP = require('./imap-server');
const utils = require('./test/utils');
const Aliases = require('#models/aliases');
const Domains = require('#models/domains');
const Mailboxes = require('#models/mailboxes');
const Messages = require('#models/messages');
const Payments = require('#models/payments');
const Threads = require('#models/threads');
const Users = require('#models/users');
const config = require('#config');
const createTangerine = require('#helpers/create-tangerine');
const createWebSocketAsPromised = require('#helpers/create-websocket-as-promised');
const getDatabase = require('#helpers/get-database');
const { encrypt } = require('#helpers/encrypt-decrypt');
(async () => {
try {
await utils.setupMongoose();
const client = new Redis();
const subscriber = new Redis();
client.setMaxListeners(0);
subscriber.setMaxListeners(0);
const wsp = createWebSocketAsPromised();
const sqlite = new SQLite({ client, subscriber });
await sqlite.listen();
const imap = new IMAP({ client, subscriber, wsp }, false);
const user = await Users.create({
email: '[email protected]',
password: '$!@#!@#!@#!@#@!'
});
user.plan = 'enhanced_protection';
user[config.userFields.planSetAt] = dayjs().startOf('day').toDate();
await Payments.create({
user: user._id,
amount: 300,
invoice_at: user[config.userFields.planSetAt],
method: 'free_beta_program',
duration: ms('30d'),
plan: user.plan,
kind: 'one-time'
});
await user.save();
const resolver = createTangerine(client);
const domain = await Domains.create({
name: 'example.com',
members: [
{
user: user._id,
group: 'admin'
}
],
plan: 'enhanced_protection',
has_smtp: true,
smtp_verified_at: new Date(),
resolver
});
const alias = await Aliases.create({
name: 'boop',
domain: domain._id,
user: user._id,
recipients: ['[email protected]']
});
const pass = await alias.createToken();
await alias.save();
console.time('read and write to database');
const session = {
user: {
id: alias.id,
username: `${alias.name}@${domain.name}`,
alias_id: alias.id,
alias_name: alias.name,
domain_id: domain.id,
domain_name: domain.name,
password: encrypt(pass),
storage_location: alias.storage_location,
alias_has_pgp: alias.has_pgp,
alias_public_key: alias.public_key,
locale: 'en',
owner_full_email: `${alias.name}@${domain.name}`
}
};
const db = await getDatabase(imap, alias, session);
console.log('db', db);
//
// TODO: sqlite error with object insertion (is this a bug?)
// <https://github.com/2do2go/json-sql/issues/47>
//
const mailbox = await Mailboxes.create({
imap,
session,
path: 'INBOX'
});
console.log('mailbox', mailbox);
const flags = [];
const date = new Date();
const raw = `
Content-Type: multipart/mixed; boundary="------------cWFvDSey27tFG0hVYLqp9hs9"
MIME-Version: 1.0
Message-ID: <beep-boop@${domain.name}>
To: ${alias.name}@${domain.name}
From: ${alias.name}@${domain.name}
Subject: test
This is a multi-part message in MIME format.
--------------cWFvDSey27tFG0hVYLqp9hs9
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
test
--------------cWFvDSey27tFG0hVYLqp9hs9
Content-Type: text/plain; charset=UTF-8; name="example.txt"
Content-Disposition: attachment; filename="example.txt"
Content-Transfer-Encoding: base64
ZXhhbXBsZQo=
--------------cWFvDSey27tFG0hVYLqp9hs9--`.trim();
const {
id,
mimeTree,
size,
bodystructure,
envelope,
idate,
hdate,
msgid,
subject,
headers
} = await imap.prepareMessage({
flags,
date,
raw
});
const thread = await Threads.getThreadId(imap, session, subject, mimeTree);
console.log('thread', thread);
const retention =
typeof mailbox.retention === 'number' ? mailbox.retention : 0;
const maildata = imap.indexer.getMaildata(mimeTree);
const message = await Messages.create({
imap,
session,
mailbox: mailbox._id,
_id: id,
root: id,
exp: retention !== 0,
rdate: new Date(Date.now() + retention),
idate,
hdate,
thread: thread._id,
flags,
size,
headers,
mimeTree,
envelope,
bodystructure,
msgid,
unseen: !flags.includes('\\Seen'),
flagged: flags.includes('\\Flagged'),
undeleted: !flags.includes('\\Deleted'),
draft: flags.includes('\\Draft'),
magic: maildata.magic,
subject,
copied: false,
attachments: maildata.attachments || [],
uid: mailbox.uidNext,
modseq: mailbox.modifyIndex + 1,
searchable: !flags.includes('\\Deleted'),
junk: mailbox.specialUse === '\\Junk',
remoteAddress: 'foo',
transaction: 'APPEND',
text: maildata.text
});
console.log('message', message);
console.timeEnd('read and write to database');
db.pragma('optimize');
db.close();
console.log('DONE!');
} catch (err) {
console.error(err);
} finally {
// eslint-disable-next-line n/prefer-global/process
process.exit(0);
}
})();