-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbot.py
executable file
·125 lines (96 loc) · 2.82 KB
/
bitbot.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
#!/usr/bin/env python
"""
bitbot - from:
pyngelyng - from:
UliBot - A Basic Uli-Oriented IRC Bot
License: GPL 2; share and enjoy!
Authors:
Sean B. Palmer, inamidst.com
Suw Charman, chocnvodka.blogware.com
Augmented by:
Dave Menninger
Requirements:
http://inamidst.com/proj/suwbot/ircbot.py
Vi har ikke noe kode igjen fra suwbot med mindre de har endret noe i
ircbot.py, noe de sikkert har.
"""
import ircbot, rpcgw
import urllib, sys # XXX
debug = False
nickname = 'botraf'
channel = '#bitraf'
prefix = "http://bitbot.bitraf.no/"
server_host = 'irc.freenode.net'
server_port = 6667
rpcgw_host = 'localhost'
rpcgw_port = 9042
import time, select
def conn_read_and_close(conn, timeout = 30):
starttime = time.time()
s = ""
while time.time() - starttime < timeout:
readyreads, ignor, ignored = select.select([conn], [], [], 1)
if readyreads != []:
nbytes = 1
tmp = conn.read()
s = s + tmp
if len(tmp) < nbytes:
conn.close()
return s.split("\n")
conn.close()
return ["Error: Timeout exceeded"]
def command(sender, nick, text):
"""Run <text> issued by <nick>. <text> is the full text, i.e.
",hepp hei hopp"
TODO: Fix timeout
"""
# strip prefix ','
text = text[1:]
# First word in text is filename, the rest is parameters
pos = text.find(' ')
file = ""
params = ""
if pos != -1:
file = text[:pos]
params = text[1+pos:]
else:
file = text
url = prefix + file + "?" + urllib.urlencode({'nick': nick,
'params': params})
try:
connection = urllib.urlopen(url)
lines = 1
recipient = sender
if connection.headers.has_key("x-max-lines"):
lines = int(connection.headers["x-max-lines"])
if connection.headers.has_key("x-recipient"):
recipient = connection.headers["x-recipient"]
output = conn_read_and_close(connection)
if len(output) == 0: return [""]
# Crop output,
return output[:lines], recipient
except:
return ['Error'], sender
def bitbot(host, port, channels, nick=nickname):
p = ircbot.Bot(nick=nick, channels=channels)
def f_command(m, origin, (cmd, channel), text, p=p):
"""Run any command over HTTP"""
if debug: p.notice(origin.sender, ',command: %s' % text)
lines, recipient = command(origin.sender, origin.nick, text)
for line in lines:
p.notice(recipient, line)
p.rule(f_command, ',command', "^,.+" )
import thread
thread.start_new_thread(rpcgw.rpcgw, (p, rpcgw_host, rpcgw_port, channel))
p.run(host, port)
def main():
if "--daemon" in sys.argv:
ircbot.setDebug(False)
ircbot.doubleFork()
debug = False
else:
ircbot.setDebug(True)
while 1:
bitbot(server_host, server_port, [channel])
if __name__=='__main__':
main()