1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*
* Copyright (C) 2004-2010 See the AUTHORS file for details.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* Email Monitor / Retrieval
* Author: imaginos <imaginos@imaginos.net>
*/
#include "MD5.h"
#include "User.h"
#include "znc.h"
#include <sstream>
using std::stringstream;
struct EmailST
{
CString sFrom;
CString sSubject;
CString sUidl;
u_int iSize;
};
class CEmailJob : public CTimer
{
public:
CEmailJob(CModule* pModule, unsigned int uInterval, unsigned int uCycles, const CString& sLabel, const CString& sDescription)
: CTimer(pModule, uInterval, uCycles, sLabel, sDescription) {}
virtual ~CEmailJob() {}
protected:
virtual void RunJob();
};
class CEmail : public CModule
{
public:
MODCONSTRUCTOR(CEmail)
{
m_iLastCheck = 0;
m_bInitialized = false;
}
virtual ~CEmail() {}
virtual bool OnLoad(const CString & sArgs, CString& sMessage) {
m_sMailPath = sArgs;
StartParser();
if (m_pUser->IsUserAttached())
StartTimer();
return true;
}
virtual void OnClientLogin()
{
stringstream s;
s << "You have " << m_ssUidls.size() << " emails.";
PutModule(s.str());
StartTimer();
}
virtual void OnClientDisconnect()
{
RemTimer("EMAIL::" + m_pUser->GetUserName());
}
void StartTimer()
{
if (!FindTimer("EMAIL::" + m_pUser->GetUserName()))
{
CEmailJob *p = new CEmailJob(this, 60, 0, "EmailMonitor", "Monitors email activity");
AddTimer(p);
}
}
virtual void OnModCommand(const CString& sCommand);
void StartParser();
void ParseEmails(const vector<EmailST> & vEmails)
{
if (!m_bInitialized)
{
m_bInitialized = true;
for (u_int a = 0; a < vEmails.size(); a++)
m_ssUidls.insert(vEmails[a].sUidl);
stringstream s;
s << "You have " << vEmails.size() << " emails.";
PutModule(s.str());
} else
{
set<CString> ssUidls;
CTable Table;
Table.AddColumn("From");
Table.AddColumn("Size");
Table.AddColumn("Subject");
for (u_int a = 0; a < vEmails.size(); a++)
{
if (m_ssUidls.find(vEmails[a].sUidl) == m_ssUidls.end())
{
//PutModule("------------------- New Email -------------------");
Table.AddRow();
Table.SetCell("From", vEmails[a].sFrom.Ellipsize(32));
Table.SetCell("Size", CString(vEmails[a].iSize));
Table.SetCell("Subject", vEmails[a].sSubject.Ellipsize(64));
}
ssUidls.insert(vEmails[a].sUidl);
}
m_ssUidls = ssUidls; // keep the list in synch
if (Table.size()) {
PutModule(Table);
stringstream s;
s << "You have " << vEmails.size() << " emails.";
PutModule(s.str());
}
}
}
private:
CString m_sMailPath;
time_t m_iLastCheck;
set<CString> m_ssUidls;
bool m_bInitialized;
};
class CEmailFolder : public CSocket
{
public:
CEmailFolder(CEmail *pModule, const CString & sMailbox) : CSocket(pModule)
{
m_pModule = pModule;
m_sMailbox = sMailbox;
EnableReadLine();
}
virtual ~CEmailFolder()
{
if (!m_sMailBuffer.empty())
ProcessMail(); // get the last one
if (!m_vEmails.empty())
m_pModule->ParseEmails(m_vEmails);
}
virtual void ReadLine(const CS_STRING & sLine)
{
if (sLine.Left(5) == "From ")
{
if (!m_sMailBuffer.empty())
{
ProcessMail();
m_sMailBuffer.clear();
}
}
m_sMailBuffer += sLine;
}
void ProcessMail()
{
EmailST tmp;
tmp.sUidl = (char *)CMD5(m_sMailBuffer.Left(255));
VCString vsLines;
VCString::iterator it;
m_sMailBuffer.Split("\n", vsLines);
for (it = vsLines.begin(); it != vsLines.end(); it++) {
CString sLine(*it);
sLine.Trim();
if (sLine.empty())
break; // out of the headers
if (sLine.Equals("From: ", false, 6))
tmp.sFrom = sLine.substr(6, CString::npos);
else if (sLine.Equals("Subject: ", false, 9))
tmp.sSubject = sLine.substr(9, CString::npos);
if ((!tmp.sFrom.empty()) && (!tmp.sSubject.empty()))
break;
}
tmp.iSize = m_sMailBuffer.length();
m_vEmails.push_back(tmp);
}
private:
CEmail *m_pModule;
CString m_sMailbox;
CString m_sMailBuffer;
vector<EmailST> m_vEmails;
};
void CEmail::OnModCommand(const CString& sCommand)
{
CString sCom = sCommand.Token(0);
if (sCom == "timers")
{
ListTimers();
} else
PutModule("Error, no such command [" + sCom + "]");
}
void CEmail::StartParser()
{
CString sParserName = "EMAIL::" + m_pUser->GetUserName();
if (m_pManager->FindSockByName(sParserName))
return; // one at a time sucker
CFile cFile(m_sMailPath);
if ((!cFile.Exists()) || (cFile.GetSize() == 0))
{
m_bInitialized = true;
return; // der
}
if (cFile.GetMTime() <= m_iLastCheck)
return; // only check if modified
int iFD = open(m_sMailPath.c_str(), O_RDONLY);
if (iFD >= 0)
{
m_iLastCheck = time(NULL);
CEmailFolder *p = new CEmailFolder(this, m_sMailPath);
p->SetRSock(iFD);
p->SetWSock(iFD);
m_pManager->AddSock(p, "EMAIL::" + m_pUser->GetUserName());
}
}
void CEmailJob::RunJob()
{
CEmail *p = (CEmail *)m_pModule;
p->StartParser();
}
MODULEDEFS(CEmail, "Monitors Email activity on local disk /var/mail/user")
|