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

Fix #39 #53

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*pyc
*.project
*.pydevproject
.idea
venv
/settings.txt
39 changes: 0 additions & 39 deletions configparser.py

This file was deleted.

100 changes: 71 additions & 29 deletions core.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,70 @@
import socket, re
import socket
import re
import yaml

from settings import read_config

class IRCBot:

logAllToConsole = False # dev boolean to toggle raw printing of all messages to console
respondToUnrecognisedCommands = False # respond to user if they enter a command that isn't registered

def __init__(self, tempCacheSize=4096):
### configuration
conf = read_config()
self.nickname = conf['nick']
self.password = conf['password']
self.channel = '#%s' % conf['channel']
self.network = conf['network']
self.port = int(conf['port'])
def __init__(self, conf, temp_cache_size=4096):
"""
Creates a new IRCBot object.

self.command_prefix = conf['command_prefix']
self.quitCmd = conf['quit']
self.logAllToConsole = conf['logAllToConsole'] == 'True'
self.respondToNotFound = conf['respondToNotFound'] == 'True'
:param conf: the configuration dictionary to use to connect, etc.
:type conf: dict
"""
# If no configuration provided, error out with a ConfError
if conf == {}:
raise ConfigurationError("No configuration provided")

# Otherwise, store the useful config info
self.nickname = conf['user']['nick']
self.password = conf['user']['password']
self.channel = '#%s' % conf['channel']['name']
self.network = conf['channel']['network']
self.port = conf['channel']['port']

self.command_prefix = conf['settings']['command_prefix']
self.logAllToConsole = conf['settings']['logAllToConsole']
self.respondToNotFound = conf['settings']['respondToNotFound']

### connection
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tempCacheSize = tempCacheSize
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.temp_cache_size = temp_cache_size

### addons
self.addonList = list()

### tests for messages received from server
self.regexIsError = re.compile(r"^ERROR.*|\\r\\nError.*")
self.regexIsJoin = re.compile(r":(?P<user>\w+)!.+\sJOIN\s")
self.regexIsQuit = re.compile(r":(?P<user>\w+)!.+\sPART\s")
self.regexIsCommand = re.compile(r"^%s{1}(?P<command>\w+)" % self.command_prefix)
self.regexIsError = re.compile(r"^ERROR.*|\\r\\nError.*")
self.regexIsJoin = re.compile(r":(?P<user>\w+)!.+\sJOIN\s")
self.regexIsQuit = re.compile(r":(?P<user>\w+)!.+\sPART\s")
self.regexIsCommand = re.compile(r"^%s{1}(?P<command>\w+)" % self.command_prefix)
self.regexCommandSplitCommand = re.compile(r"^%s{1}(?P<command>\w+)\s(?P<arguments>.*).*" % self.command_prefix)
self.regexIsChat = re.compile(r":(?P<user>\w+)!(?P<isp>.+)\sPRIVMSG\s(?P<channel>[#\w-]+)\s:(?P<message>.+)")
self.regexIsNickInUse = re.compile(r".*\s433\s.?%s.*" % self.nickname)
self.regexIsAskForLogin = re.compile(r".*ʌ")
self.regexIsChat = re.compile(r":(?P<user>\w+)!(?P<isp>.+)\sPRIVMSG\s(?P<channel>[#\w-]+)\s:(?P<message>.+)")
self.regexIsNickInUse = re.compile(r".*\s433\s.?%s.*" % self.nickname)
self.regexIsAskForLogin = re.compile(r".*ʌ")
# '...freenode.net 461 indibot JOIN :Not enough parameters\r\n'


def run(self):
self.socket.connect((self.network, self.port))
self.logInfo(self.socket.recv(self.tempCacheSize).decode("UTF-8"))
self.logInfo(self.socket.recv(self.temp_cache_size).decode("UTF-8"))
string = 'NICK %s \r\n' % self.nickname
self.socket.send(string.encode("UTF-8"))
string = 'USER %s some stuff :Python IRC\r\n' % self.nickname
self.socket.send(string.encode("UTF-8"))
string = 'JOIN %s \r\n' % self.channel
self.socket.send(string.encode("UTF-8"))

self.mainLoop()
self.main_loop()

def mainLoop(self):
def main_loop(self):
import ircHelpers # dirty hack - should be moved somewhere more applicable (ie at !!nick)
ircHelpers.start_queue_thread()
while True:
receivedData = self.socket.recv(self.tempCacheSize).decode("UTF-8")
receivedData = self.socket.recv(self.temp_cache_size).decode("UTF-8")
messageInfo = dict()

if (self.logAllToConsole):
Expand Down Expand Up @@ -139,11 +147,45 @@ def decorator(f):
return f
return decorator


class AddonBase:
commandList = dict()
behaviourList = list()
messageList = list()
joinList = list()
quitList = list()

ircBot = IRCBot()

class ConfigurationError(Exception):
pass


# So that the rest of this works, for now, parse the config file here and create ircBot
try:
with open("irc.yaml", 'r') as settings_file:
conf = yaml.load(settings_file)
# Print the configuration settings
print("=== irc server ===")
print("nick: " + conf['user']['nick'])
print("pass: " + conf['user']['password'])
print("chan: " + conf['channel']['name'])
print("netw: " + conf['channel']['network'])
print("port: " + conf['channel']['port'])

print("=== database ==")
print("db_name: " + conf['database']['name'])
print("db_user: " + conf['database']['user'])
print("db_pass: " + conf['database']['pass'])
print("db_host: " + conf['database']['host'])
print("db_port: " + conf['database']['port'])

print("=== bot settings ===")
print("cmd_pref: " + conf['settings']['prefix'])
print("logAll: " + str(conf['settings']['logAllToConsole']))
print("respond: " + str(conf['settings']['respondToNotFound']))

except FileNotFoundError:
print("Config file not found. Make sure irc.yaml exists in this directory.")
exit(1)

ircBot = IRCBot(conf)
16 changes: 9 additions & 7 deletions db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import psycopg2
import ircHelpers
import settings
import yaml

class DB:

Expand Down Expand Up @@ -45,14 +45,16 @@ def __init__(self):


def db_connect(self):
config = settings.read_config()
with open('irc.yaml', 'r') as conf_file:
config = yaml.load(conf_file)
config = config['database']
try:
conn = psycopg2.connect(
database = config['db_name'],
user = config['db_user'],
password = config['db_pass'],
host = config['db_host'],
port = config['db_port'])
database = config['name'],
user = config['user'],
password = config['pass'],
host = config['host'],
port = config['port'])
return conn
except psycopg2.Error as e:
print("!! Error connecting to db")
Expand Down
18 changes: 18 additions & 0 deletions irc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
user:
nick: "progether"
password: "progether_bot"
channel:
name: "reddit-progether"
network: "irc.freenode.net"
port: 6667
database:
name: "d2k2tmq3q2lk62"
user: "ddvzstnjeyvtkk"
pass: "qiJbYxnbFTlXBAtRiyRkXGkFub"
host: ec2-23-23-80-55.compute-1.amazonaws.com
port: 5432
settings:
prefix: "!!"
logAllToConsole: False
respondToNotFound: False

11 changes: 10 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
from core import ircBot
from addons import *

ircBot.run()

def main():
"""
Imports settings and starts the bot.
"""

ircBot.run()

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
psycopg2==2.5.1
requests==2.0.1
pyyaml
116 changes: 0 additions & 116 deletions settings.py

This file was deleted.