aboutsummaryrefslogtreecommitdiff
path: root/src/field/mod.rs
blob: e03990e325b54cb80945efce4bc42785281ec09e (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
mod id;
pub mod utils;

pub use id::ID;

use std::convert::TryInto;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("Data could not be parsed as an integer.")]
    InvalidInteger,
}

/// Hotline defines 3 data types in the wire format:
/// 1) "integer" (16-bit or 32-bit, determined by magnitude)
/// 2) "string" (ASCII)
/// 3) "binary"
#[derive(Debug, Clone)]
pub enum Value {
    Integer(u32),
    Text(String),
    Binary(Vec<u8>),
}

/// A single Hotline field: an ID plus a typed value.
#[derive(Debug, Clone)]
pub struct Field {
    pub id: ID,
    pub value: Value,
}

/// Parse the field data into the correct `Value` type based on the `id`.
///
/// # Errors
/// If an Int can't be parsed, it'll return an `InvalidInteger` error.
pub fn parse_field_value(id: ID, data: &[u8]) -> Result<Value, Error> {
    use Value::{Binary, Integer, Text};
    use ID::{
        AutomaticResponse, ChatID, ChatOptions, ChatSubject, CommunityBannerID, Data, ErrorText,
        FileComment, FileCreateDate, FileCreatorString, FileModifyDate, FileName, FileNameWithInfo,
        FileNewName, FileNewPath, FilePath, FileResumeData, FileSize, FileType, FileTypeString,
        FileXferOptions, FolderItemCount, LegacyNewsCategoryListData, NewsArticle1stChildArticle,
        NewsArticleData, NewsArticleDataFlavor, NewsArticleDate, NewsArticleFlags, NewsArticleID,
        NewsArticleListData, NewsArticleNextArticle, NewsArticleParentArticle, NewsArticlePoster,
        NewsArticlePrevArticle, NewsArticleRecurseDel, NewsArticleTitle, NewsCategoryGUID,
        NewsCategoryListData, NewsCategoryName, NewsPath, NoServerAgreement, Options,
        QuotingMessage, ReferenceNumber, ServerAgreement, ServerBanner, ServerBannerType,
        ServerBannerUrl, ServerName, TransferSize, Unknown, UserAccess, UserAlias, UserFlags,
        UserID, UserIconID, UserLogin, UserName, UserNameWithInfo, UserPassword, Version,
        WaitingCount,
    };

    match id {
        // Integers
        UserID
        | UserIconID
        | ReferenceNumber
        | TransferSize
        | ChatOptions
        | UserFlags
        | Options
        | ChatID
        | WaitingCount
        | ServerBannerType
        | NoServerAgreement
        | Version
        | CommunityBannerID
        | FileSize
        | NewsArticleID
        | NewsArticlePrevArticle
        | NewsArticleNextArticle
        | NewsArticleFlags
        | NewsArticleParentArticle
        | NewsArticle1stChildArticle
        | NewsArticleRecurseDel
        | FolderItemCount => {
            // Convert the data to an integer (u32).
            // Hotline protocol can store 16-bit or 32-bit depending on magnitude,
            // but on the wire we need to detect it. For simplicity here,
            // we'll parse up to 4 bytes. If data is 2 bytes, parse as 16-bit.
            // If 4 bytes, parse as 32-bit. If it's not 2 or 4, we do best-effort.

            let len = data.len();
            if len == 2 {
                data.try_into()
                    .map(u16::from_be_bytes)
                    .map(u32::from)
                    .map(Integer)
                    .map_err(|_| Error::InvalidInteger)
            } else if len == 4 {
                data.try_into()
                    .map(u32::from_be_bytes)
                    .map(Integer)
                    .map_err(|_| Error::InvalidInteger)
            } else {
                Err(Error::InvalidInteger)
            }
        }

        // Strings
        UserName
        | UserLogin
        | UserPassword
        | ChatSubject
        | ServerAgreement
        | FileName
        | FileTypeString
        | FileCreatorString
        | FileComment
        | FileNewName
        | AutomaticResponse
        | ServerName
        | NewsCategoryName
        | NewsArticleDataFlavor
        | NewsArticleTitle
        | NewsArticlePoster => {
            let s = String::from_utf8_lossy(data).to_string();
            Ok(Text(s))
        }

        // Some are definitely “binary,” but might happen to contain text:
        ErrorText
        | Data
        | ServerBanner
        | ServerBannerUrl
        | FileNameWithInfo
        | FilePath
        | FileResumeData
        | FileXferOptions
        | FileCreateDate
        | FileModifyDate
        | FileNewPath
        | FileType
        | QuotingMessage
        | UserAccess
        | UserAlias
        | UserNameWithInfo
        | NewsCategoryGUID
        | LegacyNewsCategoryListData
        | NewsArticleListData
        | NewsCategoryListData
        | NewsPath
        | NewsArticleDate
        | NewsArticleData
        | Unknown(_) => Ok(Binary(data.to_vec())),
    }
}