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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # ChatFile module functions

use super::rpc::Rpc;
use prost::Message;
use std::fmt;

/// include generated protobuf RPC rust definition file
mod proto {
    include!("../../../libqaul/src/rpc/protobuf_generated/rust/qaul.rpc.chatfile.rs");
}

/// Chat file module function handling
pub struct ChatFile {}

impl ChatFile {
    /// CLI command interpretation
    ///
    /// The CLI commands of the chat file module are processed here
    pub fn cli(command: &str) {
        match command {
            // send file
            cmd if cmd.starts_with("send ") => {
                let command_string = cmd.strip_prefix("send ").unwrap().to_string();
                let mut iter = command_string.split_whitespace();

                if let Some(group_id_str) = iter.next() {
                    let group_id;
                    // convert group id from string to binary version
                    match Self::id_string_to_bin(group_id_str.to_string()) {
                        Ok(id) => {
                            group_id = id.clone();
                        }
                        Err(_e) => match Self::uuid_string_to_bin(group_id_str.to_string()) {
                            Ok(id) => {
                                group_id = id.clone();
                            }
                            _ => {
                                log::error!("Invalid group id");
                                return;
                            }
                        },
                    }

                    if let Some(file_path_name) = iter.next() {
                        let descr = match iter.next() {
                            Some(description) => description.to_string(),
                            _ => "".to_string(),
                        };

                        log::trace!(
                            "send file to group={}, file path={}, description={}",
                            group_id_str,
                            file_path_name,
                            descr
                        );

                        Self::send_file(group_id, file_path_name.to_string(), descr);
                    } else {
                        log::error!("file pathname is not given");
                    }
                } else {
                    log::error!("chat send command incorrectly formatted");
                }
            }

            // request chat file history list
            cmd if cmd.starts_with("history") => {
                let mut offset: i32 = 0;
                let mut limit: i32 = 10;

                if cmd.starts_with("history ") {
                    let command_string = cmd.strip_prefix("history ").unwrap().to_string();
                    let mut iter = command_string.split_whitespace();

                    if let Some(offset_str) = iter.next() {
                        offset = offset_str.to_string().parse().unwrap();
                        if let Some(limit_str) = iter.next() {
                            limit = limit_str.to_string().parse().unwrap();
                        }
                    }
                }

                Self::send_file_history_command(offset as u32, limit as u32);
            }
            // unknown command
            _ => log::error!("unknown file command"),
        }
    }

    /// Convert Group ID from String to Binary
    fn id_string_to_bin(id: String) -> Result<Vec<u8>, String> {
        // check length
        if id.len() < 52 {
            return Err("Group ID not long enough".to_string());
        }

        // convert input
        match bs58::decode(id).into_vec() {
            Ok(id_bin) => Ok(id_bin),
            Err(e) => {
                let err = fmt::format(format_args!("{}", e));
                Err(err)
            }
        }
    }

    /// Convert Group ID from String to Binary
    fn uuid_string_to_bin(id_str: String) -> Result<Vec<u8>, String> {
        match uuid::Uuid::parse_str(id_str.as_str()) {
            Ok(id) => Ok(id.as_bytes().to_vec()),
            _ => Err("invalid group id".to_string()),
        }
    }

    /// send file via rpc
    fn send_file(group_id: Vec<u8>, file_name: String, description: String) {
        // create file send message
        let proto_message = proto::ChatFile {
            message: Some(proto::chat_file::Message::SendFileRequest(
                proto::SendFileRequest {
                    path_name: file_name.clone(),
                    group_id: group_id.clone(),
                    description: description.clone(),
                },
            )),
        };

        // encode message
        let mut buf = Vec::with_capacity(proto_message.encoded_len());
        proto_message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send message
        Rpc::send_message(
            buf,
            super::rpc::proto::Modules::Chatfile.into(),
            "".to_string(),
        );
    }

    /// send file history list command via rpc
    fn send_file_history_command(offset: u32, limit: u32) {
        // create file history message
        let proto_message = proto::ChatFile {
            message: Some(proto::chat_file::Message::FileHistory(
                proto::FileHistoryRequest { offset, limit },
            )),
        };

        // encode message
        let mut buf = Vec::with_capacity(proto_message.encoded_len());
        proto_message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send message
        Rpc::send_message(
            buf,
            super::rpc::proto::Modules::Chatfile.into(),
            "".to_string(),
        );
    }

    /// Process received RPC message
    ///
    /// Decodes received protobuf encoded binary RPC message
    /// of the file share module.
    pub fn rpc(data: Vec<u8>) {
        match proto::ChatFile::decode(&data[..]) {
            Ok(file_share) => {
                match file_share.message {
                    Some(proto::chat_file::Message::FileHistoryResponse(proto_file_history)) => {
                        // List header
                        println!("====================================");
                        println!("File Sharing Histories");
                        println!("------------------------------------");
                        println!(
                            "offset={}, limit={}, total={}",
                            proto_file_history.offset,
                            proto_file_history.limit,
                            proto_file_history.total
                        );

                        // print all messages in the feed list
                        for entry in proto_file_history.histories {
                            println!("[{}] - {}", entry.file_id, entry.file_name);
                            println!("\t Time: {}, SenderId: {}", entry.time, entry.sender_id);
                            println!("\t Group Id: {}", entry.group_id);
                            println!(
                                "\t FileSize: {}, Description: {}",
                                entry.file_size, entry.file_description
                            );
                            println!("");
                        }
                    }
                    _ => {
                        log::error!("unprocessable RPC file message");
                    }
                }
            }
            Err(error) => {
                log::error!("{:?}", error);
            }
        }
    }
}