import sys;
import socket;
import string;
import time;
import commands;

HOST='irc.freenode.net';
PORT=6667;
NICK='eleda';
IDENT='eleda';
REALNAME='Eleda';
OWNER='spectie';
CHANNELINIT='#apertium';
readbuffer='';
PASS='';
DIR='/home/spectre/svnroot/apertium/apertium-'

s=socket.socket();
s.connect((HOST, PORT));
s.send('NICK '+NICK+'\n');
s.send('USER '+IDENT+' '+HOST+' bla :'+REALNAME+'\n');

# contains list of hashes like 'requester, user, direction'
following = {};

#apertium-ca-ro
#apertium-es-ro
#apertium-sv-da

pairs = {};

pairs['en-af'] = 'en-af';
pairs['af-en'] = 'en-af';
pairs['cy-en'] = 'cy-en';
pairs['en-cy'] = 'cy-en';
pairs['en-de'] = 'en-de';
pairs['de-en'] = 'en-de';
pairs['fr-es'] = 'fr-es';
pairs['es-fr'] = 'fr-es';
pairs['oc-ca'] = 'oc-ca';
pairs['ca-oc'] = 'oc-ca';

## need to check for ' and " and etc.

def translate(string, direction): #{
	translation = 'test';
	message = string.split(':');
	cmd = 'echo "' + message[2].strip() + '" | ' + DIR + pairs[direction] + '/modes/' + direction;
	print cmd;
	translation = commands.getstatusoutput(cmd)[1];
	translation = translation.replace(' ', '.');
	return translation;
#}

def parsemsg(msg): #{
    complete = msg[1:].split(':',1);
    info = complete[0].split(' ');
    msgpart = complete[1];
    sender = info[0].split('!');

    if following.has_key(sender[0]): #{
#	s.send('PRIVMSG ' + CHANNELINIT + ' :translating...' + '\n');
	translation = translate(msg, following[sender[0]][2]);
	mesg = 'NOTICE ' + following[sender[0]][0] + ' ' +  translation + '\n';
	print mesg;
	s.send(mesg);
    #}

    if msgpart[0] == '@': #{
        cmd=msgpart[1:].split(' ');
	cmd[0] = cmd[0].strip();
	if cmd[0] == 'follow': #{
	    print '<' + sender[0] + '> ' + str(msgpart) + ' :: ' + str(cmd);
	    follow_item = (sender[0], cmd[1], cmd[2].strip());
	    following[cmd[1]] = (follow_item);
	    mesg = 'PRIVMSG ' + CHANNELINIT + ' :following ' + cmd[1] + ', direction ' + cmd[2].strip() + '\n';
	    print mesg;
	    s.send(mesg);
	#}
	if cmd[0] == 'unfollow': #{
		mesg = 'PRIVMSG ' + CHANNELINIT + ' :unfollowing ' + cmd[1].strip() + '\n';
		if following.has_key(cmd[1].strip()): #{
			del following[cmd[1].strip()];
		#}
		s.send(mesg);
	#}
	if cmd[0] == 'help': #{
	 	mesg = 'PRIVMSG ' + sender[0] + ' :' + 'Translation bot' + '\n';
		s.send(mesg);
	 	mesg = 'PRIVMSG ' + sender[0] + ' :' + '@follow <nick> <direction> - Translate a user.' + '\n';
		s.send(mesg);
	 	mesg = 'PRIVMSG ' + sender[0] + ' :' + '@unfollow <nick>           - Stop translating a user.' + '\n';
		s.send(mesg);
	 	mesg = 'PRIVMSG ' + sender[0] + ' :' + '@list                      - List available pairs.' + '\n';
		s.send(mesg);
	 	mesg = 'PRIVMSG ' + sender[0] + ' :' + '@help                      - Display this help.' + '\n';
		s.send(mesg);
	#}
	if cmd[0] == 'list': #{
		mesg = 'PRIVMSG ' + sender[0] + ' :' + 'Available translation pairs:' + '\n';
		s.send(mesg);
		mesg = 'PRIVMSG ' + sender[0] + ' :';
		for key in pairs.keys():  #{
			mesg = mesg + key + ' ';
		#}
		mesg = mesg + '\n';

		s.send(mesg);
	#}
    #}    
#}

while 1: #{
    line = s.recv(500);
    print line; 

    if line.find('Welcome to the freenode IRC Network')!=-1: #{
        s.send('JOIN ' + CHANNELINIT + '\n'); #Join a channel
    #}

    if line.find('This nickname is owned by someone else') != -1: #{
	s.send('PRIVMSG NickServ :IDENTIFY ' + PASS + '\n');
    #}

    if line.find('PRIVMSG')!=-1: #{

        parsemsg(line);
        line=line.rstrip(); #remove trailing 'rn'
        line=line.split();
        if(line[0]=='PING'): #{
            s.send('PONG '+line[1]+'\n');
	#}
    #}
#}


