Skip to content

Instantly share code, notes, and snippets.

@devstator82
Created October 5, 2011 19:42
Show Gist options
  • Save devstator82/1265458 to your computer and use it in GitHub Desktop.
Save devstator82/1265458 to your computer and use it in GitHub Desktop.
imap connections
var util = require('util');
var sys = require('sys');
var $ = require('jquery');
var db = require('../../initializers/mongodb');
var Hashtable = require('../../lib/jshashtable');
var ImapConnection = require('imap').ImapConnection;
var Channel = db.model('Channel');
var xoauth = require('../../lib/xoauth');
// Contains all the connected push instances
var instances = new Hashtable();
STATUS = {
PENDING: 0,
CONNECTING: 1,
SELECTING: 2,
SELECTED: 3,
PROCESSING: 4,
IDLING: 5,
COUNTING: 6,
AUTH_FAILURE: 7,
ERROR: 8
};
function updateCount(imap, mailbox, callback) {
var max = mailbox.messages.total;
var min = Math.max(max - 50, 1);
// Empty mailbox
if (max == 0) {
callback();
}
else {
var fetch = imap.fetch(min + ':' + max);
var messages = [];
fetch.on('message', function(msg) {
messages.push(msg)
});
fetch.on('end', function() {
var unread = 0;
messages.forEach(function(msg) {
if (msg.flags.indexOf('\\Seen') == -1)
unread++;
});
//console.log('Have ' + unread + ' unread messages');
callback();
});
}
}
function start_connection(channel) {
if (!instances.containsKey(channel._id)) {
// Create instance if it does not exist yet
instances.put(channel._id, {
imap: null,
messages: 0,
status: STATUS.PENDING
});
}
var instance = instances.get(channel._id);
xoauth.auth(channel, function(data) {
var imap = instance.imap = new ImapConnection ({
host: 'imap.gmail.com',
port: 993,
secure: true,
xoauth: data,
connTimeout: 100000,
debug: function(str) {
//console.log(str);
}
});
instance.status = STATUS.CONNECTING;
imap.connect(function(err) {
if (err) {
//console.log(err);
// Failed to login, if this is an authentication error
// mark the channel as failed.
instance.status = STATUS.AUTH_FAILURE;
}
else {
instance.status = STATUS.SELECTING;
try {
imap.openBox('INBOX', false, function(err, mailbox) {
if (err) {
console.log(err);
instance.status = STATUS.ERROR;
}
else {
instance.status = STATUS.PROCESSING;
// updateCount(imap, mailbox, function() {
// instance.status = STATUS.IDLING;
//
// imap.idle();
// });
}
});
}
catch (err) {
//console.log(err);
instance.status = STATUS.ERROR;
}
}
});
imap.on('error', function(err) {
instance.status = STATUS.ERROR
console.log('An error has occured: ' + err)
imap.logout();
// Retry after one minute
// setTimeout(function () {
// start_connection(channel);
// }, 1000);
});
imap.on('mail', function() {
// imap.done();
// console.log('Stopped idle command')
//
// updateCount(imap, mailbox, function() {
// console.log('Sending idle command');
//
// imap.idle();
// });
});
});
}
module.exports = {
index: function(req, res) {
var statuses = new Array();
for (var i = 0; i <= 7; i++)
statuses[i] = 0;
instances.each(function(key, instance) {
statuses[instance.status]++;
});
res.render('index', {
total: instances.size(),
pending: statuses[0],
connecting: statuses[1],
selecting: statuses[2],
selected: statuses[3],
processing: statuses[4],
idling: statuses[5],
counting: statuses[6],
auth_failure: statuses[7],
error: statuses[8],
});
},
load_all: function() {
Channel.find({ 'display_name': 'google' }, function (err, channels) {
channels.forEach(function(channel) {
console.log('Loading ' + channel.address.address);
start_connection(channel);
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment