Advertisement
Guest User

Untitled

a guest
May 30th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. extern crate irc;
  2. extern crate dotenv;
  3.  
  4. use irc::client::server::IrcServer;
  5. use irc::client::data::config::Config;
  6. use dotenv::dotenv;
  7. use std::env;
  8.  
  9. fn main() {
  10. dotenv().ok();
  11.  
  12. for (key, value) in env::vars() {
  13. println!("ITER: {}: {}", key, value);
  14. }
  15. let port_string = get_env_var("IRC_PORT");
  16. let port = if let Some(ref port_string) = port_string {
  17. Some(port_string.parse::<u16>().unwrap())
  18. } else {
  19. None
  20. };
  21. let config = Config {
  22. owners: get_split_env_var("IRC_OWNERS"),
  23. nickname: get_env_var("IRC_NICKNAME"),
  24. nick_password: get_env_var("IRC_PASSWORD"),
  25. alt_nicks: get_split_env_var("IRC_NICKNAME_ALTS"),
  26. username: get_env_var("IRC_USERNAME"),
  27. realname: get_env_var("IRC_REALNAME"),
  28. server: get_env_var("IRC_SERVER"),
  29. port: port,
  30. password: get_env_var("IRC_SERVER_PASSWORD"),
  31. use_ssl: Some(true),
  32. encoding: Some(String::from("UTF-8")),
  33. channels: get_split_env_var("IRC_CHANNELS"),
  34. // umodes: Some(String::from("+BCdi")),
  35. umodes: None,
  36. user_info: get_env_var("IRC_USER_INFO"),
  37. ping_time: Some(180),
  38. ping_timeout: Some(10),
  39. should_ghost: Some(true),
  40. ghost_sequence: None,
  41. options: None
  42. };
  43.  
  44. let server = IrcServer::from_config(config).unwrap();
  45. }
  46.  
  47. /// A helper method to split the given string into a `Vec` of `String`s by `,`.
  48. fn split_to_vec(var: String) -> Vec<String> {
  49. var.split(",").map(|i| i.to_owned()).collect::<Vec<String>>()
  50. }
  51.  
  52. fn get_env_var(key: &str) -> Option<String> {
  53. let val = env::var(key);
  54. match val {
  55. Ok(s) => { println!("{}: {}", key, s); Some(s) },
  56. Err(_) => { println!("{}: None", key); None },
  57. }
  58. }
  59.  
  60. fn get_split_env_var(key: &str) -> Option<Vec<String>> {
  61. let val = get_env_var(key);
  62. match val {
  63. Some(s) => { Some(split_to_vec(s)) },
  64. None => { None }
  65. }
  66. }
  67.  
  68. #[test]
  69. fn test_split() {
  70. assert_eq!(vec![String::from("abc"), String::from("def")], split_to_vec(String::from("abc,def")));
  71. assert_eq!(vec![String::from("abc")], split_to_vec(String::from("abc")));
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement