/*
    A helper which will forward a email as message
*/

// Main module of FoxyBot
var core = undefined;

var util = require("util");
var htmlToText = require('html-to-text');

// Email client
var Client = require('node-poplib-gowhich').Client;

var bot = undefined;

function setBot(botInstance)
{
    bot = botInstance;
}

function parseMessage(msg)
{
    var uid = /UserID: \[(\d+)\]/gi;
    var gid = /GuildID: \[(\d+)\]/gi;
    var cid = /ChannelID: \[(\d+)\]/gi;

    var uidMatch = uid.exec(msg.text);
    var gidMatch = gid.exec(msg.text);
    var cidMatch = cid.exec(msg.text);

    //console.log("UID: " + util.inspect(uidMatch));
    //console.log("GID: " + util.inspect(gidMatch));
    //console.log("CID: " + util.inspect(cidMatch));
    if(uidMatch != null && gidMatch != null && cidMatch != null)
    {
        msg.uid = uidMatch[1];
        msg.gid = gidMatch[1];
        msg.cid = cidMatch[1];
    }
}

var client;

// Initialize plugin and here you can add custom Foxy's commands
function registerCommands(/*bot_commands.js module*/ foxyCore)
{
    core = foxyCore;

    client = new Client({
        hostname: core.botConfig.pop.host,
        port:  core.botConfig.pop.port,
        tls: false,
        mailparser: true,
        username: core.botConfig.pop.login,
        password: core.botConfig.pop.pass
    });

	setInterval(function()
    {
        //console.log("Check email now...");
        client.connect(function()
        {
            //console.log("Connect");
            client.stat(function(err, stat)
            {
                //console.log("STAT  err " + err + " stat " + util.inspect(stat));
                if(stat.count == 0)
                {
                    client.quit();
                    return;
                }

                client.retrieveAll(function(err, messages)
                {
                    console.log("RETREIVE err " + err);
                    if(err != null)
                    {
                        client.quit();
                        return;
                    }

                    messages.forEach(function(message)
                    {
                        console.log(message.subject);
                        console.log("Message from: " + util.inspect(message.from));
                        console.log("Message body:\n===============\n\n" + util.inspect(message) + "\n\n========================\n");
                        var msgText = (typeof(message.text) != "undefined" ? message.text : htmlToText.fromString(message.html));
                        var msgRes = {text: msgText, uid: 0, gid: 0, cid: 0};
                        parseMessage(msgRes);
                        var outText = "\n" + msgText;
                        var chan = bot.channels.get(msgRes.cid != 0 ? msgRes.cid : core.botConfig.defaultChannel[0]);

                        chan.send("__I got email reply from " + message.from[0].name + " for " + (msgRes.uid != 0 ? "<@" + msgRes.uid + ">" : "someone") + "__:\n",
                            {
                                embed:
                                {
                                    color: 0xAF0000,
                                    fields: [{
                                        name : message.subject,
                                        value: outText
                                    }],
                                    footer: {
                                        text: "Note: to send email reply, you must have 'Wohlstand' (or 'Wohl') mention in every your message (letter sign 📧 means email was sent)"
                                    }
                                }, split: true
                            }
                        ).catch(core.msgSendError);
                    });

                    client.deleteAll(function(err, statuses)
                    {
                        console.log("DEL: err " + err);
                        client.quit();
                    });
                })
            });
        });
    }, 20000);
}

module.exports =
{
    setBot:   setBot,
    registerCommands:   registerCommands
};


@@ -585,176 +585,98 @@
 t);%0A
-%0A    //console.log(%22UID: %22 + util.inspect(uidMatch));%0A    //console.log(%22GID: %22 + util.inspect(gidMatch));%0A    //console.log(%22CID: %22 + util.inspect(cidMatch));%0A
+    try%0A    %7B%0A        if(uidMatch != null)%0A            msg.uid = uidMatch%5B1%5D;%0A   
     
+ 
 if(
-u
+g
 idMa
@@ -690,32 +690,56 @@
 null
- && gidMatch != null && 
+)%0A            msg.gid = gidMatch%5B1%5D;%0A        if(
 cidM
@@ -752,26 +752,24 @@
 = null)%0A    
-%7B%0A
         msg.
@@ -760,39 +760,39 @@
             msg.
-u
+c
 id = 
-u
+c
 idMatch%5B1%5D;%0A    
@@ -795,64 +795,83 @@
     
+%7D%0A
     
-msg.gid = gidMatch%5B1%5D;
+catch(e)
 %0A    
+%7B%0A
     
-msg.cid = cidMatch%5B1%5D
+    console.log(%22parseMessage: FAILED: %22 + e.name)
 ;%0A  

Allow to use every meta marker independently (don't require all of them)