aboutsummaryrefslogtreecommitdiff
path: root/src/format
diff options
context:
space:
mode:
authorTill Hoeppner2015-03-23 16:41:44 +0100
committerTill Hoeppner2015-03-23 16:41:44 +0100
commitd972eed6b596d415a0aa6117a05bd107dbb8a0ae (patch)
tree824b4d8bdb40f3b89c7ceb21e20114f76d6ed895 /src/format
downloadilc-d972eed6b596d415a0aa6117a05bd107dbb8a0ae.tar.gz
ilc-d972eed6b596d415a0aa6117a05bd107dbb8a0ae.tar.xz
ilc-d972eed6b596d415a0aa6117a05bd107dbb8a0ae.zip
Initial commit.
Diffstat (limited to 'src/format')
-rw-r--r--src/format/mod.rs17
-rw-r--r--src/format/weechat3.rs96
2 files changed, 113 insertions, 0 deletions
diff --git a/src/format/mod.rs b/src/format/mod.rs
new file mode 100644
index 0000000..9f6d30d
--- /dev/null
+++ b/src/format/mod.rs
@@ -0,0 +1,17 @@
+//! Traits and structs for conversion between various formats.
+//! As the source format may not provide the same information as the
+//! target format, all formats must allow for omittable information.
+
+use std::io::{ self, BufRead, Write };
+
+use log::Event;
+
+pub mod weechat3;
+
+pub trait Encode<W> where W: Write {
+ fn encode(&self, output: W, event: &Event) -> io::Result<()>;
+}
+
+pub trait Decode<R, O> where R: BufRead, O: Iterator<Item = ::Result<Event>> {
+ fn decode(&mut self, input: R) -> O;
+}
diff --git a/src/format/weechat3.rs b/src/format/weechat3.rs
new file mode 100644
index 0000000..f546983
--- /dev/null
+++ b/src/format/weechat3.rs
@@ -0,0 +1,96 @@
+use std::io::BufRead;
+use std::borrow::ToOwned;
+
+use log::Event;
+use format::Decode;
+
+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 OTHER_LINES: Regex = regex!(r"(.*)");
+
+static TIME_DATE_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
+
+pub struct Iter<R> where R: BufRead {
+ input: R,
+ buffer: String
+}
+
+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()
+ }
+
+ println!("Reading line...");
+ self.buffer.clear();
+ match self.input.read_line(&mut self.buffer) {
+ Ok(0) | Err(_) => return None,
+ Ok(_) => ()
+ }
+ let line = &self.buffer;
+ println!("Read line: {}", line);
+ 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(),
+ time: time(cap.at(0).unwrap())
+ }))
+ } else if let Some(cap) = ACTION_LINE.captures(line) {
+ return Some(Ok(Event::Action {
+ from: cap.at(1).unwrap().to_owned(),
+ content: cap.at(2).unwrap().to_owned(),
+ time: time(cap.at(0).unwrap())
+ }))
+ } else if let Some(cap) = OTHER_LINES.captures(line) {
+ if cap.at(4) == Some("has") && cap.at(5) == Some("kicked") {
+ return Some(Ok(Event::Kick {
+ kicked_nick: cap.at(6).unwrap().to_owned(),
+ kicking_nick: cap.at(3).unwrap().to_owned(),
+ kick_message: cap.at(4).unwrap().to_owned(),
+ time: time(cap.at(0).unwrap())
+ }))
+ } else if cap.at(3) == Some("has") && cap.at(5) == Some("changed") && cap.at(6) == Some("topic") {
+ return Some(Ok(Event::Topic {
+ new_topic: cap.at(5).unwrap().to_owned(),
+ time: time(cap.at(0).unwrap())
+ }))
+ } else if cap.at(3) == Some("Mode") {
+ return Some(Ok(Event::Mode {
+ time: time(cap.at(0).unwrap())
+ }))
+ } else if cap.at(5) == Some("has") && cap.at(6) == Some("joined") {
+ return Some(Ok(Event::Join {
+ nick: cap.at(3).unwrap().to_owned(),
+ mask: String::new(),
+ time: time(cap.at(0).unwrap())
+ }))
+ } else if cap.at(5) == Some("now") && cap.at(6) == Some("known") {
+
+ }
+ }
+ Some(Err(::IlcError::Parse(format!("Line `{}` didn't match any rules.", line))))
+ }
+}
+
+impl<R> Decode<R, Iter<R>> for Weechat3 where R: BufRead {
+ fn decode(&mut self, input: R) -> Iter<R> {/*
+ for line in input.lines() {
+ let line = &*try!(line);
+ } else {
+ handler.err(&format!("Malformatted line: {}", line));
+ }
+ }*/
+ Iter {
+ input: input,
+ buffer: String::new()
+ }
+ }
+}