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
use elementtree::Element;
use std::str::FromStr;
use std::string::ParseError;

#[derive(Debug, Clone)]
pub struct Bind {
    id: String,
    bind_type: String,
    pub body: Option<Element>,
    pub jid: String
}

impl Bind {
    pub fn new() -> Bind {
        Bind {
            id: String::new(),
            bind_type: String::new(),
            jid: String::new(),
            body: None
        }
    }

    pub fn set_type(mut self, t: &str) -> Self {
        self.bind_type = t.to_string();

        self
    }

    pub fn set_id(mut self, id: &str) -> Self {
        self.id = id.to_string();

        self
    }
}

impl FromStr for Bind {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let root = Element::from_reader(s.as_bytes()).unwrap();

        let bind_type = root.get_attr("type").unwrap_or("").to_string();
        let id = root.get_attr("id").unwrap_or("").to_string();
        let jid = match root.find("jid") {
            Some(jid) => jid.text().to_string(),
            None => String::new()
        };

        Ok(Bind {
            bind_type: bind_type,
            id: id,
            jid: jid,
            body: Some(root)
        })
    }
}

impl ToString for Bind {
    fn to_string(&self) -> String {
        format!("<iq type='{bind_type}' id='{id}'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>", id=self.id, bind_type=self.bind_type)
    }
}


#[derive(Debug, Clone)]
pub struct Generic {
    pub id: String,
    pub iq_type: String,
    pub body: Option<Element>
}

impl Generic {
    pub fn new() -> Generic {
        Generic {
            id: String::new(),
            iq_type: String::new(),
            body: None
        }
    }

    pub fn set_type(mut self, t: &str) -> Self {
        self.iq_type = t.to_string();

        self
    }

    pub fn set_id(mut self, id: &str) -> Self {
        self.id = id.to_string();

        self
    }
}

impl FromStr for Generic {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let root = Element::from_reader(s.as_bytes()).unwrap();

        let iq_type = root.get_attr("type").unwrap_or("").to_string();
        let id = root.get_attr("id").unwrap_or("").to_string();

        Ok(Generic {
            iq_type: iq_type,
            id: id,
            body: Some(root)
        })
    }
}

impl ToString for Generic {
    fn to_string(&self) -> String {
        String::new()
    }
}