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

113 lines
2.5 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
2018-10-08 17:19:07 -04:00
import util
2015-01-02 16:59:08 +00:00
parser = OptionParser()
2018-10-05 16:02:38 -04:00
parser.add_option(
"-s",
"--server",
dest="server",
2018-10-08 17:19:07 -04:00
default="127.0.0.1:6667",
2018-10-05 16:02:38 -04:00
help="the server to connect to",
metavar="SERVER",
)
parser.add_option(
"-c",
"--channel",
dest="channel",
default="#tildetown",
help="the channel to join",
metavar="CHANNEL",
)
parser.add_option(
"-n",
"--nick",
dest="nick",
2018-10-09 02:43:19 -04:00
default="quotebot",
2018-10-05 16:02:38 -04:00
help="the nick to use",
metavar="NICK",
)
2015-01-02 16:59:08 +00:00
(options, args) = parser.parse_args()
def random_quote(channel):
2018-10-05 16:02:38 -04:00
quote = os.popen("/home/frs/quotes/randquote.py").read()
if len(quote) >= 256:
quote = quote[:253] + "..."
2018-10-08 17:19:07 -04:00
util.sendmsg(ircsock, channel, quote)
2018-10-05 16:02:38 -04:00
2015-01-02 16:59:08 +00:00
def haiku(channel):
2018-10-05 16:02:38 -04:00
h = os.popen("haiku").read().replace("\n", " // ")
2018-10-08 17:19:07 -04:00
util.sendmsg(ircsock, channel, h)
2015-01-02 16:59:08 +00:00
def say_mentions(user, message):
2018-10-09 02:43:19 -04:00
nick = util.get_user_from_message(message)
2018-10-05 16:02:38 -04:00
menschns = (
2018-10-09 02:43:19 -04:00
os.popen("/home/karlen/bin/mensch -u {} -t 24 -z +0".format(user))
2018-10-05 16:02:38 -04:00
.read()
.replace("\t", ": ")
.split("\n")
)
for mention in menschns:
if not "" == mention:
toSend = "PRIVMSG " + nick + " :" + mention + "\n"
if len(toSend) >= 256:
toSend = toSend[:253] + "..."
ircsock.send(toSend)
2015-01-02 16:59:08 +00:00
def say_chatty(channel):
2018-10-05 16:02:38 -04:00
chattyOut = os.popen("/home/karlen/bin/chatty").read().split("\n")
for line in chattyOut:
if line:
2018-10-08 17:19:07 -04:00
util.sendmsg(ircsock, channel, line)
2018-10-05 16:02:38 -04:00
2018-10-05 13:56:15 -04:00
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
ircmsg = ircsock.recv(2048).decode("utf-8")
2018-10-09 02:43:19 -04:00
ircmsg = ircmsg.strip("\r\n")
2015-01-02 16:59:08 +00:00
2018-10-08 17:19:07 -04:00
formatted = util.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-09 02:43:19 -04:00
user = formatted.split("\t")[1]
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg.find(":!quote") != -1:
random_quote(options.channel)
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg.find(":!mentions") != -1:
say_mentions(user, ircmsg)
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg.find(":!chatty") != -1:
say_chatty(options.channel)
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg.find(":!haiku") != -1:
haiku(options.channel)
2015-01-02 16:59:08 +00:00
2018-10-05 16:02:38 -04:00
if ircmsg[:4] == "PING":
2018-10-09 02:43:19 -04:00
util.ping(ircsock, ircmsg)
2018-10-05 16:02:38 -04:00
sys.stdout.flush()
2018-10-09 02:43:19 -04:00
# time.sleep(1)
2015-01-02 16:59:08 +00:00
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2018-10-08 17:19:07 -04:00
util.connect(ircsock, options)
2015-01-02 16:59:08 +00:00
listen()