From adf62395931f4b90491cc1338b9a5105da98fbbe Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Sat, 8 Sep 2018 06:25:55 -0400 Subject: Factored Data.Tox.Message out of Network.Tox.Crypto.Transport. --- src/Data/Tox/Message.hs | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/Data/Tox/Message.hs (limited to 'src/Data/Tox') diff --git a/src/Data/Tox/Message.hs b/src/Data/Tox/Message.hs new file mode 100644 index 00000000..b77307a2 --- /dev/null +++ b/src/Data/Tox/Message.hs @@ -0,0 +1,83 @@ +-- | This module assigns meaningful symbolic names to Tox message ids and +-- classifies messages as lossy or lossless. +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE ViewPatterns #-} +module Data.Tox.Message where + +import Data.Word + +-- | The one-byte type code prefix that classifies a 'CryptoMessage'. +newtype MessageID = MessageID Word8 deriving (Eq,Enum,Ord,Bounded) +pattern Padding = MessageID 0 -- ^ 0 padding (skipped until we hit a non zero (data id) byte) +pattern PacketRequest = MessageID 1 -- ^ 1 packet request packet (lossy packet) +pattern KillPacket = MessageID 2 -- ^ 2 connection kill packet (lossy packet) +pattern UnspecifiedPacket003 = MessageID 3 -- ^ 3+ unspecified +pattern PING = MessageID 16 -- ^ 16+ reserved for Messenger usage (lossless packets) +-- TODO: rename to ALIVE 16 +-- SHARE_RELAYS 17 +-- FRIEND_REQUESTS 18 +pattern ONLINE = MessageID 24 -- 1 byte +pattern OFFLINE = MessageID 25 -- 1 byte +-- LOSSLESS_RANGE_SIZE 32 +pattern NICKNAME = MessageID 48 -- up to 129 bytes +pattern STATUSMESSAGE = MessageID 49 -- up to 1008 bytes +pattern USERSTATUS = MessageID 50 -- 2 bytes +pattern TYPING = MessageID 51 -- 2 bytes +-- LOSSY_RANGE_SIZE 63 +pattern MESSAGE = MessageID 64 -- up to 1373 bytes +pattern ACTION = MessageID 65 -- up to 1373 bytes +pattern MSI = MessageID 69 +pattern FILE_SENDREQUEST = MessageID 80 -- 1+1+4+8+32+max255 = up to 301 +pattern FILE_CONTROL = MessageID 81 -- 8 bytes if seek, otherwise 4 +pattern FILE_DATA = MessageID 82 -- up to 1373 +pattern INVITE_GROUPCHAT = MessageID 96 -- 0x60 +-- TODO: rename to INVITE_CONFERENCE 96 +pattern ONLINE_PACKET = MessageID 97 -- 0x61 +pattern DIRECT_GROUPCHAT = MessageID 98 -- 0x62 +-- TODO: rename to DIRECT_CONFERENCE 98 +pattern MESSAGE_GROUPCHAT = MessageID 99 -- 0x63 +-- TODO: rename to MESSAGE_CONFERENCE 99 +-- LOSSLESS_RANGE_START 160 +pattern MessengerLossy192 = MessageID 192 -- ^ 192+ reserved for Messenger usage (lossy packets) +pattern LOSSY_GROUPCHAT = MessageID 199 -- 0xC7 +pattern Messenger255 = MessageID 255 -- ^ 255 reserved for Messenger usage (lossless packet) + +instance Show MessageID where + show Padding = "Padding" + show PacketRequest = "PacketRequest" + show KillPacket = "KillPacket" + show UnspecifiedPacket003 = "UnspecifiedPacket003" + show PING = "PING" + show ONLINE = "ONLINE" + show OFFLINE = "OFFLINE" + show NICKNAME = "NICKNAME" + show STATUSMESSAGE = "STATUSMESSAGE" + show USERSTATUS = "USERSTATUS" + show TYPING = "TYPING" + show MESSAGE = "MESSAGE" + show ACTION = "ACTION" + show MSI = "MSI" + show FILE_SENDREQUEST = "FILE_SENDREQUEST" + show FILE_CONTROL = "FILE_CONTROL" + show FILE_DATA = "FILE_DATA" + show INVITE_GROUPCHAT = "INVITE_GROUPCHAT" + show ONLINE_PACKET = "ONLINE_PACKET" + show DIRECT_GROUPCHAT = "DIRECT_GROUPCHAT" + show MESSAGE_GROUPCHAT = "MESSAGE_GROUPCHAT" + show MessengerLossy192 = "MessengerLossy192" + show LOSSY_GROUPCHAT = "LOSSY_GROUPCHAT" + show Messenger255 = "Messenger255" + show (MessageID n) = "MessageID " ++ show n + +data LossyOrLossless = Lossless | Lossy + deriving (Eq,Ord,Enum,Show,Bounded) + +-- | Classify a packet as lossy or lossless. +lossyness :: MessageID -> LossyOrLossless +lossyness (fromEnum -> x) | x < 3 = Lossy +lossyness (fromEnum -> x) | {-16 <= x,-} x < 192 = Lossless +lossyness (fromEnum -> x) | 192 <= x, x < 255 = Lossy +lossyness (fromEnum -> 255) = Lossless + + -- cgit v1.2.3