summaryrefslogtreecommitdiff
path: root/src/Data/Tox/Message.hs
blob: b77307a294b37ddd289fd13d38b7a1f4c630eccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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