This repository has been archived by the owner on Oct 23, 2022. It is now read-only.
forked from sbp/phenny
-
Notifications
You must be signed in to change notification settings - Fork 4
/
phenny
executable file
·220 lines (168 loc) · 6.45 KB
/
phenny
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
"""
phenny - An IRC Bot
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
Note: DO NOT EDIT THIS FILE.
Run ./phenny, then edit ~/.phenny/default.py
Then run ./phenny again
Additional changes re: Pinkie's custom config options by Jordan Kinsley <[email protected]>
"""
import sys, os, imp
import argparse
from textwrap import dedent as trim
dotdir = os.path.expanduser('~/.phenny')
def check_python_version():
if sys.version_info < (3, 0):
error = 'Error: Requires Python 3.0 or later, from www.python.org'
print(error, file=sys.stderr)
sys.exit(1)
def create_default_config(fn):
f = open(fn, 'w')
print(trim("""\
nick = 'phenny'
host = 'irc.example.net'
port = 6667
ssl = False
# Obtain a key from Youtube to access their V3 API
youtube_api_key = None
# Derpibooru needs a key to access NSFW images: check your account settings
derpibooru_key = None
# uncomment the next line to use a custom ca_certs file
# ca_certs = '/home/phenny/.phenny/custom.crt'
# Set to true to turn on certain debugging messages like raw message output
debug = False
ipv6 = False
channels = ['#example', '#test']
owner = 'yournickname'
# password is the NickServ password, serverpass is the server password
# password = 'example'
# serverpass = 'serverpass'
# 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:
exclude = ['admin']
ignore = ['']
# Ignore these users for some commands or modules (like tell.py)
user_ignore = ['anon']
# If you want to enumerate a list of modules rather than disabling
# some, use "enable = ['example']", which takes precedent over exclude
#
# enable = []
# Directories to load user modules from
# e.g. /path/to/my/modules
extra = []
# Channels that rule34.py will consider to be NSFW
nsfw = ['#nsfw']
# Channels that party.py will throw parties in
party = ['#party','#nsfw']
# Services to load: maps channel names to white or black lists
external = {
'#liberal': ['!'], # allow all
'#conservative': [], # allow none
'*': ['!'] # default whitelist, allow all
}
# EOF
"""), file=f)
f.close()
def create_default_config_file(dotdir):
print('Creating a default config file at ~/.phenny/default.py...')
default = os.path.join(dotdir, 'default.py')
create_default_config(default)
print('Done; now you can edit default.py, and run phenny! Enjoy.')
sys.exit(0)
def create_dotdir(dotdir):
print('Creating a config directory at ~/.phenny...')
try: os.mkdir(dotdir)
except Exception as e:
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)
create_default_config_file(dotdir)
def check_dotdir():
default = os.path.join(dotdir, 'default.py')
if not os.path.isdir(dotdir):
create_dotdir(dotdir)
elif not os.path.isfile(default):
create_default_config_file(dotdir)
def config_names(config):
config = config or 'default'
def files(d):
names = os.listdir(d)
return list(os.path.join(d, fn) for fn in names if fn.endswith('.py'))
here = os.path.join('.', config)
if os.path.isfile(here):
return [here]
if os.path.isfile(here + '.py'):
return [here + '.py']
if os.path.isdir(here):
return files(here)
there = os.path.join(dotdir, config)
if os.path.isfile(there):
return [there]
if os.path.isfile(there + '.py'):
return [there + '.py']
if os.path.isdir(there):
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)
def main(argv=None):
# Step One: Parse The Command Line
parser = argparse.ArgumentParser(description="A Python IRC bot.")
parser.add_argument('-c', '--config', metavar='fn',
help='use this configuration file or directory')
args = parser.parse_args(argv)
# Step Two: Check Dependencies
check_python_version() # require python2.4 or later
if not args.config:
check_dotdir() # require ~/.phenny, or make it and exit
# Step Three: Load The Configurations
config_modules = []
for config_name in config_names(args.config):
name = os.path.basename(config_name).split('.')[0] + '_config'
module = imp.load_source(name, config_name)
module.filename = config_name
if not hasattr(module, 'prefix'):
module.prefix = r'\.'
if not hasattr(module, 'name'):
module.name = 'Phenny Palmersbot, http://inamidst.com/phenny/'
if not hasattr(module, 'port'):
module.port = 6667
if not hasattr(module, 'debug'):
module.debug = False
if not hasattr(module, 'ssl'):
module.ssl = False
if not hasattr(module, 'ca_certs'):
module.ca_certs = '/etc/ssl/certs/ca-certificates.crt'
if not hasattr(module, 'nsfw'):
module.nsfw = []
if not hasattr(module, 'party'):
module.party = []
if not hasattr(module, 'user_ignore'):
module.user_ignore = []
if not hasattr(module, 'ipv6'):
module.ipv6 = False
if not hasattr(module, 'password'):
module.password = None
if module.host == 'irc.example.net':
error = ('Error: you must edit the config file first!\n' +
"You're currently using %s" % module.filename)
print(error, file=sys.stderr)
sys.exit(1)
config_modules.append(module)
# Step Four: Load Phenny
try: from __init__ import run
except ImportError:
try: from phenny import run
except ImportError:
print("Error: Couldn't find phenny to import", file=sys.stderr)
sys.exit(1)
# Step Five: Initialise And Run The Phennies
# @@ ignore SIGHUP
for config_module in config_modules:
run(config_module) # @@ thread this
if __name__ == '__main__':
main()