diff options
author | Joe Crayne <joe@jerkface.net> | 2018-09-08 06:25:55 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-09-08 08:36:05 -0400 |
commit | adf62395931f4b90491cc1338b9a5105da98fbbe (patch) | |
tree | 1177cc719fdd2e5449e03963fb32b24b7017a94a /src/Data | |
parent | ad20be57786ad34f80192206c480d575392b4ebb (diff) |
Factored Data.Tox.Message out of Network.Tox.Crypto.Transport.
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Tox/Message.hs | 83 |
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 #-} | ||
6 | module Data.Tox.Message where | ||
7 | |||
8 | import Data.Word | ||
9 | |||
10 | -- | The one-byte type code prefix that classifies a 'CryptoMessage'. | ||
11 | newtype MessageID = MessageID Word8 deriving (Eq,Enum,Ord,Bounded) | ||
12 | pattern Padding = MessageID 0 -- ^ 0 padding (skipped until we hit a non zero (data id) byte) | ||
13 | pattern PacketRequest = MessageID 1 -- ^ 1 packet request packet (lossy packet) | ||
14 | pattern KillPacket = MessageID 2 -- ^ 2 connection kill packet (lossy packet) | ||
15 | pattern UnspecifiedPacket003 = MessageID 3 -- ^ 3+ unspecified | ||
16 | pattern PING = MessageID 16 -- ^ 16+ reserved for Messenger usage (lossless packets) | ||
17 | -- TODO: rename to ALIVE 16 | ||
18 | -- SHARE_RELAYS 17 | ||
19 | -- FRIEND_REQUESTS 18 | ||
20 | pattern ONLINE = MessageID 24 -- 1 byte | ||
21 | pattern OFFLINE = MessageID 25 -- 1 byte | ||
22 | -- LOSSLESS_RANGE_SIZE 32 | ||
23 | pattern NICKNAME = MessageID 48 -- up to 129 bytes | ||
24 | pattern STATUSMESSAGE = MessageID 49 -- up to 1008 bytes | ||
25 | pattern USERSTATUS = MessageID 50 -- 2 bytes | ||
26 | pattern TYPING = MessageID 51 -- 2 bytes | ||
27 | -- LOSSY_RANGE_SIZE 63 | ||
28 | pattern MESSAGE = MessageID 64 -- up to 1373 bytes | ||
29 | pattern ACTION = MessageID 65 -- up to 1373 bytes | ||
30 | pattern MSI = MessageID 69 | ||
31 | pattern FILE_SENDREQUEST = MessageID 80 -- 1+1+4+8+32+max255 = up to 301 | ||
32 | pattern FILE_CONTROL = MessageID 81 -- 8 bytes if seek, otherwise 4 | ||
33 | pattern FILE_DATA = MessageID 82 -- up to 1373 | ||
34 | pattern INVITE_GROUPCHAT = MessageID 96 -- 0x60 | ||
35 | -- TODO: rename to INVITE_CONFERENCE 96 | ||
36 | pattern ONLINE_PACKET = MessageID 97 -- 0x61 | ||
37 | pattern DIRECT_GROUPCHAT = MessageID 98 -- 0x62 | ||
38 | -- TODO: rename to DIRECT_CONFERENCE 98 | ||
39 | pattern MESSAGE_GROUPCHAT = MessageID 99 -- 0x63 | ||
40 | -- TODO: rename to MESSAGE_CONFERENCE 99 | ||
41 | -- LOSSLESS_RANGE_START 160 | ||
42 | pattern MessengerLossy192 = MessageID 192 -- ^ 192+ reserved for Messenger usage (lossy packets) | ||
43 | pattern LOSSY_GROUPCHAT = MessageID 199 -- 0xC7 | ||
44 | pattern Messenger255 = MessageID 255 -- ^ 255 reserved for Messenger usage (lossless packet) | ||
45 | |||
46 | instance 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 | |||
73 | data LossyOrLossless = Lossless | Lossy | ||
74 | deriving (Eq,Ord,Enum,Show,Bounded) | ||
75 | |||
76 | -- | Classify a packet as lossy or lossless. | ||
77 | lossyness :: MessageID -> LossyOrLossless | ||
78 | lossyness (fromEnum -> x) | x < 3 = Lossy | ||
79 | lossyness (fromEnum -> x) | {-16 <= x,-} x < 192 = Lossless | ||
80 | lossyness (fromEnum -> x) | 192 <= x, x < 255 = Lossy | ||
81 | lossyness (fromEnum -> 255) = Lossless | ||
82 | |||
83 | |||