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

    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.

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


#ifndef ddformchecker_H
#define ddformchecker_H

#include <ddstringrow.h>
#include <dditem.h>
#include <ddwebcgi.h>
#include <ddconfig.h>
#include <dddatatable.h>
#include <ddstringset.h>
#include <dddataobject.h>

class ddformrule;
class ddformrulecall;
class ddformruleparser;
class dduserid;
class ddusermanager;
class ddwebsite;
class ddformmanager;
class ddmailmanager;

typedef vector<ddformrule*> ddformrulevec;


/// ddformchecker : Klasse zum Testen eines bestimmten Eingabeformulars.
/**

*/

class ddformchecker : public ddbasenamedobject
{

public:

	ddformchecker();
	ddformchecker( ddformmanager*	parent, ddformruleparser& parser, ddconfigsection& rules );
	virtual ~ddformchecker();

	bool init_form_rules( ddformruleparser& parser, ddconfigsection& rules );
	bool check_form( dditem& form, dduserid& user );
  void check_form( dditem& form, dduserid& user, ddwebcgi& cgi );
  
protected:

	bool add_rules( ddformruleparser& parser, const ddstring& key, const ddstring& querystring );
	
	ddformmanager*	_parent;
 	ddformrulevec 	_rules;
 	ddconfigsection _messages;
  ddstring				_errkey;

};


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

    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.

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


typedef ddmapptr<ddformchecker> ddformcheckermap;

/// ddformmanager: Klasse zum Testen von Eingabeformularen.

/**
	ddformmanager ist die basisklasse des message managers und deshalb eine dbtable.
	Als unvollständige klasse kann sie nicht standalone benutzt werden, daher ist
	der konstrukur protected.
*/

class ddformmanager : public ddbaseobject
{

friend class ddformrulecall;
friend class ddformchecker;

public:

	ddformmanager();
	virtual ~ddformmanager();
	
  void init_form_rules( ddwebsite& mysite );
  
	/**
  	Gibt eine Referenz auf die interne Kill-Word Liste zurück.
  	Killwords sind "verbotene Worte", wie indizierte Spiel, Spamlinks 
  	etc. Beiträge mit verbotenen Worten werden gar nicht erst in die 
  	Datenbank geschrieben.
  */

  inline ddstringset& get_blocked_words()
  {
  	return _killwords;
  }
  
  
	/**
  	Gibt eine Referenz auf die interne Zensur-Liste zurück.
  	Zensierte Worte werden durch *Z* ersetzt.
  */

  inline ddstringset& get_censored_words()
  {
  	return _badwords;
  }
  
  /**
  	@returns true für 'böse' postings.
  	FIX!
  	Eigentlich sollte das posting automatisch per group_id 2
  	in den müll wandern, aber da nicht alle tables eine group_id
  	haben, gibts diesen hack.
  */
  
  inline bool contains_blocked_words()
  {
  	return _contains_blocked_words;
  }
  
	bool check_form( const ddstring& formname, dditem& form, dduserid& user, ddwebcgi& cgi );
 
protected:
	
	bool set_error_message( dditem& form, const ddstring& key, const ddstring& msgkey );
		
	bool check_accepting_terms( ddstring& src );	
	bool check_blocked_word( ddstring& src );
	bool check_bad_word( ddstring& src );
	
	bool check_user_is_member( ddstring& src );  
 	bool check_nickname_exists( ddstring& src );  
  bool check_new_nickname_exists( ddstring& src );
  bool check_nickname_valid( ddstring& src );
  bool check_new_nickname_valid( ddstring& src );
  
  bool check_nickmail_valid( ddstring& src );
	bool check_chain_posting( ddstring& src );
	bool check_repeated_posting( ddstring& src );
	bool check_mail_alert( ddstring& src );
	bool check_state_valid( ddstring& src );
	bool check_group_valid( ddstring& src );
	
	ddwebsite*						_website;
	dduserid*							_currentuser;
	ddwebcgi*							_currentcgi;
	int										_posting_delay;
	bool									_contains_blocked_words;
	bool									_has_error;
	ddthreadmutex					_checkerlock;
	ddformcheckermap			_formcheckermap;
	ddconfigsection 			_messages;
	
	ddstringset  					_killwords;	
	ddstringset						_badwords;
	
	ddstring 							_errorkey;
	ddstring 							_errorsymbol;
	ddstring 							_censorstring;

};


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

    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.

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


/// ddformrule: eine einzelne Regel


typedef bool (ddformrulecall:: *ddrulecall)( ddstring& );

class ddformrule : public ddbasenamedobject
{

public:

	ddformrule( bool is_op_and=true, ddformrule* child1=0, ddformrule* child2=0 );
	virtual ~ddformrule();

  virtual bool apply( ddstring& src, ddstring& message );
  virtual void dump( ddstring blank );

protected:

  bool        _is_op_and;
  ddformrule* _child1;
  ddformrule* _child2;


};


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

    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.

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


class ddformrulecall : public ddformrule
{

public:

  ddformrulecall( ddformmanager* parent, ddrulecall call, const ddstring& cmdkey, const ddstring& msgkey, const ddstring& param="" );

  bool apply( ddstring& src, ddstring& message );
 
  // standard test functions
  bool check_empty( ddstring& src );
  bool check_not_empty( ddstring& src );
  bool check_contains( ddstring& src );
 	bool check_minsize( ddstring& src );
  bool check_maxsize( ddstring& src );
	bool check_equal( ddstring& src );
	bool check_not_equal( ddstring& src );
	bool check_less_than( ddstring& src );
	bool check_greater_than( ddstring& src );
	bool check_is_subject( ddstring& src );
	
	bool check_accepting_terms( ddstring& src );
	bool check_blocked_word( ddstring& src );
	bool check_bad_word( ddstring& src );	
	bool check_user_is_member( ddstring& src );
	bool check_nickname_exists( ddstring& src );
	bool check_new_nickname_exists( ddstring& src );	
	bool check_nickname_valid( ddstring& src );
  bool check_new_nickname_valid( ddstring& src );
  bool check_nickmail_valid( ddstring& src );
	bool check_chain_posting( ddstring& src );
	bool check_repeated_posting( ddstring& src );
	bool check_mail_alert( ddstring& src );
	bool check_state_valid( ddstring& src );
	bool check_group_valid( ddstring& src );
		
protected:

	ddformmanager*	_parent;
	ddrulecall		 	_call;
  ddstring    		_cmdkey;
 	ddstring	  		_msgkey;
	ddstring	  		_param;
	
	 
};


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

    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.

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


/// ddformruleparser : Erzeugt die Regelmenge für ein Form aus der Config-Datei.
/**

*/

typedef map<ddstring,ddrulecall> ddrulecallmap;

class ddformruleparser : public ddbaseobject
{

public:

	ddformruleparser( ddformmanager*	parent );
	virtual ~ddformruleparser();

  ddformrule* parse( const ddstring& rule );

protected:

	void init_call_map();
	void split( const ddstring& rule, const ddstring& sep );

  ddformrule* parse_expression();
  ddformrule* parse_term();
  ddformrule* parse_primary();

	ddformmanager*	 _parent;
  ddrulecallmap    _calls;
	ddstringrow         _rules;
  size_t           _idx;
  
};

#endif
