From 781defb7076ddfddf723ca08cd0a518b6657b64f Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 16 Apr 2013 01:58:29 -0500 Subject: Move extras header files out of extras so when users copy modules out they dont need the headers too --- modules/CMakeLists.txt | 1 + modules/commands/cs_fantasy_stats.cpp | 2 +- modules/commands/cs_fantasy_top.cpp | 2 +- modules/database/db_sql.cpp | 2 +- modules/database/db_sql_live.cpp | 2 +- modules/extra/httpd.h | 226 ---------------------------------- modules/extra/ldap.h | 173 -------------------------- modules/extra/sql.h | 216 -------------------------------- modules/extra/ssl.h | 9 -- modules/extra/xmlrpc.h | 40 ------ modules/httpd.h | 226 ++++++++++++++++++++++++++++++++++ modules/ldap.h | 173 ++++++++++++++++++++++++++ modules/sql.h | 216 ++++++++++++++++++++++++++++++++ modules/ssl.h | 9 ++ modules/xmlrpc.h | 40 ++++++ 15 files changed, 669 insertions(+), 668 deletions(-) delete mode 100644 modules/extra/httpd.h delete mode 100644 modules/extra/ldap.h delete mode 100644 modules/extra/sql.h delete mode 100644 modules/extra/ssl.h delete mode 100644 modules/extra/xmlrpc.h create mode 100644 modules/httpd.h create mode 100644 modules/ldap.h create mode 100644 modules/sql.h create mode 100644 modules/ssl.h create mode 100644 modules/xmlrpc.h (limited to 'modules') diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt index bce14a6ff..928466e41 100644 --- a/modules/CMakeLists.txt +++ b/modules/CMakeLists.txt @@ -159,5 +159,6 @@ function(build_subdir) endif(HAS_FUNCTION) endfunction(build_subdir) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) build_modules(${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/modules/commands/cs_fantasy_stats.cpp b/modules/commands/cs_fantasy_stats.cpp index b1b878c63..df24710a0 100644 --- a/modules/commands/cs_fantasy_stats.cpp +++ b/modules/commands/cs_fantasy_stats.cpp @@ -12,7 +12,7 @@ /*************************************************************************/ #include "module.h" -#include "../extra/sql.h" +#include "sql.h" class MySQLInterface : public SQL::Interface { diff --git a/modules/commands/cs_fantasy_top.cpp b/modules/commands/cs_fantasy_top.cpp index 2f445028b..9baceeda3 100644 --- a/modules/commands/cs_fantasy_top.cpp +++ b/modules/commands/cs_fantasy_top.cpp @@ -12,7 +12,7 @@ /*************************************************************************/ #include "module.h" -#include "../extra/sql.h" +#include "sql.h" class MySQLInterface : public SQL::Interface { diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp index 4025fec69..894037026 100644 --- a/modules/database/db_sql.cpp +++ b/modules/database/db_sql.cpp @@ -9,7 +9,7 @@ */ #include "module.h" -#include "../extra/sql.h" +#include "sql.h" using namespace SQL; diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp index a26b08cf7..239c543c2 100644 --- a/modules/database/db_sql_live.cpp +++ b/modules/database/db_sql_live.cpp @@ -1,5 +1,5 @@ #include "module.h" -#include "../extra/sql.h" +#include "sql.h" #include "../commands/os_session.h" using namespace SQL; diff --git a/modules/extra/httpd.h b/modules/extra/httpd.h deleted file mode 100644 index 6127a51e0..000000000 --- a/modules/extra/httpd.h +++ /dev/null @@ -1,226 +0,0 @@ -#ifndef ANOPE_HTTPD_H -#define ANOPE_HTTPD_H - -enum HTTPError -{ - HTTP_ERROR_OK = 200, - HTTP_FOUND = 302, - HTTP_BAD_REQUEST = 400, - HTTP_PAGE_NOT_FOUND = 404, - HTTP_NOT_SUPPORTED = 505 -}; - -/* A message to someone */ -struct HTTPReply -{ - HTTPError error; - Anope::string content_type; - std::map headers; - typedef std::list > cookie; - std::vector cookies; - - HTTPReply() : error(HTTP_ERROR_OK), length(0) { } - - HTTPReply(const HTTPReply& other) : error(other.error), length(other.length) - { - content_type = other.content_type; - headers = other.headers; - cookies = other.cookies; - - for (unsigned i = 0; i < other.out.size(); ++i) - out.push_back(new Data(other.out[i]->buf, other.out[i]->len)); - } - - ~HTTPReply() - { - for (unsigned i = 0; i < out.size(); ++i) - delete out[i]; - out.clear(); - } - - struct Data - { - char *buf; - size_t len; - - Data(const char *b, size_t l) - { - this->buf = new char[l]; - memcpy(this->buf, b, l); - this->len = l; - } - - ~Data() - { - delete [] buf; - } - }; - - std::deque out; - size_t length; - - void Write(const Anope::string &message) - { - this->out.push_back(new Data(message.c_str(), message.length())); - this->length += message.length(); - } - - void Write(const char *b, size_t l) - { - this->out.push_back(new Data(b, l)); - this->length += l; - } -}; - -/* A message from soneone */ -struct HTTPMessage -{ - std::map headers; - std::map cookies; - std::map get_data; - std::map post_data; - Anope::string content; -}; - -class HTTPClient; -class HTTPProvider; - -class HTTPPage : public Base -{ - Anope::string url; - Anope::string content_type; - - public: - HTTPPage(const Anope::string &u, const Anope::string &ct = "text/html") : url(u), content_type(ct) { } - - const Anope::string &GetURL() const { return this->url; } - - const Anope::string &GetContentType() const { return this->content_type; } - - /** Called when this page is requested - * @param The server this page is on - * @param The page name - * @param The client requesting the page - * @param The HTTP header sent from the client to request the page - * @param The HTTP header that will be sent back to the client - */ - virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) = 0; -}; - -class HTTPClient : public ClientSocket, public BinarySocket, public Base -{ - protected: - void WriteClient(const Anope::string &message) - { - BinarySocket::Write(message + "\r\n"); - } - - public: - HTTPClient(ListenSocket *l, int f, const sockaddrs &a) : ClientSocket(l, a), BinarySocket() { } - - virtual const Anope::string GetIP() - { - return this->clientaddr.addr(); - } - - virtual void SendError(HTTPError err, const Anope::string &msg) = 0; - virtual void SendReply(HTTPReply *) = 0; -}; - -class HTTPProvider : public ListenSocket, public Service -{ - Anope::string ip; - unsigned short port; - public: - Anope::string ext_ip; - std::vector ext_headers; - - HTTPProvider(Module *c, const Anope::string &n, const Anope::string &i, const unsigned short p) : ListenSocket(i, p, i.find(':') != Anope::string::npos), Service(c, "HTTPProvider", n), ip(i), port(p) { } - - const Anope::string &GetIP() const - { - return this->ip; - } - - unsigned short GetPort() const - { - return this->port; - } - - virtual bool RegisterPage(HTTPPage *page) = 0; - virtual void UnregisterPage(HTTPPage *page) = 0; - virtual HTTPPage* FindPage(const Anope::string &name) = 0; -}; - -namespace HTTPUtils -{ - inline Anope::string URLDecode(const Anope::string &url) - { - Anope::string decoded; - - for (unsigned i = 0; i < url.length(); ++i) - { - const char& c = url[i]; - - if (c == '%' && i + 2 < url.length()) - { - Anope::string dest; - Anope::Unhex(url.substr(i + 1, 2), dest); - decoded += dest; - i += 2; - } - else if (c == '+') - decoded += ' '; - else - decoded += c; - } - - return decoded; - } - - inline Anope::string URLEncode(const Anope::string &url) - { - Anope::string encoded; - - for (unsigned i = 0; i < url.length(); ++i) - { - const char& c = url[i]; - - if (isalnum(c) || c == '.' || c == '-' || c == '*' || c == '_') - encoded += c; - else if (c == ' ') - encoded += '+'; - else - encoded += "%" + Anope::Hex(c); - } - - return encoded; - } - - inline Anope::string Escape(const Anope::string &src) - { - Anope::string dst; - - for (unsigned i = 0; i < src.length(); ++i) - { - switch (src[i]) - { - case '<': - dst += "<"; - break; - case '>': - dst += ">"; - break; - case '"': - dst += """; - break; - default: - dst += src[i]; - } - } - - return dst; - } -} - -#endif // ANOPE_HTTPD_H diff --git a/modules/extra/ldap.h b/modules/extra/ldap.h deleted file mode 100644 index 65be27687..000000000 --- a/modules/extra/ldap.h +++ /dev/null @@ -1,173 +0,0 @@ -#ifndef ANOPE_LDAP_H -#define ANOPE_LDAP_H - -typedef int LDAPQuery; - -class LDAPException : public ModuleException -{ - public: - LDAPException(const Anope::string &reason) : ModuleException(reason) { } - - virtual ~LDAPException() throw() { } -}; - -struct LDAPModification -{ - enum LDAPOperation - { - LDAP_ADD, - LDAP_DEL, - LDAP_REPLACE - }; - - LDAPOperation op; - Anope::string name; - std::vector values; -}; -typedef std::vector LDAPMods; - -struct LDAPAttributes : public std::map > -{ - size_t size(const Anope::string &attr) const - { - const std::vector& array = this->getArray(attr); - return array.size(); - } - - const std::vector keys() const - { - std::vector k; - for (const_iterator it = this->begin(), it_end = this->end(); it != it_end; ++it) - k.push_back(it->first); - return k; - } - - const Anope::string &get(const Anope::string &attr) const - { - const std::vector& array = this->getArray(attr); - if (array.empty()) - throw LDAPException("Empty attribute " + attr + " in LDAPResult::get"); - return array[0]; - } - - const std::vector& getArray(const Anope::string &attr) const - { - const_iterator it = this->find(attr); - if (it == this->end()) - throw LDAPException("Unknown attribute " + attr + " in LDAPResult::getArray"); - return it->second; - } -}; - -struct LDAPResult -{ - std::vector messages; - Anope::string error; - - enum QueryType - { - QUERY_UNKNOWN, - QUERY_BIND, - QUERY_SEARCH, - QUERY_ADD, - QUERY_DELETE, - QUERY_MODIFY - }; - - QueryType type; - LDAPQuery id; - - LDAPResult() - { - this->type = QUERY_UNKNOWN; - this->id = -1; - } - - size_t size() const - { - return this->messages.size(); - } - - bool empty() const - { - return this->messages.empty(); - } - - const LDAPAttributes &get(size_t sz) const - { - if (sz >= this->messages.size()) - throw LDAPException("Index out of range"); - return this->messages[sz]; - } - - const Anope::string &getError() const - { - return this->error; - } -}; - -class LDAPInterface -{ - public: - Module *owner; - - LDAPInterface(Module *m) : owner(m) { } - virtual ~LDAPInterface() { } - - virtual void OnResult(const LDAPResult &r) = 0; - virtual void OnError(const LDAPResult &err) = 0; - virtual void OnDelete() { } -}; - -class LDAPProvider : public Service -{ - public: - LDAPProvider(Module *c, const Anope::string &n) : Service(c, "LDAPProvider", n) { } - - /** Attempt to bind to the LDAP server as an admin - * @param i The LDAPInterface the result is sent to - * @return The query ID - */ - virtual LDAPQuery BindAsAdmin(LDAPInterface *i) = 0; - - /** Bind to LDAP - * @param i The LDAPInterface the result is sent to - * @param who The binddn - * @param pass The password - * @return The query ID - */ - virtual LDAPQuery Bind(LDAPInterface *i, const Anope::string &who, const Anope::string &pass) = 0; - - /** Search ldap for the specified filter - * @param i The LDAPInterface the result is sent to - * @param base The base DN to search - * @param filter The filter to apply - * @return The query ID - */ - virtual LDAPQuery Search(LDAPInterface *i, const Anope::string &base, const Anope::string &filter) = 0; - - /** Add an entry to LDAP - * @param i The LDAPInterface the result is sent to - * @param dn The dn of the entry to add - * @param attributes The attributes - * @return The query ID - */ - virtual LDAPQuery Add(LDAPInterface *i, const Anope::string &dn, LDAPMods &attributes) = 0; - - /** Delete an entry from LDAP - * @param i The LDAPInterface the result is sent to - * @param dn The dn of the entry to delete - * @return The query ID - */ - virtual LDAPQuery Del(LDAPInterface *i, const Anope::string &dn) = 0; - - /** Modify an existing entry in LDAP - * @param i The LDAPInterface the result is sent to - * @param base The base DN to modify - * @param attributes The attributes to modify - * @return The query ID - */ - virtual LDAPQuery Modify(LDAPInterface *i, const Anope::string &base, LDAPMods &attributes) = 0; -}; - -#endif // ANOPE_LDAP_H diff --git a/modules/extra/sql.h b/modules/extra/sql.h deleted file mode 100644 index 1c8fdd3aa..000000000 --- a/modules/extra/sql.h +++ /dev/null @@ -1,216 +0,0 @@ -/* - * (C) 2003-2013 Anope Team - * Contact us at team@anope.org - * - * Please read COPYING and README for further details. - */ - -namespace SQL -{ - - class Data : public Serialize::Data - { - public: - typedef std::map Map; - Map data; - std::map types; - - ~Data() - { - Clear(); - } - - std::iostream& operator[](const Anope::string &key) anope_override - { - std::stringstream *&ss = data[key]; - if (!ss) - ss = new std::stringstream(); - return *ss; - } - - std::set KeySet() const anope_override - { - std::set keys; - for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - keys.insert(it->first); - return keys; - } - - size_t Hash() const anope_override - { - size_t hash = 0; - for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - if (!it->second->str().empty()) - hash ^= Anope::hash_cs()(it->second->str()); - return hash; - } - - std::map GetData() const - { - std::map d; - for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - d[it->first] = it->second; - return d; - } - - void Clear() - { - for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - delete it->second; - this->data.clear(); - } - - void SetType(const Anope::string &key, Type t) anope_override - { - this->types[key] = t; - } - - Type GetType(const Anope::string &key) const anope_override - { - std::map::const_iterator it = this->types.find(key); - if (it != this->types.end()) - return it->second; - return DT_TEXT; - } - }; - - /** A SQL exception, can be thrown at various points - */ - class Exception : public ModuleException - { - public: - Exception(const Anope::string &reason) : ModuleException(reason) { } - - virtual ~Exception() throw() { } - }; - - /** A SQL query - */ - - struct QueryData - { - Anope::string data; - bool escape; - }; - - struct Query - { - Anope::string query; - std::map parameters; - - Query() { } - Query(const Anope::string &q) : query(q) { } - - Query& operator=(const Anope::string &q) - { - this->query = q; - this->parameters.clear(); - return *this; - } - - bool operator==(const Query &other) const - { - return this->query == other.query; - } - - inline bool operator!=(const Query &other) const - { - return !(*this == other); - } - - template void SetValue(const Anope::string &key, const T& value, bool escape = true) - { - try - { - Anope::string string_value = stringify(value); - this->parameters[key].data = string_value; - this->parameters[key].escape = escape; - } - catch (const ConvertException &ex) { } - } - }; - - /** A result from a SQL query - */ - class Result - { - protected: - /* Rows, column, item */ - std::vector > entries; - Query query; - Anope::string error; - public: - unsigned int id; - Anope::string finished_query; - - Result() : id(0) { } - Result(unsigned int i, const Query &q, const Anope::string &fq, const Anope::string &err = "") : query(q), error(err), id(i), finished_query(fq) { } - - inline operator bool() const { return this->error.empty(); } - - inline const unsigned int GetID() const { return this->id; } - inline const Query &GetQuery() const { return this->query; } - inline const Anope::string &GetError() const { return this->error; } - - int Rows() const { return this->entries.size(); } - - const std::map &Row(size_t index) const - { - try - { - return this->entries.at(index); - } - catch (const std::out_of_range &) - { - throw Exception("Out of bounds access to SQLResult"); - } - } - - const Anope::string Get(size_t index, const Anope::string &col) const - { - const std::map rows = this->Row(index); - - std::map::const_iterator it = rows.find(col); - if (it == rows.end()) - throw Exception("Unknown column name in SQLResult: " + col); - - return it->second; - } - }; - - /* An interface used by modules to retrieve the results - */ - class Interface - { - public: - Module *owner; - - Interface(Module *m) : owner(m) { } - virtual ~Interface() { } - - virtual void OnResult(const Result &r) = 0; - virtual void OnError(const Result &r) = 0; - }; - - /** Class providing the SQL service, modules call this to execute queries - */ - class Provider : public Service - { - public: - Provider(Module *c, const Anope::string &n) : Service(c, "SQL::Provider", n) { } - - virtual void Run(Interface *i, const Query &query) = 0; - - virtual Result RunQuery(const Query &query) = 0; - - virtual std::vector CreateTable(const Anope::string &table, const Data &data) = 0; - - virtual Query BuildInsert(const Anope::string &table, unsigned int id, Data &data) = 0; - - virtual Query GetTables(const Anope::string &prefix) = 0; - - virtual Anope::string FromUnixtime(time_t) = 0; - }; - -} - diff --git a/modules/extra/ssl.h b/modules/extra/ssl.h deleted file mode 100644 index 6eb97582c..000000000 --- a/modules/extra/ssl.h +++ /dev/null @@ -1,9 +0,0 @@ - -class SSLService : public Service -{ - public: - SSLService(Module *o, const Anope::string &n) : Service(o, "SSLService", n) { } - - virtual void Init(Socket *s) = 0; -}; - diff --git a/modules/extra/xmlrpc.h b/modules/extra/xmlrpc.h deleted file mode 100644 index d5a5ea4a7..000000000 --- a/modules/extra/xmlrpc.h +++ /dev/null @@ -1,40 +0,0 @@ -#include "httpd.h" - -class XMLRPCRequest -{ - std::map replies; - - public: - Anope::string name; - Anope::string id; - std::deque data; - HTTPReply& r; - - XMLRPCRequest(HTTPReply &_r) : r(_r) { } - inline void reply(const Anope::string &dname, const Anope::string &ddata) { this->replies.insert(std::make_pair(dname, ddata)); } - inline const std::map &get_replies() { return this->replies; } -}; - -class XMLRPCServiceInterface; - -class XMLRPCEvent -{ - public: - virtual ~XMLRPCEvent() { } - virtual bool Run(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) = 0; -}; - -class XMLRPCServiceInterface : public Service -{ - public: - XMLRPCServiceInterface(Module *creator, const Anope::string &sname) : Service(creator, "XMLRPCServiceInterface", sname) { } - - virtual void Register(XMLRPCEvent *event) = 0; - - virtual void Unregister(XMLRPCEvent *event) = 0; - - virtual Anope::string Sanitize(const Anope::string &string) = 0; - - virtual void Reply(XMLRPCRequest &request) = 0; -}; - diff --git a/modules/httpd.h b/modules/httpd.h new file mode 100644 index 000000000..6127a51e0 --- /dev/null +++ b/modules/httpd.h @@ -0,0 +1,226 @@ +#ifndef ANOPE_HTTPD_H +#define ANOPE_HTTPD_H + +enum HTTPError +{ + HTTP_ERROR_OK = 200, + HTTP_FOUND = 302, + HTTP_BAD_REQUEST = 400, + HTTP_PAGE_NOT_FOUND = 404, + HTTP_NOT_SUPPORTED = 505 +}; + +/* A message to someone */ +struct HTTPReply +{ + HTTPError error; + Anope::string content_type; + std::map headers; + typedef std::list > cookie; + std::vector cookies; + + HTTPReply() : error(HTTP_ERROR_OK), length(0) { } + + HTTPReply(const HTTPReply& other) : error(other.error), length(other.length) + { + content_type = other.content_type; + headers = other.headers; + cookies = other.cookies; + + for (unsigned i = 0; i < other.out.size(); ++i) + out.push_back(new Data(other.out[i]->buf, other.out[i]->len)); + } + + ~HTTPReply() + { + for (unsigned i = 0; i < out.size(); ++i) + delete out[i]; + out.clear(); + } + + struct Data + { + char *buf; + size_t len; + + Data(const char *b, size_t l) + { + this->buf = new char[l]; + memcpy(this->buf, b, l); + this->len = l; + } + + ~Data() + { + delete [] buf; + } + }; + + std::deque out; + size_t length; + + void Write(const Anope::string &message) + { + this->out.push_back(new Data(message.c_str(), message.length())); + this->length += message.length(); + } + + void Write(const char *b, size_t l) + { + this->out.push_back(new Data(b, l)); + this->length += l; + } +}; + +/* A message from soneone */ +struct HTTPMessage +{ + std::map headers; + std::map cookies; + std::map get_data; + std::map post_data; + Anope::string content; +}; + +class HTTPClient; +class HTTPProvider; + +class HTTPPage : public Base +{ + Anope::string url; + Anope::string content_type; + + public: + HTTPPage(const Anope::string &u, const Anope::string &ct = "text/html") : url(u), content_type(ct) { } + + const Anope::string &GetURL() const { return this->url; } + + const Anope::string &GetContentType() const { return this->content_type; } + + /** Called when this page is requested + * @param The server this page is on + * @param The page name + * @param The client requesting the page + * @param The HTTP header sent from the client to request the page + * @param The HTTP header that will be sent back to the client + */ + virtual bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) = 0; +}; + +class HTTPClient : public ClientSocket, public BinarySocket, public Base +{ + protected: + void WriteClient(const Anope::string &message) + { + BinarySocket::Write(message + "\r\n"); + } + + public: + HTTPClient(ListenSocket *l, int f, const sockaddrs &a) : ClientSocket(l, a), BinarySocket() { } + + virtual const Anope::string GetIP() + { + return this->clientaddr.addr(); + } + + virtual void SendError(HTTPError err, const Anope::string &msg) = 0; + virtual void SendReply(HTTPReply *) = 0; +}; + +class HTTPProvider : public ListenSocket, public Service +{ + Anope::string ip; + unsigned short port; + public: + Anope::string ext_ip; + std::vector ext_headers; + + HTTPProvider(Module *c, const Anope::string &n, const Anope::string &i, const unsigned short p) : ListenSocket(i, p, i.find(':') != Anope::string::npos), Service(c, "HTTPProvider", n), ip(i), port(p) { } + + const Anope::string &GetIP() const + { + return this->ip; + } + + unsigned short GetPort() const + { + return this->port; + } + + virtual bool RegisterPage(HTTPPage *page) = 0; + virtual void UnregisterPage(HTTPPage *page) = 0; + virtual HTTPPage* FindPage(const Anope::string &name) = 0; +}; + +namespace HTTPUtils +{ + inline Anope::string URLDecode(const Anope::string &url) + { + Anope::string decoded; + + for (unsigned i = 0; i < url.length(); ++i) + { + const char& c = url[i]; + + if (c == '%' && i + 2 < url.length()) + { + Anope::string dest; + Anope::Unhex(url.substr(i + 1, 2), dest); + decoded += dest; + i += 2; + } + else if (c == '+') + decoded += ' '; + else + decoded += c; + } + + return decoded; + } + + inline Anope::string URLEncode(const Anope::string &url) + { + Anope::string encoded; + + for (unsigned i = 0; i < url.length(); ++i) + { + const char& c = url[i]; + + if (isalnum(c) || c == '.' || c == '-' || c == '*' || c == '_') + encoded += c; + else if (c == ' ') + encoded += '+'; + else + encoded += "%" + Anope::Hex(c); + } + + return encoded; + } + + inline Anope::string Escape(const Anope::string &src) + { + Anope::string dst; + + for (unsigned i = 0; i < src.length(); ++i) + { + switch (src[i]) + { + case '<': + dst += "<"; + break; + case '>': + dst += ">"; + break; + case '"': + dst += """; + break; + default: + dst += src[i]; + } + } + + return dst; + } +} + +#endif // ANOPE_HTTPD_H diff --git a/modules/ldap.h b/modules/ldap.h new file mode 100644 index 000000000..65be27687 --- /dev/null +++ b/modules/ldap.h @@ -0,0 +1,173 @@ +#ifndef ANOPE_LDAP_H +#define ANOPE_LDAP_H + +typedef int LDAPQuery; + +class LDAPException : public ModuleException +{ + public: + LDAPException(const Anope::string &reason) : ModuleException(reason) { } + + virtual ~LDAPException() throw() { } +}; + +struct LDAPModification +{ + enum LDAPOperation + { + LDAP_ADD, + LDAP_DEL, + LDAP_REPLACE + }; + + LDAPOperation op; + Anope::string name; + std::vector values; +}; +typedef std::vector LDAPMods; + +struct LDAPAttributes : public std::map > +{ + size_t size(const Anope::string &attr) const + { + const std::vector& array = this->getArray(attr); + return array.size(); + } + + const std::vector keys() const + { + std::vector k; + for (const_iterator it = this->begin(), it_end = this->end(); it != it_end; ++it) + k.push_back(it->first); + return k; + } + + const Anope::string &get(const Anope::string &attr) const + { + const std::vector& array = this->getArray(attr); + if (array.empty()) + throw LDAPException("Empty attribute " + attr + " in LDAPResult::get"); + return array[0]; + } + + const std::vector& getArray(const Anope::string &attr) const + { + const_iterator it = this->find(attr); + if (it == this->end()) + throw LDAPException("Unknown attribute " + attr + " in LDAPResult::getArray"); + return it->second; + } +}; + +struct LDAPResult +{ + std::vector messages; + Anope::string error; + + enum QueryType + { + QUERY_UNKNOWN, + QUERY_BIND, + QUERY_SEARCH, + QUERY_ADD, + QUERY_DELETE, + QUERY_MODIFY + }; + + QueryType type; + LDAPQuery id; + + LDAPResult() + { + this->type = QUERY_UNKNOWN; + this->id = -1; + } + + size_t size() const + { + return this->messages.size(); + } + + bool empty() const + { + return this->messages.empty(); + } + + const LDAPAttributes &get(size_t sz) const + { + if (sz >= this->messages.size()) + throw LDAPException("Index out of range"); + return this->messages[sz]; + } + + const Anope::string &getError() const + { + return this->error; + } +}; + +class LDAPInterface +{ + public: + Module *owner; + + LDAPInterface(Module *m) : owner(m) { } + virtual ~LDAPInterface() { } + + virtual void OnResult(const LDAPResult &r) = 0; + virtual void OnError(const LDAPResult &err) = 0; + virtual void OnDelete() { } +}; + +class LDAPProvider : public Service +{ + public: + LDAPProvider(Module *c, const Anope::string &n) : Service(c, "LDAPProvider", n) { } + + /** Attempt to bind to the LDAP server as an admin + * @param i The LDAPInterface the result is sent to + * @return The query ID + */ + virtual LDAPQuery BindAsAdmin(LDAPInterface *i) = 0; + + /** Bind to LDAP + * @param i The LDAPInterface the result is sent to + * @param who The binddn + * @param pass The password + * @return The query ID + */ + virtual LDAPQuery Bind(LDAPInterface *i, const Anope::string &who, const Anope::string &pass) = 0; + + /** Search ldap for the specified filter + * @param i The LDAPInterface the result is sent to + * @param base The base DN to search + * @param filter The filter to apply + * @return The query ID + */ + virtual LDAPQuery Search(LDAPInterface *i, const Anope::string &base, const Anope::string &filter) = 0; + + /** Add an entry to LDAP + * @param i The LDAPInterface the result is sent to + * @param dn The dn of the entry to add + * @param attributes The attributes + * @return The query ID + */ + virtual LDAPQuery Add(LDAPInterface *i, const Anope::string &dn, LDAPMods &attributes) = 0; + + /** Delete an entry from LDAP + * @param i The LDAPInterface the result is sent to + * @param dn The dn of the entry to delete + * @return The query ID + */ + virtual LDAPQuery Del(LDAPInterface *i, const Anope::string &dn) = 0; + + /** Modify an existing entry in LDAP + * @param i The LDAPInterface the result is sent to + * @param base The base DN to modify + * @param attributes The attributes to modify + * @return The query ID + */ + virtual LDAPQuery Modify(LDAPInterface *i, const Anope::string &base, LDAPMods &attributes) = 0; +}; + +#endif // ANOPE_LDAP_H diff --git a/modules/sql.h b/modules/sql.h new file mode 100644 index 000000000..1c8fdd3aa --- /dev/null +++ b/modules/sql.h @@ -0,0 +1,216 @@ +/* + * (C) 2003-2013 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + */ + +namespace SQL +{ + + class Data : public Serialize::Data + { + public: + typedef std::map Map; + Map data; + std::map types; + + ~Data() + { + Clear(); + } + + std::iostream& operator[](const Anope::string &key) anope_override + { + std::stringstream *&ss = data[key]; + if (!ss) + ss = new std::stringstream(); + return *ss; + } + + std::set KeySet() const anope_override + { + std::set keys; + for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) + keys.insert(it->first); + return keys; + } + + size_t Hash() const anope_override + { + size_t hash = 0; + for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) + if (!it->second->str().empty()) + hash ^= Anope::hash_cs()(it->second->str()); + return hash; + } + + std::map GetData() const + { + std::map d; + for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) + d[it->first] = it->second; + return d; + } + + void Clear() + { + for (Map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) + delete it->second; + this->data.clear(); + } + + void SetType(const Anope::string &key, Type t) anope_override + { + this->types[key] = t; + } + + Type GetType(const Anope::string &key) const anope_override + { + std::map::const_iterator it = this->types.find(key); + if (it != this->types.end()) + return it->second; + return DT_TEXT; + } + }; + + /** A SQL exception, can be thrown at various points + */ + class Exception : public ModuleException + { + public: + Exception(const Anope::string &reason) : ModuleException(reason) { } + + virtual ~Exception() throw() { } + }; + + /** A SQL query + */ + + struct QueryData + { + Anope::string data; + bool escape; + }; + + struct Query + { + Anope::string query; + std::map parameters; + + Query() { } + Query(const Anope::string &q) : query(q) { } + + Query& operator=(const Anope::string &q) + { + this->query = q; + this->parameters.clear(); + return *this; + } + + bool operator==(const Query &other) const + { + return this->query == other.query; + } + + inline bool operator!=(const Query &other) const + { + return !(*this == other); + } + + template void SetValue(const Anope::string &key, const T& value, bool escape = true) + { + try + { + Anope::string string_value = stringify(value); + this->parameters[key].data = string_value; + this->parameters[key].escape = escape; + } + catch (const ConvertException &ex) { } + } + }; + + /** A result from a SQL query + */ + class Result + { + protected: + /* Rows, column, item */ + std::vector > entries; + Query query; + Anope::string error; + public: + unsigned int id; + Anope::string finished_query; + + Result() : id(0) { } + Result(unsigned int i, const Query &q, const Anope::string &fq, const Anope::string &err = "") : query(q), error(err), id(i), finished_query(fq) { } + + inline operator bool() const { return this->error.empty(); } + + inline const unsigned int GetID() const { return this->id; } + inline const Query &GetQuery() const { return this->query; } + inline const Anope::string &GetError() const { return this->error; } + + int Rows() const { return this->entries.size(); } + + const std::map &Row(size_t index) const + { + try + { + return this->entries.at(index); + } + catch (const std::out_of_range &) + { + throw Exception("Out of bounds access to SQLResult"); + } + } + + const Anope::string Get(size_t index, const Anope::string &col) const + { + const std::map rows = this->Row(index); + + std::map::const_iterator it = rows.find(col); + if (it == rows.end()) + throw Exception("Unknown column name in SQLResult: " + col); + + return it->second; + } + }; + + /* An interface used by modules to retrieve the results + */ + class Interface + { + public: + Module *owner; + + Interface(Module *m) : owner(m) { } + virtual ~Interface() { } + + virtual void OnResult(const Result &r) = 0; + virtual void OnError(const Result &r) = 0; + }; + + /** Class providing the SQL service, modules call this to execute queries + */ + class Provider : public Service + { + public: + Provider(Module *c, const Anope::string &n) : Service(c, "SQL::Provider", n) { } + + virtual void Run(Interface *i, const Query &query) = 0; + + virtual Result RunQuery(const Query &query) = 0; + + virtual std::vector CreateTable(const Anope::string &table, const Data &data) = 0; + + virtual Query BuildInsert(const Anope::string &table, unsigned int id, Data &data) = 0; + + virtual Query GetTables(const Anope::string &prefix) = 0; + + virtual Anope::string FromUnixtime(time_t) = 0; + }; + +} + diff --git a/modules/ssl.h b/modules/ssl.h new file mode 100644 index 000000000..6eb97582c --- /dev/null +++ b/modules/ssl.h @@ -0,0 +1,9 @@ + +class SSLService : public Service +{ + public: + SSLService(Module *o, const Anope::string &n) : Service(o, "SSLService", n) { } + + virtual void Init(Socket *s) = 0; +}; + diff --git a/modules/xmlrpc.h b/modules/xmlrpc.h new file mode 100644 index 000000000..d5a5ea4a7 --- /dev/null +++ b/modules/xmlrpc.h @@ -0,0 +1,40 @@ +#include "httpd.h" + +class XMLRPCRequest +{ + std::map replies; + + public: + Anope::string name; + Anope::string id; + std::deque data; + HTTPReply& r; + + XMLRPCRequest(HTTPReply &_r) : r(_r) { } + inline void reply(const Anope::string &dname, const Anope::string &ddata) { this->replies.insert(std::make_pair(dname, ddata)); } + inline const std::map &get_replies() { return this->replies; } +}; + +class XMLRPCServiceInterface; + +class XMLRPCEvent +{ + public: + virtual ~XMLRPCEvent() { } + virtual bool Run(XMLRPCServiceInterface *iface, HTTPClient *client, XMLRPCRequest &request) = 0; +}; + +class XMLRPCServiceInterface : public Service +{ + public: + XMLRPCServiceInterface(Module *creator, const Anope::string &sname) : Service(creator, "XMLRPCServiceInterface", sname) { } + + virtual void Register(XMLRPCEvent *event) = 0; + + virtual void Unregister(XMLRPCEvent *event) = 0; + + virtual Anope::string Sanitize(const Anope::string &string) = 0; + + virtual void Reply(XMLRPCRequest &request) = 0; +}; + -- cgit