summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/LuaSmtp.h37
-rw-r--r--include/MailServer.h14
-rw-r--r--lib/LuaSmtp.cc166
-rw-r--r--lib/MailServer.cc53
4 files changed, 259 insertions, 11 deletions
diff --git a/include/LuaSmtp.h b/include/LuaSmtp.h
new file mode 100644
index 0000000..93a9435
--- /dev/null
+++ b/include/LuaSmtp.h
@@ -0,0 +1,37 @@
+/*
+ * Baltisot
+ * Copyright (C) 1999-2008 Nicolas "Pixel" Noble
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef __LUASMTP_H__
+#define __LUASMTP_H__
+
+#include <BLua.h>
+
+class LuaMailHandler;
+
+class LuaLuaMailHandler : public LuaObject {
+ public:
+ static void pushstatics(Lua *) throw (GeneralException);
+ LuaLuaMailHandler(LuaMailHandler *);
+ protected:
+ virtual void pushmembers(Lua *);
+ LuaMailHandler * mh;
+};
+
+
+#endif
diff --git a/include/MailServer.h b/include/MailServer.h
index e39ad93..296ba50 100644
--- a/include/MailServer.h
+++ b/include/MailServer.h
@@ -30,14 +30,19 @@
class MailHandler : public Base {
public:
- MailHandler() {}
- virtual ~MailHandler() {}
- virtual void ProcessMail(Handle * in, const String & from, std::vector<String> tos) = 0;
+ MailHandler();
+ virtual ~MailHandler();
+ virtual bool doProcessMail(Handle * in, const String & from, std::vector<String> tos) = 0;
+ static bool ProcessMail(Handle * in, const String & from, std::vector<String> tos);
+ void OnTop();
+ private:
+ static MailHandler * head;
+ MailHandler * next, * prev;
};
class MailServer : public Task {
public:
- MailServer(MailHandler *, int = 2500, const String & = String("GruiK Server v0.2")) throw (GeneralException);
+ MailServer(int = 2500, const String & = String("GruiK Server v0.2")) throw (GeneralException);
virtual ~MailServer();
virtual String GetName();
@@ -48,7 +53,6 @@ class MailServer : public Task {
Socket Listener;
String name;
int localport;
- MailHandler * handler;
};
#endif
diff --git a/lib/LuaSmtp.cc b/lib/LuaSmtp.cc
new file mode 100644
index 0000000..8652ece
--- /dev/null
+++ b/lib/LuaSmtp.cc
@@ -0,0 +1,166 @@
+/*
+ * Baltisot
+ * Copyright (C) 1999-2008 Nicolas "Pixel" Noble
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "MailServer.h"
+#include "LuaSmtp.h"
+#include "LuaHandle.h"
+#include "LuaTask.h"
+
+class LuaMailHandler : public MailHandler {
+ public:
+ LuaMailHandler(Lua * _L);
+ virtual ~LuaMailHandler();
+ virtual bool doProcessMail(Handle * in, const String & from, std::vector<String> tos);
+ private:
+ Lua * L;
+ static int max_id;
+ int id;
+};
+
+LuaLuaMailHandler::LuaLuaMailHandler(LuaMailHandler * _mh) : mh(_mh) { }
+
+enum LuaMailHandler_methods_t {
+ LUAMAILHANDLER_ONTOP = 0,
+};
+
+enum LuaMailHandler_functions_t {
+ LUAMAILHANDLER_NEWMAILHANDLER = 0,
+// LUAMAILHANDLER_IMAILHANDLER,
+// LUAMAILHANDLER_NMAILHANDLER,
+};
+
+struct lua_functypes_t LuaMailHandler_methods[] = {
+ { LUAMAILHANDLER_ONTOP, "OnTop", 0, 0, { } },
+ { -1, 0, 0, 0, 0 },
+};
+
+struct lua_functypes_t LuaMailHandler_functions[] = {
+ { LUAMAILHANDLER_NEWMAILHANDLER, "MailHandler", 1, 1, { BLUA_FUNCTION } },
+// { LUAMAILHANDLER_IMAILHANDLER, "iMailHandler", 0, 0, { } },
+// { LUAMAILHANDLER_NMAILHANDLER, "nMailHandler", 2, 2, { BLUA_USERDATA, BLUA_STRING | BLUA_NIL } },
+ { -1, 0, 0, 0, 0 },
+};
+
+class sLua_LuaMailHandler : public Base {
+ public:
+ DECLARE_METHOD(LuaMailHandler, LUAMAILHANDLER_ONTOP);
+
+ DECLARE_FUNCTION(LuaMailHandler, LUAMAILHANDLER_NEWMAILHANDLER);
+// DECLARE_FUNCTION(LuaMailHandler, LUAMAILHANDLER_IMAILHANDLER);
+// DECLARE_FUNCTION(LuaMailHandler, LUAMAILHANDLER_NMAILHANDLER);
+
+ private:
+ static int LuaMailHandler_proceed(Lua * L, int n, LuaMailHandler * obj, int caller);
+ static int LuaMailHandler_proceed_statics(Lua * L, int n, int caller);
+};
+
+void LuaLuaMailHandler::pushmembers(Lua * L) {
+ pushme(L, mh, "LuaMailHandler");
+
+ PUSH_METHOD(LuaMailHandler, LUAMAILHANDLER_ONTOP);
+}
+
+void LuaLuaMailHandler::pushstatics(Lua * L) throw (GeneralException) {
+ CHECK_METHODS(LuaMailHandler);
+ CHECK_FUNCTIONS(LuaMailHandler);
+
+ PUSH_FUNCTION(LuaMailHandler, LUAMAILHANDLER_NEWMAILHANDLER);
+// PUSH_FUNCTION(LuaMailHandler, LUAMAILHANDLER_IMAILHANDLER);
+// PUSH_FUNCTION(LuaMailHandler, LUAMAILHANDLER_NMAILHANDLER);
+}
+
+#define MAILHANDLER_REGISTRY "MAILHANDLERS_KEYS"
+
+LuaMailHandler::LuaMailHandler(Lua * _L) : MailHandler(), L(_L->Father()) {
+ id = max_id++; /***FIXME***/
+
+ L->push(MAILHANDLER_REGISTRY);
+ L->gettable(LUA_REGISTRYINDEX);
+ L->push((lua_Number) id);
+ L->copy(2);
+ L->settable();
+ L->pop();
+}
+
+LuaMailHandler::~LuaMailHandler() {
+ L->push(MAILHANDLER_REGISTRY);
+ L->gettable(LUA_REGISTRYINDEX);
+ L->push((lua_Number) id);
+ L->push();
+ L->settable();
+ L->pop();
+}
+
+bool LuaMailHandler::doProcessMail(Handle * in, const String & from, std::vector<String> tos) {
+ return true;
+}
+
+int LuaMailHandler::max_id = 1;
+
+int sLua_LuaMailHandler::LuaMailHandler_proceed(Lua * L, int n, LuaMailHandler * obj, int caller) {
+ int r = 0;
+
+ switch (caller) {
+ case LUAMAILHANDLER_ONTOP:
+ obj->OnTop();
+ break;
+ }
+
+ return r;
+}
+
+int sLua_LuaMailHandler::LuaMailHandler_proceed_statics(Lua * L, int n, int caller) {
+ int r = 0;
+// MailHandler ** p;
+
+ switch (caller) {
+ case LUAMAILHANDLER_NEWMAILHANDLER:
+ {
+ LuaLuaMailHandler lld(new LuaMailHandler(L));
+ lld.pushdestruct(L);
+ r = 1;
+ }
+ break;
+/*
+ case LUAMAILHANDLER_IMAILHANDLER:
+ L->getglobal("nMailHandler");
+ p = (MailHandler **) L->newuser(sizeof(MailHandler *));
+ *p = 0;
+ L->push();
+ r = 3;
+ break;
+ case LUAMAILHANDLER_NMAILHANDLER:
+ p = (MailHandler **) L->touserdata(1);
+ if (*p) {
+ *p = (*p)->Next();
+ } else {
+ *p = MailHandler::First();
+ }
+ if (*p) {
+ L->push((*p)->GetPattern());
+ } else {
+ L->push();
+ }
+ r = 1;
+ break;
+*/
+ }
+
+ return r;
+}
diff --git a/lib/MailServer.cc b/lib/MailServer.cc
index d0bb165..56446de 100644
--- a/lib/MailServer.cc
+++ b/lib/MailServer.cc
@@ -31,7 +31,7 @@ const timeval timeout_sending = { 80, 0 };
class ProcessSMTPRequest : public Task {
public:
- ProcessSMTPRequest(const Socket & out, MailHandler * _handler) : s(out), handler(_handler) {
+ ProcessSMTPRequest(const Socket & out) : s(out) {
SetBurst();
}
virtual ~ProcessSMTPRequest() {
@@ -47,7 +47,6 @@ class ProcessSMTPRequest : public Task {
Task * c;
String from;
std::vector<String> tos;
- MailHandler * handler;
};
static Regex single_dot("^\\.$"), dotted_line("^\\..+$");
@@ -159,8 +158,7 @@ int ProcessSMTPRequest::Do() {
}
}
- if (handler)
- handler->ProcessMail(out, from, tos);
+ MailHandler::ProcessMail(out, from, tos);
delete out;
@@ -176,7 +174,50 @@ int ProcessSMTPRequest::Do() {
}
}
-MailServer::MailServer(MailHandler * _handler, int _port, const String & _name) throw (GeneralException) : name(_name), localport(_port), handler(_handler) {
+MailHandler * MailHandler::head = 0;
+
+MailHandler::MailHandler() {
+ prev = 0;
+ next = head;
+ head = this;
+ if (next)
+ next->prev = this;
+}
+
+MailHandler::~MailHandler() {
+ if (prev)
+ prev->next = next;
+ if (next)
+ next->prev = prev;
+ if (head == this)
+ head = next;
+}
+
+bool MailHandler::ProcessMail(Handle * in, const String & from, std::vector<String> tos) {
+ MailHandler * cur;
+
+ for (cur = head; cur; cur = cur->next) {
+ if (cur->ProcessMail(in, from, tos))
+ return true;
+ }
+ return false;
+}
+
+void MailHandler::OnTop() {
+ if (head == this)
+ return;
+ if (prev)
+ prev->next = next;
+ if (next)
+ next->prev = prev;
+ prev = 0;
+ next = head;
+ head = this;
+ if (next)
+ next->prev = this;
+}
+
+MailServer::MailServer(int _port, const String & _name) throw (GeneralException) : name(_name), localport(_port) {
bool r;
r = Listener.SetLocal("", localport);
@@ -202,7 +243,7 @@ int MailServer::Do() throw (GeneralException) {
try {
Socket s = Listener.Accept();
s.SetNonBlock();
- new ProcessSMTPRequest(s, handler);
+ new ProcessSMTPRequest(s);
}
catch (GeneralException) {
}