~singpolyma/cheogram-muc-bridge

3d6657e165a247cf1807496f1c44912674a61f5c — Stephen Paul Weber a month ago 16411dd
Detect ghost presence from DB instead of by looking for tag in nick
2 files changed, 16 insertions(+), 13 deletions(-)

M Session.hs
M gateway.hs
M Session.hs => Session.hs +14 -10
@@ 62,17 62,21 @@ sendPresence config presence@XMPP.Presence {
	XMPP.presenceType = typ,
	XMPP.presencePayloads = payloads
} targetMuc = do
	mkSession config typ (Just from) target
	ghost <- liftIO $ DB.query (Config.db config)
		(s"SELECT COUNT(1) FROM sessions WHERE target_muc = ? AND target_nick = ? LIMIT 1")
		(bareTxt from, fromNick)
	when (ghost == [DB.Only (0::Int)]) $ do
		mkSession config typ (Just from) target

	XMPP.putStanza $ presence {
		XMPP.presenceFrom = Just (proxyJid config from),
		XMPP.presenceTo = Just target,
		XMPP.presencePayloads = map (\payload ->
			case XML.isNamed (s"{http://jabber.org/protocol/muc#user}x") payload of
				[_] -> mucJoinX
				_ -> payload
		) payloads
	}
		XMPP.putStanza $ presence {
				XMPP.presenceFrom = Just (proxyJid config from),
				XMPP.presenceTo = Just target,
				XMPP.presencePayloads = map (\payload ->
					case XML.isNamed (s"{http://jabber.org/protocol/muc#user}x") payload of
						[_] -> mucJoinX
						_ -> payload
				) payloads
			}
	where
	target = maybeAddNick targetMuc (fromNick ++ s"[x]")
	fromNick = XMPP.strResource fromResource

M gateway.hs => gateway.hs +2 -3
@@ 46,9 46,8 @@ handlePresence config presence@XMPP.Presence {
		return ()
	| hasMucCode 110 presence = -- done joining room, clean up old data
		Session.cleanOld config from
	| Just resource <- XMPP.jidResource from,
	  not (s"[x]" `T.isInfixOf` XMPP.strResource resource) = forM_ (targets config from) $
		Session.sendPresence config presence
	| otherwise = forM_ (targets config from) $
			Session.sendPresence config presence
handlePresence _ _ = return ()

handlePresenceError :: Config.Config -> XMPP.Presence -> XMPP.XMPP ()