summary refs log tree commit diff
path: root/src/util/util/email/index.ts
blob: b9786e4f6c5e3f0884cde748f120512cefaabda3 (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
	Fosscord: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Fosscord and Fosscord Contributors
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import fs from "node:fs";
import path from "node:path";
import { SentMessageInfo, Transporter } from "nodemailer";
import { User } from "../../entities";
import { Config } from "../Config";
import { generateToken } from "../Token";
import MailGun from "./transports/MailGun";
import MailJet from "./transports/MailJet";
import SendGrid from "./transports/SendGrid";
import SMTP from "./transports/SMTP";

const ASSET_FOLDER_PATH = path.join(
	__dirname,
	"..",
	"..",
	"..",
	"..",
	"assets",
);
export const EMAIL_REGEX =
	/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

export function adjustEmail(email?: string): string | undefined {
	if (!email) return email;
	// body parser already checked if it is a valid email
	const parts = <RegExpMatchArray>email.match(EMAIL_REGEX);
	if (!parts || parts.length < 5) return undefined;

	return email;
	// // TODO: The below code doesn't actually do anything.
	// const domain = parts[5];
	// const user = parts[1];

	// // TODO: check accounts with uncommon email domains
	// if (domain === "gmail.com" || domain === "googlemail.com") {
	// 	// replace .dots and +alternatives -> Gmail Dot Trick https://support.google.com/mail/answer/7436150 and https://generator.email/blog/gmail-generator
	// 	const v = user.replace(/[.]|(\+.*)/g, "") + "@gmail.com";
	// }

	// if (domain === "google.com") {
	// 	// replace .dots and +alternatives -> Google Staff GMail Dot Trick
	// 	const v = user.replace(/[.]|(\+.*)/g, "") + "@google.com";
	// }

	// return email;
}

const transporters: {
	[key: string]: () => Promise<Transporter<unknown> | void>;
} = {
	smtp: SMTP,
	mailgun: MailGun,
	mailjet: MailJet,
	sendgrid: SendGrid,
};

export const Email: {
	transporter: Transporter | null;
	init: () => Promise<void>;
	generateLink: (
		type: "verify" | "reset",
		id: string,
		email: string,
	) => Promise<string>;
	sendVerifyEmail: (user: User, email: string) => Promise<SentMessageInfo>;
	sendResetPassword: (user: User, email: string) => Promise<SentMessageInfo>;
	sendPasswordChanged: (
		user: User,
		email: string,
	) => Promise<SentMessageInfo>;
	doReplacements: (
		template: string,
		user: User,
		emailVerificationUrl?: string,
		passwordResetUrl?: string,
		ipInfo?: {
			ip: string;
			city: string;
			region: string;
			country_name: string;
		},
	) => string;
} = {
	transporter: null,
	init: async function () {
		const { provider } = Config.get().email;
		if (!provider) return;

		const transporterFn = transporters[provider];
		if (!transporterFn)
			return console.error(`[Email] Invalid provider: ${provider}`);
		console.log(`[Email] Initializing ${provider} transport...`);
		const transporter = await transporterFn();
		if (!transporter) return;
		this.transporter = transporter;
		console.log(`[Email] ${provider} transport initialized.`);
	},
	/**
	 * Replaces all placeholders in an email template with the correct values
	 */
	doReplacements: function (
		template,
		user,
		emailVerificationUrl?,
		passwordResetUrl?,
		ipInfo?: {
			ip: string;
			city: string;
			region: string;
			country_name: string;
		},
	) {
		const { instanceName } = Config.get().general;

		const replacements = [
			["{instanceName}", instanceName],
			["{userUsername}", user.username],
			["{userDiscriminator}", user.discriminator],
			["{userId}", user.id],
			["{phoneNumber}", user.phone?.slice(-4)],
			["{userEmail}", user.email],
			["{emailVerificationUrl}", emailVerificationUrl],
			["{passwordResetUrl}", passwordResetUrl],
			["{ipAddress}", ipInfo?.ip],
			["{locationCity}", ipInfo?.city],
			["{locationRegion}", ipInfo?.region],
			["{locationCountryName}", ipInfo?.country_name],
		];

		// loop through all replacements and replace them in the template
		for (const [key, value] of Object.values(replacements)) {
			if (!value) continue;
			template = template.replaceAll(key as string, value);
		}

		return template;
	},
	/**
	 *
	 * @param id user id
	 * @param email user email
	 */
	generateLink: async function (type, id, email) {
		const token = (await generateToken(id, email)) as string;
		const instanceUrl =
			Config.get().general.frontPage || "http://localhost:3001";
		const link = `${instanceUrl}/${type}#token=${token}`;
		return link;
	},
	/**
	 * Sends an email to the user with a link to verify their email address
	 */
	sendVerifyEmail: async function (user, email) {
		if (!this.transporter) return;

		// generate a verification link for the user
		const link = await this.generateLink("verify", user.id, email);

		// load the email template
		const rawTemplate = fs.readFileSync(
			path.join(
				ASSET_FOLDER_PATH,
				"email_templates",
				"verify_email.html",
			),
			{ encoding: "utf-8" },
		);

		// replace email template placeholders
		const html = this.doReplacements(rawTemplate, user, link);

		// extract the title from the email template to use as the email subject
		const subject = html.match(/<title>(.*)<\/title>/)?.[1] || "";

		// construct the email
		const message = {
			from:
				Config.get().general.correspondenceEmail || "noreply@localhost",
			to: email,
			subject,
			html,
		};

		// send the email
		return this.transporter.sendMail(message);
	},
	/**
	 * Sends an email to the user with a link to reset their password
	 */
	sendResetPassword: async function (user, email) {
		if (!this.transporter) return;

		// generate a password reset link for the user
		const link = await this.generateLink("reset", user.id, email);

		// load the email template
		const rawTemplate = await fs.promises.readFile(
			path.join(
				ASSET_FOLDER_PATH,
				"email_templates",
				"password_reset_request.html",
			),
			{ encoding: "utf-8" },
		);

		// replace email template placeholders
		const html = this.doReplacements(rawTemplate, user, undefined, link);

		// extract the title from the email template to use as the email subject
		const subject = html.match(/<title>(.*)<\/title>/)?.[1] || "";

		// construct the email
		const message = {
			from:
				Config.get().general.correspondenceEmail || "noreply@localhost",
			to: email,
			subject,
			html,
		};

		// send the email
		return this.transporter.sendMail(message);
	},
	/**
	 * Sends an email to the user notifying them that their password has been changed
	 */
	sendPasswordChanged: async function (user, email) {
		if (!this.transporter) return;

		// load the email template
		const rawTemplate = await fs.promises.readFile(
			path.join(
				ASSET_FOLDER_PATH,
				"email_templates",
				"password_changed.html",
			),
			{ encoding: "utf-8" },
		);

		// replace email template placeholders
		const html = this.doReplacements(rawTemplate, user);

		// extract the title from the email template to use as the email subject
		const subject = html.match(/<title>(.*)<\/title>/)?.[1] || "";

		// construct the email
		const message = {
			from:
				Config.get().general.correspondenceEmail || "noreply@localhost",
			to: email,
			subject,
			html,
		};

		// send the email
		return this.transporter.sendMail(message);
	},
};