This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhonbot
executable file
·172 lines (134 loc) · 4.74 KB
/
honbot
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
#!/usr/bin/env python
"""
honbot - A Heroes Of Newerth chatserver Bot
Copyright 2011, Anton Romanov
Heavily inspired by phenny:
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
Note: DO NOT EDIT THIS FILE.
Run ./honbot, then edit ~/.honbot/default.py
Then run ./honbot again
"""
import sys, os, imp, optparse
from textwrap import dedent as trim
dotdir = os.path.expanduser('~/.honbot')
def check_python_version():
if sys.version_info < (2, 4):
error = 'Error: Requires Python 2.4 or later, from www.python.org'
print >> sys.stderr, error
sys.exit(1)
def create_default_config(fn):
f = open(fn, 'w')
print >> f, trim("""\
region = 'na' # possible values - 'na' for na/eu/aus/lat, 'sea' for garena sea and 'cis' for garena cis
nick = 'honbot'
password = 'example'
owner = 'yournickname'
api_key = ''
# exclude modules from loading
exclude = ['mumble','fixmm','logger','forumcheck']
# prefix for commands
prefix = '[\.\!]'
#
#
# If you want to enumerate a list of modules rather than disabling
# some, use "enable = ['example']", which takes precedent over exclude
# storage and ping are required for correct operation
# enable = ['admin','config','ping']
# Directories to load user modules from
# e.g. /path/to/my/modules
extra = []
# delay (in seconds) between reconnection attempts
delay = 20
# EOF
""")
f.close()
def create_dotdir(dotdir):
print 'Creating a config directory at ~/.honbot...'
try: os.mkdir(dotdir)
except Exception, e:
print >> sys.stderr, 'There was a problem creating %s:' % dotdir
print >> sys.stderr, e.__class__, str(e)
print >> sys.stderr, 'Please fix this and then run honbot again.'
sys.exit(1)
print 'Creating a default config file at ~/.honbot/default.py...'
default = os.path.join(dotdir, 'default.py')
create_default_config(default)
print 'Done; now you can edit default.py, and run honbot! Enjoy.'
sys.exit(0)
def check_dotdir():
if not os.path.isdir(dotdir):
create_dotdir(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 >> sys.stderr, "Error: Couldn't find a config file!"
print >> sys.stderr, 'What happened to ~/.honbot/default.py?'
sys.exit(1)
class Logger(object):
def __init__(self, filename="Default.log", stdout=False, stderr=False):
if stdout:
self.terminal = sys.stdout
elif stderr:
self.terminal = sys.stderr
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
self.log.flush()
def main(argv=None):
# Step One: Parse The Command Line
parser = optparse.OptionParser('%prog [options]')
parser.add_option('-l', '--log', metavar='/path/to/file.log',
help='Log stdout/stderr to this file')
parser.add_option('-c', '--config', metavar='fn',
help='use this configuration file or directory')
opts, args = parser.parse_args(argv)
if args: print >> sys.stderr, 'Warning: ignoring spurious arguments'
if opts.log:
sys.stdout = Logger(opts.log, stdout=True)
sys.stderr = Logger(opts.log, stderr=True)
# Step Two: Check Dependencies
check_python_version() # require python2.4 or later
check_dotdir() # require ~/.honbot, or make it and exit
# Step Three: Load The Configurations
config_modules = []
for config_name in config_names(opts.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'\.'
config_modules.append(module)
# Step Four: Load honbot
try: from __init__ import run
except ImportError:
try: from honbot import run
except ImportError:
print >> sys.stderr, "Error: Couldn't find honbot to import"
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__':
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
main()