/***************************************************************************

    libdope
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


#ifndef DDMAILMANAGER_H
#define DDMAILMANAGER_H

#include <ddstringrow.h>
#include <dditemset.h>
#include <ddthread.h>
#include <ddwebview.h>
#include <deque>
#include <ddthreadmutex.h>


/// ddmail : einfache struktur, um mails in der wait-queue su representieren.
/**
*/

class ddmail : public ddbaseobject
{

public: 
	
	ddstring receiver;
	ddstring message;
	ddstring subject;
	ddstring sender;
	
	ddmail()
	{
	}
	
	ddmail( const ddstring& rec,	const ddstring& msg ,	const ddstring& subj="", const ddstring& asender="" )
	: receiver( rec ),	message( msg ) , subject( subj ), sender( asender )
	{
	}
	
	virtual ~ddmail()
	{
	}
	
};


/***************************************************************************

    supportware
    Copyright (C) 2001-2008 by c.holzheuer
    c.holzheuer@sourceworx.org

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

***************************************************************************/


/// ddmailmanager
/**

*/

typedef deque<ddmail> ddmailqueue;



class ddmailmanager : public ddthreadrunnable 
{

public: 
	
	ddmailmanager();
	virtual ~ddmailmanager();
	
	void 	 init_mailer( ddconfig& config, const ddstring& path );
	size_t enqueue_mail( const ddstring& recv, const ddstring& msg, const ddstring& subject="", const ddstring& sender=""  );
	
	void   create_nick_mail( const ddstring& nickname, const ddstring& nickmail, const ddstring& newnick );
	void   create_passwd_mail( const ddstring& nickname, const ddstring& nickmail, const ddstring& passwd );
	void   create_login_mail( const ddstring& nickname, const ddstring& nickmail, const ddstring& passwd, const ddstring& domain );
	void	 create_alert_mails( const ddstring& idparent, const ddstring& nickmail, const ddstring& nickname, const ddstring& subject, const ddstring& domain="www" );
	void	 create_hint_mail( const ddstring& nickname, const ddstring& nickmail, const ddstring& subject, const ddstring& sysmessage="", const ddstring& syslink = "" );
	
	void   run();
	
	// warum static? wird sonst nirgends bennutzt
	//static void send_mail( const ddstring& msg, const ddstring& subj="", const ddstring& recv="", const ddstring& sender="" );
	void send_mail( const ddstring& msg, const ddstring& subj="", const ddstring& recv="", const ddstring& sender="" );
	void pipe_mail( dditem& templ, const ddstring& recv, const ddstring& subj, const ddstring& sender );

	bool is_alertet( const ddstring& nickname, const ddstring& parentid );
	void clear_alert_flag( const ddstring& nickname, const ddstring& parentid );
	void set_alert_flag( const ddstring& nickname, const ddstring& parentid );
	
	
	inline int get_reply_count()
	{
		return _replymails;
	}
	
	
	inline void reset_reply_count()
	{
		_replymails = 0;
	}
	
	
	inline dditem& get_item( const ddstring& key )
	{
		if( _templates.contains_key( key ) )
			return _templates[ key ];
		throw runtime_error( "mailer: get_item: key not found: " + key );			
	}
	
	
protected:

	void   create_reply_mail( const ddstring& parentid, const ddstring& nickmail, const ddstring& nickname, const ddstring& subject, const ddstring& domain, bool isguest );
	
	static ddstring themailcmd;
	
	int						_replymails;
	dditemset	  	_templates;
	ddmailqueue		_queue;
	ddthreadmutex	_mutex;	
	ddwebview			_mailalerts;
	set<ddstring>	_alertcache;
};

#endif
