2011-09-22 15:44:13 -04:00
|
|
|
#!/usr/bin/env python3
|
2008-02-21 07:06:33 -05:00
|
|
|
"""
|
|
|
|
phenny - An IRC Bot
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
2008-03-07 16:33:00 -05:00
|
|
|
|
|
|
|
Note: DO NOT EDIT THIS FILE.
|
|
|
|
Run ./phenny, then edit ~/.phenny/default.py
|
|
|
|
Then run ./phenny again
|
2008-02-21 07:06:33 -05:00
|
|
|
"""
|
|
|
|
|
2012-06-05 00:29:25 -04:00
|
|
|
import argparse
|
2017-04-10 01:50:00 -04:00
|
|
|
import imp
|
|
|
|
import os
|
|
|
|
import sys
|
2008-02-21 07:06:33 -05:00
|
|
|
from textwrap import dedent as trim
|
|
|
|
|
|
|
|
dotdir = os.path.expanduser('~/.phenny')
|
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def check_python_version():
|
|
|
|
if sys.version_info < (3, 4):
|
|
|
|
error = 'Error: Requires Python 3.4 or later, from www.python.org'
|
2012-02-17 22:10:29 -05:00
|
|
|
print(error, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def create_default_config(fn):
|
2012-02-17 22:10:29 -05:00
|
|
|
f = open(fn, 'w')
|
|
|
|
print(trim("""\
|
|
|
|
nick = 'phenny'
|
|
|
|
host = 'irc.example.net'
|
|
|
|
port = 6667
|
|
|
|
ssl = False
|
|
|
|
ipv6 = False
|
|
|
|
channels = ['#example', '#test']
|
|
|
|
owner = 'yournickname'
|
|
|
|
|
|
|
|
# password is the NickServ password, serverpass is the server password
|
|
|
|
# password = 'example'
|
|
|
|
# serverpass = 'serverpass'
|
|
|
|
|
2014-04-01 18:53:17 -04:00
|
|
|
# linx-enabled features (.linx, .lnx)
|
2012-11-26 22:04:36 -05:00
|
|
|
# leave the api key blank to not use them and be sure to add the 'linx' module to the ignore list.
|
|
|
|
linx_api_key = ""
|
|
|
|
|
2012-02-17 22:10:29 -05:00
|
|
|
# These are people who will be able to use admin.py's functions...
|
|
|
|
admins = [owner, 'someoneyoutrust']
|
|
|
|
# But admin.py is disabled by default, as follows:
|
2013-03-21 00:33:35 -04:00
|
|
|
exclude = ['admin', 'linx', 'foodforus']
|
2012-02-17 22:10:29 -05:00
|
|
|
|
|
|
|
ignore = ['']
|
|
|
|
|
|
|
|
# If you want to enumerate a list of modules rather than disabling
|
|
|
|
# some, use "enable = ['example']", which takes precedent over exclude
|
2017-04-10 01:50:00 -04:00
|
|
|
#
|
2012-02-17 22:10:29 -05:00
|
|
|
# enable = []
|
|
|
|
|
|
|
|
# Directories to load user modules from
|
|
|
|
# e.g. /path/to/my/modules
|
|
|
|
extra = []
|
|
|
|
|
|
|
|
# Services to load: maps channel names to white or black lists
|
2017-04-10 01:50:00 -04:00
|
|
|
external = {
|
2012-02-17 22:10:29 -05:00
|
|
|
'#liberal': ['!'], # allow all
|
|
|
|
'#conservative': [], # allow none
|
|
|
|
'*': ['!'] # default whitelist, allow all
|
|
|
|
}
|
|
|
|
|
|
|
|
# EOF
|
|
|
|
"""), file=f)
|
|
|
|
f.close()
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
2012-04-24 10:31:27 -04:00
|
|
|
def create_default_config_file(dotdir):
|
2012-05-31 01:55:11 -04:00
|
|
|
print('Creating a default config file at ~/.phenny/default.py...')
|
|
|
|
default = os.path.join(dotdir, 'default.py')
|
|
|
|
create_default_config(default)
|
2012-04-24 10:31:27 -04:00
|
|
|
|
2012-05-31 01:55:11 -04:00
|
|
|
print('Done; now you can edit default.py, and run phenny! Enjoy.')
|
|
|
|
sys.exit(0)
|
2012-04-24 10:31:27 -04:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def create_dotdir(dotdir):
|
2012-02-17 22:10:29 -05:00
|
|
|
print('Creating a config directory at ~/.phenny...')
|
2017-04-10 01:50:00 -04:00
|
|
|
try:
|
|
|
|
os.mkdir(dotdir)
|
|
|
|
except Exception as e:
|
2012-02-17 22:10:29 -05:00
|
|
|
print('There was a problem creating %s:' % dotdir, file=sys.stderr)
|
|
|
|
print(e.__class__, str(e), file=sys.stderr)
|
|
|
|
print('Please fix this and then run phenny again.', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2012-05-31 01:55:11 -04:00
|
|
|
create_default_config_file(dotdir)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def check_dotdir():
|
2012-05-31 01:55:11 -04:00
|
|
|
default = os.path.join(dotdir, 'default.py')
|
2012-04-24 10:31:27 -04:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
if not os.path.isdir(dotdir):
|
2012-02-17 22:10:29 -05:00
|
|
|
create_dotdir(dotdir)
|
2017-04-10 01:50:00 -04:00
|
|
|
elif not os.path.isfile(default):
|
2012-05-31 01:55:11 -04:00
|
|
|
create_default_config_file(dotdir)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def config_names(config):
|
2012-02-17 22:10:29 -05:00
|
|
|
config = config or 'default'
|
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
def files(d):
|
2012-02-17 22:10:29 -05:00
|
|
|
names = os.listdir(d)
|
|
|
|
return list(os.path.join(d, fn) for fn in names if fn.endswith('.py'))
|
|
|
|
|
|
|
|
here = os.path.join('.', config)
|
2017-04-10 01:50:00 -04:00
|
|
|
if os.path.isfile(here):
|
2012-02-17 22:10:29 -05:00
|
|
|
return [here]
|
2017-04-10 01:50:00 -04:00
|
|
|
if os.path.isfile(here + '.py'):
|
2012-02-17 22:10:29 -05:00
|
|
|
return [here + '.py']
|
2017-04-10 01:50:00 -04:00
|
|
|
if os.path.isdir(here):
|
2012-02-17 22:10:29 -05:00
|
|
|
return files(here)
|
|
|
|
|
|
|
|
there = os.path.join(dotdir, config)
|
2017-04-10 01:50:00 -04:00
|
|
|
if os.path.isfile(there):
|
2012-02-17 22:10:29 -05:00
|
|
|
return [there]
|
2017-04-10 01:50:00 -04:00
|
|
|
if os.path.isfile(there + '.py'):
|
2012-02-17 22:10:29 -05:00
|
|
|
return [there + '.py']
|
2017-04-10 01:50:00 -04:00
|
|
|
if os.path.isdir(there):
|
2012-02-17 22:10:29 -05:00
|
|
|
return files(there)
|
|
|
|
|
|
|
|
print("Error: Couldn't find a config file!", file=sys.stderr)
|
|
|
|
print('What happened to ~/.phenny/default.py?', file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
def main(argv=None):
|
2012-02-17 22:10:29 -05:00
|
|
|
# Step One: Parse The Command Line
|
2017-04-10 01:50:00 -04:00
|
|
|
|
2012-06-05 00:29:25 -04:00
|
|
|
parser = argparse.ArgumentParser(description="A Python IRC bot.")
|
2017-04-10 01:50:00 -04:00
|
|
|
parser.add_argument('-c', '--config', metavar='fn',
|
|
|
|
help='use this configuration file or directory')
|
2012-06-05 00:29:25 -04:00
|
|
|
args = parser.parse_args(argv)
|
2017-04-10 01:50:00 -04:00
|
|
|
|
2012-02-17 22:10:29 -05:00
|
|
|
# Step Two: Check Dependencies
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
check_python_version()
|
2012-06-05 00:29:25 -04:00
|
|
|
if not args.config:
|
2017-04-10 01:50:00 -04:00
|
|
|
check_dotdir() # require ~/.phenny, or make it and exit
|
|
|
|
|
2012-02-17 22:10:29 -05:00
|
|
|
# Step Three: Load The Configurations
|
2017-04-10 01:50:00 -04:00
|
|
|
|
2012-02-17 22:10:29 -05:00
|
|
|
config_modules = []
|
2017-04-10 01:50:00 -04:00
|
|
|
for config_name in config_names(args.config):
|
2012-02-17 22:10:29 -05:00
|
|
|
name = os.path.basename(config_name).split('.')[0] + '_config'
|
|
|
|
module = imp.load_source(name, config_name)
|
|
|
|
module.filename = config_name
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
if not hasattr(module, 'prefix'):
|
2012-02-17 22:10:29 -05:00
|
|
|
module.prefix = r'\.'
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
if not hasattr(module, 'name'):
|
2012-02-17 22:10:29 -05:00
|
|
|
module.name = 'Phenny Palmersbot, http://inamidst.com/phenny/'
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
if not hasattr(module, 'port'):
|
2012-02-17 22:10:29 -05:00
|
|
|
module.port = 6667
|
2017-04-10 01:50:00 -04:00
|
|
|
|
2012-02-17 22:10:29 -05:00
|
|
|
if not hasattr(module, 'ssl'):
|
|
|
|
module.ssl = False
|
2014-06-01 21:52:05 -04:00
|
|
|
|
|
|
|
if not hasattr(module, 'ca_certs'):
|
|
|
|
module.ca_certs = None
|
2017-04-10 01:50:00 -04:00
|
|
|
|
|
|
|
if not hasattr(module, 'ssl_cert'):
|
|
|
|
module.ssl_cert = None
|
|
|
|
|
|
|
|
if not hasattr(module, 'ssl_key'):
|
|
|
|
module.ssl_key = None
|
|
|
|
|
2012-02-17 22:10:29 -05:00
|
|
|
if not hasattr(module, 'ipv6'):
|
|
|
|
module.ipv6 = False
|
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
if not hasattr(module, 'password'):
|
2012-02-17 22:10:29 -05:00
|
|
|
module.password = None
|
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
if module.host == 'irc.example.net':
|
|
|
|
error = ('Error: you must edit the config file first!\n' +
|
|
|
|
"You're currently using %s" % module.filename)
|
2012-02-17 22:10:29 -05:00
|
|
|
print(error, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
config_modules.append(module)
|
|
|
|
|
|
|
|
# Step Four: Load Phenny
|
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
try:
|
|
|
|
from __init__ import run
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
from phenny import run
|
|
|
|
except ImportError:
|
2012-02-17 22:10:29 -05:00
|
|
|
print("Error: Couldn't find phenny to import", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Step Five: Initialise And Run The Phennies
|
|
|
|
|
|
|
|
# @@ ignore SIGHUP
|
2017-04-10 01:50:00 -04:00
|
|
|
for config_module in config_modules:
|
|
|
|
run(config_module) # @@ thread this
|
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2017-04-10 01:50:00 -04:00
|
|
|
if __name__ == '__main__':
|
2012-02-17 22:10:29 -05:00
|
|
|
main()
|