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
//! Send and receive messages.

use std::borrow::Cow;
use std::collections::HashMap;
use std::io::Read;

use reqwest::{Client, StatusCode};
use reqwest::header::{Accept, ContentType};
use reqwest::mime::Mime;
use data_encoding::HEXLOWER;

use ::errors::ApiError;
use ::types::BlobId;
use ::MSGAPI_URL;


/// Map HTTP response status code to an ApiError if it isn't "200".
///
/// Optionally, you can pass in the meaning of a 400 response code.
pub fn map_response_code(status: &StatusCode, bad_request_meaning: Option<ApiError>)
                         -> Result<(), ApiError> {
    match *status {
        // 200
        StatusCode::Ok => Ok(()),
        // 400
        StatusCode::BadRequest => match bad_request_meaning {
            Some(error) => Err(error),
            None => Err(ApiError::Other(format!("Bad response status code: {}", StatusCode::BadRequest))),
        },
        // 401
        StatusCode::Unauthorized => Err(ApiError::BadCredentials),
        // 402
        StatusCode::PaymentRequired => Err(ApiError::NoCredits),
        // 404
        StatusCode::NotFound => Err(ApiError::IdNotFound),
        // 413
        StatusCode::PayloadTooLarge => Err(ApiError::MessageTooLong),
        // 500
        StatusCode::InternalServerError => Err(ApiError::ServerError),
        e @ _ => Err(ApiError::Other(format!("Bad response status code: {}", e))),
    }
}

/// Different ways to specify a message recipient in basic mode.
#[derive(Debug)]
pub enum Recipient<'a> {
    /// Recipient identity (8 characters)
    Id(Cow<'a, str>),
    /// Recipient phone number (E.164), without leading +
    Phone(Cow<'a, str>),
    /// Recipient e-mail address
    Email(Cow<'a, str>),
}

impl<'a> Recipient<'a> {
    pub fn new_id<T: Into<Cow<'a, str>>>(id: T) -> Self {
        Recipient::Id(id.into())
    }

    pub fn new_phone<T: Into<Cow<'a, str>>>(phone: T) -> Self {
        Recipient::Phone(phone.into())
    }

    pub fn new_email<T: Into<Cow<'a, str>>>(email: T) -> Self {
        Recipient::Email(email.into())
    }
}

/// Send a message to the specified recipient in basic mode.
pub fn send_simple(from: &str, to: &Recipient, secret: &str, text: &str) -> Result<String, ApiError> {

    let client = Client::new().expect("Could not initialize HTTP client");

    // Check text length (max 3500 bytes)
    // Note: Strings in Rust are UTF8, so len() returns the byte count.
    if text.len() > 3500 {
        return Err(ApiError::MessageTooLong);
    }

    // Prepare POST data
    let mut params = HashMap::new();
    params.insert("from", from);
    params.insert("text", text);
    params.insert("secret", secret);
    match *to {
        Recipient::Id(ref id) => params.insert("to", id),
        Recipient::Phone(ref phone) => params.insert("phone", phone),
        Recipient::Email(ref email) => params.insert("email", email),
    };

    // Send request
    let mut res = client.post(&format!("{}/send_simple", MSGAPI_URL))
        .expect("Could not parse URL")
        .form(&params)?
        .header(Accept::json())
        .send()?;
    try!(map_response_code(&res.status(), Some(ApiError::BadSenderOrRecipient)));

    // Read and return response body
    let mut body = String::new();
    try!(res.read_to_string(&mut body));

    Ok(body)
}

/// Send an encrypted E2E message to the specified recipient.
pub fn send_e2e(from: &str,
                to: &str,
                secret: &str,
                nonce: &[u8],
                ciphertext: &[u8],
                additional_params: Option<HashMap<String, String>>)
                -> Result<String, ApiError> {
    let client = Client::new().expect("Could not initialize HTTP client");

    // Prepare POST data
    let mut params = match additional_params {
        Some(p) => p,
        None => HashMap::new(),
    };
    params.insert("from".into(), from.into());
    params.insert("to".into(), to.into());
    params.insert("secret".into(), secret.into());
    params.insert("nonce".into(), HEXLOWER.encode(nonce));
    params.insert("box".into(), HEXLOWER.encode(ciphertext));

    // Send request
    let mut res = client.post(&format!("{}/send_e2e", MSGAPI_URL))
        .expect("Could not parse URL")
        .form(&params)?
        .header(Accept::json())
        .send()?;
    try!(map_response_code(&res.status(), Some(ApiError::BadSenderOrRecipient)));

    // Read and return response body
    let mut body = String::new();
    try!(res.read_to_string(&mut body));

    Ok(body)
}

/// Upload a blob to the blob server.
pub fn blob_upload(from: &str, secret: &str, data: &[u8]) -> Result<BlobId, ApiError> {
    let client = Client::new().expect("Could not initialize HTTP client");

    // Build URL
    let url = format!("{}/upload_blob?from={}&secret={}", MSGAPI_URL, from, secret);

    // Build multipart/form-data request body
    let boundary = "3ma-d84f64f5-a138-4b0a-9e25-339257990c81-3ma".to_string();
    let mut req_body = Vec::new();
    req_body.extend_from_slice("--".as_bytes());
    req_body.extend_from_slice(&boundary.as_bytes());
    req_body.extend_from_slice("\r\n".as_bytes());
    req_body.extend_from_slice("Content-Disposition: form-data; name=\"blob\"\r\n".as_bytes());
    req_body.extend_from_slice("Content-Type: application/octet-stream\r\n\r\n".as_bytes());
    req_body.extend_from_slice(data);
    req_body.extend_from_slice("\r\n--".as_bytes());
    req_body.extend_from_slice(&boundary.as_bytes());
    req_body.extend_from_slice("--\r\n".as_bytes());

    // Send request
    let mimetype: Mime = format!("multipart/form-data; boundary={}", boundary)
        .parse().expect("Could not parse multipart/form-data mime type");
    let mut res = client.post(&url)
        .expect("Could not parse URL")
        .body(req_body)
        .header(Accept::text())
        .header(ContentType(mimetype))
        .send()?;
    try!(map_response_code(&res.status(), Some(ApiError::BadBlob)));

    // Read response body containing blob ID
    let mut body = String::new();
    res.read_to_string(&mut body)?;

    BlobId::from_str(body.trim())
}

#[cfg(test)]
mod tests {
    use std::iter::repeat;
    use ::errors::ApiError;
    use super::*;

    #[test]
    fn test_max_length_ok() {
        let text: String = repeat("à").take(3500 / 2).collect();
        let result = send_simple("TESTTEST", &Recipient::new_id("ECHOECHO"), "secret", &text);
        match result {
            Err(ApiError::MessageTooLong) => panic!(),
            _ => (),
        }
    }

    #[test]
    fn test_max_length_too_long() {
        let mut text: String = repeat("à").take(3500 / 2).collect();
        text.push('x');
        let result = send_simple("TESTTEST", &Recipient::new_id("ECHOECHO"), "secret", &text);
        match result {
            Err(ApiError::MessageTooLong) => (),
            _ => panic!(),
        }
    }

}