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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#[macro_use]
extern crate serde_derive;
extern crate reqwest;
extern crate serde_json;
#[macro_use]
extern crate url;

use serde_json::{Value};

use std::collections::HashMap;

#[derive(Deserialize, Debug)]
struct JoinInfo {
    room_id: String,
}

#[derive(Deserialize, Debug)]
struct SinceInfo {
    next_batch: String,
}

#[derive(Deserialize, Debug)]
struct AccesstokenInfo {
    access_token: String,
}

#[derive(Deserialize, Debug)]
pub struct LoginInfo {
    pub server_name: String,
    pub username: String,
    pub password: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct ServerInfo {
    pub server_name: String,
    pub access_token: String,
}


use std::rc::Rc;

pub struct MatrixRoom {
    id: String,
    latest_since: Option<String>,
    client : Rc<reqwest::Client>,
    info : ServerInfo,
}

pub struct MatrixHomeserver {
    client : Rc<reqwest::Client>,
    info: ServerInfo,
}

use url::percent_encoding::{utf8_percent_encode, USERINFO_ENCODE_SET,  PATH_SEGMENT_ENCODE_SET};

define_encode_set! {
    pub ACCESS_TOKEN_ENCODE_SET = [USERINFO_ENCODE_SET] | {
        '%', '&'
    }
}

impl MatrixHomeserver {
    pub fn new(info : ServerInfo) -> Self {
        MatrixHomeserver {
            client : Rc::new(reqwest::Client::new()),
            info: info
        }
    }
    pub fn login(info : LoginInfo) -> Self {
        let client = reqwest::Client::new();

        let mut res = client.get(
            &format!("{}/_matrix/client/r0/login",
                     info.server_name))
            .send()
            .unwrap();

        let mut pwlogin : bool = false;
        
        let v: Value = serde_json::from_str(&(res.text().unwrap())).unwrap();
        match v["flows"].as_array() {
             Some(flowlist) =>               
                for flow in flowlist {
                    match flow["type"].as_str() {
                        Some("m.login.password") => {println!("Found login option `m.login.password`"); pwlogin = true;}
                        _ => ()
                    }
                },
            _ => ()
        }
        if !pwlogin {panic!("Passwort-Login nicht möglich")}

        let mut map : HashMap<String,String> = HashMap::new();

        map.insert("type".to_owned() , "m.login.password".to_owned());
        map.insert("user".to_owned() , info.username);
        map.insert("password".to_owned() , info.password);
    
        let mut res = client.post(
            &format!("{}/_matrix/client/r0/login",
                     info.server_name))
            .json(&map)
            .send()
            .unwrap();

        //println!("{}",res.text().unwrap());
        let at_info: AccesstokenInfo = res.json().unwrap();
        MatrixHomeserver {
            client : Rc::new(client),
            info: ServerInfo {
                server_name: info.server_name,
                access_token: at_info.access_token,
            }
        }
    }
    pub fn join(&self, room_name : String) -> MatrixRoom {
        let map : HashMap<String,String> = HashMap::new();
        
        let mut res = self.client.post(
            &format!("{}{}{}{}{}",
                    self.info.server_name,
                    "/_matrix/client/r0/join/",
                    utf8_percent_encode(&room_name, PATH_SEGMENT_ENCODE_SET).to_string(),
                    "?access_token=",
                    utf8_percent_encode(&self.info.access_token, ACCESS_TOKEN_ENCODE_SET).to_string()))
            .json(&map)
            .send()
            .unwrap();
        let info: JoinInfo = res.json().unwrap();
        MatrixRoom {
            id: info.room_id,
            latest_since: None,
            client : self.client.clone(),
            info : self.info.clone(),
        }            
    }
    pub fn create_room(&self, room_name : String) -> Option<MatrixRoom> {
        let mut map : HashMap<String,String> = HashMap::new();
        
        map.insert("room_alias_name".to_owned() , room_name);
        map.insert("preset".to_owned() , "public_chat".to_owned());
        
        let mut res = self.client.post(
            &format!("{}/_matrix/client/r0/createRoom?access_token={}",
                     self.info.server_name,
                     utf8_percent_encode(&self.info.access_token, ACCESS_TOKEN_ENCODE_SET).to_string()))
            .json(&map)
            .send()
            .unwrap();
        let info: Result<JoinInfo, _> = res.json();
        match info {
            Ok(info) => Some(MatrixRoom {
                id: info.room_id,
                latest_since: None,
                client : self.client.clone(),
                info : self.info.clone(),
            }),
            _ => None
        }
    }
}

extern crate rand;

impl MatrixRoom {
    pub fn get_new_messages(&mut self) -> Vec<String> {
        match self.latest_since.clone() {
            None => {    
                let mut res = self.client.get(
                    &format!("{}{}{}{}{}",
                             self.info.server_name,
                             "/_matrix/client/r0/sync?filter={\"room\":{\"rooms\":[\"",
                             utf8_percent_encode(&self.id, PATH_SEGMENT_ENCODE_SET).to_string(),
                             "\"],\"timeline\":{\"limit\":1}}}&access_token=",
                             utf8_percent_encode(&self.info.access_token, ACCESS_TOKEN_ENCODE_SET).to_string()))
                    .send()
                    .unwrap();
                //println!("{}",res.text().unwrap());
                let info: SinceInfo = res.json().unwrap();
                self.latest_since = Some(info.next_batch);
                Vec::new()
            },
            Some(since) => {
                let mut res = self.client.get(
                    &format!("{}{}{}{}{}{}{}",
                             self.info.server_name,
                             "/_matrix/client/r0/sync?since=",
                             since,
                             "&filter={\"room\":{\"rooms\":[\"",
                             utf8_percent_encode(&self.id, PATH_SEGMENT_ENCODE_SET).to_string(),
                             "\"]}}&access_token=",
                             utf8_percent_encode(&self.info.access_token, ACCESS_TOKEN_ENCODE_SET).to_string()))
                    .send()
                    .unwrap();
                let mut vec = Vec::new();

                let v: Value = serde_json::from_str(&(res.text().unwrap())).unwrap();
                self.latest_since = Some(v["next_batch"].as_str().unwrap().to_owned());
                match v["rooms"]["join"][&self.id]["timeline"]["events"].as_array() {
                    Some(eventlist) =>               
                        for event in eventlist {
                            match event["content"]["msgtype"].as_str() {
                                Some("m.text") => vec.push(event["content"]["body"].as_str().unwrap().to_owned()),
                                _ => ()
                            }
                        },
                    _ => ()
                }

                vec
            }
        }
            
    }
    pub fn send_message(&self, message: String ) {
        let mut map : HashMap<String,String> = HashMap::new();
        
        map.insert("msgtype".to_owned() , "m.text".to_owned());
        map.insert("body".to_owned() , message);
    
        self.client.put(
            &format!("{}/_matrix/client/r0/rooms/{}/send/m.room.message/{}?access_token={}",
                     self.info.server_name,                     
                     utf8_percent_encode(&self.id, PATH_SEGMENT_ENCODE_SET).to_string(),
                     rand::random::<u64>(),
                     utf8_percent_encode(&self.info.access_token, ACCESS_TOKEN_ENCODE_SET).to_string())) 
            .json(&map)
            .send()
            .unwrap();
    }
}