add log messages for irc
[electrum-server.git] / backends / irc / __init__.py
index cc3f363..571aedb 100644 (file)
@@ -39,7 +39,7 @@ class IrcThread(threading.Thread):
         if self.report_host:
             self.host = self.report_host
         if not self.nick:
-            self.nick = Hash(self.report_host)[:10]
+            self.nick = Hash(self.host)[:5].encode("hex")
         self.prepend = 'E_'
         if config.get('server', 'coin') == 'litecoin':
             self.prepend = 'EL_'
@@ -72,26 +72,36 @@ class IrcThread(threading.Thread):
 
     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)
@@ -106,14 +116,15 @@ class IrcThread(threading.Thread):
                         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')
@@ -134,6 +145,7 @@ class IrcThread(threading.Thread):
                         self.peers = {}
             except:
                 traceback.print_exc(file=sys.stdout)
+                time.sleep(1)
             finally:
                 s.close()
 
@@ -191,6 +203,10 @@ class ServerProcessor(Processor):
         elif method == 'server.version':
             result = VERSION
 
+        elif method == 'server.getpid':
+            import os
+            result = os.getpid()
+
         elif method == 'server.stop':
             self.shared.stop()
             result = 'stopping, please wait until all threads terminate.'