Files
tilde-projects/Code/irc/tilde_bot.py
T

124 lines
2.4 KiB
Python
Raw Normal View History

2015-01-02 16:59:08 +00:00
#!/usr/bin/python
# http://wiki.shellium.org/w/Writing_an_IRC_bot_in_Python
# Import some necessary libraries.
import socket
import os
import sys
from optparse import OptionParser
import get_users
import mentions
import formatter
parser = OptionParser()
2018-10-05 16:02:38 -04:00
parser.add_option(
"-s",
"--server",
dest="server",
default="127.0.0.1",
help="the server to connect to",
metavar="SERVER",
)
parser.add_option(
"-c",
"--channel",
dest="channel",
default="#bot_test",
help="the channel to join",
metavar="CHANNEL",
)
parser.add_option(
"-n",
"--nick",
dest="nick",
default="tilde_bot",
help="the nick to use",
metavar="NICK",
)
2015-01-02 16:59:08 +00:00
(options, args) = parser.parse_args()
2018-10-05 16:02:38 -04:00
def ping(pong):
ircsock.send("PONG {}\n".format(pong))
def sendmsg(chan, msg):
ircsock.send("PRIVMSG " + chan + " :" + msg + "\n")
2015-01-02 16:59:08 +00:00
def joinchan(chan):
2018-10-05 16:02:38 -04:00
ircsock.send("JOIN " + chan + "\n")
2015-01-02 16:59:08 +00:00
def hello():
2018-10-05 16:02:38 -04:00
ircsock.send("PRIVMSG " + channel + " :Hello!\n")
2015-01-02 16:59:08 +00:00
def tilde(channel, user, time):
2018-10-05 16:02:38 -04:00
# h = os.popen("haiku").read().replace("\n", " // ")
msg = time + ":" + user
print(msg)
ircsock.send("PRIVMSG " + channel + " :" + msg + "\n")
2015-01-02 16:59:08 +00:00
def connect(server, channel, botnick):
2018-10-05 16:02:38 -04:00
ircsock.connect((server, 6667))
ircsock.send(
"USER "
+ botnick
+ " "
+ botnick
+ " "
+ botnick
+ " :This bot is a result of a tutoral covered on http://shellium.org/wiki.\n"
) # user authentication
ircsock.send("NICK " + botnick + "\n")
joinchan(channel)
2015-01-02 16:59:08 +00:00
def get_user_from_message(msg):
2018-10-05 16:02:38 -04:00
try:
i1 = msg.index(":") + 1
i2 = msg.index("!")
return msg[i1:i2]
except ValueError:
return ""
2015-01-02 16:59:08 +00:00
def listen():
2018-10-05 16:02:38 -04:00
while 1:
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
ircmsg = ircsock.recv(2048)
ircmsg = ircmsg.strip("\n\r")
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
2018-10-05 13:56:15 -04:00
2018-10-05 16:02:38 -04:00
formatted = formatter.format_message(ircmsg)
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if "" == formatted:
continue
2018-10-05 13:56:15 -04:00
2018-10-05 16:02:38 -04:00
print(formatted)
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
split = formatted.split("\t")
time = split[0]
user = split[1]
messageText = split[2]
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg.find(":!tilde") != -1:
tilde(options.channel, user, time)
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg[:4] == "PING":
ping(ircmsg.split(" ")[1])
sys.stdout.flush()
2015-01-02 16:59:08 +00:00
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect(options.server, options.channel, options.nick)
listen()