Skip to content

Commit

Permalink
Mailcow
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewstech committed Dec 17, 2023
1 parent acef720 commit 240f3f2
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
99 changes: 99 additions & 0 deletions commands/newEmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const fetch = require("node-fetch");
const { SlashCommandBuilder,EmbedBuilder } = require("discord.js");
const staff = require("../models/staff");
const emails = require("../models/emails");
const Loading = require("../components/loading");
const DmUser = require("../components/DmUser");
const MailcowApi = require("../components/MailcowApi");


module.exports = {
data: new SlashCommandBuilder()
.setName("create-email")
.setDescription("STAFF ONLY: Create a new email for a user.")
.addStringOption((option) =>
option
.setName("email")
.setDescription("The first part of the email to create.")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("domain")
.setDescription("The domain to create the email on.")
.setRequired(true)
)
.addUserOption((option) =>
option
.setName("user")
.setDescription("The user to create the email for.")
.setRequired(true)
),
async execute(interaction) {
await Loading(interaction, false);
if (!(await staff.findOne({ _id: interaction.user.id }))) {
const embed = new EmbedBuilder()
.setDescription("Only staff can use this command!")
.setColor("#0096ff");
return await interaction.editReply({ embeds: [embed], ephemeral: true });
}
const email = interaction.options.getString("email").toLowerCase();
let domain = interaction.options.getString("domain").toLowerCase().replace(/\.is-a\.dev$/, "");
const user = interaction.options.getUser("user");

// generate a strong password
const password = Math.random().toString(36).slice(-10);

const userDoc = await emails.findOne({ _id: user.id });
if (userDoc.EmailCount >= 2) {
const embed = new EmbedBuilder()
.setDescription("This user already has 2 emails!")
.setColor("#0096ff");
return await interaction.editReply({ embeds: [embed], ephemeral: true });
}
const mailcow = new MailcowApi(process.env.MAILCOW_TOKEN);
const domains = await mailcow.get("/api/v1/get/domain");
const domainDoc = domains.filter((d) => d.domain === domain)[0];
if (!domainDoc) {
// If the domain doesn't exist, create it
await mailcow.post("/api/v1/add/domain", {
"active": "1",
"aliases": "10",
"backupmx": "0",
"domain": domain,
"mailboxes": "1",
"maxquota": "100",
"quota": "50",
"relay_all_recipients": "0",
"restart_sogo": "0"
});
}
// create the email account
await mailcow.post("/api/v1/add/mailbox", {
"active": "1",
"domain": domain,
"local_part": email,
"name": `${user.username}`,
"quota": "50",
"password": password
});
// add the email to the database
await emails.updateOne(
{ _id: user.id },
{
$push: {
emails: {
email: `${email}@${domain}.is-a.dev`,
},
},
$inc: { EmailCount: 1 },
},
{ upsert: true }
);

const embed = new EmbedBuilder()
.setDescription(`Created email ${email}@${domain}.is-a.dev with password ${password}`)
.setColor("#0096ff");
await interaction.editReply({ embeds: [embed], ephemeral: true });
}
};
39 changes: 39 additions & 0 deletions components/MailcowApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Endpoint = 'https://mail.is-a.dev'

export default class MailcowApi {
constructor (token) {
this.token = token
}

async get (path) {
const response = await fetch(`${Endpoint}${path}`, {
headers: {
'X-API-Key': this.token
}
})
return response.json()
}

async post (path, data) {
const response = await fetch(`${Endpoint}${path}`, {
method: 'POST',
headers: {
'X-API-Key': this.token,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
return response.json()
}

async delete (path) {
const response = await fetch(`${Endpoint}${path}`, {
method: 'DELETE',
headers: {
'X-API-Key': this.token
}
})
return response.json()
}
}

10 changes: 10 additions & 0 deletions models/emails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require("mongoose");

const EmailSchema = new mongoose.Schema({
_id: String, // Discord ID
DomainCount: String,
emails: Array,
EmailCount: String,
});

module.exports = mongoose.model("email", EmailSchema, "email");

0 comments on commit 240f3f2

Please sign in to comment.