Merge commit 'refs/merge-requests/2' of git://gitorious.org/electrum/electrum into...
[electrum-server.git] / server.py
index 8fce06c..bb939f5 100755 (executable)
--- a/server.py
+++ b/server.py
@@ -22,9 +22,9 @@ Todo:
 """
 
 
-import time, socket, operator, thread, ast, sys
+import time, socket, operator, thread, ast, sys,re
 import psycopg2, binascii
-import bitcoin
+import bitcoinrpc
 
 from Abe.abe import hash_to_address, decode_check_address
 from Abe.DataStore import DataStore as Datastore_class
@@ -60,10 +60,10 @@ peer_list = {}
 
 class MyStore(Datastore_class):
 
-    def safe_sql(self,sql):
+    def safe_sql(self,sql, params=()):
         try:
             dblock.acquire()
-            ret = self.selectall(sql)
+            ret = self.selectall(sql,params)
             dblock.release()
             return ret
         except:
@@ -123,8 +123,8 @@ class MyStore(Datastore_class):
               JOIN txin ON (txin.tx_id = tx.tx_id)
               JOIN txout prevout ON (txin.txout_id = prevout.txout_id)
               JOIN pubkey ON (pubkey.pubkey_id = prevout.pubkey_id)
-             WHERE pubkey.pubkey_hash = '%s'
-               AND cc.in_longest = 1"""%dbhash)
+             WHERE pubkey.pubkey_hash = ?
+               AND cc.in_longest = 1""", (dbhash,))
 
     def get_address_out_rows_memorypool(self, dbhash):
         return self.safe_sql(""" SELECT
@@ -137,7 +137,7 @@ class MyStore(Datastore_class):
               JOIN txin ON (txin.tx_id = tx.tx_id)
               JOIN txout prevout ON (txin.txout_id = prevout.txout_id)
               JOIN pubkey ON (pubkey.pubkey_id = prevout.pubkey_id)
-             WHERE pubkey.pubkey_hash ='%s' """%(dbhash))
+             WHERE pubkey.pubkey_hash = ? """, (dbhash,))
 
     def get_address_in_rows(self, dbhash):
         return self.safe_sql(""" SELECT
@@ -156,8 +156,8 @@ class MyStore(Datastore_class):
               JOIN tx ON (tx.tx_id = block_tx.tx_id)
               JOIN txout ON (txout.tx_id = tx.tx_id)
               JOIN pubkey ON (pubkey.pubkey_id = txout.pubkey_id)
-             WHERE pubkey.pubkey_hash = '%s'
-               AND cc.in_longest = 1"""%(dbhash))
+             WHERE pubkey.pubkey_hash = ?
+               AND cc.in_longest = 1""", (dbhash,))
 
     def get_address_in_rows_memorypool(self, dbhash):
         return self.safe_sql( """ SELECT
@@ -169,7 +169,7 @@ class MyStore(Datastore_class):
               FROM tx
               JOIN txout ON (txout.tx_id = tx.tx_id)
               JOIN pubkey ON (pubkey.pubkey_id = txout.pubkey_id)
-             WHERE pubkey.pubkey_hash = '%s' """%(dbhash))
+             WHERE pubkey.pubkey_hash = ? """, (dbhash,))
 
     def get_txpoints(self, addr):
         version, binaddr = decode_check_address(addr)
@@ -278,8 +278,8 @@ class MyStore(Datastore_class):
 
 
 def send_tx(tx):
-    import bitcoin
-    conn = bitcoin.connect_to_local()
+    import bitcoinrpc
+    conn = bitcoinrpc.connect_to_local()
     try:
         v = conn.importtransaction(tx)
     except:
@@ -336,7 +336,7 @@ def client_thread(ipaddr,conn):
             sessions[session_id] = {}
             for a in addresses:
                 sessions[session_id][a] = ''
-            out = repr( (session_id, config.get('server','banner')) )
+            out = repr( (session_id, config.get('server','banner').replace('\\n','\n') ) )
             sessions_last_time[session_id] = time.time()
 
         elif cmd=='poll': 
@@ -401,7 +401,7 @@ ds = BCDataStream.BCDataStream()
 
 def memorypool_update(store):
 
-    conn = bitcoin.connect_to_local()
+    conn = bitcoinrpc.connect_to_local()
     try:
         v = conn.getmemorypool()
     except:
@@ -442,8 +442,8 @@ def irc_thread():
         try:
             s = socket.socket()
             s.connect(('irc.freenode.net', 6667))
-            s.send('USER '+NICK+' '+NICK+' bla :'+NICK+'\n') 
-            s.send('NICK '+NICK+'\n') 
+            s.send('USER '+config.get('server','host')+' '+NICK+' bla :'+NICK+'\n') 
+            s.send('NICK '+NICK+'\n')
             s.send('JOIN #electrum\n')
             t = 0
             while not stopping:
@@ -463,9 +463,12 @@ def irc_thread():
                             s.send('USERHOST %s\n'%item)
                 elif '302' in line: # answer to /userhost
                     k = line.index('302')
-                    name = line[k+2].split('=')[0]
-                    host = line[k+2].split('@')[1]
-                    peer_list[name] = host
+                    m = re.match( "^:(.*?)=\+~(.*?)@(.*?)$", line[k+2] )
+                    if m:
+                        name = m.group(1)
+                        host = m.group(2)
+                        ip = m.group(3)
+                        peer_list[name] = (ip,host)
                 elif time.time() - t > 5*60:
                     s.send('NAMES #electrum\n')
                     t = time.time()