def run(self):
ircname = self.getname()
+ print_log("joining IRC")
while not self.processor.shared.stopped():
try:
s = socket.socket()
s.connect(('irc.freenode.net', 6667))
- s.settimeout(300)
+ s.settimeout(0.1)
except:
s.close()
+ print_log("IRC: reconnect in 10 s")
time.sleep(10)
continue
self.message = ''
+ out_msg = []
+
try:
s.send('USER electrum 0 * :' + self.host + ' ' + ircname + '\n')
s.send('NICK ' + self.nick + '\n')
s.send('JOIN #electrum\n')
t = 0
+
while not self.processor.shared.stopped():
try:
data = s.recv(2048)
+ except socket.timeout, e:
+ if out_msg:
+ m = out_msg.pop(0)
+ s.send(m)
+ continue
except:
print_log( "irc: socket error" )
time.sleep(1)
line = line.strip('\r')
if not line:
continue
+ # print_log("<--", line)
line = line.split()
if line[0] == 'PING':
- s.send('PONG ' + line[1] + '\n')
+ out_msg.append('PONG ' + line[1] + '\n')
elif '353' in line: # answer to /names
k = line.index('353')
for item in line[k+1:]:
if item.startswith(self.prepend):
- s.send('WHO %s\n' % item)
+ out_msg.append('WHO %s\n' % item)
elif '352' in line: # answer to /who
# warning: this is a horrible hack which apparently works
k = line.index('352')
host = line[k+9]
ports = line[k+10:]
self.peers[name] = (ip, host, ports)
+ elif 'KICK' in line:
+ try:
+ print_log("KICK", line[3] + line[4])
+ except:
+ print_log("KICK", "error")
if time.time() - t > 5*60:
#self.processor.push_response({'method': 'server.peers', 'params': [self.get_peers()]})
Processor.__init__(self)
self.daemon = True
self.banner = config.get('server', 'banner')
- self.password = config.get('server', 'password')
if config.get('server', 'irc') == 'yes':
self.irc = IrcThread(self, config)
params = request['params']
result = None
- if method in ['server.stop', 'server.info', 'server.debug']:
- try:
- password = request['params'][0]
- except:
- password = None
-
- if password != self.password:
- self.push_response(session,
- {'id': request['id'],
- 'result': None,
- 'error': 'incorrect password'})
- return
-
if method == 'server.banner':
result = self.banner.replace('\\n', '\n')
elif method == 'server.version':
result = VERSION
- elif method == 'server.stop':
- self.shared.stop()
- result = 'stopping, please wait until all threads terminate.'
-
- elif method == 'server.info':
- result = map(lambda s: {"time": s.time,
- "name": s.name,
- "address": s.address,
- "version": s.version,
- "subscriptions": len(s.subscriptions)},
- self.dispatcher.request_dispatcher.get_sessions())
-
- elif method == 'server.debug':
- try:
- s = request['params'][1]
- except:
- s = None
-
- if s:
- from guppy import hpy
- h = hpy()
- bp = self.dispatcher.request_dispatcher.processors['blockchain']
- try:
- result = str(eval(s))
- except:
- result = "error"
else:
print_log("unknown method", method)