/src/main.rs

https://gitlab.com/owbone/rust-e · Rust · 108 lines · 71 code · 16 blank · 21 comment · 8 complexity · 95cb54eb92075a8b63776e0bfcbff370 MD5 · raw file

  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2015 Oliver Bone
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. extern crate getopts;
  23. extern crate regex;
  24. mod irc;
  25. use getopts::Options;
  26. use std::env;
  27. use std::iter::FromIterator;
  28. use std::io::BufReader;
  29. use std::io::Error as IoError;
  30. use std::str::FromStr;
  31. use std::net::TcpStream;
  32. type IrcStream = TcpStream;
  33. static DEFAULT_PORT_NUMBER: u16 = 6667;
  34. fn main() {
  35. let mut opts = Options::new();
  36. opts.optopt("s", "server", "Address", "ADDRESS");
  37. opts.optopt("p", "port", "Port", "PORT");
  38. let args = Vec::from_iter(env::args());
  39. let program = args[0].clone();
  40. let matches = match opts.parse(args) {
  41. Ok(m) => { m }
  42. Err(e) => {
  43. println!("Invalid address: {}", e);
  44. return usage(&program, opts);
  45. }
  46. };
  47. let hostname = match matches.opt_str("s") {
  48. Some(a) => { a }
  49. None => { return usage(&program, opts); }
  50. };
  51. let port = if let Some(p) = matches.opt_str("p") {
  52. match u16::from_str(&p) {
  53. Ok(p) => { p }
  54. Err(e) => {
  55. println!("Invalid port: {}", e);
  56. return usage(&program, opts);
  57. }
  58. }
  59. }
  60. else {
  61. DEFAULT_PORT_NUMBER
  62. };
  63. println!("Connecting to {}:{}...", hostname, port);
  64. match tcp_connect(&hostname, port) {
  65. Ok(stream) => {
  66. println!("Connected.");
  67. let stream_buf = BufReader::new(stream);
  68. let mut message_stream = irc::MessageStream::new(stream_buf);
  69. while let Ok(message) = message_stream.read_one() {
  70. match message.command {
  71. irc::Command::Notice(params) => {
  72. println!("Notice @ {} : {}",
  73. params.target,
  74. params.message);
  75. }
  76. }
  77. }
  78. }
  79. Err(e) => {
  80. println!("Failed to connect: {}!", e);
  81. }
  82. }
  83. }
  84. fn usage(program: &str, opts: Options) {
  85. let brief = format!("Usage: {} [options]", program);
  86. print!("{}", opts.usage(&brief));
  87. }
  88. fn tcp_connect(hostname: &str, port: u16) -> Result<IrcStream, IoError> {
  89. let address = format!("{}:{}", hostname, port);
  90. let tcp_stream = try!(TcpStream::connect(&address[..]));
  91. Ok(tcp_stream)
  92. }