#!/usr/bin/perl
# Copyright (C) 2005  Torbjörn 'Azoff' Svensson <azoff@se.linux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# use: cat irssi.log | perl irssi2mirc.pl > mirc.log

use warnings;
use strict;

while (<>) {

    # msg
    if ($_ =~ /^(\S+) <.(.*)$/) {
        #00:01 < foo> bar
        #[00:01] <foo> bar
        print "\[$1\] \<$2";
        
    # act
    } elsif ($_ =~ /^(\S+)  \* (.+)$/) {
        #20:13  * foobar does something
        #[20:13] * foobar does something
        print "\[$1\] * $2";
        
    # nick
    } elsif ($_ =~ /^(\S+) -!- (\S+) is now known as (\S+)$/) {
        #21:48 -!- foo is now known as bar
        #[21:48] *** foo is now known as bar
        print "\[$1\] *** $2 is now known as $3";
        
    # kick
    } elsif ($_ =~ /^(\S+) -!- (\S+) was kicked from \S+ by (\S+) \[(\S*)\]$/) {
        #21:06 -!- bar was kicked from #test by foo [bar]
        #[21:06] *** bar was kicked by foo (bar)
        print "\[$1\] *** $2 was kicked by $3 ($4)";
        
    # topic
    } elsif ($_ =~ /^(\S+) -!- (\S+) changed the topic of \S+ to:(\S+)$/) {
        #16:31 -!- foobar changed the topic of #test to: This is a topic
        #[10:29] *** foobar changes topic to 'This is a topic'
        print "\[$1\] *** $2 changes topic to '$3'";

    # quit
    } elsif ($_ =~ /^(\S+) -!- (\S+) \S+ has quit \[(.*)\]$/) {
        #00:20 -!- foobar [~foo@foobar.tld] has quit [Read error: Connection reset by peer]
        #[16:43] *** foobar has left #test (Quit: This computer has gone to sleep)
        print "\[$1\] *** $2 has left #test ($3)";

    # part
    } elsif ($_ =~ /^(\S+) -!- (\S+) \[(\S+)\] has left (\S+) \[(.*)\]$/) {
        #14:58 -!- foobar [~foo@foobar.tld] has left #test [foobar]
        #[14:58] *** foobar (foo!foo@foobar.tld) has left #test (foobar)
        print "\[$1\] *** $2 ($3) has left $4 ($5)";

    # join
    } elsif ($_ =~ /^(\S+) -!- (\S+) \[(\S+)\] has joined (\S+)$/) {
        #10:20 -!- foobar [~foo@foobar.tld] has joined #test
        #[16:19] *** foobar (foo!foo@foobar.tld) has joined #test
        print "\[$1\] *** $2 ($3) has joined $4";

    # mode
    } elsif ($_ =~ /^(\S+) -!- mode\S+ \[(.+)\] by (\S+)$/) {
        #19:28 -!- mode/#test [+oo foo bar] by foobar
        #[16:28] *** ChanServ sets mode: +o foobar
        print "\[$1\] *** $3 sets mode: $2";
    
    } else {
        print STDERR "ERROR!: $_\n";
        next;
    }

    print "\n";
}

