diff options
author | Till Hoeppner | 2015-04-01 23:29:45 +0200 |
---|---|---|
committer | Till Hoeppner | 2015-04-01 23:29:45 +0200 |
commit | a02f3c09482807220c5642b0e03d8f2d8aea243a (patch) | |
tree | 19a8a91aabb11755b99520cbe7689cbb8b6f8ad5 /src | |
parent | bead9324f053957f73042b1e33905aaa73b57649 (diff) | |
download | ilc-a02f3c09482807220c5642b0e03d8f2d8aea243a.tar.gz ilc-a02f3c09482807220c5642b0e03d8f2d8aea243a.tar.xz ilc-a02f3c09482807220c5642b0e03d8f2d8aea243a.zip |
Don't match with regex anymore
Diffstat (limited to 'src')
-rw-r--r-- | src/format/weechat3.rs | 66 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/log.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 18 |
4 files changed, 76 insertions, 18 deletions
diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs index d0dba67..0837896 100644 --- a/src/format/weechat3.rs +++ b/src/format/weechat3.rs @@ -1,18 +1,21 @@ use std::io::{ self, BufRead, Write }; use std::borrow::ToOwned; +use std::iter::{ Iterator, AdditiveIterator }; use log::Event; use format::{ Encode, Decode }; -use regex::Regex; +use l::LogLevel::Info; + +//use regex::Regex; use chrono::*; pub struct Weechat3; -static NORMAL_LINE: Regex = regex!(r"^(\d+-\d+-\d+ \d+:\d+:\d+)\t[@%+~&]?([^ <-]\S+)\t(.*)"); -static ACTION_LINE: Regex = regex!(r"^(\d+-\d+-\d+ \d+:\d+:\d+)\t \*\t(\S+) (.*)"); -static OTHER_LINES: Regex = regex!(r"^(\d+-\d+-\d+ \d+:\d+:\d+)\s(?:--|<--|-->)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)(.*)\n$"); +//static NORMAL_LINE: Regex = regex!(r"^(\d+-\d+-\d+ \d+:\d+:\d+)\t[@%+~&]?([^ <-]\S+)\t(.*)"); +//static ACTION_LINE: Regex = regex!(r"^(\d+-\d+-\d+ \d+:\d+:\d+)\t \*\t(\S+) (.*)"); +//static OTHER_LINES: Regex = regex!(r"^(\d+-\d+-\d+ \d+:\d+:\d+)\s(?:--|<--|-->)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)(.*)\n$"); //static OTHER_LINES: Regex = regex!(r"(.+)"); static TIME_DATE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S"; @@ -25,8 +28,14 @@ pub struct Iter<R> where R: BufRead { impl<R> Iterator for Iter<R> where R: BufRead { type Item = ::Result<Event>; fn next(&mut self) -> Option<::Result<Event>> { - fn time(s: &str) -> i64 { - UTC.datetime_from_str(s, TIME_DATE_FORMAT).unwrap().timestamp() + fn timestamp(date: &str, time: &str) -> i64 { + UTC.datetime_from_str(&format!("{} {}", date, time), TIME_DATE_FORMAT).unwrap().timestamp() + } + fn join(s: &[&str]) -> String { + let len = s.iter().map(|s| s.len()).sum(); + let mut out = s.iter().fold(String::with_capacity(len), + |mut s, b| { s.push_str(b); s.push(' '); s }); + out.pop(); out } fn mask(s: &str) -> String { s.trim_left_matches('(').trim_right_matches(')').to_owned() @@ -38,11 +47,39 @@ impl<R> Iterator for Iter<R> where R: BufRead { Ok(0) | Err(_) => return None, Ok(_) => () } - let line = &self.buffer; + + let tokens = self.buffer.split(|c: char| c.is_whitespace()).collect::<Vec<_>>(); + if log_enabled!(Info) { + info!("Parsing {:?}", tokens); + } + match tokens.as_ref() { + [date, time, "-->", nick, host, "has", "joined", channel, _..] => return Some(Ok(Event::Join { + nick: nick.to_owned(), channel: channel.to_owned(), mask: mask(host), + time: timestamp(date, time) + })), + [date, time, "<--", nick, host, "has", "left", channel, reason, _..] => return Some(Ok(Event::Part { + nick: nick.to_owned(), channel: channel.to_owned(), mask: mask(host), + reason: reason.to_owned(), time: timestamp(date, time) + })), + [date, time, "--", "irc:", "disconnected", "from", "server", _..] => return Some(Ok(Event::Disconnect { + time: timestamp(date, time) + })), + [date, time, "*", nick, msg..] => return Some(Ok(Event::Action { + from: nick.to_owned(), content: join(msg), + time: timestamp(date, time) + })), + [date, time, nick, msg..] => return Some(Ok(Event::Msg { + from: nick.to_owned(), + content: join(msg), + time: timestamp(date, time) + })), + _ => () + } + /* if let Some(cap) = NORMAL_LINE.captures(line) { return Some(Ok(Event::Msg { - from: cap.at(1).unwrap().to_owned(), - content: cap.at(2).unwrap().to_owned(), + from: cap.at(2).unwrap().to_owned(), + content: cap.at(3).unwrap().to_owned(), time: time(cap.at(1).unwrap()) })) } else if let Some(cap) = ACTION_LINE.captures(line) { @@ -97,7 +134,7 @@ impl<R> Iterator for Iter<R> where R: BufRead { time: time(cap.at(1).unwrap()) })) } - } + }*/ } } } @@ -118,13 +155,13 @@ impl<W> Encode<W> for Weechat3 where W: Write { } match event { &Event::Msg { ref from, ref content, ref time } => { - try!(write!(&mut output, "{}\t{}\t{}\n", date(*time), from, content)) + try!(writeln!(&mut output, "{}\t{}\t{}", date(*time), from, content)) }, &Event::Action { ref from, ref content, ref time } => { - try!(write!(&mut output, "{}\t*\t{} {}\n", date(*time), from, content)) + try!(writeln!(&mut output, "{}\t*\t{} {}", date(*time), from, content)) }, &Event::Join { ref nick, ref mask, ref channel, ref time } => { - try!(write!(&mut output, "{}\t-->\t{} ({}) has joined {}\n", + try!(writeln!(&mut output, "{}\t-->\t{} ({}) has joined {}", date(*time), nick, mask, channel)) }, &Event::Part { ref nick, ref mask, ref channel, ref time, ref reason } => { @@ -142,6 +179,9 @@ impl<W> Encode<W> for Weechat3 where W: Write { } try!(write!(&mut output, "\n")) }, + &Event::Disconnect { ref time } => { + try!(writeln!(&mut output, "{}\t--\tirc: disconnected from server", date(*time))) + }, _ => () } Ok(()) @@ -1,7 +1,9 @@ -#![feature(plugin)] +#![feature(plugin, slice_patterns, convert, core)] #![plugin(regex_macros)] extern crate regex; extern crate chrono; +#[macro_use] +extern crate log as l; pub mod log; pub mod format; @@ -11,6 +11,12 @@ pub struct Log { /// and topic changes. #[derive(Debug)] pub enum Event { + Connect { + time: i64 + }, + Disconnect { + time: i64 + }, Msg { from: String, content: String, diff --git a/src/main.rs b/src/main.rs index b5f65a9..4bf22f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ extern crate ilc; extern crate docopt; -extern crate "rustc-serialize" as rustc_serialize; +extern crate rustc_serialize; extern crate libc; extern crate regex; @@ -14,16 +14,26 @@ use docopt::Docopt; use ilc::format::{ self, Encode, Decode }; -static USAGE: &'static str = " +static USAGE: &'static str = r#" +d8b 888 +Y8P 888 + 888 +888 888 .d8888b +888 888 d88P" +888 888 888 +888 888 Y88b. +888 888 "Y8888P + A converter and statistics utility for IRC log files. Usage: ilc parse <file>... + ilc Options: -h --help Show this screen. -v --version Show the version (duh). -"; +"#; #[derive(RustcDecodable, Debug)] struct Args { @@ -49,7 +59,7 @@ fn main() { let iter = parser.decode(f); let events: Vec<_> = iter.collect(); for e in events { - parser.encode(io::stdout(), &e.unwrap()); + drop(parser.encode(io::stdout(), &e.unwrap())); } } } |