Ulrich Holtzhausen wrote:
Hey,

Well I have the following:

data_received = s.recv(1024)
print data_received

That only prints this when the bot connects:

''':irc.opera.com NOTICE AUTH :*** Looking up your hostname...'''

Nothing else, so I am not sure whether it would actually save anything other than that if I chose to have it save do a file. I will try that though. Maybe there is some other way of doing this?
I made an IRC bot based on this same file when I was first starting Python, too.
So forgive the code for any readability issues it may have.
Basically what it does is monitors private messages for "themes" or "quit"
if it gets "themes" it replies with a bunch of themes.
The bot was for pyweek a few years ago.
Anyway,  you could just as easily monitor for a ctcp request.
See if the code makes any sense.
I'd give more help, but I'm in extremely busy right now.
Freenode doesn't let you send private messages if you don't identify yourself, but it's simple to create a password.
Just do a /msg NickServ register password
SO make sure you register your bot prior to using this, or you won't be able to send private messages.
-Luke
import os
import socket
import string
import time

class IrcBot:
    def __init__(s,host,nick,port=6667,password="",realname="eyearesee bot"):
        s.password = password
        s.connected = False
        s.host = host
        s.port = port
        s.nick = nick
        s.realname = realname
        s.sock = socket.socket()
        s.channels = []
    def connect(s):
        if s.connected:
            return False
        s.sock.connect((s.host,s.port))
        s.sock.send("NICK %s\r\n" % s.nick)
        s.sock.send("USER %s %s bla :%s\r\n" % (s.nick, s.host, s.realname))
        s.connected = True
    def disconnect(s):
        s.connected = False
        s.sock = socket.socket()
    def joinchannel(s,channel):
        if channel not in s.channels:
            s.channels.append(channel)
            if not s.connected:
                s.connect()
            s.sock.send("JOIN %s\r\n" % channel)
    def identify(s, password):
        if s.connected:
            s.sock.send("PRIVMSG nickserv :identify abcd\r\n")
    def leavechannel(s,channel):
        if channel in s.channels:
            s.channels.remove(channel)
        if s.connected:
            s.sock.send("LEAVE %s\r\n" % channel)
    def sendchannel(s,channel,msg):
        if channel not in s.channels:
            return False
        s.sock.send("PRIVMSG %s %s" % (channel, msg))
    def senduser(s, user, msg):
        if s.connected:
            s.sock.send("PRIVMSG "+user+" :%s\r\n" % msg)
            return True
        return False
    def send(s, msg):
        if s.connected:
            s.sock.send(msg)
            return True
        return False
    def recv(s, size=1024):
        if s.connected:
            return s.sock.recv(size)

testbot = IrcBot("irc.freenode.net","nick")

channame = "#botwar"
testbot.joinchannel(channame)
testbot.identify('password')
themes = ['theme1', 'theme2', 'theme3']
readbuffer = ""
#testbot.leavechannel(channame)
#testbot.disconnect()
time_to_quit = False
while not time_to_quit:
    readbuffer=readbuffer + testbot.recv()
    temp=string.split(readbuffer, "\n")
    readbuffer=temp.pop( )
    
    for line in temp:
        line=string.rstrip(line)
        parse=string.split(line,maxsplit=3)
        print line
        if(parse[0]=="PING"):
            s.send("PONG %s\r\n" % line[1])
            
        if(parse[1] == "PRIVMSG"):
            if parse[2] == testbot.nick:

                #name = parse[0].split("!")[1:]
                name = parse[0].split("!")[0].strip(":")
                print name
                message = string.join(line.strip(":").split(":")[1:], ':')
                print message
                if name != "WebStats":
                    if message == "themes":
                        testbot.senduser(name ,"Themes:")
                        for theme in themes:
                            testbot.senduser(name, theme)
                    elif message == "quit":
                        testbot.senduser(name, "Okay, Quitting.")
                        testbot.leavechannel(channame)
                        testbot.disconnect()
                        time_to_quit = True
                        break
                    else:
                        testbot.senduser(name, "unrecognized command.")
            if parse[2] == channame:
                pass
                #the following code monitors for "themes" in the
                #channel, and outputs similarly as above.
                #
                #message = string.join(line.strip(":").split(":")[1:], ':')
                #if message.find("themes") != -1:
                #    s.sendchannel(channame, "Themes:")
                #    for theme in themes:
                #        s.sendchannel(channame, theme)

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to