Add IMAP server func

This commit is contained in:
fullgream 2025-05-27 04:09:38 +03:00
parent c5bd42ece7
commit d78fbf730e
3 changed files with 95 additions and 0 deletions

24
imap/index.js Normal file
View File

@ -0,0 +1,24 @@
const net = require("net");
const imap = require("./logic");
//const EventEmitter = require("events");
const server = net.createServer((socket) => {
const client = new imap.Client(socket);
socket.write("* OK IMAP4rev1 NodeIMAP Ready\r\n");
let state = "not_authenticated";
let currentMailbox = null;
//let tag = "";
socket.on("data", (data) => {
const line = data.toString().trim();
console.debug("IMAP data:", line);
const splitted = line.split(/\s{1,}/g);
if (splitted.length < 2) return socket.write(". BAD unknown command\r\n");
const [tag, command, ...args] = splitted;
client.command(tag, command, args);
});
});
module.exports = server;

56
imap/logic.js Normal file
View File

@ -0,0 +1,56 @@
class IMAPController {
constructor (socket, tag) {
this.socket = socket;
this.tag = tag;
}
out (answer) {
this.socket.write(`${this.tag} ${answer}`);
}
untaggedOut (answer) {
this.socket.write(answer);
}
close () {
this.socket.end();
}
}
class Commands {
constructor (client) {
this.client = client;
}
async capability (args, controller) {
/*if (args.length > 0)
return out();*/
controller.untaggedOut("* CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED\r\n");
controller.out("OK CAPABILITY completed\r\n");
}
async logout(args, controller) {
controller.untaggedOut("* BYE Logging out\r\n");
controller.out("OK LOGOUT completed\r\n");
controller.close();
}
}
class Client {
constructor (socket) {
this.socket = socket;
this.commands = new Commands(this);
}
command (tag, command, args) {
//console.debug("this >>", this, this.socket, this.commands);
const cmd = this.commands[command.toLowerCase()];
if (cmd === undefined) {
return this.socket.write(`${tag} BAD unknown command\r\n`);
}
cmd.call(this.commands, args, new IMAPController(this.socket, tag));
}
}
module.exports = { Client };

View File

@ -3,6 +3,8 @@ const fs = require("fs");
const net = require('net');
const { EventEmitter } = require("events");
const imap = require("./imap");
const initalConfig = `{
"pop3-config": {
"user": "example",
@ -55,3 +57,16 @@ pop3.init().then(() => {
//console.log(str);
}, 1);
});
imap.listen(
global.config["imap-config"].port,
global.config["imap-config"].host,
(err) => {
if (err) throw err;
console.log(`IMAP server running on ${
global.config["imap-config"].host
}:${
global.config["imap-config"].port
}`);
}
);