1 - save the below as bot.py
import irc.bot
import irc.strings
from irc.client import ip_numstr_to_quad, ip_quad_to_numstr
from math import *
import os
import re
import subprocess
class TestBot(irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
c.join(self.channel)
def on_privmsg(self, c, e):
self.do_command(e, e.arguments[0])
def on_pubmsg(self, c, e):
a = e.arguments[0].split(":", 1)
if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(self.connection.get_nickname()):
self.do_command(e, a[1].strip())
return
def on_dccmsg(self, c, e):
# non-chat DCC messages are raw bytes; decode as text
text = e.arguments[0].decode('utf-8')
c.privmsg("You said: " + text)
def on_dccchat(self, c, e):
if len(e.arguments) != 2:
return
args = e.arguments[1].split()
if len(args) == 4:
try:
address = ip_numstr_to_quad(args[2])
port = int(args[3])
except ValueError:
return
self.dcc_connect(address, port)
def do_command(self, e, cmd):
nick = e.source.nick
c = self.connection
if cmd == "disconnect":
self.disconnect()
elif cmd == "die":
self.die()
elif re.search(r"^exec\s+.*", cmd):
command = re.sub(r"^exec\s", "", cmd)
command1 = command.split()
output = subprocess.Popen(command1 ,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
mgso = output.stdout.read().split("\n")
for i in mgso:
if i == "":
continue
c.privmsg(nick, i)
mgse = output.stderr.read().split("\n")
for i in mgse:
if i == "":
continue
c.privmsg(nick, i)
elif cmd == "test":
c.privmsg(nick, "result")
elif re.search(r"^math\s+.*", cmd):
result = ""
command = re.sub(r"^math\s", "", cmd)
toexec = "result = " + command
exec(toexec)
c.privmsg(nick, result)
elif cmd == "stats":
for chname, chobj in self.channels.items():
c.notice(nick, "--- Channel statistics ---")
c.notice(nick, "Channel: " + chname)
users = sorted(chobj.users())
c.notice(nick, "Users: " + ", ".join(users))
opers = sorted(chobj.opers())
c.notice(nick, "Opers: " + ", ".join(opers))
voiced = sorted(chobj.voiced())
c.notice(nick, "Voiced: " + ", ".join(voiced))
elif cmd == "dcc":
dcc = self.dcc_listen()
c.ctcp("DCC", nick, "CHAT chat %s %d" % (
ip_quad_to_numstr(dcc.localaddress),
dcc.localport))
else:
c.notice(nick, "Not understood: " + cmd)
def main():
import sys
if len(sys.argv) != 4:
print("Usage: testbot <server[:port]> <channel> <nickname>")
sys.exit(1)
s = sys.argv[1].split(":", 1)
server = s[0]
if len(s) == 2:
try:
port = int(s[1])
except ValueError:
print("Error: Erroneous port.")
sys.exit(1)
else:
port = 6667
channel = sys.argv[2]
nickname = sys.argv[3]
bot = TestBot(channel, nickname, server, port)
bot.start()
if __name__ == "__main__":
main()
2- the options available of the bot are
- disconnect
- die
- dcc to open chat
- exec to execute command on server
- math to do basic math
3- usage
connect the bot to server (127.0.0.1), and it will server on channel "#channel1" with the name bot
$ python bot.py localhost "#channel1" "bot
4- after the run the command above the bot will join the chat room channel #channel1
- snapshot of the bot joining in the channel
5- open a private message to run the command on the bot
for example
- list ip configuration
exec ifconfig lo
- list pi value
math pi
-get factorial of 5
math factorial(5)
-get value of cos of pi
math cos(pi)
- snapshot of bot interaction
No comments:
Post a Comment