Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cleanup, like remove writeIrcLog #75

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions irc_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@

Keeping a log and reading incoming material.
"""
from collections import deque
from datetime import datetime
import json
import logging
import os
import re
import shutil
import socket

Expand All @@ -34,8 +30,6 @@ def __init__(self):
"nick": "marvin",
"realname": "Marvin The All Mighty dbwebb-bot",
"ident": None,
"irclogfile": "irclog.txt",
"irclogmax": 20,
"dirIncoming": "incoming",
"dirDone": "done",
"lastfm": None,
Expand All @@ -44,10 +38,6 @@ def __init__(self):
# Socket for IRC server
self.SOCKET = None

# Keep a log of the latest messages
self.IRCLOG = None


def connectToServer(self):
"""Connect to the IRC Server"""

Expand Down Expand Up @@ -92,7 +82,6 @@ def connectToServer(self):
def sendPrivMsg(self, message, channel):
"""Send and log a PRIV message"""
if channel == self.CONFIG["channel"]:
self.ircLogAppend(user=self.CONFIG["nick"].ljust(8), message=message)
self.MSG_LOG.debug("%s <%s> %s", channel, self.CONFIG["nick"], message)

msg = "PRIVMSG {CHANNEL} :{MSG}\r\n".format(CHANNEL=channel, MSG=message)
Expand Down Expand Up @@ -143,25 +132,6 @@ def receive(self):

return lines

def ircLogAppend(self, line=None, user=None, message=None):
"""Read incoming message and guess encoding"""
if not user:
user = re.search(r"(?<=:)\w+", line[0]).group(0)

if not message:
message = ' '.join(line[3:]).lstrip(':')

self.IRCLOG.append({
'time': datetime.now().strftime("%H:%M").rjust(5),
'user': user,
'msg': message
})

def ircLogWriteToFile(self):
"""Write IRClog to file"""
with open(self.CONFIG["irclogfile"], 'w', encoding="UTF-8") as f:
json.dump(list(self.IRCLOG), f, indent=2)

def readincoming(self):
"""
Read all files in the directory incoming, send them as a message if
Expand All @@ -187,12 +157,7 @@ def readincoming(self):

def mainLoop(self):
"""For ever, listen and answer to incoming chats"""
self.IRCLOG = deque([], self.CONFIG["irclogmax"])

while 1:
# Write irclog
self.ircLogWriteToFile()

# Check in any in the incoming directory
self.readincoming()

Expand Down Expand Up @@ -229,7 +194,6 @@ def checkMarvinActions(self, words):
words[2],
words[0].split(":")[1].split("!")[0],
" ".join(words[3:]))
self.ircLogAppend(words)

if words[1] == 'PRIVMSG':
raw = ' '.join(words[3:])
Expand Down
12 changes: 0 additions & 12 deletions marvin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def getAllActions():
marvinBudord,
marvinQuote,
marvinStats,
marvinIrcLog,
marvinWeather,
marvinSun,
marvinSayHi,
Expand Down Expand Up @@ -233,17 +232,6 @@ def marvinStats(row):
return msg


def marvinIrcLog(row):
"""
Provide a link to the irclog
"""
msg = None
if any(r in row for r in ["irc", "irclog", "log", "irclogg", "logg", "historik"]):
msg = getString("irclog")

return msg


def marvinSayHi(row):
"""
Say hi with a nice message.
Expand Down
25 changes: 0 additions & 25 deletions marvin_general_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,11 @@
Make general actions for Marvin, one function for each action.
"""
import datetime
import json
import random

# Load all strings from file
with open("marvin_strings.json", encoding="utf-8") as f:
STRINGS = json.load(f)

lastDateGreeted = None


def getString(key, key1=None):
"""
Get a string from the string database.
"""
data = STRINGS[key]
if isinstance(data, list):
res = data[random.randint(0, len(data) - 1)]
elif isinstance(data, dict):
if key1 is None:
res = data
else:
res = data[key1]
if isinstance(res, list):
res = res[random.randint(0, len(res) - 1)]
elif isinstance(data, str):
res = data

return res


def getAllGeneralActions():
"""
Return all general actions as an array.
Expand Down
4 changes: 1 addition & 3 deletions marvin_strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

"whois": "Jag är en tjänstvillig själ som gillar webbprogrammering. Jag bor på GitHub https://github.com/mosbth/irc2phpbb och du kan diskutera mig i forumet http://dbwebb.se/t/20",

"menu": "[ vem är | lyssna | le | lunch [var] | citat | budord 1 - 5 | source | väder | solen | dagens video | nöje/paus/strip/comic [slump] | grill | birthday/födelsedag | nameday/namnsdag | stats | irclog | google/googla | explain/förklara | uptime | stream | princip | skämt/joke | hjälp ]",
"menu": "[ vem är | le | lunch [var] | citat | budord 1 - 5 | source | väder | solen | dagens video | nöje/paus/strip/comic [slump] | grill | birthday/födelsedag | nameday/namnsdag | stats | google/googla | explain/förklara | uptime | stream | princip | skämt/joke | hjälp ]",

"ircstats": "Statistik för kanalen finns här: http://dbwebb.se/irssistats/db-o-webb.html",

"irclog": "Loggen över de senaste 100 inläggen i kanalen finns här: https://dbwebb.se/irclog",

"google": [
"Googla {}",
"LMGTFY {}",
Expand Down
5 changes: 0 additions & 5 deletions test_marvin_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,6 @@ def testStats(self):
self.assertStringsOutput(marvin_actions.marvinStats, "stats", "ircstats")
self.assertActionSilent(marvin_actions.marvinStats, "statistics")

def testIRCLog(self):
"""Test that marvin can provide a link to the IRC log"""
self.assertStringsOutput(marvin_actions.marvinIrcLog, "irc", "irclog")
self.assertActionSilent(marvin_actions.marvinIrcLog, "ircstats")

def testSayHi(self):
"""Test that marvin responds to greetings"""
with mock.patch("marvin_actions.random") as r:
Expand Down