~singpolyma/cheogram

2421f452736188ac721d51abda72d929692294e0 — Stephen Paul Weber 6 years ago 03b1912
Support short codes

Closes #51
2 files changed, 17 insertions(+), 5 deletions(-)

M Main.hs
M Util.hs
M Main.hs => Main.hs +9 -4
@@ 1067,17 1067,22 @@ component db backendHost toRoomPresences toRejoinManager toJoinPartDebouncer toC
	receivedStanza (ReceivedPresence p) = mkStanzaRec p
	receivedStanza (ReceivedIQ iq) = mkStanzaRec iq

mapToBackend backendHost jid
	| Just localpart <- strNode <$> jidNode jid,
	  Just ('+', tel) <- T.uncons localpart,
	  T.all isDigit tel = parseJID (localpart <> fromString "@" <> backendHost)
mapToBackend backendHost (JID { jidNode = Just node })
	| Just ('+', tel) <- T.uncons localpart,
	  T.all isDigit tel = result
	| Just _ <- parsePhoneContext localpart = result
	| otherwise = Nothing
	where
	result = parseJID (localpart ++ s"@" ++ backendHost)
	localpart = strNode node
mapToBackend _ _ = Nothing

normalizeTel fullTel
	| Just ('+',e164) <- T.uncons fullTel,
	  T.all isDigit e164 = Just fullTel
	| T.length tel == 10 = Just (s"+1" ++ tel)
	| T.length tel == 11, s"1" `T.isPrefixOf` tel = Just (T.cons '+' tel)
	| T.length tel == 5 || T.length tel == 6 = Just (tel ++ s";phone-context=ca-us.phone-context.soprani.ca")
	| otherwise = Nothing
	where
	tel = T.filter isDigit fullTel

M Util.hs => Util.hs +8 -1
@@ 2,8 2,9 @@ module Util where

import Prelude ()
import BasicPrelude
import Data.Char (isDigit)
import Control.Applicative (many)

import Control.Error (hush)
import Data.Time (getCurrentTime)
import Data.XML.Types (Element(..), Node(NodeElement), isNamed, elementText, elementChildren, attributeText)
import Crypto.Random (getSystemDRG, withRandomBytes)


@@ 57,6 58,12 @@ unescapeJid txt = fromString result
			("20", ' '), ("22", '"'), ("26", '&'), ("27", '\''), ("2f", '/'), ("3a", ':'), ("3c", '<'), ("3e", '>'), ("40", '@'), ("5c", '\\')
		]

parsePhoneContext :: Text -> Maybe (Text, Text)
parsePhoneContext txt = hush $ Atto.parseOnly (
		(,) <$> Atto.takeWhile isDigit <* Atto.string (s";phone-context=") <*> Atto.takeTill (Atto.inClass " ;")
		<* Atto.endOfInput
	) txt

bareTxt (XMPP.JID (Just node) domain _) = mconcat [XMPP.strNode node, s"@", XMPP.strDomain domain]
bareTxt (XMPP.JID Nothing domain _) = XMPP.strDomain domain