├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── lib ├── imap-connection.js └── imap-server.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Forbes Lindesay 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 11 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 13 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 14 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 15 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 16 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # imap-server 2 | 3 | An IMAP server in node.js 4 | 5 | This project is a work in progress and is just starting. The long term goal is to implement [RFC3501](http://tools.ietf.org/html/rfc3501) in all its glory. 6 | 7 | The current status is that it's a proof of concept good enough to pass Mozilla Thunderbird's checks as a valid IMAP server. It won't actually send any messages yet. 8 | 9 | ## Prior Art 10 | 11 | There are many implementations in other high level languages on GitHub, mostly incomplete. 12 | 13 | - [xpensia/ImapServer](https://github.com/xpensia/ImapServer) - language: JavaScript 14 | - [dirk/go-imap](https://github.com/dirk/go-imap/blob/master/imap.go) - language: go 15 | - [pasha010/imap4-server](https://github.com/pasha010/imap4-server/tree/master/src/net/imap4) - language: Java 16 | - [defunct/depot](https://github.com/defunct/depot/tree/master/src/main/java/com/goodworkalan/depot) - language: Java 17 | - [aox/aox](https://github.com/aox/aox) - language: C++ 18 | 19 | There is also an IMAP command parser extracted from [inbox](https://github.com/andris9/inbox) by [@andris9](https://github.com/andris9) 20 | 21 | - [imap-parser](https://github.com/hashmail/imap-parser) 22 | 23 | ## Contributing 24 | 25 | At this stage, open an issue and I'll add you to the contributors team so we can get started. 26 | 27 | ## Planned API 28 | 29 | The plan is to try and make it really simple to set up an IMAP server. They key is that we want an API that is protocol agnostic so that an identical API can be implemented for POP3. It's very much a work in progress, so don't expect it to stay the same. 30 | 31 | ```javascript 32 | var fs = require('fs'); 33 | var imap = require('imap-server') 34 | 35 | // SSL 36 | var certs = { 37 | key: fs.readFileSync('./ssl/key.pem'), 38 | cert: fs.readFileSync('./ssl/cert.pem') 39 | }; 40 | 41 | // IMAP server 42 | 43 | var server = imap({options}) 44 | 45 | server.authenticate(function (username, password, callback) { 46 | //authenticate user and either call callback with error as first arg or 'e-mail address' of user as second arg. 47 | }) 48 | 49 | server.mailBoxes(function (email, queryArgs, callback) { 50 | //return a list of the users mail boxes 51 | }) 52 | 53 | server.mailItems(function (email, queryArgs, callback) { 54 | //return a list of the users mail items 55 | }) 56 | 57 | server.addMailItem(function (email, mailItem, callback) { 58 | //write the mail message to the database 59 | }) 60 | server.updateMailItem(function (email, mailItem, callback) { 61 | //write the mail message to the database 62 | }) 63 | 64 | db.on('message', function (message) { 65 | //push messages to clients that support some kind of push type protocol 66 | server.push(message.email, message.body) 67 | }) 68 | 69 | // port 143 70 | var net = require('net') 71 | net.createServer(server).listen(process.env.IMAP_PORT || 143) 72 | // port 993 73 | var tls = require('tls') 74 | tls.createServer(certs, server).listen(process.env.IMAPS_PORT || 993) 75 | ``` 76 | 77 | TODO: plugin architecture for the future 78 | 79 | ## License 80 | 81 | MIT 82 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var net = require('net') 2 | 3 | var Parser = require('imap-parser') 4 | 5 | net.createServer(function (connection) { 6 | function write(str) { 7 | console.log('S: ' + JSON.stringify(str)) 8 | connection.write(str + '\r\n') 9 | } 10 | connection 11 | .pipe(new Parser()) 12 | .on('data', function (data) { 13 | function done(status, message) { 14 | write(data[0] + ' ' + (status || 'OK') + ' ' + data[1] + ' ' + (message || 'completed')) 15 | } 16 | var command = data[1].toUpperCase() 17 | switch (command) { 18 | case 'CAPABILITY': 19 | write('* CAPABILITY IMAP4rev1') 20 | done() 21 | break 22 | case 'LOGIN': 23 | var user = data[2] 24 | var pass = data[3] 25 | console.log('login(' + JSON.stringify(user) + ', ' + JSON.stringify(pass) + ')') 26 | done() 27 | break 28 | case 'LOGOUT': 29 | write('* BYE') 30 | done() 31 | break 32 | case 'LSUB': 33 | done() 34 | default: 35 | console.dir(data) 36 | break 37 | } 38 | }) 39 | .on('log', function (data) { 40 | console.log('C: ' + JSON.stringify(data)) 41 | }) 42 | write('* OK IMAP4rev1 Service Ready') 43 | connection.on('end', function () { 44 | console.log('CLOSED') 45 | }) 46 | }).listen(143) -------------------------------------------------------------------------------- /lib/imap-connection.js: -------------------------------------------------------------------------------- 1 | 2 | function ImapConnection(server, stream) { 3 | this.server = server 4 | } -------------------------------------------------------------------------------- /lib/imap-server.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = createServer 3 | function createServer() { 4 | this.db = {} 5 | 6 | function createConnection(stream) { 7 | return new ImapConnection(createConnection, stream) 8 | } 9 | 10 | Object.keys(createServer.prototype) 11 | .forEach(function (key) { 12 | createConnection[key] = createServer.prototype[key] 13 | }) 14 | } 15 | 16 | createServer.prototype.authenticate = function (fn) { 17 | this.db.authenticate = fn 18 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imap-server", 3 | "version": "0.0.0", 4 | "description": "An IMAP server in node.js", 5 | "main": "index.js", 6 | "dependencies": { 7 | "imap-parser": "https://github.com/hashmail/imap-parser/archive/feature/multi-line.tar.gz" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/hashmail/imap-server.git" 15 | }, 16 | "author": "ForbesLindesay", 17 | "license": "MIT" 18 | } 19 | --------------------------------------------------------------------------------