/* * 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 Task * doProcessMail(Handle * in, const String & from, std::vector 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); } #define MAILHANDLER_REGISTRY "MAILHANDLERS_KEYS" 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); L->push(MAILHANDLER_REGISTRY); L->newtable(); L->settable(LUA_REGISTRYINDEX); } 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(1); 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(); } Task * LuaMailHandler::doProcessMail(Handle * in, const String & from, std::vector tos) { Task * r = 0; Lua * oldL = L; Lua * L = oldL->thread(true); oldL->pop(); int j; std::vector::iterator i; L->push(MAILHANDLER_REGISTRY); L->gettable(LUA_REGISTRYINDEX); L->push((lua_Number) id); L->gettable(); L->remove(); LuaHandle lh(in); lh.push(L); L->push(from); L->newtable(); for (i = tos.begin(), j = 1; i != tos.end(); i++, j++) { L->push((lua_Number) j); L->push(*i); L->settable(); } L->call(3, 1); if (L->isfunction()) { r = new LuaTask(L, 0, true); } else { L->pop(); L->weaken(); } return r; } 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; }