From a02f3c09482807220c5642b0e03d8f2d8aea243a Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Wed, 1 Apr 2015 23:29:45 +0200 Subject: Don't match with regex anymore --- src/format/weechat3.rs | 66 ++++++++++++++++++++++++++++++++++++++++---------- src/lib.rs | 4 ++- src/log.rs | 6 +++++ src/main.rs | 18 +++++++++++--- 4 files changed, 76 insertions(+), 18 deletions(-) (limited to 'src') 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 where R: BufRead { impl Iterator for Iter where R: BufRead { type Item = ::Result; fn next(&mut self) -> Option<::Result> { - 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 Iterator for Iter where R: BufRead { Ok(0) | Err(_) => return None, Ok(_) => () } - let line = &self.buffer; + + let tokens = self.buffer.split(|c: char| c.is_whitespace()).collect::>(); + 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 Iterator for Iter where R: BufRead { time: time(cap.at(1).unwrap()) })) } - } + }*/ } } } @@ -118,13 +155,13 @@ impl Encode 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 Encode 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(()) diff --git a/src/lib.rs b/src/lib.rs index 0e49d17..1cceb27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/log.rs b/src/log.rs index 1b6ed45..b15d128 100644 --- a/src/log.rs +++ b/src/log.rs @@ -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 ... + 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())); } } } -- cgit v1.2.3