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
use encoding::EncodingRef;
use encoding::all::UTF_8;
use loirc::{MonitorSettings, ReconnectionSettings};
use ::core::{dispatch, Error};
use ::listener::Listener;

/// Settings for the dispatcher.
pub struct Settings<'a> {
    /// Address of the irc server.
    pub addr: &'a str,
    /// Preferred nickname.
    pub nickname: &'a str,
    /// Username.
    pub username: &'a str,
    /// Real name.
    pub realname: &'a str,
    /// Reconnection settings. If None, reconnection is disabled.
    pub reconnection: ReconnectionSettings,
    /// Monitor settings. If None, monitoring is disabled.
    pub monitor: Option<MonitorSettings>,
    /// Automatically identify after reconnection.
    pub auto_ident: bool,
    /// Automatically reply to ping requests.
    pub auto_ping: bool,
    /// Encoding used for the connection.
    pub encoding: EncodingRef,
    /// Server password
    pub password: &'a str,
}

impl<'a> Settings<'a> {

    /// Create new settings with sensible default values.
    ///
    /// The default values are:
    ///
    /// ```ignore
    /// username: "hiirc",
    /// realname: "hiirc",
    /// reconnection: ReonnectionSettings::DoNotReconnect,
    /// monitor: None,
    /// auto_ident: true,
    /// auto_ping: true,
    /// encoding: UTF_8,
    /// ```
    pub fn new<'b>(addr: &'b str, nickname: &'b str) -> Settings<'b> {
        Settings {
            addr: addr,
            nickname: nickname,
            username: "hiirc",
            realname: "hiirc",
            reconnection: ReconnectionSettings::DoNotReconnect,
            monitor: None,
            auto_ident: true,
            auto_ping: true,
            encoding: UTF_8,
            password: "",
        }
    }

    /// Modify the username.
    pub fn username(mut self, username: &'a str) -> Settings<'a> {
        self.username = username;
        self
    }

    /// Modify the realname.
    pub fn realname(mut self, realname: &'a str) -> Settings<'a> {
        self.realname = realname;
        self
    }

    /// Modify the reconnection settings.
    pub fn reconnection(mut self, reconnection: ReconnectionSettings) -> Settings<'a> {
        self.reconnection = reconnection;
        self
    }

    /// Modify the monitor settings.
    pub fn monitor(mut self, monitor: Option<MonitorSettings>) -> Settings<'a> {
        self.monitor = monitor;
        self
    }

    /// Enable/disable automatic indentification.
    pub fn auto_ident(mut self, auto_ident: bool) -> Settings<'a> {
        self.auto_ident = auto_ident;
        self
    }

    /// Enable/disable automatic ping replies.
    pub fn auto_ping(mut self, auto_ping: bool) -> Settings<'a> {
        self.auto_ping = auto_ping;
        self
    }

    /// Modify the encoding used for this connection.
    pub fn encoding(mut self, encoding: EncodingRef) -> Settings<'a> {
        self.encoding = encoding;
        self
    }

    /// Modify the server password.
    pub fn password(mut self, password: &'a str) -> Settings<'a> {
        self.password = password;
        self
    }

    /// Connect to the server and begin dispatching events using the given `Listener`.
    pub fn dispatch<L>(self, listener: L) -> Result<(), Error>
        where L: Listener
    {
        dispatch(listener, self)
    }

}