From dd7de6cfb93f8812189f8365a6c8659d0686678d Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Sat, 28 Jul 2012 13:25:00 +0100
Subject: Allow Swiftob message listeners to ignore the bot's messages


diff --git a/Swiften/MUC/MUC.h b/Swiften/MUC/MUC.h
index 85f4564..ff03ebc 100644
--- a/Swiften/MUC/MUC.h
+++ b/Swiften/MUC/MUC.h
@@ -89,14 +89,15 @@ namespace Swift {
 			/* boost::signal<void (const blah&)> onItemsResult; */
 			
 
+			const std::string& getOwnNick() const {
+				return ownMUCJID.getResource();
+			}
+
 		private:
 			bool isFromMUC(const JID& j) const {
 				return ownMUCJID.equals(j, JID::WithoutResource);
 			}
 
-			const std::string& getOwnNick() const {
-				return ownMUCJID.getResource();
-			}
 
 		private:
 			void handleIncomingPresence(Presence::ref presence);
diff --git a/Swiftob/Commands.cpp b/Swiftob/Commands.cpp
index 38e5f57..fd1b983 100644
--- a/Swiftob/Commands.cpp
+++ b/Swiftob/Commands.cpp
@@ -75,8 +75,16 @@ bool Commands::runCommand(const std::string& name, const std::string& params, Sw
 }
 
 void Commands::runListeners(Swift::Message::ref message) {
+	bool ownMessage = false;
+	JID room = message->getFrom().toBare();
+	if (mucs_->contains(room)) {
+		MUC::ref muc = mucs_->getMUC(room);
+		ownMessage = muc->getOwnNick() == message->getFrom().getResource();
+	}
 	foreach (ListenerCallback listener, listeners_) {
-		listener(message);
+		if (!ownMessage || listener.second) {
+			listener.first(message);
+		}
 	}
 }
 
diff --git a/Swiftob/Commands.h b/Swiftob/Commands.h
index d5aac2c..1b8e432 100644
--- a/Swiftob/Commands.h
+++ b/Swiftob/Commands.h
@@ -21,8 +21,8 @@ namespace Swift {
 
 class Storage;
 class Commands {
-	typedef boost::function<void(Swift::Message::ref)> ListenerCallback;
 	public:
+		typedef std::pair<boost::function<void(Swift::Message::ref)>, bool> ListenerCallback;
 		enum RoleList {Anyone, Owner};
 	public:
 		class Command {
diff --git a/Swiftob/LuaCommands.cpp b/Swiftob/LuaCommands.cpp
index d2f0f07..5639bd9 100644
--- a/Swiftob/LuaCommands.cpp
+++ b/Swiftob/LuaCommands.cpp
@@ -53,12 +53,16 @@ static int l_register_listener(lua_State *L) {
 	commands = static_cast<LuaCommands*>(lua_touserdata(L, -1));
 	lua_pop(L, 1);
 	if (!lua_isfunction(L, 1)) {
-		return luaL_error(L, "register_listener parameter must be a callback function");
+		return luaL_error(L, "register_listener parameter1 must be a callback function");
 	}
+	if (!lua_isboolean(L, 2)) {
+		return luaL_error(L, "register_listener parameter2 must be a boolean");
+	}
+	bool ownMessages = lua_toboolean(L, 2);
 	lua_pushvalue(L, 1);
 	int callbackIndex = luaL_ref(L, LUA_REGISTRYINDEX);
 	lua_pop(L, 1);
-	commands->getCommands()->registerListener(boost::bind(&LuaCommands::handleLuaListener, commands, callbackIndex, L, _1));
+	commands->getCommands()->registerListener(Commands::ListenerCallback(boost::bind(&LuaCommands::handleLuaListener, commands, callbackIndex, L, _1), ownMessages));
 	return 0;
 }
 
@@ -428,7 +432,7 @@ void LuaCommands::loadScript(boost::filesystem::path filePath) {
 	lua_pushlightuserdata(lua, storage);
 	lua_setfield(lua, LUA_REGISTRYINDEX, STORAGE);
 	lua_register(lua, "swiftob_register_command", &l_register_command);
-	lua_register(lua, "swiftob_register_listener", &l_register_listener);
+	lua_register(lua, "swiftob_register_listener", &l_register_listener); /*p1 function callback, p2 bool - call for own messages*/
 	lua_register(lua, "swiftob_reply_to", &l_reply_to);
 	lua_register(lua, "swiftob_get_software_version", &l_get_software_version);
 	lua_register(lua, "swiftob_muc_input_to_jid", &l_muc_input_to_jid);
diff --git a/Swiftob/scripts/badWords.lua b/Swiftob/scripts/badWords.lua
index 2c16214..d801f97 100644
--- a/Swiftob/scripts/badWords.lua
+++ b/Swiftob/scripts/badWords.lua
@@ -11,5 +11,5 @@ function bad_words(body, muc, nick, message)
   end
 end
 
-swiftob_register_listener(bad_words)
+swiftob_register_listener(bad_words, false)
 
diff --git a/Swiftob/scripts/irssiLogs.lua b/Swiftob/scripts/irssiLogs.lua
index 3cecd9d..22134a1 100644
--- a/Swiftob/scripts/irssiLogs.lua
+++ b/Swiftob/scripts/irssiLogs.lua
@@ -19,5 +19,5 @@ function irssi_log_message(body, muc, nick, message)
 	irssi_log_to_file(muc, time.." <"..nick.."> "..body)
 end
 
-swiftob_register_listener(irssi_log_message)
+swiftob_register_listener(irssi_log_message, true)
 
diff --git a/Swiftob/scripts/logAllMessages.lua b/Swiftob/scripts/logAllMessages.lua
index a14c0f3..f8c602a 100644
--- a/Swiftob/scripts/logAllMessages.lua
+++ b/Swiftob/scripts/logAllMessages.lua
@@ -3,4 +3,4 @@ function log_a_message(body, muc, nick, message)
    print(body)
 end
 
-swiftob_register_listener(log_a_message)
+swiftob_register_listener(log_a_message, true)
diff --git a/Swiftob/scripts/urlGrabber.lua b/Swiftob/scripts/urlGrabber.lua
index 13fe182..180c582 100644
--- a/Swiftob/scripts/urlGrabber.lua
+++ b/Swiftob/scripts/urlGrabber.lua
@@ -26,5 +26,5 @@ function url_grabber(body, muc, nick, message)
 	end
 end
 
-swiftob_register_listener(url_grabber)
+swiftob_register_listener(url_grabber, false)
 
-- 
cgit v0.10.2-6-g49f6