summaryrefslogtreecommitdiff
path: root/src/Data/Tox
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Tox')
-rw-r--r--src/Data/Tox/Message.hs83
1 files changed, 83 insertions, 0 deletions
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 @@
1-- | This module assigns meaningful symbolic names to Tox message ids and
2-- classifies messages as lossy or lossless.
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE PatternSynonyms #-}
5{-# LANGUAGE ViewPatterns #-}
6module Data.Tox.Message where
7
8import Data.Word
9
10-- | The one-byte type code prefix that classifies a 'CryptoMessage'.
11newtype MessageID = MessageID Word8 deriving (Eq,Enum,Ord,Bounded)
12pattern Padding = MessageID 0 -- ^ 0 padding (skipped until we hit a non zero (data id) byte)
13pattern PacketRequest = MessageID 1 -- ^ 1 packet request packet (lossy packet)
14pattern KillPacket = MessageID 2 -- ^ 2 connection kill packet (lossy packet)
15pattern UnspecifiedPacket003 = MessageID 3 -- ^ 3+ unspecified
16pattern PING = MessageID 16 -- ^ 16+ reserved for Messenger usage (lossless packets)
17-- TODO: rename to ALIVE 16
18-- SHARE_RELAYS 17
19-- FRIEND_REQUESTS 18
20pattern ONLINE = MessageID 24 -- 1 byte
21pattern OFFLINE = MessageID 25 -- 1 byte
22-- LOSSLESS_RANGE_SIZE 32
23pattern NICKNAME = MessageID 48 -- up to 129 bytes
24pattern STATUSMESSAGE = MessageID 49 -- up to 1008 bytes
25pattern USERSTATUS = MessageID 50 -- 2 bytes
26pattern TYPING = MessageID 51 -- 2 bytes
27-- LOSSY_RANGE_SIZE 63
28pattern MESSAGE = MessageID 64 -- up to 1373 bytes
29pattern ACTION = MessageID 65 -- up to 1373 bytes
30pattern MSI = MessageID 69
31pattern FILE_SENDREQUEST = MessageID 80 -- 1+1+4+8+32+max255 = up to 301
32pattern FILE_CONTROL = MessageID 81 -- 8 bytes if seek, otherwise 4
33pattern FILE_DATA = MessageID 82 -- up to 1373
34pattern INVITE_GROUPCHAT = MessageID 96 -- 0x60
35-- TODO: rename to INVITE_CONFERENCE 96
36pattern ONLINE_PACKET = MessageID 97 -- 0x61
37pattern DIRECT_GROUPCHAT = MessageID 98 -- 0x62
38-- TODO: rename to DIRECT_CONFERENCE 98
39pattern MESSAGE_GROUPCHAT = MessageID 99 -- 0x63
40-- TODO: rename to MESSAGE_CONFERENCE 99
41-- LOSSLESS_RANGE_START 160
42pattern MessengerLossy192 = MessageID 192 -- ^ 192+ reserved for Messenger usage (lossy packets)
43pattern LOSSY_GROUPCHAT = MessageID 199 -- 0xC7
44pattern Messenger255 = MessageID 255 -- ^ 255 reserved for Messenger usage (lossless packet)
45
46instance Show MessageID where
47 show Padding = "Padding"
48 show PacketRequest = "PacketRequest"
49 show KillPacket = "KillPacket"
50 show UnspecifiedPacket003 = "UnspecifiedPacket003"
51 show PING = "PING"
52 show ONLINE = "ONLINE"
53 show OFFLINE = "OFFLINE"
54 show NICKNAME = "NICKNAME"
55 show STATUSMESSAGE = "STATUSMESSAGE"
56 show USERSTATUS = "USERSTATUS"
57 show TYPING = "TYPING"
58 show MESSAGE = "MESSAGE"
59 show ACTION = "ACTION"
60 show MSI = "MSI"
61 show FILE_SENDREQUEST = "FILE_SENDREQUEST"
62 show FILE_CONTROL = "FILE_CONTROL"
63 show FILE_DATA = "FILE_DATA"
64 show INVITE_GROUPCHAT = "INVITE_GROUPCHAT"
65 show ONLINE_PACKET = "ONLINE_PACKET"
66 show DIRECT_GROUPCHAT = "DIRECT_GROUPCHAT"
67 show MESSAGE_GROUPCHAT = "MESSAGE_GROUPCHAT"
68 show MessengerLossy192 = "MessengerLossy192"
69 show LOSSY_GROUPCHAT = "LOSSY_GROUPCHAT"
70 show Messenger255 = "Messenger255"
71 show (MessageID n) = "MessageID " ++ show n
72
73data LossyOrLossless = Lossless | Lossy
74 deriving (Eq,Ord,Enum,Show,Bounded)
75
76-- | Classify a packet as lossy or lossless.
77lossyness :: MessageID -> LossyOrLossless
78lossyness (fromEnum -> x) | x < 3 = Lossy
79lossyness (fromEnum -> x) | {-16 <= x,-} x < 192 = Lossless
80lossyness (fromEnum -> x) | 192 <= x, x < 255 = Lossy
81lossyness (fromEnum -> 255) = Lossless
82
83