-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpybot.py
executable file
·144 lines (135 loc) · 4.43 KB
/
pybot.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
#!/usr/bin/env python3
if __name__ == "__main__":
import argparse
import os
import sys
import time
import bot
import confman
import logger
import util
DEBUG = False
parser = argparse.ArgumentParser(
description="a python irc bot that does stuff")
parser.add_argument(
'--config',
'-c',
nargs=1,
help='path to config file (/your/path/to/pybotrc)',
default=None)
parser.add_argument(
'--nick',
'-n',
nargs=1,
help='bot\'s irc nickname',
default=None)
parser.add_argument(
'--owner',
'-o',
nargs=1,
help='who owns the bot (admin commands)',
default=None)
parser.add_argument(
'--server',
'-s',
nargs=1,
help='server to connect to (irc.yourserver.com)',
default=None)
parser.add_argument(
'--port',
'-p',
nargs=1,
help='port at the address to connect to (yourserver.com:<port>)',
default=None)
parser.add_argument(
'--channels',
nargs=1,
help='channels to join ("#channel1, #channel2")',
default=None)
parser.add_argument(
'--debug',
'-d',
help='debug (foreground) mode',
action='store_true')
args = parser.parse_args()
if args.debug:
DEBUG = True
if args.config:
config = args.config[0]
else:
config = "~/.pybotrc"
if (args.nick or args.server or args.channels or args.owner or args.port) and not (
args.nick and args.server and args.channels and args.owner and args.port):
parser.error("specifying any manual setting requires all be present!")
if args.nick or args.server or args.channels or args.owner or args.port:
config = None
botslist = list()
if not DEBUG and hasattr(
os, 'fork'): # are we on a system that can fork()?
pid = os.fork()
if pid == 0: # child
if os.name == "posix":
print("starting bot in the background, pid " +
util.bcolors.GREEN + str(os.getpid()) + util.bcolors.ENDC)
else:
print("starting bot in the background, pid " + str(os.getpid()))
if config is None:
b = bot.Bot(
network=args.server[0],
local_nickname=args.nick[0],
local_channels=args.channels[0],
local_owner=args.owner[0],
local_port=args.port[0])
b.start()
else:
cm = confman.ConfManager(config)
net_list = cm.getNetworks()
for c in cm.getNetworks():
b = bot.Bot(cm, c, DEBUG)
b.start()
elif pid > 0:
sys.exit(0)
# don't background; either we're in debug (foreground) mode, or on windows
else:
if os.name == 'nt':
print('in debug mode; forking unsupported on windows.')
DEBUG = True
print("starting bot, pid " + util.bcolors.GREEN +
str(os.getpid()) + util.bcolors.ENDC)
if not config:
b = bot.Bot(
d=True,
network=args.server[0],
local_nickname=args.nick[0],
local_channels=args.channels[0],
local_owner=args.owner[0],
local_port=args.port[0])
b.daemon = True
b.start()
botslist.append(b)
else:
try:
f = open(os.path.expanduser(config))
except IOError:
print("Could not open conf file " + config)
sys.exit(1)
if config:
cm = confman.ConfManager(config)
net_list = cm.getNetworks()
for c in cm.getNetworks():
b = bot.Bot(conf=cm, network=c, d=DEBUG)
b.daemon = True
b.start()
botslist.append(b)
try:
while True:
time.sleep(5)
except (KeyboardInterrupt, SystemExit):
l = logger.Logger()
l.write(logger.Logger.INFO, "killed by ctrl+c or term signal")
for b in botslist:
b.save_persistence()
b.s.send(("QUIT :because I got killed\n").encode())
print()
print("keyboard interrupt caught; exiting")
sys.exit(1)