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
use bytes::{BufMut, Bytes, BytesMut};
use prost::Message;

use crate::protocol::oicq;
use crate::protocol::packet::*;
use crate::Engine;

impl Engine {
    pub fn build_oicq_request_packet(&self, uin: i64, command_id: u16, body: &[u8]) -> Bytes {
        let req = oicq::Message {
            uin: uin as u32,
            command: command_id,
            body: Bytes::from(body.to_vec()),
            encryption_method: oicq::EncryptionMethod::ECDH,
        };
        self.transport.oicq_codec.encode(req)
    }

    pub fn uni_packet_with_seq(&self, seq: i32, command: &str, body: Bytes) -> Packet {
        Packet {
            packet_type: PacketType::Simple,
            encrypt_type: EncryptType::D2Key,
            seq_id: seq,
            body,
            command_name: command.to_owned(),
            uin: self.uin(),
            ..Default::default()
        }
    }

    pub fn uni_packet(&self, command: &str, body: Bytes) -> Packet {
        let seq = self.next_seq();
        self.uni_packet_with_seq(seq as i32, command, body)
    }
}

pub fn pack_uni_request_data(data: &[u8]) -> Bytes {
    let mut r = BytesMut::new();
    r.put_slice(&[0x0A]);
    r.put_slice(data);
    r.put_slice(&[0x0B]);
    Bytes::from(r)
}

pub trait PbToBytes<B: Message> {
    fn to_bytes(&self) -> Bytes;
}

impl<B: Message> PbToBytes<B> for B {
    fn to_bytes(&self) -> Bytes {
        let mut buf = BytesMut::new();
        prost::Message::encode(self, &mut buf).expect("prost encode failed");
        buf.freeze()
    }
}