-
Notifications
You must be signed in to change notification settings - Fork 2
/
spiffy.py
87 lines (71 loc) · 2.8 KB
/
spiffy.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
#!/usr/bin/env python
import sys
import os
import imp
from twisted.internet import reactor
from bot import BotFactory, config_defaults
connections = {}
if __name__ == '__main__':
if sys.version_info < (2, 5):
print >> sys.stderr, 'Error: Requires Python 2.5 or later, from www.python.org'
sys.exit(1)
pidfile = "spiffy.pid"
try:
pf = file(pidfile,'r')
pid = pf.read().strip()
try:
pid = int(pid)
except ValueError:
pid = None
pf.close()
except IOError:
pid = None
if pid:
print >> sys.stderr, "Pidfile (%s) already exists. Is spiffy already running?" % pidfile
sys.exit(1)
pid = os.getpid()
file(pidfile,'w+').write("%s" % pid)
config_name = os.path.join(sys.path[0], 'config.py')
if not os.path.isfile(config_name):
print >> sys.stderr, 'Error: No config(.py) file found.'
sys.exit(1)
config = imp.load_source('config', config_name)
if hasattr(config, 'networks'):
for network in config.networks:
serverconfig = {}
for x in config_defaults:
serverconfig[x] = config_defaults[x]
for x in config.__dict__:
if not x.startswith('__'):
serverconfig[x] = config.__dict__[x]
for x in config.networks[network]:
serverconfig[x] = config.networks[network][x]
serverconfig['network'] = network
if 'host' not in serverconfig:
print 'Error: Could not connect to %s. No host given.' % network
else:
print 'Creating connection to %s' % network
server = serverconfig['host']
port = 6667
if isinstance(server, (tuple, list)):
server = server[0]
ssl = False
if ':' in server:
server, port = server.split(':')
if port.startswith('+'):
port = port[1:]
from twisted.internet import ssl
contextFactory = ssl.ClientContextFactory()
ssl = True
port = int(port)
serverconfig['activeserver'] = (server, port or 6667)
if ssl:
reactor.connectSSL(server, port or 6667, BotFactory(serverconfig, connections), contextFactory)
else:
reactor.connectTCP(server, port or 6667, BotFactory(serverconfig, connections))
reactor.run()
if os.path.exists(pidfile):
os.remove(pidfile)
else:
print >> sys.stderr, "Error: No networks to connect to."
sys.exit(1)