From e866dceae987acd51d43bd457351bd2188c5f95a Mon Sep 17 00:00:00 2001 From: Till Höppner Date: Tue, 23 Feb 2016 17:00:53 +0100 Subject: Test CI --- src/format/binary.rs | 25 ++-- src/format/energymech.rs | 299 +++++++++++++++++++++++++++-------------------- src/format/irssi.rs | 205 ++++++++++++++++++++++++++++++++ src/format/mod.rs | 38 ++++-- src/format/msgpack.rs | 26 +++-- src/format/weechat.rs | 265 +++++++++++++++++++++++------------------ 6 files changed, 584 insertions(+), 274 deletions(-) create mode 100644 src/format/irssi.rs (limited to 'src/format') diff --git a/src/format/binary.rs b/src/format/binary.rs index a7ae7ca..7cc4281 100644 --- a/src/format/binary.rs +++ b/src/format/binary.rs @@ -12,39 +12,46 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::io::{ BufRead, Write }; +use std::io::{BufRead, Write}; use std::iter::Iterator; use event::Event; use context::Context; -use format::{ Encode, Decode }; +use format::{Decode, Encode}; -use bincode::{ self, SizeLimit }; +use bincode::{self, SizeLimit}; pub struct Binary; pub struct Iter<'a> { - input: &'a mut BufRead + input: &'a mut BufRead, } impl<'a> Iterator for Iter<'a> { type Item = ::Result>; fn next(&mut self) -> Option<::Result>> { - Some(bincode::rustc_serialize::decode_from::<_, Event>(&mut self.input, SizeLimit::Infinite) - .map_err(|_| ::IlcError::BincodeDecode)) + Some(bincode::rustc_serialize::decode_from::<_, Event>(&mut self.input, + SizeLimit::Infinite) + .map_err(|_| ::IlcError::BincodeDecode)) } } impl Encode for Binary { - fn encode<'a>(&'a self, _context: &'a Context, mut output: &'a mut Write, event: &'a Event) -> ::Result<()> { + fn encode<'a>(&'a self, + _context: &'a Context, + mut output: &'a mut Write, + event: &'a Event) + -> ::Result<()> { bincode::rustc_serialize::encode_into(event, &mut output, SizeLimit::Infinite) .map_err(|_| ::IlcError::BincodeEncode) } } impl Decode for Binary { - fn decode<'a>(&'a mut self, _context: &'a Context, input: &'a mut BufRead) - -> Box>> + 'a> { + fn decode<'a>(&'a mut self, + _context: &'a Context, + input: &'a mut BufRead) + -> Box>> + 'a> { Box::new(Iter { input: input }) } } diff --git a/src/format/energymech.rs b/src/format/energymech.rs index ba82458..e8dded6 100644 --- a/src/format/energymech.rs +++ b/src/format/energymech.rs @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::io::{ BufRead, Write }; -use std::borrow::{ ToOwned, Cow }; -use std::iter::{ Iterator }; +use std::io::{BufRead, Write}; +use std::borrow::{Cow, ToOwned}; +use std::iter::Iterator; -use event::{ Event, Type, Time }; +use event::{Event, Time, Type}; use context::Context; -use format::{ Encode, Decode, rejoin, strip_one }; +use format::{Decode, Encode, rejoin, strip_one}; -use l::LogLevel::Info; +use log::LogLevel::Info; use chrono::*; @@ -31,7 +31,7 @@ static TIME_FORMAT: &'static str = "%H:%M:%S"; pub struct Iter<'a> { context: &'a Context, input: &'a mut BufRead, - buffer: Vec + buffer: Vec, } impl<'a> Iterator for Iter<'a> { @@ -42,11 +42,13 @@ impl<'a> Iterator for Iter<'a> { let m = time[4..6].parse::().unwrap(); let s = time[7..9].parse::().unwrap(); if let Some(date) = context.override_date { - Time::Timestamp(context.timezone.from_local_date(&date) - .and_time(NaiveTime::from_hms(h, m, s)) - .single() - .expect("Transformed log times can't be represented, due to timezone transitions") - .timestamp()) + Time::Timestamp(context.timezone + .from_local_date(&date) + .and_time(NaiveTime::from_hms(h, m, s)) + .single() + .expect("Transformed log times can't be represented, due \ + to timezone transitions") + .timestamp()) } else { Time::Hms(h as u8, m as u8, s as u8) } @@ -56,15 +58,21 @@ impl<'a> Iterator for Iter<'a> { self.buffer.clear(); match self.input.read_until(b'\n', &mut self.buffer) { Ok(0) | Err(_) => return None, - Ok(_) => () + Ok(_) => (), } let buffer = String::from_utf8_lossy(&self.buffer); let mut split_tokens: Vec = Vec::new(); - let tokens = buffer.split( |c: char| { - if c.is_whitespace() { split_tokens.push(c); true } else { false } - }).collect::>(); + let tokens = buffer.split(|c: char| { + if c.is_whitespace() { + split_tokens.push(c); + true + } else { + false + } + }) + .collect::>(); if log_enabled!(Info) { info!("Original: `{}`", buffer); @@ -72,142 +80,175 @@ impl<'a> Iterator for Iter<'a> { } match &tokens[..tokens.len() - 1] { - [time, "*", nick, content..] => return Some(Ok(Event { - ty: Type::Action { - from: nick.to_owned().into(), - content: rejoin(content, &split_tokens[3..]) - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - })), - [time, "***", old, "is", "now", "known", "as", new] => return Some(Ok(Event { - ty: Type::Nick { - old_nick: old.to_owned().into(), - new_nick: new.to_owned().into() - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - - })), - [time, "***", nick, "sets", "mode:", mode, masks..] => return Some(Ok(Event { - ty: Type::Mode { - nick: Some(nick.to_owned().into()), - mode: mode.to_owned().into(), - masks: rejoin(&masks, &split_tokens[6..]).to_owned().into() - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - - })), - [time, "***", "Joins:", nick, host] => return Some(Ok(Event { - ty: Type::Join { - nick: nick.to_owned().into(), - mask: Some(strip_one(host).into()) - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - - })), - [time, "***", "Parts:", nick, host, reason..] => return Some(Ok(Event { - ty: Type::Part { - nick: nick.to_owned().into(), - mask: Some(strip_one(host).into()), - reason: Some(strip_one(&rejoin(reason, &split_tokens[5..])).into()) - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - - })), - [time, "***", "Quits:", nick, host, reason..] => return Some(Ok(Event { - ty: Type::Quit { - nick: nick.to_owned().into(), - mask: Some(strip_one(host).into()), - reason: Some(strip_one(&rejoin(reason, &split_tokens[5..])).into()) - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - - })), - [time, "***", nick, "changes", "topic", "to", topic..] => return Some(Ok(Event { - ty: Type::TopicChange { - nick: Some(nick.to_owned().into()), - new_topic: strip_one(&rejoin(topic, &split_tokens[6..])).into() - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - - })), - [time, nick, content..] - if nick.starts_with('<') && nick.ends_with('>') - => return Some(Ok(Event { - ty: Type::Msg { - from: strip_one(nick).into(), - content: rejoin(content, &split_tokens[2..]) - }, - time: parse_time(&self.context, time), - channel: self.context.channel.clone().map(Into::into) - })), - _ => () + [time, "*", nick, content..] => { + return Some(Ok(Event { + ty: Type::Action { + from: nick.to_owned().into(), + content: rejoin(content, &split_tokens[3..]), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [time, "***", old, "is", "now", "known", "as", new] => { + return Some(Ok(Event { + ty: Type::Nick { + old_nick: old.to_owned().into(), + new_nick: new.to_owned().into(), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [time, "***", nick, "sets", "mode:", mode, masks..] => { + return Some(Ok(Event { + ty: Type::Mode { + nick: Some(nick.to_owned().into()), + mode: mode.to_owned().into(), + masks: rejoin(&masks, &split_tokens[6..]).to_owned().into(), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [time, "***", "Joins:", nick, host] => { + return Some(Ok(Event { + ty: Type::Join { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [time, "***", "Parts:", nick, host, reason..] => { + return Some(Ok(Event { + ty: Type::Part { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[5..])).into()), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [time, "***", "Quits:", nick, host, reason..] => { + return Some(Ok(Event { + ty: Type::Quit { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[5..])).into()), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [time, "***", nick, "changes", "topic", "to", topic..] => { + return Some(Ok(Event { + ty: Type::TopicChange { + nick: Some(nick.to_owned().into()), + new_topic: strip_one(&rejoin(topic, &split_tokens[6..])).into(), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [time, nick, content..] if nick.starts_with('<') && nick.ends_with('>') => { + return Some(Ok(Event { + ty: Type::Msg { + from: strip_one(nick).into(), + content: rejoin(content, &split_tokens[2..]), + }, + time: parse_time(&self.context, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + _ => (), } } } } impl Decode for Energymech { - fn decode<'a>(&'a mut self, context: &'a Context, input: &'a mut BufRead) -> Box>> + 'a> { + fn decode<'a>(&'a mut self, + context: &'a Context, + input: &'a mut BufRead) + -> Box>> + 'a> { Box::new(Iter { context: context, input: input, - buffer: Vec::new() + buffer: Vec::new(), }) } } impl Encode for Energymech { - fn encode<'a>(&'a self, context: &'a Context, mut output: &'a mut Write, event: &'a Event) -> ::Result<()> { + fn encode<'a>(&'a self, + context: &'a Context, + mut output: &'a mut Write, + event: &'a Event) + -> ::Result<()> { match event { &Event { ty: Type::Msg { ref from, ref content }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] <{}> {}", - time.with_format(&context.timezone, TIME_FORMAT), from, content)) - }, + try!(writeln!(&mut output, + "[{}] <{}> {}", + time.with_format(&context.timezone, TIME_FORMAT), + from, + content)) + } &Event { ty: Type::Action { ref from, ref content }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] * {} {}", - time.with_format(&context.timezone, TIME_FORMAT), from, content)) - }, + try!(writeln!(&mut output, + "[{}] * {} {}", + time.with_format(&context.timezone, TIME_FORMAT), + from, + content)) + } &Event { ty: Type::Nick { ref old_nick, ref new_nick }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] *** {} is now known as {}", - time.with_format(&context.timezone, TIME_FORMAT), old_nick, new_nick)) - }, + try!(writeln!(&mut output, + "[{}] *** {} is now known as {}", + time.with_format(&context.timezone, TIME_FORMAT), + old_nick, + new_nick)) + } &Event { ty: Type::Mode { ref nick, ref mode, ref masks }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] *** {} sets mode: {} {}", - time.with_format(&context.timezone, TIME_FORMAT), - nick.as_ref().expect("Nickname not present, but required."), - mode, masks)) - }, + try!(writeln!(&mut output, + "[{}] *** {} sets mode: {} {}", + time.with_format(&context.timezone, TIME_FORMAT), + nick.as_ref().expect("Nickname not present, but required."), + mode, + masks)) + } &Event { ty: Type::Join { ref nick, ref mask }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] *** Joins: {} ({})", - time.with_format(&context.timezone, TIME_FORMAT), nick, - mask.as_ref().expect("Mask not present, but required."))) - }, + try!(writeln!(&mut output, + "[{}] *** Joins: {} ({})", + time.with_format(&context.timezone, TIME_FORMAT), + nick, + mask.as_ref().expect("Mask not present, but required."))) + } &Event { ty: Type::Part { ref nick, ref mask, ref reason }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] *** Parts: {} ({}) ({})", - time.with_format(&context.timezone, TIME_FORMAT), nick, - mask.as_ref().expect("Mask not present, but required."), - reason.as_ref().unwrap_or(&Cow::Borrowed("")))) - }, + try!(writeln!(&mut output, + "[{}] *** Parts: {} ({}) ({})", + time.with_format(&context.timezone, TIME_FORMAT), + nick, + mask.as_ref().expect("Mask not present, but required."), + reason.as_ref().unwrap_or(&Cow::Borrowed("")))) + } &Event { ty: Type::Quit { ref nick, ref mask, ref reason }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] *** Quits: {} ({}) ({})", - time.with_format(&context.timezone, TIME_FORMAT), nick, - mask.as_ref().expect("Mask not present, but required."), - reason.as_ref().expect("Reason not present, but required."))) - }, + try!(writeln!(&mut output, + "[{}] *** Quits: {} ({}) ({})", + time.with_format(&context.timezone, TIME_FORMAT), + nick, + mask.as_ref().expect("Mask not present, but required."), + reason.as_ref().expect("Reason not present, but required."))) + } &Event { ty: Type::TopicChange { ref nick, ref new_topic }, ref time, .. } => { - try!(writeln!(&mut output, "[{}] *** {} changes topic to '{}'", - time.with_format(&context.timezone, TIME_FORMAT), - nick.as_ref().expect("Nick not present, but required."), - new_topic)) - }, - _ => () + try!(writeln!(&mut output, + "[{}] *** {} changes topic to '{}'", + time.with_format(&context.timezone, TIME_FORMAT), + nick.as_ref().expect("Nick not present, but required."), + new_topic)) + } + _ => (), } Ok(()) } diff --git a/src/format/irssi.rs b/src/format/irssi.rs new file mode 100644 index 0000000..6afcd61 --- /dev/null +++ b/src/format/irssi.rs @@ -0,0 +1,205 @@ +// Copyright 2015 Till Höppner +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::io::{ BufRead, Write }; +use std::borrow::{ ToOwned }; +use std::iter::{ Iterator }; + +use event::{ Event, Type, Time }; +use context::Context; +use format::{ Encode, Decode, rejoin, strip_one }; + +use l::LogLevel::Info; + +pub struct Irssi; + +static LOG_OPEN_FORMAT: &'static str = "%a %b %e %T %Y"; +static LINE_FORMAT: &'static str = "%H:%M"; + +pub struct Iter<'a> { + context: &'a Context, + input: &'a mut BufRead, + buffer: Vec +} + +impl<'a> Iterator for Iter<'a> { + type Item = ::Result>; + fn next(&mut self) -> Option<::Result>> { + fn parse_time(c: &Context, date: &str, time: &str) -> Time { + Time::from_format(&c.timezone, &format!("{} {}", date, time), TIME_DATE_FORMAT) + } + + loop { + self.buffer.clear(); + match self.input.read_until(b'\n', &mut self.buffer) { + Ok(0) | Err(_) => return None, + Ok(_) => () + } + + let buffer = String::from_utf8_lossy(&self.buffer); + + let mut split_tokens: Vec = Vec::new(); + let tokens = buffer.split(|c: char| { + if c.is_whitespace() { split_tokens.push(c); true } else { false } + }).collect::>(); + + if log_enabled!(Info) { + info!("Original: `{}`", buffer); + info!("Parsing: {:?}", tokens); + } + + match &tokens[..tokens.len() - 1] { + ["---", "Log", "opened", day_of_week, month, day, time, year] => { + year + }, + ["---", "Log", "closed", day_of_week, month, day, time, year] + => return Some(Ok(Event { + ty: Type::Disconnect, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into) + })), + [time, "-!-", nick, host, "has", "joined", channel] + => return Some(Ok(Event { + ty: Type::Join { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + }, + channel: Some(channel.to_owned().into()), + time: parse_time(&self.context, date, time) + })), + [time, "-!-", nick, host, "has", "left", channel, reason..] + => return Some(Ok(Event { + ty: Type::Part { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[8..])).into()), + }, + channel: Some(channel.to_owned().into()), + time: parse_time(&self.context, date, time) + })), + [time, "-!-", nick, host, "has", "quit", reason..] + => return Some(Ok(Event { + ty: Type::Quit { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[7..])).into()), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into) + })), + // TODO: reorder + [date, time, "--", notice, content..] + if notice.starts_with("Notice(") + => return Some(Ok(Event { + ty: Type::Notice { + from: notice["Notice(".len()..notice.len() - 2].to_owned().into(), + content: rejoin(content, &split_tokens[4..]), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into) + })), + [date, time, "--", nick, verb, "now", "known", "as", new_nick] + if verb == "is" || verb == "are" + => return Some(Ok(Event { + ty: Type::Nick { + old_nick: nick.to_owned().into(), + new_nick: new_nick.to_owned().into() + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into) + })), + [date, time, sp, "*", nick, msg..] + if sp.clone().is_empty() + => return Some(Ok(Event { + ty: Type::Action { + from: nick.to_owned().into(), + content: rejoin(msg, &split_tokens[5..]), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into) + })), + [date, time, nick, msg..] + => return Some(Ok(Event { + ty: Type::Msg { + from: nick.to_owned().into(), + content: rejoin(msg, &split_tokens[3..]), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into) + })), + _ => () + } + } + } +} + +impl Decode for Irssi { + fn decode<'a>(&'a mut self, context: &'a Context, input: &'a mut BufRead) -> Box>> + 'a> { + Box::new(Iter { + context: context, + input: input, + buffer: Vec::new() + }) + } +} + +impl Encode for Irssi { + fn encode<'a>(&'a self, context: &'a Context, mut output: &'a mut Write, event: &'a Event) -> ::Result<()> { + match event { + &Event { ty: Type::Msg { ref from, ref content, .. }, ref time, .. } => { + try!(writeln!(&mut output, "{}\t{}\t{}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) + }, + &Event { ty: Type::Action { ref from, ref content, .. }, ref time, .. } => { + try!(writeln!(&mut output, "{}\t *\t{} {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) + }, + &Event { ty: Type::Join { ref nick, ref mask, .. }, ref channel, ref time } => { + try!(writeln!(&mut output, "{}\t-->\t{} ({}) has joined {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, + mask.as_ref().expect("Hostmask not present, but required."), + channel.as_ref().expect("Channel not present, but required."))) + }, + &Event { ty: Type::Part { ref nick, ref mask, ref reason }, ref channel, ref time } => { + try!(write!(&mut output, "{}\t<--\t{} ({}) has left {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, + mask.as_ref().expect("Hostmask not present, but required."), + channel.as_ref().expect("Channel not present, but required."))); + if reason.is_some() && reason.as_ref().unwrap().len() > 0 { + try!(write!(&mut output, " ({})", reason.as_ref().unwrap())); + } + try!(write!(&mut output, "\n")) + }, + &Event { ty: Type::Quit { ref nick, ref mask, ref reason }, ref time, .. } => { + try!(write!(&mut output, "{}\t<--\t{} ({}) has quit", + time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, + mask.as_ref().expect("Hostmask not present, but required."))); + if reason.is_some() && reason.as_ref().unwrap().len() > 0 { + try!(write!(&mut output, " ({})", reason.as_ref().unwrap())); + } + try!(write!(&mut output, "\n")) + }, + &Event { ty: Type::Disconnect, ref time, .. } => { + try!(writeln!(&mut output, "{}\t--\tirc: disconnected from server", + time.with_format(&context.timezone, TIME_DATE_FORMAT))) + }, + &Event { ty: Type::Notice { ref from, ref content }, ref time, .. } => { + try!(writeln!(&mut output, "{}\t--\tNotice({}): {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) + }, + _ => () + } + Ok(()) + } +} diff --git a/src/format/mod.rs b/src/format/mod.rs index cea6855..8873db8 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -17,7 +17,7 @@ //! target format, all formats must allow for omittable information. use std::iter; -use std::io::{ BufRead, Write }; +use std::io::{BufRead, Write}; use std::borrow::Cow; use event::Event; @@ -35,17 +35,27 @@ mod binary; mod msgpack; pub trait Encode { - fn encode<'a>(&'a self, context: &'a Context, output: &'a mut Write, event: &'a Event) -> ::Result<()>; + fn encode<'a>(&'a self, + context: &'a Context, + output: &'a mut Write, + event: &'a Event) + -> ::Result<()>; } pub trait Decode { - fn decode<'a>(&'a mut self, context: &'a Context, input: &'a mut BufRead) -> Box>> + 'a>; + fn decode<'a>(&'a mut self, + context: &'a Context, + input: &'a mut BufRead) + -> Box>> + 'a>; } pub struct Dummy; impl Decode for Dummy { - fn decode<'a>(&'a mut self, _context: &'a Context, _input: &'a mut BufRead) -> Box>> + 'a> { + fn decode<'a>(&'a mut self, + _context: &'a Context, + _input: &'a mut BufRead) + -> Box>> + 'a> { Box::new(iter::empty()) } } @@ -54,10 +64,10 @@ pub fn decoder(format: &str) -> Option> { match format { "energymech" | "em" => Some(Box::new(Energymech)), "weechat" | "w" => Some(Box::new(Weechat)), -// "irssi" => Some(Box::new(irssi::Irssi)), + // "irssi" => Some(Box::new(irssi::Irssi)), "binary" => Some(Box::new(Binary)), "msgpack" => Some(Box::new(Msgpack)), - _ => None + _ => None, } } @@ -65,18 +75,24 @@ pub fn encoder(format: &str) -> Option> { match format { "energymech" | "em" => Some(Box::new(Energymech)), "weechat" | "w" => Some(Box::new(Weechat)), -// "irssi" => Some(Box::new(irssi::Irssi)), + // "irssi" => Some(Box::new(irssi::Irssi)), "binary" => Some(Box::new(Binary)), "msgpack" => Some(Box::new(Msgpack)), - _ => None + _ => None, } } fn rejoin(s: &[&str], splits: &[char]) -> Cow<'static, str> { let len = s.iter().map(|s| s.len()).fold(0, |a, b| a + b); - let mut out = s.iter().zip(splits.iter()).fold(String::with_capacity(len), - |mut s, (b, &split)| { s.push_str(b); s.push(split); s }); - out.pop(); Cow::Owned(out) + let mut out = s.iter() + .zip(splits.iter()) + .fold(String::with_capacity(len), |mut s, (b, &split)| { + s.push_str(b); + s.push(split); + s + }); + out.pop(); + Cow::Owned(out) } fn strip_one(s: &str) -> String { diff --git a/src/format/msgpack.rs b/src/format/msgpack.rs index 022e373..36af1aa 100644 --- a/src/format/msgpack.rs +++ b/src/format/msgpack.rs @@ -12,21 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::io::{ BufRead, Write }; +use std::io::{BufRead, Write}; use std::iter::Iterator; use event::Event; use context::Context; -use format::{ Encode, Decode }; +use format::{Decode, Encode}; -use rustc_serialize::{ Encodable, Decodable }; -use msgpack::{ Encoder, Decoder }; +use rustc_serialize::{Decodable, Encodable}; +use msgpack::{Decoder, Encoder}; use rmp::decode::ReadError; pub struct Msgpack; pub struct Iter<'a> { - input: &'a mut BufRead + input: &'a mut BufRead, } impl<'a> Iterator for Iter<'a> { @@ -36,21 +36,27 @@ impl<'a> Iterator for Iter<'a> { match Event::decode(&mut Decoder::new(&mut self.input)) { Ok(e) => Some(Ok(e)), Err(decode::Error::InvalidMarkerRead(ReadError::UnexpectedEOF)) => None, - Err(e) => Some(Err(::IlcError::MsgpackDecode(e))) + Err(e) => Some(Err(::IlcError::MsgpackDecode(e))), } } } impl Encode for Msgpack { - fn encode<'a>(&'a self, _context: &'a Context, output: &'a mut Write, event: &'a Event) -> ::Result<()> { + fn encode<'a>(&'a self, + _context: &'a Context, + output: &'a mut Write, + event: &'a Event) + -> ::Result<()> { event.encode(&mut Encoder::new(output)) - .map_err(|e| ::IlcError::MsgpackEncode(e)) + .map_err(|e| ::IlcError::MsgpackEncode(e)) } } impl Decode for Msgpack { - fn decode<'a>(&'a mut self, _context: &'a Context, input: &'a mut BufRead) - -> Box>> + 'a> { + fn decode<'a>(&'a mut self, + _context: &'a Context, + input: &'a mut BufRead) + -> Box>> + 'a> { Box::new(Iter { input: input }) } } diff --git a/src/format/weechat.rs b/src/format/weechat.rs index 30fdc24..ccb0726 100644 --- a/src/format/weechat.rs +++ b/src/format/weechat.rs @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::io::{ BufRead, Write }; -use std::borrow::{ ToOwned }; -use std::iter::{ Iterator }; +use std::io::{BufRead, Write}; +use std::borrow::ToOwned; +use std::iter::Iterator; -use event::{ Event, Type, Time }; +use event::{Event, Time, Type}; use context::Context; -use format::{ Encode, Decode, rejoin, strip_one }; +use format::{Decode, Encode, rejoin, strip_one}; -use l::LogLevel::Info; +use log::LogLevel::Info; pub struct Weechat; @@ -29,7 +29,7 @@ static TIME_DATE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S"; pub struct Iter<'a> { context: &'a Context, input: &'a mut BufRead, - buffer: Vec + buffer: Vec, } impl<'a> Iterator for Iter<'a> { @@ -43,15 +43,21 @@ impl<'a> Iterator for Iter<'a> { self.buffer.clear(); match self.input.read_until(b'\n', &mut self.buffer) { Ok(0) | Err(_) => return None, - Ok(_) => () + Ok(_) => (), } let buffer = String::from_utf8_lossy(&self.buffer); let mut split_tokens: Vec = Vec::new(); let tokens = buffer.split(|c: char| { - if c.is_whitespace() { split_tokens.push(c); true } else { false } - }).collect::>(); + if c.is_whitespace() { + split_tokens.push(c); + true + } else { + false + } + }) + .collect::>(); if log_enabled!(Info) { info!("Original: `{}`", buffer); @@ -59,141 +65,170 @@ impl<'a> Iterator for Iter<'a> { } match &tokens[..tokens.len() - 1] { - [date, time, "-->", nick, host, "has", "joined", channel, _..] - => return Some(Ok(Event { - ty: Type::Join { - nick: nick.to_owned().into(), - mask: Some(strip_one(host).into()), - }, - channel: Some(channel.to_owned().into()), - time: parse_time(&self.context, date, time) - })), - [date, time, "<--", nick, host, "has", "left", channel, reason..] - => return Some(Ok(Event { - ty: Type::Part { - nick: nick.to_owned().into(), - mask: Some(strip_one(host).into()), - reason: Some(strip_one(&rejoin(reason, &split_tokens[8..])).into()), - }, - channel: Some(channel.to_owned().into()), - time: parse_time(&self.context, date, time) - })), - [date, time, "<--", nick, host, "has", "quit", reason..] - => return Some(Ok(Event { - ty: Type::Quit { - nick: nick.to_owned().into(), - mask: Some(strip_one(host).into()), - reason: Some(strip_one(&rejoin(reason, &split_tokens[7..])).into()), - }, - time: parse_time(&self.context, date, time), - channel: self.context.channel.clone().map(Into::into) - })), - [date, time, "--", notice, content..] - if notice.starts_with("Notice(") - => return Some(Ok(Event { - ty: Type::Notice { - from: notice["Notice(".len()..notice.len() - 2].to_owned().into(), - content: rejoin(content, &split_tokens[4..]), - }, - time: parse_time(&self.context, date, time), - channel: self.context.channel.clone().map(Into::into) - })), - [date, time, "--", "irc:", "disconnected", "from", "server", _..] - => return Some(Ok(Event { - ty: Type::Disconnect, - time: parse_time(&self.context, date, time), - channel: self.context.channel.clone().map(Into::into) - })), - [date, time, "--", nick, verb, "now", "known", "as", new_nick] - if verb == "is" || verb == "are" - => return Some(Ok(Event { - ty: Type::Nick { - old_nick: nick.to_owned().into(), - new_nick: new_nick.to_owned().into() - }, - time: parse_time(&self.context, date, time), - channel: self.context.channel.clone().map(Into::into) - })), - [date, time, sp, "*", nick, msg..] - if sp.clone().is_empty() - => return Some(Ok(Event { - ty: Type::Action { - from: nick.to_owned().into(), - content: rejoin(msg, &split_tokens[5..]), - }, - time: parse_time(&self.context, date, time), - channel: self.context.channel.clone().map(Into::into) - })), - [date, time, nick, msg..] - => return Some(Ok(Event { - ty: Type::Msg { - from: nick.to_owned().into(), - content: rejoin(msg, &split_tokens[3..]), - }, - time: parse_time(&self.context, date, time), - channel: self.context.channel.clone().map(Into::into) - })), - _ => () + [date, time, "-->", nick, host, "has", "joined", channel, _..] => { + return Some(Ok(Event { + ty: Type::Join { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + }, + channel: Some(channel.to_owned().into()), + time: parse_time(&self.context, date, time), + })) + } + [date, time, "<--", nick, host, "has", "left", channel, reason..] => { + return Some(Ok(Event { + ty: Type::Part { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[8..])).into()), + }, + channel: Some(channel.to_owned().into()), + time: parse_time(&self.context, date, time), + })) + } + [date, time, "<--", nick, host, "has", "quit", reason..] => { + return Some(Ok(Event { + ty: Type::Quit { + nick: nick.to_owned().into(), + mask: Some(strip_one(host).into()), + reason: Some(strip_one(&rejoin(reason, &split_tokens[7..])).into()), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [date, time, "--", notice, content..] if notice.starts_with("Notice(") => { + return Some(Ok(Event { + ty: Type::Notice { + from: notice["Notice(".len()..notice.len() - 2].to_owned().into(), + content: rejoin(content, &split_tokens[4..]), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [date, time, "--", "irc:", "disconnected", "from", "server", _..] => { + return Some(Ok(Event { + ty: Type::Disconnect, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [date, time, "--", nick, verb, "now", "known", "as", new_nick] if verb == "is" || + verb == "are" => { + return Some(Ok(Event { + ty: Type::Nick { + old_nick: nick.to_owned().into(), + new_nick: new_nick.to_owned().into(), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [date, time, sp, "*", nick, msg..] if sp.clone().is_empty() => { + return Some(Ok(Event { + ty: Type::Action { + from: nick.to_owned().into(), + content: rejoin(msg, &split_tokens[5..]), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + [date, time, nick, msg..] => { + return Some(Ok(Event { + ty: Type::Msg { + from: nick.to_owned().into(), + content: rejoin(msg, &split_tokens[3..]), + }, + time: parse_time(&self.context, date, time), + channel: self.context.channel.clone().map(Into::into), + })) + } + _ => (), } } } } impl Decode for Weechat { - fn decode<'a>(&'a mut self, context: &'a Context, input: &'a mut BufRead) -> Box>> + 'a> { + fn decode<'a>(&'a mut self, + context: &'a Context, + input: &'a mut BufRead) + -> Box>> + 'a> { Box::new(Iter { context: context, input: input, - buffer: Vec::new() + buffer: Vec::new(), }) } } impl Encode for Weechat { - fn encode<'a>(&'a self, context: &'a Context, mut output: &'a mut Write, event: &'a Event) -> ::Result<()> { + fn encode<'a>(&'a self, + context: &'a Context, + mut output: &'a mut Write, + event: &'a Event) + -> ::Result<()> { match event { &Event { ty: Type::Msg { ref from, ref content, .. }, ref time, .. } => { - try!(writeln!(&mut output, "{}\t{}\t{}", - time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) - }, + try!(writeln!(&mut output, + "{}\t{}\t{}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), + from, + content)) + } &Event { ty: Type::Action { ref from, ref content, .. }, ref time, .. } => { - try!(writeln!(&mut output, "{}\t *\t{} {}", - time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) - }, + try!(writeln!(&mut output, + "{}\t *\t{} {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), + from, + content)) + } &Event { ty: Type::Join { ref nick, ref mask, .. }, ref channel, ref time } => { - try!(writeln!(&mut output, "{}\t-->\t{} ({}) has joined {}", - time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, - mask.as_ref().expect("Hostmask not present, but required."), - channel.as_ref().expect("Channel not present, but required."))) - }, + try!(writeln!(&mut output, + "{}\t-->\t{} ({}) has joined {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), + nick, + mask.as_ref().expect("Hostmask not present, but required."), + channel.as_ref().expect("Channel not present, but required."))) + } &Event { ty: Type::Part { ref nick, ref mask, ref reason }, ref channel, ref time } => { - try!(write!(&mut output, "{}\t<--\t{} ({}) has left {}", - time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, - mask.as_ref().expect("Hostmask not present, but required."), - channel.as_ref().expect("Channel not present, but required."))); + try!(write!(&mut output, + "{}\t<--\t{} ({}) has left {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), + nick, + mask.as_ref().expect("Hostmask not present, but required."), + channel.as_ref().expect("Channel not present, but required."))); if reason.is_some() && reason.as_ref().unwrap().len() > 0 { try!(write!(&mut output, " ({})", reason.as_ref().unwrap())); } try!(write!(&mut output, "\n")) - }, + } &Event { ty: Type::Quit { ref nick, ref mask, ref reason }, ref time, .. } => { - try!(write!(&mut output, "{}\t<--\t{} ({}) has quit", - time.with_format(&context.timezone, TIME_DATE_FORMAT), nick, - mask.as_ref().expect("Hostmask not present, but required."))); + try!(write!(&mut output, + "{}\t<--\t{} ({}) has quit", + time.with_format(&context.timezone, TIME_DATE_FORMAT), + nick, + mask.as_ref().expect("Hostmask not present, but required."))); if reason.is_some() && reason.as_ref().unwrap().len() > 0 { try!(write!(&mut output, " ({})", reason.as_ref().unwrap())); } try!(write!(&mut output, "\n")) - }, + } &Event { ty: Type::Disconnect, ref time, .. } => { - try!(writeln!(&mut output, "{}\t--\tirc: disconnected from server", - time.with_format(&context.timezone, TIME_DATE_FORMAT))) - }, + try!(writeln!(&mut output, + "{}\t--\tirc: disconnected from server", + time.with_format(&context.timezone, TIME_DATE_FORMAT))) + } &Event { ty: Type::Notice { ref from, ref content }, ref time, .. } => { - try!(writeln!(&mut output, "{}\t--\tNotice({}): {}", - time.with_format(&context.timezone, TIME_DATE_FORMAT), from, content)) - }, - _ => () + try!(writeln!(&mut output, + "{}\t--\tNotice({}): {}", + time.with_format(&context.timezone, TIME_DATE_FORMAT), + from, + content)) + } + _ => (), } Ok(()) } -- cgit v1.2.3