rendered paste body#include <gloox/gloox.h>#include <gloox/client.h>#include <gloox/clientbase.h>#include <gloox/messagehandler.h>#include <gloox/tag.h>#include <gloox/stanza.h>#include <gloox/connectionlistener.h>#include <gloox/presencehandler.h>extern "C" {#include <lua5.1/lua.h>#include <lua5.1/lualib.h>#include <lua5.1/lauxlib.h>}using namespace gloox;lua_State *L = NULL;lua_State *calcL = NULL;char BotName[26];class Bot* theBot = NULL;int lcJoinMUC(lua_State* L);int lcSleep(lua_State* L);int lcMemUsage(lua_State* L);class Bot : public MessageHandler, ConnectionListener // , PresenceHandler{ public: Bot() { ::theBot = this; lua_getglobal(L, "BotJID"); lua_getglobal(L, "BotPass"); lua_getglobal(L, "BotName"); if(!lua_isstring(L, -1) || !lua_isstring(L, -2) || !lua_isstring(L, -3)) { printf("Please, in the body of the script, set BotJID, BotPass, and BotName!\n"); return; } JID jid( lua_tostring(L, -3) ); j = new Client( jid, lua_tostring(L, -2) ); strncpy(BotName, lua_tostring(L, -1), 25); j->registerConnectionListener( this ); j->registerMessageHandler( this ); j->setInitialPriority( 5 ); j->setAutoPresence( true ); printf("Connecting...\n"); j->connect(); } virtual void handleMessage( Stanza *stanza ) { if(stanza->findChildWithAttrib("xmlns","jabber:x:delay")) { printf("History message, ignoring.\n"); return; } printf("Message.\n"); /* push functions and arguments */ lua_getglobal(L, "onMessage"); /* function to be called */ if(!lua_isfunction(L, -1)) { printf("Unhandled message... please add an onMessage function to the script\n"); return; } lua_newtable(L); lua_pushstring(L, stanza->body().c_str()); lua_setfield(L,-2, "body"); lua_pushstring(L, stanza->from().full().c_str()); lua_setfield(L,-2, "fromFull"); lua_pushstring(L, stanza->from().bare().c_str()); lua_setfield(L,-2, "fromBare"); lua_pushstring(L, stanza->to().full().c_str()); lua_setfield(L,-2, "to"); lua_pushnumber(L, stanza->subtype()); lua_setfield(L,-2, "type"); /* do the call (2 arguments, 1 result) */ if (lua_pcall(L, 1, 1, 0) != 0) { printf("Error running function `onMessage': %s",lua_tostring(L, -1)); return; } if(lua_istable(L, -1)) { Tag *t = new Tag( "message" ); lua_getfield(L, -1, "type"); t->addAttribute( "type", luaL_optlstring(L, -1, "chat", NULL) ); lua_pop(L,1); lua_getfield(L, -1, "to"); t->addAttribute( "to", luaL_optlstring(L, -1, stanza->from().bare().c_str(), NULL )); // lua_pop(L,1); lua_getfield(L, -1, "from"); t->addAttribute( "from", luaL_optlstring(L, -1, j->jid().full().c_str(), NULL )); lua_pop(L,1); lua_getfield(L, -1, "body"); t->addChild( new Tag( "body", luaL_optlstring(L, -1, "", NULL) ) ); lua_pop(L,1); j->send( t ); // Send it! } } virtual void onConnect() { // do something when the connection is established printf("Connecting..."); if(L) { lua_pushinteger(L, StanzaMessageChat); lua_setglobal(L, "MSG_CHAT"); lua_pushinteger(L, StanzaMessageGroupchat); lua_setglobal(L, "MSG_GROUPCHAT"); lua_pushinteger(L, StanzaMessageNormal); lua_setglobal(L, "MSG_NORMAL"); lua_register(L, "JoinMUC", lcJoinMUC); lua_register(L, "Sleep", lcSleep); lua_register(L, "MemUsage", lcMemUsage); } else printf("Lua not loaded?!\n"); printf("Connected.\n"); lua_getglobal(L, "onConnect"); /* function to be called */ if(!lua_isfunction(L, -1)) { printf("No onConnect() function found.\n"); return; } /*Tag *t = new Tag( "presence" ); t->addAttribute( "to", "test@conference.jabber.org/Betty" ); t->addChild(new Tag( "status", "Betty!" ) ); j->send( t ); */ /* do the call (2 arguments, 1 result) */ if (lua_pcall(L, 0, 0, 0) != 0) return; // error(L, "error running function `f': %s",lua_tostring(L, -1)); } virtual bool onTLSConnect( const CertInfo& info ) { // decide whether you trust the certificate, examine the CertInfo structure // It's not like a bot is bothered with this :) return true; // if you trust it, otherwise return false } virtual void onDisconnect(gloox::ConnectionError e) { } public: Client* j;};int main( int argc, char* argv[] ){ L = lua_open(); /* opens Lua */ if(L == NULL) { printf("Unable to initialize Lua.\n"); exit(1); } else { } luaL_openlibs(L); if(luaL_dofile(L, "bot.lua")) { printf("Unable to load/run the bot file\n\t%s\n", lua_tostring(L, -1)); exit(1); } else printf("File loaded successfully.\n"); Bot b;}int lcJoinMUC(lua_State* L){ printf("Joining MUC...\n"); if(!lua_isstring(L,1)) { printf("Ooops, not passed a string :(\n"); return 0; } Tag *t = new Tag( "presence" ); t->addAttribute( "to", lua_tostring(L, 1) ); // t->addChild("status", "Betty!"); t->addChild(new Tag( "status", "" ) ); printf(t->xml().c_str() ); theBot->j->send( t ); // printf("Conference (%s) joined.\n",lua_tostring(L, 1)); return 0;}int lcSleep(lua_State* L){ sleep((int)lua_tonumber(L, 1)); return 0;} int lcMemUsage(lua_State* L){ lua_pushinteger(L, lua_gc(L, LUA_GCCOUNT, 0)); return 1;}