aboutsummaryrefslogtreecommitdiff
path: root/src/message/signalproxy/rpccall/message.rs
blob: bf0629a35bc8e37d397854eeb32865d6a6fd5303 (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
use crate::primitive::{BufferInfo, Message, Variant};

use super::{Direction, RpcCall, RpcCallType};

/// Called when a new IRC message has been received, and the client should display or store it.
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct DisplayMessage {
    pub message: Message,
}

impl RpcCallType for DisplayMessage {
    const NAME: &str = "2displayMessage(Message)";
    const DIRECTION: Direction = Direction::ServerToClient;

    fn to_network(&self) -> Result<Vec<crate::primitive::Variant>, crate::ProtocolError> {
        Ok(vec![
            Variant::ByteArray(Self::NAME.to_string()),
            self.message.clone().into(),
        ])
    }

    fn from_network(
        size: usize,
        input: &mut crate::primitive::VariantList,
    ) -> Result<(usize, RpcCall), crate::ProtocolError>
    where
        Self: Sized,
    {
        Ok((
            size,
            RpcCall::DisplayMessage(DisplayMessage {
                message: input.remove(0).try_into().unwrap(),
            }),
        ))
    }
}

/// Status message for an IRC network to be shown in the client’s status bar (if available).
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct DisplayStatusMessage {
    pub network: String,
    pub message: String,
}

impl RpcCallType for DisplayStatusMessage {
    const NAME: &str = "2displayStatusMsg(QString,QString)";
    const DIRECTION: Direction = Direction::ServerToClient;

    fn to_network(&self) -> Result<Vec<crate::primitive::Variant>, crate::ProtocolError> {
        Ok(vec![
            Variant::ByteArray(Self::NAME.to_string()),
            self.network.clone().into(),
            self.message.clone().into(),
        ])
    }

    fn from_network(
        size: usize,
        input: &mut crate::primitive::VariantList,
    ) -> Result<(usize, RpcCall), crate::ProtocolError>
    where
        Self: Sized,
    {
        Ok((
            size,
            DisplayStatusMessage {
                network: input.remove(0).try_into().unwrap(),
                message: input.remove(0).try_into().unwrap(),
            }
            .into(),
        ))
    }
}

#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct SendInput {
    buffer: BufferInfo,
    message: String,
}

impl RpcCallType for SendInput {
    const NAME: &str = "2sendInput(BufferInfo,QString)";
    const DIRECTION: Direction = Direction::ClientToServer;

    fn to_network(&self) -> Result<Vec<Variant>, crate::ProtocolError> {
        Ok(vec![
            Variant::ByteArray(Self::NAME.to_string()),
            self.buffer.clone().into(),
            self.message.clone().into(),
        ])
    }

    fn from_network(
        size: usize,
        input: &mut crate::primitive::VariantList,
    ) -> Result<(usize, RpcCall), crate::ProtocolError>
    where
        Self: Sized,
    {
        Ok((
            size,
            Self {
                buffer: input.remove(0).try_into().unwrap(),
                message: input.remove(0).try_into().unwrap(),
            }
            .into(),
        ))
    }
}