view src/context.h @ 74:b7449114ebb0

start coding on new config syntax
author carl
date Sun, 10 Jul 2005 14:19:00 -0700
parents 2b369f7db7bf
children 1142e46be550
line wrap: on
line source

#ifndef context_include
#define context_include

#include "tokenizer.h"
#include <map>


enum status {oksofar,       // not rejected yet
             white,         // whitelisted
             black,         // blacklisted
             reject,        // rejected by a dns list
             reject_tag,    // too many bad html tags
             reject_host};  // too many hosts/urls in body

class DNSBL;
class CONTEXT;
class recorder;

typedef map<char *, char *, ltstr>        string_map;
typedef set<int>                          int_set;
typedef list<char *>                      string_list;
typedef DNSBL *                           DNSBLP;
typedef list<DNSBLP>                      dnsblp_list;
typedef map<char *, DNSBLP, ltstr>        dnsblp_map;
typedef CONTEXT *                         CONTEXTP;
typedef list<CONTEXTP>                    context_list;
typedef map<char *, CONTEXTP, ltstr>      context_map;
typedef map<char *, int, ltstr>           ns_mapper;

struct DNSBL {
    char    *name;      // nickname for this dns based list
    char    *suffix;    // blacklist suffix like blackholes.five-ten-sg.com
    char    *message;   // error message with one or two %s operators for the ip address replacement
    DNSBL(char *n, char *s, char *m);
};

class CONTEXT {
    CONTEXTP        parent;
    char *          name;
    context_map     children;           // map child context names to their contexts
    string_set      env_to;             //
    string_map      env_from;           // map senders to white/black/unknown
    context_map     env_from_context;   // map senders to a child context
    char *          env_from_default;   // default value for senders that are not found in the map white/black/unknown/inherit
    bool            content_filtering;  //
    char *          content_suffix;     // for sbl url body filtering
    char *          content_message;    // ""
    string_set      content_host_ignore;// hosts to ignore for content sbl checking
    string_set      content_tlds;       //
    string_set      html_tags;          // set of valid html tags
    int             host_limit;         // limit on host names
    char *          host_limit_message; // error message for excessive host names
    bool            host_random;        // pick a random selection of host names rather than error for excessive hosts
    int             tag_limit;          // limit on bad html tags
    char *          tag_limit_message;  // error message for excessive bad html tags
    dnsblp_map      dnsbl_names;        // name to dnsbl mapping for lists that are available in this context and children
    dnsblp_list     dnsbl_list;         // list of dnsbls to be used in this context

public:
    CONTEXT(CONTEXTP parent_, char *name_);
    ~CONTEXT();
    CONTEXTP    get_parent()                                {return parent;};
    char*       get_full_name(char *buf, int size);
    void        add_context(CONTEXTP child)                 {children[child->name] = child;};
    bool        allow_env_to(char *to)                      {return (parent) ? parent->cover_env_to(to) : true;};
    bool        cover_env_to(char *to);

    void        add_to(char *to)                            {env_to.insert(to);};
    void        add_from(char *from, char *status)          {env_from[from] = status;};
    void        add_from_context(char *from, CONTEXTP con)  {env_from_context[from] = con;};
    void        set_from_default(char *status)              {env_from_default = status;};
    char*       find_from(char *from);
    CONTEXTP    find_from_context(char *from);
    CONTEXTP    find_from_context_name(char *name);

    void        set_content_filtering(bool filter)          {content_filtering = filter;};
    void        set_content_suffix(char *suffix)            {content_suffix    = suffix;};
    void        set_content_message(char *message)          {content_message   = message;};
    void        add_ignore(char *host)                      {content_host_ignore.insert(host);};
    void        add_tld(char *tld)                          {content_tlds.insert(tld);};

    void        set_host_limit(int limit)                   {host_limit         = limit;};
    void        set_host_message(char *message)             {host_limit_message = message;};
    void        set_host_random(bool random)                {host_random        = random;};
    void        set_tag_limit(int limit)                    {tag_limit          = limit;};
    void        set_tag_message(char *message)              {tag_limit_message  = message;};
    void        add_tag(char *tag)                          {html_tags.insert(tag);};

    void        add_dnsbl(char *name, DNSBLP dns)           {dnsbl_names[name] = dns;};
    void        add_dnsbl(DNSBLP dns)                       {dnsbl_list.push_back(dns);};
    DNSBLP      find_dnsbl(char *name);

    int             get_host_limit()                        {return host_limit;};
    bool            get_host_random()                       {return host_random;};
    char*           get_content_suffix()                    {return content_suffix;};
    char*           get_content_message()                   {return content_message;};
    string_set&     get_content_host_ignore()               {return content_host_ignore;};
    string_set&     get_content_tlds()                      {return content_tlds;};
    string_set&     get_html_tags()                         {return html_tags;};
    dnsblp_list&    get_dnsbl_list()                        {return dnsbl_list;};
    bool            get_content_filtering()                 {return content_filtering;};

    bool        acceptable_content(recorder &memory, char *&msg);
    bool        ignore_host(char *host);

    void        dump(int level = 0);
};


struct CONFIG {
    // the only mutable stuff once it has been loaded from the config file
    int         reference_count;    // protected by the global config_mutex
    // all the rest is constant after loading from the config file
    int             generation;
    time_t          load_time;
    string_set      config_files;
    context_list    contexts;       // owns all the contexts, not just top level contexts
    context_map     env_to;         // map recipient to a filtering context
    CONTEXTP        default_context;// for env_to values that don't have their own specific filtering context
    // the default context is also used for some of the content filtering values

    CONFIG();
    ~CONFIG();
    void        add_context(CONTEXTP con);
    void        add_to(char *to, CONTEXTP con)      {env_to[to] = con;};
    CONTEXTP    find_context(char *to, char *from);

    char*       get_content_suffix()                        {return default_context->get_content_suffix()      ;};
    char*       get_content_message()                       {return default_context->get_content_message()     ;};
    string_set& get_content_host_ignore()                   {return default_context->get_content_host_ignore() ;};
    string_set& get_content_tlds()                          {return default_context->get_content_tlds()        ;};
    string_set& get_html_tags()                             {return default_context->get_html_tags()           ;};

    void        dump();
};

extern char *token_black;
extern char *token_content;
extern char *token_context;
extern char *token_dccfrom;
extern char *token_dccto;
extern char *token_default;
extern char *token_dnsbl;
extern char *token_dnsbll;
extern char *token_envfrom;
extern char *token_envto;
extern char *token_filter;
extern char *token_host_limit;
extern char *token_html_limit;
extern char *token_html_tags;
extern char *token_ignore;
extern char *token_include;
extern char *token_inherit;
extern char *token_lbrace;
extern char *token_many;
extern char *token_off;
extern char *token_ok;
extern char *token_ok2;
extern char *token_on;
extern char *token_rbrace;
extern char *token_semi;
extern char *token_soft;
extern char *token_tld;
extern char *token_unknown;
extern char *token_white;

extern string_set all_strings;      // owns all the strings, only modified by the config loader thread

void discard(string_set &s);
char* register_string(string_set &s, char *name);
char* register_string(char *name);
CONFIG *parse_config(char *fn);
bool load_conf(CONFIG &dc, char *fn);
void token_init();

#endif