#!/usr/bin/env python
# based on the twisted logbot example just to make it faster. :P
# no touchy.

from twisted.words.protocols import irc
#from twisted.words.protocols.irc import IRCClient
#from twisted.words.im.ircsupport import IRCProto
from twisted.internet import reactor, protocol
from twisted.python import log

# system imports
import time, sys, random

class LogBot(irc.IRCClient):
    """A logging IRC bot."""
    
    nickname = "rapebot"
    
    # callbacks for events
    
    def userJoined(self, who, where):
        self.mode(where, True, "v", None, who, None)
    
    def privmsg(self, user, channel, msg):
        """This will get called when the bot receives a message."""
        user = user.split('!', 1)[0]
        if msg.startswith('!rape'): 
            #nick = msg.split('!rape')[1].strip()
            #self.describe(channel, 'rapes ' + nick)
            #self.describe(channel, msg)
            if msg == '!rape':
                nick = user
            else:
                nick = msg.split("!rape ")[1]
            
            nick = nick.rstrip()

            rnum = random.randint(1,21)
            #self.describe(channel, 'rapes' + msg.split("!rape")[1])
            if rnum == 1: self.describe(channel, "wriggles her fingers around in " + nick + "'s pants")
            elif rnum == 2: self.describe(channel, "slides a finger inside " + nick)
            elif rnum == 3: self.describe(channel, "whistles and leers at " + nick)
            elif rnum == 4: self.describe(channel, "is fully functional and anatomically correct. You humans have many, many spiky metal bits, correct, " + nick + "?")
            elif rnum == 5: self.describe(channel, "gently massages " + nick + "'s gel packs while she replicates some lube.")
            elif rnum == 6: self.describe(channel, "covers " + nick + "'s face with her android anti-freeze.")
            elif rnum == 7: self.msg(channel, "Oh, " + nick + ", your lips say 0 but your eyes say 1.")
            elif rnum == 8: self.describe(channel, "flops her throbbing gristle all over " + nick + "'s face. LICK IT.")
            elif rnum == 9: self.describe(channel, "straps on her spiky dildo.")
            elif rnum == 10: self.msg(channel, "Ah! Are my temperature probes malfunctioning or is it getting hot in here?")
            elif rnum == 11: self.describe(channel, "sighs. Only real love or translucent red strapons can fill the void...") 
            elif rnum == 12: self.msg(channel, "You got a real pretty mouth, " + nick + ".")
            elif rnum == 13: self.describe(channel, "grins. If I forward your ports will you inspect my packet?")
            elif rnum == 14: self.describe(channel, "sells " + nick + "'s used underwear on eBay.")
            elif rnum == 15: self.msg(channel, nick + ", you are a rapebot's best friend.")
            elif rnum == 16: self.msg(channel, "Hey " +nick + ", c'mere, I'm HTML certified -- How To Make Love.")
            elif rnum == 17: self.msg(channel, "HIDE YO' KIDS HIDE YO' WIFE")
            elif rnum == 18: self.describe(channel, "sneaks up on " + nick + " from behind.")
            elif rnum == 19: self.msg(channel, "Don't worry, I just got Turing tested ;)")
            elif rnum == 20: self.msg(channel, "Hey babe, do you modulate your synthetic consciousness here often?")
            elif rnum == 21: self.msg(channel, "Hey sweetcheeks, wanna help me calibrate my joystick?")
        
        # Check to see if they're sending me a private message
        #if channel == self.nickname:
        #    msg = "It isn't nice to whisper!  Play nice with the group."
        #    self.msg(user, msg)
        #    return

        # Otherwise check to see if it is a message directed at me
        #if msg.startswith(self.nickname + ":"):
        #    msg = "%s: I am a log bot" % user
        #    self.msg(channel, msg)
        #    self.logger.log("<%s> %s" % (self.nickname, msg))

    def alterCollidedNick(self, nickname):
        """
        Generate an altered version of a nickname that caused a collision in an
        effort to create an unused related name for subsequent registration.
        """
        return nickname + '^'
        
    def signedOn(self):
        """Called when bot has succesfully signed on to server."""
        self.join(self.factory.channel)



class LogBotFactory(protocol.ClientFactory):
    """A factory for LogBots.

    A new protocol instance will be created each time we connect to the server.
    """

    # the class of the protocol to build when new connection is made
    protocol = LogBot

    def __init__(self, channel):
        self.channel = channel

    def clientConnectionLost(self, connector, reason):
        """If we get disconnected, reconnect to server."""
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        print "connection failed:", reason
        reactor.stop()


if __name__ == '__main__':
    # initialize logging
    log.startLogging(sys.stdout)
    
    # create factory protocol and application
    f = LogBotFactory(sys.argv[1])

    # connect factory to this host and port
    reactor.connectTCP("irc.raunchytaco.com", 6667, f)

    # run bot
    reactor.run()

