mirror of
https://github.com/znc/znc.git
synced 2026-06-28 14:01:35 +02:00
moving to standard makefile system, and cpp only extension
git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@46 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
#include "main.h"
|
||||
#include "User.h"
|
||||
#include "Nick.h"
|
||||
#include "Modules.h"
|
||||
#include "Chan.h"
|
||||
#include "Utils.h"
|
||||
#include "md5.h"
|
||||
#include <pwd.h>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
/*
|
||||
* Email Monitor / Retrieval
|
||||
* Author: imaginos <imaginos@imaginos.net>
|
||||
*
|
||||
* $Log$
|
||||
* Revision 1.1 2005/03/30 18:46:35 imaginos
|
||||
* moving to standard makefile system, and cpp only extension
|
||||
*
|
||||
* Revision 1.1.1.1 2004/08/24 00:08:52 prozacx
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
struct EmailST
|
||||
{
|
||||
string sFrom;
|
||||
string sSubject;
|
||||
string sUidl;
|
||||
u_int iSize;
|
||||
};
|
||||
|
||||
class CEmailJob : public CTimer
|
||||
{
|
||||
public:
|
||||
CEmailJob( CModule* pModule, unsigned int uInterval, unsigned int uCycles, const string& sLabel, const string& 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()
|
||||
{
|
||||
vector<Csock*> vSocks = m_pManager->FindSocksByName( "EMAIL::" + m_pUser->GetUserName() );
|
||||
for( u_int a = 0; a < vSocks.size(); a++ )
|
||||
m_pManager->DelSockByAddr( vSocks[a] );
|
||||
}
|
||||
|
||||
virtual bool OnLoad(const string & sArgs) {
|
||||
m_sMailPath = sArgs;
|
||||
|
||||
StartParser();
|
||||
if ( m_pUser->IsUserAttached() )
|
||||
StartTimer();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
virtual void OnUserAttached()
|
||||
{
|
||||
stringstream s;
|
||||
s << "You have " << m_ssUidls.size() << " emails.";
|
||||
PutModule( s.str() );
|
||||
StartTimer();
|
||||
}
|
||||
virtual void OnUserDetached()
|
||||
{
|
||||
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 string GetDescription()
|
||||
{
|
||||
return ( "Monitors Email activity on local disk /var/mail/user" );
|
||||
}
|
||||
|
||||
virtual void OnModCommand( const string& 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<string> 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", CUtils::Ellipsize( vEmails[a].sFrom, 32 ) );
|
||||
Table.SetCell( "Size", CUtils::ToString( vEmails[a].iSize ) );
|
||||
Table.SetCell( "Subject", CUtils::Ellipsize( vEmails[a].sSubject, 64 ) );
|
||||
}
|
||||
ssUidls.insert( vEmails[a].sUidl );
|
||||
}
|
||||
|
||||
m_ssUidls = ssUidls; // keep the list in synch
|
||||
|
||||
if (Table.size()) {
|
||||
unsigned int uTableIdx = 0;
|
||||
string sLine;
|
||||
while ( Table.GetLine( uTableIdx++, sLine ) ) {
|
||||
PutModule( sLine );
|
||||
}
|
||||
|
||||
stringstream s;
|
||||
s << "You have " << vEmails.size() << " emails.";
|
||||
PutModule( s.str() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
string m_sMailPath;
|
||||
u_int m_iLastCheck;
|
||||
set<string> m_ssUidls;
|
||||
bool m_bInitialized;
|
||||
};
|
||||
|
||||
class CEmailFolder : public Csock
|
||||
{
|
||||
public:
|
||||
CEmailFolder( CEmail *pModule, const string & sMailbox ) : Csock()
|
||||
{
|
||||
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.substr( 0, 5 ) == "From " )
|
||||
{
|
||||
if ( !m_sMailBuffer.empty() )
|
||||
{
|
||||
ProcessMail();
|
||||
m_sMailBuffer.clear();
|
||||
}
|
||||
}
|
||||
m_sMailBuffer += sLine;
|
||||
}
|
||||
|
||||
void ProcessMail()
|
||||
{
|
||||
EmailST tmp;
|
||||
tmp.sUidl = (char *)CMD5( m_sMailBuffer.substr( 0, 255 ) );
|
||||
string sLine;
|
||||
u_int iPos = 0;
|
||||
while( ::ReadLine( m_sMailBuffer, sLine, iPos ) )
|
||||
{
|
||||
CUtils::Trim( sLine );
|
||||
if ( sLine.empty() )
|
||||
break; // out of the headers
|
||||
|
||||
if ( strncasecmp( sLine.c_str(), "From: ", 6 ) == 0 )
|
||||
tmp.sFrom = sLine.substr( 6, string::npos );
|
||||
else if ( strncasecmp( sLine.c_str(), "Subject: ", 9 ) == 0 )
|
||||
tmp.sSubject = sLine.substr( 9, string::npos );
|
||||
|
||||
if ( ( !tmp.sFrom.empty() ) && ( !tmp.sSubject.empty() ) )
|
||||
break;
|
||||
}
|
||||
tmp.iSize = m_sMailBuffer.length();
|
||||
m_vEmails.push_back( tmp );
|
||||
}
|
||||
private:
|
||||
CEmail *m_pModule;
|
||||
string m_sMailbox;
|
||||
string m_sMailBuffer;
|
||||
vector<EmailST> m_vEmails;
|
||||
};
|
||||
|
||||
void CEmail::OnModCommand( const string& sCommand )
|
||||
{
|
||||
u_int iPos = sCommand.find( " " );
|
||||
string sCom, sArgs;
|
||||
if ( iPos == string::npos )
|
||||
sCom = sCommand;
|
||||
else
|
||||
{
|
||||
sCom = sCommand.substr( 0, iPos );
|
||||
sArgs = sCommand.substr( iPos + 1, string::npos );
|
||||
}
|
||||
|
||||
if ( sCom == "timers" )
|
||||
{
|
||||
ListTimers();
|
||||
} else
|
||||
PutModule( "Error, no such command [" + sCom + "]" );
|
||||
}
|
||||
|
||||
void CEmail::StartParser()
|
||||
{
|
||||
string 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( (Csock *)p, "EMAIL::" + m_pUser->GetUserName() );
|
||||
}
|
||||
}
|
||||
|
||||
void CEmailJob::RunJob()
|
||||
{
|
||||
CEmail *p = (CEmail *)m_pModule;
|
||||
p->StartParser();
|
||||
}
|
||||
MODULEDEFS(CEmail)
|
||||
|
||||
Reference in New Issue
Block a user