<commit_before>var Listener = require('mail-listener2');
var dbhelper = require('./dbhelper');
var crypto = require('crypto');
var path = require('path');
var cheerio = require('cheerio');
var fs = require('fs');

var listener;
var attachment_dir = '/tmp';

function md5sum(str) {
    var sum = crypto.createHash('md5');
    sum.update(str);
    return sum.digest('hex');
}

exports.initConfig = function (mail) {
    attachment_dir = mail.attachmentsDir;

    listener = new Listener({
        username: mail.auth.mailaddr,
        password: <PASSWORD>,
        host: mail.imap.host,
        port: mail.imap.port,
        tls: true,
        tlsOptions: {rejectUnauthorized: false},
        mailbox: 'INBOX',
        searchFilter: [ 'UNSEEN' ],
        markSeen: true,
        fetchUnreadOnStart: true,
        mailParserOptions: {streamAttachments: false},
        attachments: true,
        attachmentOptions: {directory: 'attachments/'}
    });

    listener.on("server:connected", function () {
        console.log("imap connected LMY Saaby!");
    });
    listener.on("server:disconnected", function () {
        console.log("imap disconnected");
    });
    listener.on("error", function (err) {
        console.log(err);
    });

    listener.on("mail", function (mail, seqno, attributes) {
        // get DOM of html content
        // var $ = cheerio.load(mail.html);
        console.log(mail);

        // process attachments
        if (!!mail.attachments) {
            for (var i = 0; i < mail.attachments.length; ++i) {
                var att = mail.attachments[i];
                if (att.fileName == 'ATT00001') {
                    // if this attachment is created by Exchange, just ignore it.
                    continue;
                }
                var name = md5sum(att.content.toString());

                // when saving file on server, we don't care about file extensions
                var of = fs.createWriteStream(path.join(attachment_dir, name));
                of.write(att.content);
                of.end(function() { console.log('save attachment done.'); });

                // change the references if needed
                // if (!!att.contentId) {
                //     var targets = $('[src="cid:'+att.contentId+'"]');
                //     targets.attr('src', '/attachments/'+name);
                // }
            }
        }
        
        // write the mail received into database
        // TODO: add support for attachments
        console.log('adding new mail to database');
        dbhelper(
            mail.subject || '',<commit_msg>added support for attachments download.<commit_after>var Listener = require('mail-listener2');
var dbhelper = require('./dbhelper');
var crypto = require('crypto');
var path = require('path');
//var cheerio = require('cheerio');
var fs = require('fs');

var listener;
var attachment_dir = '/tmp';

function md5sum(str) {
    var sum = crypto.createHash('md5');
    sum.update(str);
    return sum.digest('hex');
}

exports.initConfig = function (mail) {
    attachment_dir = mail.attachmentsDir;

    listener = new Listener({
        username: mail.auth.mailaddr,
        password: <PASSWORD>,
        host: mail.imap.host,
        port: mail.imap.port,
        tls: true,
        tlsOptions: {rejectUnauthorized: false},
        mailbox: 'INBOX',
        searchFilter: [ 'UNSEEN' ],
        markSeen: true,
        fetchUnreadOnStart: true,
        mailParserOptions: {streamAttachments: false},
        attachments: true,
        attachmentOptions: {directory: 'attachments/'}
    });

    listener.on("server:connected", function () {
        console.log("imap connected LMY Saaby!");
    });
    listener.on("server:disconnected", function () {
        console.log("imap disconnected");
    });
    listener.on("error", function (err) {
        console.log(err);
    });

    listener.on("mail", function (mail, seqno, attributes) {
        // get DOM of html content
        // var $ = cheerio.load(mail.html);
        console.log(mail);

        // process attachments
        if (!!mail.attachments) {
            var appended = '';
            for (var i = 0; i < mail.attachments.length; ++i) {
                var att = mail.attachments[i];
                // if (att.fileName == 'ATT00001') {
                //     // if this attachment is created by Exchange, just ignore it.
                //     continue;
                // }
                var name = md5sum(att.content.toString())+'_'+att.generatedFileName;

                // when saving file on server, we don't care about file extensions
                var of = fs.createWriteStream(path.join(attachment_dir, name));
                of.write(att.content);
                of.end(function() { console.log('save attachment done.'); });

                // change the references if needed
                if (!!att.contentId && !!mail.html) {
                    var p = new RegExp('cid:'+att.contentId, 'g');
                    mail.html = mail.html.replace(p, '/attachments/'+name);
                    // var targets = $('[src="cid:'+att.contentId+'"]');
                    // targets.attr('src', '/attachments/'+name);
                }
                // appended += '<p>Attachment '+att.generatedFileName+': <form method="get" action="/attachments/'+name+'"><button type="submit">Download!</button></form>';
                appended += '<p>Attachment '+att.generatedFileName+': <a href="/attachments/'+name+'" download>download</a></p>';
            }
            mail.html += appended;
        }
        
        // write the mail received into database
        // TODO: add support for attachments
        console.log('adding new mail to database');
        dbhelper(
            mail.subject || '',
{'fraction_non_alphanumeric': 0.14453868790029428, 'fraction_numerical': 0.003462004500605851, 'mean_word_length': 2.0125130344108446, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1623721', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1553, 'n_words': 382}