2016-01-25 11:07:39 +00:00
|
|
|
# An IRC bot named Flea
|
|
|
|
# Copyright (C) 2016 Kris Lamoureux
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
|
|
|
from core.config import *
|
2016-01-28 13:20:37 +00:00
|
|
|
import core.irclib as irclib
|
|
|
|
|
|
|
|
# Built-in to Python 2.7
|
2016-02-02 14:18:20 +00:00
|
|
|
import __builtin__
|
2016-01-28 07:54:43 +00:00
|
|
|
import socket
|
|
|
|
import ssl
|
2016-02-02 14:18:20 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2016-02-03 23:14:45 +00:00
|
|
|
import re
|
2016-02-02 14:18:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Allows reimporting modules
|
|
|
|
class ImportRollback:
|
|
|
|
def __init__(self):
|
|
|
|
# Dictionary of loaded modules
|
|
|
|
self.curMods = sys.modules.copy()
|
|
|
|
self.newImport = __builtin__.__import__
|
|
|
|
|
|
|
|
# Directory of plugins
|
|
|
|
self.plugins = os.getcwd()+"/plugins/"
|
|
|
|
|
|
|
|
# Add the plugins location to the path variable
|
|
|
|
# Helps the system find the plugin modules
|
|
|
|
sys.path.append(self.plugins)
|
|
|
|
|
|
|
|
# Override builtin import function with install()
|
|
|
|
__builtin__.__import__ = self.install
|
|
|
|
self.newMods = {}
|
|
|
|
|
|
|
|
# Import modules
|
|
|
|
def install(self, mod, globals=None, locals=None, fromlist=[]):
|
|
|
|
self.newMods[mod] = 1
|
|
|
|
return apply(self.newImport, (mod, globals, locals, fromlist))
|
|
|
|
|
|
|
|
# Delete modules
|
|
|
|
def reset(self):
|
|
|
|
for mod in self.newMods.keys():
|
|
|
|
if not self.curMods.has_key(mod):
|
|
|
|
del(sys.modules[mod])
|
|
|
|
|
|
|
|
__builtin__.__import__ = self.newImport
|
|
|
|
|
|
|
|
|
2016-02-29 10:05:46 +00:00
|
|
|
# Print and log to logfile
|
|
|
|
def prntlog(message, logfile):
|
|
|
|
print message
|
|
|
|
if logfile:
|
|
|
|
logfile.write(message+"\n")
|
|
|
|
|
|
|
|
|
|
|
|
def PluginsImport(log=False):
|
2016-02-02 14:18:20 +00:00
|
|
|
# Get root of Flea
|
|
|
|
current = os.getcwd()
|
|
|
|
# Path to /plugins/ under /Flea/
|
|
|
|
plugins = current+"/plugins/"
|
|
|
|
|
2016-02-03 23:14:45 +00:00
|
|
|
# List of plugins
|
|
|
|
plugin_list = []
|
|
|
|
|
2016-02-02 14:18:20 +00:00
|
|
|
# If /plugins/ exists change directory to it
|
|
|
|
if os.path.exists(plugins):
|
|
|
|
os.chdir(plugins)
|
|
|
|
|
|
|
|
# Go through every item in /plugins/
|
|
|
|
for item in os.listdir(plugins):
|
|
|
|
|
|
|
|
# Only import directory plugins (no single files)
|
|
|
|
if os.path.isdir(plugins+item):
|
2016-02-29 10:05:46 +00:00
|
|
|
prntlog("[Plugins] Initializing "+item, log)
|
2016-02-03 23:14:45 +00:00
|
|
|
plugin = __import__(item+".main")
|
|
|
|
plugin_list.append(plugin)
|
2016-02-02 14:18:20 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
os.chdir(current)
|
2016-02-03 23:14:45 +00:00
|
|
|
return plugin_list
|
|
|
|
|
2016-02-02 14:18:20 +00:00
|
|
|
|
2016-01-25 11:07:39 +00:00
|
|
|
def main():
|
2016-02-02 14:18:20 +00:00
|
|
|
|
2016-02-03 23:14:45 +00:00
|
|
|
# Create irclib irc object
|
|
|
|
irc = irclib.irc()
|
|
|
|
|
2016-02-02 14:18:20 +00:00
|
|
|
# Parse main settings.conf file
|
2016-02-03 23:14:45 +00:00
|
|
|
irc.config = cfgParser("settings.conf")
|
2016-01-28 07:54:43 +00:00
|
|
|
|
2016-02-29 10:05:46 +00:00
|
|
|
# If logging is enabled, open log file.
|
|
|
|
if irc.config["logging"]:
|
|
|
|
log = open("log.txt", 'a')
|
|
|
|
irc.log = log
|
|
|
|
else:
|
|
|
|
log = False
|
|
|
|
|
2016-02-02 14:18:20 +00:00
|
|
|
# Keep track of modules for a rollback
|
|
|
|
importctrl = ImportRollback()
|
|
|
|
|
|
|
|
# Import /plugins/
|
2016-02-03 23:14:45 +00:00
|
|
|
if irc.config["plugins"]:
|
2016-02-29 10:05:46 +00:00
|
|
|
plugins = PluginsImport(log)
|
|
|
|
|
2016-02-03 23:14:45 +00:00
|
|
|
if not plugins:
|
2016-02-29 10:05:46 +00:00
|
|
|
prntlog("[Plugins] Failed to load.", log)
|
2016-02-02 14:18:20 +00:00
|
|
|
|
2016-01-28 13:20:37 +00:00
|
|
|
# Set debug to true/false inside irc() object
|
2016-02-03 23:14:45 +00:00
|
|
|
irc.debug = irc.config["debug"]
|
2016-01-28 13:20:37 +00:00
|
|
|
|
2016-01-28 07:54:43 +00:00
|
|
|
# Create socket object
|
2016-01-28 13:20:37 +00:00
|
|
|
irc.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2016-01-28 07:54:43 +00:00
|
|
|
|
|
|
|
# Wrap socket object to create SSLSocket object
|
2016-01-28 13:20:37 +00:00
|
|
|
irc.sock = ssl.wrap_socket(irc.sock)
|
2016-01-28 07:54:43 +00:00
|
|
|
|
|
|
|
# Connect to IRC server
|
2016-02-29 10:05:46 +00:00
|
|
|
host = irc.config["host"]
|
|
|
|
port = irc.config["port"]
|
|
|
|
|
|
|
|
irc.sock.connect((host, port))
|
|
|
|
prntlog("Connecting to "+host+':'+str(port), log)
|
2016-01-28 07:54:43 +00:00
|
|
|
|
|
|
|
# Display SSL information to the user
|
2016-01-28 13:20:37 +00:00
|
|
|
ssl_info = irc.sock.cipher()
|
2016-01-28 07:54:43 +00:00
|
|
|
if ssl_info != None:
|
2016-02-29 10:05:46 +00:00
|
|
|
prntlog("[SSL] Cipher: "+ssl_info[0], log)
|
|
|
|
prntlog("[SSL] Version: "+ssl_info[1], log)
|
|
|
|
prntlog("[SSL] Bits: "+str(ssl_info[2]), log)
|
2016-01-28 07:54:43 +00:00
|
|
|
|
2016-01-28 13:20:37 +00:00
|
|
|
# Send User/Nick message to establish user on the server
|
2016-02-03 23:14:45 +00:00
|
|
|
irc.User(irc.config["ident"], irc.config["mode"],
|
|
|
|
irc.config["unused"], irc.config["realname"])
|
2016-01-28 13:20:37 +00:00
|
|
|
|
2016-02-03 23:14:45 +00:00
|
|
|
irc.Nick(irc.config["nick"])
|
2016-01-28 13:20:37 +00:00
|
|
|
|
2016-01-28 07:54:43 +00:00
|
|
|
while True:
|
|
|
|
# Buffer to store data from server
|
|
|
|
data = ''
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# Receive data from connection
|
2016-01-28 13:20:37 +00:00
|
|
|
tmpdata = irc.sock.recv(4096)
|
2016-01-28 07:54:43 +00:00
|
|
|
data = data + tmpdata
|
|
|
|
|
|
|
|
if len(tmpdata) < 4096:
|
|
|
|
break
|
2016-02-02 14:18:20 +00:00
|
|
|
|
2016-01-28 07:54:43 +00:00
|
|
|
# If no incoming data exists then connection has closed
|
|
|
|
if len(tmpdata) == 0:
|
2016-02-29 10:05:46 +00:00
|
|
|
print "Connection closed."
|
|
|
|
raw_input()
|
|
|
|
sys.exit(0)
|
2016-01-28 07:54:43 +00:00
|
|
|
|
|
|
|
# Split data to easily deal with it
|
|
|
|
data = tmpdata.split("\r\n")
|
2016-01-28 13:20:37 +00:00
|
|
|
|
|
|
|
# Parse IRC line by line
|
2016-01-28 07:54:43 +00:00
|
|
|
for line in data:
|
2016-01-28 13:20:37 +00:00
|
|
|
|
|
|
|
# Ignore empty lines
|
2016-01-28 07:54:43 +00:00
|
|
|
if len(line) > 0:
|
2016-01-28 13:20:37 +00:00
|
|
|
|
2016-02-29 10:05:46 +00:00
|
|
|
# Print/log line, parse it and respond
|
|
|
|
prntlog(line, log)
|
2016-02-03 23:14:45 +00:00
|
|
|
irc.pack = irc.Parser(line)
|
|
|
|
|
|
|
|
# Run all plugins main() function
|
2016-02-29 10:05:46 +00:00
|
|
|
wait = ''
|
2016-02-03 23:14:45 +00:00
|
|
|
if irc.config["plugins"]:
|
|
|
|
for plugin in plugins:
|
2016-02-29 10:05:46 +00:00
|
|
|
wait = plugin.main.main(irc)
|
|
|
|
if wait == "QUIT":
|
|
|
|
break
|
2016-02-02 14:18:20 +00:00
|
|
|
|
2016-01-28 13:20:37 +00:00
|
|
|
# Ping Pong, keep the connection alive.
|
2016-02-03 23:14:45 +00:00
|
|
|
if irc.pack["cmd"] == "PING":
|
|
|
|
irc.Pong(irc.pack["text"])
|
2016-01-28 13:20:37 +00:00
|
|
|
|
|
|
|
# Send user mode message after command 001
|
2016-02-03 23:14:45 +00:00
|
|
|
elif irc.pack["cmd"] == "001":
|
|
|
|
irc.Mode(irc.config["nick"], irc.config["mode"])
|
2016-01-28 13:20:37 +00:00
|
|
|
|
2016-02-03 23:14:45 +00:00
|
|
|
elif irc.pack["cmd"] == "NOTICE":
|
|
|
|
if irc.pack["ident"] == "NickServ":
|
2016-02-02 14:18:20 +00:00
|
|
|
# Send password after NickServ informs you
|
|
|
|
# that your nick is registered
|
|
|
|
pattern = r"[Tt]his nickname is registered"
|
2016-02-03 23:14:45 +00:00
|
|
|
if re.search(pattern, irc.pack["text"]):
|
|
|
|
irc.Identify(irc.config["password"])
|
|
|
|
irc.Join(irc.config["channel"])
|
2016-01-28 07:54:43 +00:00
|
|
|
|
2016-02-29 10:05:46 +00:00
|
|
|
if log: log.flush()
|
|
|
|
|
|
|
|
# Wait for QUIT to be returned from any plugin's main() function
|
|
|
|
if wait == "QUIT":
|
|
|
|
# Quit, close connection and logfile.
|
|
|
|
irc.Quit("Fleabot https://github.com/Kris619/Flea")
|
|
|
|
irc.sock.close()
|
|
|
|
if log: log.close()
|
|
|
|
|
|
|
|
print "Press the [ENTER] key to close."
|
|
|
|
raw_input()
|
|
|
|
sys.exit(0)
|
|
|
|
|
2016-01-25 11:07:39 +00:00
|
|
|
|
2016-02-02 14:18:20 +00:00
|
|
|
main()
|