-
Notifications
You must be signed in to change notification settings - Fork 38
/
cardinal.py
executable file
·159 lines (132 loc) · 4.92 KB
/
cardinal.py
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
#!/usr/bin/env python
import os
import sys
import argparse
import logging
import logging.config
from twisted.internet import reactor
from cardinal.config import ConfigParser, ConfigSpec
from cardinal.bot import CardinalBotFactory
def setup_logging(config=None):
if config is None:
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
else:
logging.config.dictConfig(config)
return logging.getLogger(__name__)
if __name__ == "__main__":
# Create a new instance of ArgumentParser with a description about Cardinal
arg_parser = argparse.ArgumentParser(description="""
Cardinal IRC bot
A Twisted IRC bot designed to be simple to use and and easy to extend.
https://github.com/JohnMaguire/Cardinal
""", formatter_class=argparse.RawDescriptionHelpFormatter)
arg_parser.add_argument('config', metavar='config',
help='custom config location')
# Parse command-line arguments
args = arg_parser.parse_args()
config_file = args.config
# Define the config spec and create a parser for our internal config
spec = ConfigSpec()
spec.add_option('nickname', str, 'Cardinal')
spec.add_option('password', str, None)
spec.add_option('username', str, None)
spec.add_option('realname', str, None)
spec.add_option('network', str, 'irc.darkscience.net')
spec.add_option('port', int, 6697)
spec.add_option('server_password', str, None)
spec.add_option('server_commands', list, [])
spec.add_option('ssl', bool, True)
spec.add_option('storage', str, os.path.join(
os.path.dirname(os.path.realpath(sys.argv[0])),
'storage'
))
spec.add_option('channels', list, ['#bots'])
spec.add_option('censored_words', dict, {})
spec.add_option('plugins', list, [
"admin",
"github",
"google",
"help",
"join_on_invite",
"lastfm",
"ping",
"remind",
"sed",
"seen",
"timezone",
"urbandict",
"urls",
"weather",
"wikipedia",
"youtube"
])
spec.add_option('blacklist', dict, {})
spec.add_option('logging', dict, None)
parser = ConfigParser(spec)
# Load config file
try:
config = parser.load_config(config_file)
except Exception:
# Need to setup a logger early
logger = setup_logging()
logger.exception("Unable to load config: {}".format(config_file))
sys.exit(1)
# Config loaded, setup the logger
logger = setup_logging(config['logging'])
logger.info("Config loaded: {}".format(config_file))
# Determine storage directory
if config['storage'] is not None:
if config['storage'].startswith('/'):
config['storage'] = config['storage']
else:
config['storage'] = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
config['storage']
)
logger.info("Storage path: {}".format(config['storage']))
directories = [
os.path.join(config['storage'], 'database'),
os.path.join(config['storage'], 'logs'),
]
for directory in directories:
if not os.path.exists(directory):
logger.info(
"Initializing storage directory: {}".format(directory))
os.makedirs(directory)
# If no username is supplied, default to nickname
if config['username'] is None:
config['username'] = config['nickname']
# Instance a new factory, and connect with/without SSL
logger.debug("Instantiating CardinalBotFactory")
factory = CardinalBotFactory(config['network'],
config['server_password'],
config['server_commands'],
config['channels'],
config['nickname'],
config['password'],
config['username'],
config['realname'],
config['plugins'],
config['censored_words'],
config['blacklist'],
config['storage'])
if not config['ssl']:
logger.info(
"Connecting over plaintext to %s:%d" %
(config['network'], config['port'])
)
reactor.connectTCP(config['network'], config['port'], factory)
else:
logger.info(
"Connecting over SSL to %s:%d" %
(config['network'], config['port'])
)
# For SSL, we need to import the SSL module from Twisted
from twisted.internet import ssl
reactor.connectSSL(config['network'], config['port'], factory,
ssl.ClientContextFactory())
# Run the Twisted reactor
reactor.run()