tilde-projects/Code/irc/topicbot.py

216 lines
6.0 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2015-01-02 16:59:08 +00:00
# http://wiki.shellium.org/w/Writing_an_IRC_bot_in_Python
# Import some necessary libraries.
import socket
import os
import sys
import fileinput
import random
2017-03-30 21:39:44 +00:00
import time
import argparse
2015-01-02 16:59:08 +00:00
import inflect
2018-10-09 06:43:19 +00:00
import util
2015-01-02 16:59:08 +00:00
parser = argparse.ArgumentParser()
2015-01-02 16:59:08 +00:00
parser.add_argument(
2018-10-05 20:02:38 +00:00
"-s",
"--server",
dest="server",
2018-10-09 06:43:19 +00:00
default="127.0.0.1:6667",
2018-10-05 20:02:38 +00:00
help="the server to connect to",
metavar="SERVER",
)
parser.add_argument(
2018-10-05 20:02:38 +00:00
"-c",
"--channels",
dest="channels",
nargs="+",
default=["#bot_test"],
help="the channels to join",
metavar="CHANNELS",
2018-10-05 20:02:38 +00:00
)
parser.add_argument(
2018-10-05 20:02:38 +00:00
"-n",
"--nick",
dest="nick",
default="topicbot",
help="the nick to use",
metavar="NICK",
)
2015-01-02 16:59:08 +00:00
args = parser.parse_args()
2015-01-02 16:59:08 +00:00
p = inflect.engine()
2018-10-05 20:02:38 +00:00
2015-01-02 16:59:08 +00:00
def get_topic(channel, user, time):
2018-10-05 20:02:38 +00:00
# topic scores are saved as <USER>&^%<GETS SCORE>&^%<SETS SCORE>
with open("topicscores.txt", "r") as scorefile:
scores = scorefile.readlines()
userscore = 1
found = False
with open("topicscores.txt", "w") as scorefile:
for idx, score in enumerate(scores):
data = score.strip("\n").split("&^%")
if data[0] == user:
found = True
userscore = int(data[1]) + 1
scores[idx] = data[0] + "&^%" + str(userscore) + "&^%" + data[2] + "\n"
scorefile.writelines(scores)
if not found:
scorefile.write(user + "&^%1&^%0\n")
with open("topics_" + channel + ".txt", "r") as topics:
topic = topics.readlines()[-1].strip("\n").split("&^%", 3)
2018-10-09 06:43:19 +00:00
byuser = util.get_name(topic[1])
util.sendmsg(
ircsock,
channel,
"I've told you {} times! It's \"{}\" (set by {} {})".format(
p.number_to_words(userscore),
topic[2],
byuser,
util.pretty_date(int(topic[0])),
),
2018-10-05 20:02:38 +00:00
)
2015-01-02 16:59:08 +00:00
def count_topic(channel, user, time, msg):
2018-10-05 20:02:38 +00:00
with open("topics_" + channel + ".txt", "a") as topics:
topics.write(time + "&^%" + user + "&^%" + msg + "\n")
with open("topicscores.txt", "r") as scorefile:
scores = scorefile.readlines()
userscore = 1
found = False
with open("topicscores.txt", "w") as scorefile:
for idx, score in enumerate(scores):
data = score.strip("\n").split("&^%")
if data[0] == user:
found = True
userscore = int(data[2]) + 1
scores[idx] = data[0] + "&^%" + data[1] + "&^%" + str(userscore) + "\n"
scorefile.writelines(scores)
if not found:
scorefile.write(user + "&^%0&^%1")
2018-10-09 06:43:19 +00:00
util.sendmsg(
ircsock,
channel,
"{} has changed the topic {} times!".format(user, p.number_to_words(userscore)),
2018-10-05 20:02:38 +00:00
)
2015-01-02 16:59:08 +00:00
def set_topic(channel, user, time, msg):
2018-10-05 20:02:38 +00:00
ircsock.send("TOPIC " + channel + " :" + msg + "\n")
count_topic(channel, user, time, msg)
2015-01-02 16:59:08 +00:00
2015-04-06 14:40:08 +00:00
def random_topic(channel, user, time, setTopic=False):
2015-01-02 16:59:08 +00:00
with open("randomtopics.txt") as rtopics:
2018-10-05 20:02:38 +00:00
msg = random.choice(rtopics.readlines()).strip("\n")
if setTopic:
set_topic(channel, user, time, msg)
else:
2018-10-09 06:43:19 +00:00
util.sendmsg(ircsock, channel, "Suggested Topic: {}".format(msg))
2018-10-05 20:02:38 +00:00
2015-01-02 16:59:08 +00:00
def rollcall(channel):
2018-10-09 06:43:19 +00:00
util.sendmsg(
ircsock,
channel,
"topicbot reporting! I respond to !topic !settopic !suggesttopic !thistory",
2018-10-05 20:02:38 +00:00
)
2015-01-02 16:59:08 +00:00
def topic_score(channel):
2018-10-09 06:43:19 +00:00
util.sendmsg(ircsock, channel, "Not implemented yet")
2018-10-05 20:02:38 +00:00
2015-01-02 16:59:08 +00:00
def topic_scores(channel):
2018-10-09 06:43:19 +00:00
util.sendmsg(ircsock, channel, "Not implemented yet")
2018-10-05 20:02:38 +00:00
2015-01-02 16:59:08 +00:00
def topic_history(channel, user, count):
2018-10-05 20:02:38 +00:00
try:
iCount = int(count.split()[1])
2018-10-09 06:43:19 +00:00
except (ValueError, IndexError):
2018-10-05 20:02:38 +00:00
iCount = 3
if iCount > 10:
iCount = 10
if iCount < 1:
iCount = 3
with open("topics_" + channel + ".txt", "r") as topicsfile:
# topics = topicsfile.readlines()[-iCount:].reverse()
2018-10-09 06:43:19 +00:00
util.sendmsg(
ircsock,
channel,
"Ok, here are the last {} topics".format(p.number_to_words(iCount)),
2018-10-05 20:02:38 +00:00
)
for idx, topic in enumerate(reversed(topicsfile.readlines()[-iCount:])):
topic = topic.strip("\n").split("&^%", 3)
2018-10-09 06:43:19 +00:00
byuser = util.get_name(topic[1])
util.sendmsg(
ircsock,
channel,
"{}: {} (set by {} {})".format(
str(idx + 1), topic[2], byuser, util.pretty_date(int(topic[0]))
),
2018-10-05 20:02:38 +00:00
)
2015-01-02 16:59:08 +00:00
def listen():
2018-10-05 20:02:38 +00:00
while 1:
ircmsg = ircsock.recv(2048).decode("utf-8")
for msg in ircmsg.split("\n"):
msg = msg.strip("\n\r")
2015-01-02 16:59:08 +00:00
if msg[:4] == "PING":
util.ping(ircsock, msg)
continue
2015-01-02 16:59:08 +00:00
formatted = util.format_message(msg)
2015-01-02 16:59:08 +00:00
if "" == formatted:
time.sleep(1)
continue
2015-01-02 16:59:08 +00:00
# print formatted
2015-01-02 16:59:08 +00:00
msgtime, user, command, channel, messageText = formatted.split("\t")
2015-01-02 16:59:08 +00:00
if command == "TOPIC" and user != args.nick:
count_topic(channel, user, msgtime, messageText)
2015-01-02 16:59:08 +00:00
if msg.find(":!topic") != -1:
get_topic(channel, user, msgtime)
2015-01-02 16:59:08 +00:00
if msg.find(":!settopic") != -1:
set_topic(channel, user, msgtime, messageText[10:])
2015-01-02 16:59:08 +00:00
if msg.find(":!tscores") != -1:
topic_scores(channel)
elif msg.find(":!tscores") != -1:
topic_score(channel)
2015-01-02 16:59:08 +00:00
if msg.find(":!randomtopic") != -1:
random_topic(channel, user, msgtime, True)
if msg.find(":!suggesttopic") != -1:
random_topic(channel, user, msgtime, False)
2015-01-02 16:59:08 +00:00
if msg.find(":!thistory") != -1:
topic_history(channel, user, messageText)
2015-01-02 16:59:08 +00:00
if msg.find(":!rollcall") != -1:
rollcall(channel)
2015-01-02 16:59:08 +00:00
sys.stdout.flush()
time.sleep(1)
2015-01-02 16:59:08 +00:00
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
util.connect(ircsock, args)
2015-01-02 16:59:08 +00:00
listen()