Omnimaga

General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: Spyro543 on November 19, 2011, 11:14:34 am

Title: ircbot: Library for Python for making bots
Post by: Spyro543 on November 19, 2011, 11:14:34 am
I just made a library for Python 2.7 that allows you to easily create IRC bots.
Here is the source code:
Code: [Select]
import socket
import string
import time
s = socket.socket(AF_INET, SOCK_STREAM)

def connect(server, port=6667):
    s.connect((server, 6667))

def ready():
    x = s.recv(2048)
    if "376" in x:
        return True
    else:
        return False

def register(nick):
    s.send("NICK %s\r\n" % nick)
    s.send("USER  %s 8 *  : %s\r\n" % (nick, nick))

def join(channel):
    s.send("JOIN %s\r\n" % channel)

def send_msg(reciever, message):
    s.send("PRIVMSG %s :%s" % (reciever, message))

def send_raw(data):
    s.send(data)

def recieve():
    x = s.recv(2048)
    if x != "":
        return x

def username():
    x = s.recv(2048)
    return x.split("!")[0].strip(":")

def hostmask():
    return x.split("!")[1].split(" ")[0]

def message(message):
    x = s.recv(2048)
    msg = x.rpartition("PRIVMSG %s :" % (channel))
    return msg

def format_recv():
    return "<%s> %s" % (username(), message())

def timestamp():
    return "%s:%s:%s" % (time.localtime()[3], time.localtime()[4], time.localtime()[5])

def op(channel, reciever):
    s.send("MODE %s +o %s\r\n" % (channel, reciever))

def deop(channel, reciever):
    s.send("MODE %s -o %s\r\n" % (channel, reciever))

def voice(channel, reciever):
    s.send("MODE %s +v %s\r\n" % (channel, reciever))

def devoice(channel, reciever):
    s.send("MODE %s -v %s\r\n" % (channel, reciever))

def ban(channel, reciever):
    s.send("MODE %s +b %s!*@*\r\n" % (channel, reciever))

def kick(channel, nick, reason=None):
    s.send("KICK %s %s %s\r\n" % (channel, nick, reason))

def data_present(check):
    x = s.recv(2048)
    if check in x:
        return True
    else:
        return False

def nick(nick):
    s.send("NICK %s")

def disconnect(reason="Disconnected."):
    s.send("QUIT :%s" % reason)
    s.close()

if "PING" in s.recv(2048):
    ping = s.recv.rstrip()
    ping = split(ping)
    s.send("PONG %s" % ping[1])
Here's all the commands you can currently use:

connect(server) - Connects to the IRC server.
ready() - Returns true if the end of the MOTD is reached.
register(nick) - Registers with the nick.
join(channel) - Joins the channel.
send_msg(reciever, message) - Sends a message to reciever.
send_raw(data) - Sends raw IRC data to the server.
recieve() - Returns the IRC data recieved if it is not empty.
username() - Returns the username of the last message sent.
hostmask() - Returns the hostmask of the last message sent.
message() - Returns the last message sent.
format_recv() - Returns the last message sent like you would see it in an IRC client.
timestamp() - Returns the current time as a string.
op(channel, reciever) - Ops the reciever.
deop(channel, reciever) - Deops the reciever.
voice(channel, reciever) - Gives the reciever voice.
devoice(channel, reciever) - Removes voice from the reciever.
ban(channel, reciever) - Bans the reciever.
kick(channel, reciever[, reason]) Kicks the reciever.
disconnect([reason]) - Disconnects from IRC.
nick(new_nick) - Changes your bot's nick.
data_present(check) - Returns true if <check> is in the reciving IRC data.

It also includes a ping responder, so your bot should never time out.

Soon, I'll attach the .py file and an installer batch file.
Yay! Download the install.zip, unzip it, run install.bat, and yay!

Bug reports and suggestions welcome!
Title: Re: ircbot: Library for Python for making bots
Post by: Juju on November 19, 2011, 01:58:53 pm
Nice, that might be useful :D

So you have to make an infinite loop that checks for message()?
Title: Re: ircbot: Library for Python for making bots
Post by: Netham45 on November 19, 2011, 02:22:26 pm
Instead of checking for 'End of /MOTD command', you should check for the number that is before it.

Code: [Select]
irc.server.net 376: End of /MOTD
IRC servers that run in different languages may change the End of /motd text, but the number will pretty much always be there.

http://www.mirc.net/raws/

Those are almost always better to look for than the text.
Title: Re: ircbot: Library for Python for making bots
Post by: alberthrocks on November 19, 2011, 02:53:20 pm
Instead of checking for 'End of /MOTD command', you should check for the number that is before it.

Code: [Select]
irc.server.net 376: End of /MOTD
IRC servers that run in different languages may change the End of /motd text, but the number will pretty much always be there.

http://www.mirc.net/raws/

Those are almost always better to look for than the text.
Ahh, yeah... /me is guilty for telling Spyro to check for "End of /MOTD" :P

(I've always been using that method to check :P For user handling, I do string splitting.)

Either way, it looks like a very nice resource to use! :D
Title: Re: ircbot: Library for Python for making bots
Post by: Spyro543 on November 19, 2011, 03:08:38 pm
I'll add more commands, like one to disconnect, one to change nickname, one to check if there is any data...
Title: Re: ircbot: Library for Python for making bots
Post by: Spyro543 on November 19, 2011, 05:02:38 pm
Just added a zip file to first post that contains install batch file and source. Also I added 3 more commands. (This is a legal double post since you're allowed to post again after an hour if it's a project update.)