2019-01-26 20:05:48 +00:00
|
|
|
// Copyright (C) 2019 Maxime “pep” Buquet <pep@bouah.net>
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
use crate::data_forms::{DataForm, DataFormType, Field, FieldType};
|
|
|
|
use crate::ns;
|
|
|
|
use crate::util::error::Error;
|
|
|
|
|
|
|
|
/// Structure representing a `http://jabber.org/network/serverinfo` form type.
|
2019-03-20 18:10:01 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Default)]
|
2019-01-26 20:05:48 +00:00
|
|
|
pub struct ServerInfo {
|
|
|
|
/// Abuse addresses
|
|
|
|
pub abuse: Vec<String>,
|
|
|
|
|
|
|
|
/// Admin addresses
|
|
|
|
pub admin: Vec<String>,
|
|
|
|
|
|
|
|
/// Feedback addresses
|
|
|
|
pub feedback: Vec<String>,
|
|
|
|
|
|
|
|
/// Sales addresses
|
|
|
|
pub sales: Vec<String>,
|
|
|
|
|
|
|
|
/// Security addresses
|
|
|
|
pub security: Vec<String>,
|
|
|
|
|
|
|
|
/// Support addresses
|
|
|
|
pub support: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<DataForm> for ServerInfo {
|
2019-04-12 08:58:42 +00:00
|
|
|
type Error = Error;
|
2019-01-26 20:05:48 +00:00
|
|
|
|
|
|
|
fn try_from(form: DataForm) -> Result<ServerInfo, Error> {
|
|
|
|
if form.type_ != DataFormType::Result_ {
|
|
|
|
return Err(Error::ParseError("Wrong type of form."));
|
|
|
|
}
|
|
|
|
if form.form_type != Some(String::from(ns::SERVER_INFO)) {
|
|
|
|
return Err(Error::ParseError("Wrong FORM_TYPE for form."));
|
|
|
|
}
|
2019-03-20 18:10:01 +00:00
|
|
|
let mut server_info = ServerInfo::default();
|
2019-01-26 20:05:48 +00:00
|
|
|
for field in form.fields {
|
|
|
|
if field.type_ != FieldType::ListMulti {
|
|
|
|
return Err(Error::ParseError("Field is not of the required type."));
|
|
|
|
}
|
2023-08-30 13:55:18 +00:00
|
|
|
if field.var.as_deref() == Some("abuse-addresses") {
|
2019-01-26 20:05:48 +00:00
|
|
|
server_info.abuse = field.values;
|
2023-08-30 13:55:18 +00:00
|
|
|
} else if field.var.as_deref() == Some("admin-addresses") {
|
2019-01-26 20:05:48 +00:00
|
|
|
server_info.admin = field.values;
|
2023-08-30 13:55:18 +00:00
|
|
|
} else if field.var.as_deref() == Some("feedback-addresses") {
|
2019-01-26 20:05:48 +00:00
|
|
|
server_info.feedback = field.values;
|
2023-08-30 13:55:18 +00:00
|
|
|
} else if field.var.as_deref() == Some("sales-addresses") {
|
2019-01-26 20:05:48 +00:00
|
|
|
server_info.sales = field.values;
|
2023-08-30 13:55:18 +00:00
|
|
|
} else if field.var.as_deref() == Some("security-addresses") {
|
2019-01-26 20:05:48 +00:00
|
|
|
server_info.security = field.values;
|
2023-08-30 13:55:18 +00:00
|
|
|
} else if field.var.as_deref() == Some("support-addresses") {
|
2019-01-26 20:05:48 +00:00
|
|
|
server_info.support = field.values;
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown form field var."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(server_info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ServerInfo> for DataForm {
|
|
|
|
fn from(server_info: ServerInfo) -> DataForm {
|
|
|
|
DataForm {
|
|
|
|
type_: DataFormType::Result_,
|
|
|
|
form_type: Some(String::from(ns::SERVER_INFO)),
|
|
|
|
title: None,
|
|
|
|
instructions: None,
|
|
|
|
fields: vec![
|
|
|
|
generate_address_field("abuse-addresses", server_info.abuse),
|
|
|
|
generate_address_field("admin-addresses", server_info.admin),
|
|
|
|
generate_address_field("feedback-addresses", server_info.feedback),
|
|
|
|
generate_address_field("sales-addresses", server_info.sales),
|
|
|
|
generate_address_field("security-addresses", server_info.security),
|
|
|
|
generate_address_field("support-addresses", server_info.support),
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate `Field` for addresses
|
|
|
|
pub fn generate_address_field<S: Into<String>>(var: S, values: Vec<String>) -> Field {
|
|
|
|
Field {
|
2023-08-30 13:55:18 +00:00
|
|
|
var: Some(var.into()),
|
2019-01-26 20:05:48 +00:00
|
|
|
type_: FieldType::ListMulti,
|
|
|
|
label: None,
|
|
|
|
required: false,
|
|
|
|
options: vec![],
|
|
|
|
values,
|
|
|
|
media: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(ServerInfo, 72);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(ServerInfo, 144);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let form = DataForm {
|
|
|
|
type_: DataFormType::Result_,
|
|
|
|
form_type: Some(String::from(ns::SERVER_INFO)),
|
|
|
|
title: None,
|
|
|
|
instructions: None,
|
|
|
|
fields: vec![
|
|
|
|
Field {
|
2023-08-30 13:55:18 +00:00
|
|
|
var: Some(String::from("abuse-addresses")),
|
2019-01-26 20:05:48 +00:00
|
|
|
type_: FieldType::ListMulti,
|
|
|
|
label: None,
|
|
|
|
required: false,
|
|
|
|
options: vec![],
|
|
|
|
values: vec![],
|
|
|
|
media: vec![],
|
|
|
|
},
|
|
|
|
Field {
|
2023-08-30 13:55:18 +00:00
|
|
|
var: Some(String::from("admin-addresses")),
|
2019-01-26 20:05:48 +00:00
|
|
|
type_: FieldType::ListMulti,
|
|
|
|
label: None,
|
|
|
|
required: false,
|
|
|
|
options: vec![],
|
|
|
|
values: vec![
|
|
|
|
String::from("xmpp:admin@foo.bar"),
|
|
|
|
String::from("https://foo.bar/chat/"),
|
|
|
|
String::from("mailto:admin@foo.bar"),
|
|
|
|
],
|
|
|
|
media: vec![],
|
|
|
|
},
|
|
|
|
Field {
|
2023-08-30 13:55:18 +00:00
|
|
|
var: Some(String::from("feedback-addresses")),
|
2019-01-26 20:05:48 +00:00
|
|
|
type_: FieldType::ListMulti,
|
|
|
|
label: None,
|
|
|
|
required: false,
|
|
|
|
options: vec![],
|
|
|
|
values: vec![],
|
|
|
|
media: vec![],
|
|
|
|
},
|
|
|
|
Field {
|
2023-08-30 13:55:18 +00:00
|
|
|
var: Some(String::from("sales-addresses")),
|
2019-01-26 20:05:48 +00:00
|
|
|
type_: FieldType::ListMulti,
|
|
|
|
label: None,
|
|
|
|
required: false,
|
|
|
|
options: vec![],
|
|
|
|
values: vec![],
|
|
|
|
media: vec![],
|
|
|
|
},
|
|
|
|
Field {
|
2023-08-30 13:55:18 +00:00
|
|
|
var: Some(String::from("security-addresses")),
|
2019-01-26 20:05:48 +00:00
|
|
|
type_: FieldType::ListMulti,
|
|
|
|
label: None,
|
|
|
|
required: false,
|
|
|
|
options: vec![],
|
|
|
|
values: vec![
|
|
|
|
String::from("xmpp:security@foo.bar"),
|
|
|
|
String::from("mailto:security@foo.bar"),
|
|
|
|
],
|
|
|
|
media: vec![],
|
|
|
|
},
|
|
|
|
Field {
|
2023-08-30 13:55:18 +00:00
|
|
|
var: Some(String::from("support-addresses")),
|
2019-01-26 20:05:48 +00:00
|
|
|
type_: FieldType::ListMulti,
|
|
|
|
label: None,
|
|
|
|
required: false,
|
|
|
|
options: vec![],
|
|
|
|
values: vec![String::from("mailto:support@foo.bar")],
|
|
|
|
media: vec![],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
let server_info = ServerInfo {
|
|
|
|
abuse: vec![],
|
|
|
|
admin: vec![
|
|
|
|
String::from("xmpp:admin@foo.bar"),
|
|
|
|
String::from("https://foo.bar/chat/"),
|
|
|
|
String::from("mailto:admin@foo.bar"),
|
|
|
|
],
|
|
|
|
feedback: vec![],
|
|
|
|
sales: vec![],
|
|
|
|
security: vec![
|
|
|
|
String::from("xmpp:security@foo.bar"),
|
|
|
|
String::from("mailto:security@foo.bar"),
|
|
|
|
],
|
|
|
|
support: vec![String::from("mailto:support@foo.bar")],
|
|
|
|
};
|
|
|
|
|
|
|
|
// assert_eq!(DataForm::from(server_info), form);
|
2019-03-20 18:09:14 +00:00
|
|
|
assert_eq!(ServerInfo::try_from(form).unwrap(), server_info);
|
2019-01-26 20:05:48 +00:00
|
|
|
}
|
|
|
|
}
|