-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoim
executable file
·127 lines (119 loc) · 3.9 KB
/
bitcoim
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
#! /usr/bin/python
# -*- coding: utf-8 -*-
# vi: sts=4 et sw=4
# Configuration handling
from ConfigParser import NoSectionError, NoOptionError, SafeConfigParser
config = SafeConfigParser()
config.read('/etc/bitcoim/bitcoim.conf')
# Logging
from logging import basicConfig, critical, debug, info, warning, \
DEBUG, INFO, WARNING, ERROR, CRITICAL
loglevels = {'debug': DEBUG, 'info': INFO, 'warning': WARNING, \
'error': ERROR, 'critical': CRITICAL}
try:
loglevel = loglevels[config.get('main program', 'loglevel')]
except (IndexError, NoSectionError, NoOptionError):
loglevel = INFO
basicConfig(level=loglevel)
if loglevel <= DEBUG:
xmppdebug = ['socket']
else:
xmppdebug = []
# Set up XMPP
import sys
reload(sys)
sys.setdefaultencoding('utf8')
try:
from xmpp import client, protocol
except ImportError:
critical("Error: this program needs the Python XMPP library.")
sys.exit(1)
# Set up the database
from bitcoim.db import SQL, Database
try:
dbfile = config.get('database', 'file')
except NoSectionError:
critical("No [database] section found in configuration")
sys.exit(1)
except NoOptionError:
critical("No 'file' option found in section [database] of the configuration")
sys.exit(1)
debug("Opening database")
SQL(url=dbfile)
database = Database(dbfile)
database.upgrade(1)
# Start connection to the Bitcoin controller
try:
bitcoinUser = config.get('bitcoin', 'user')
bitcoinPass = config.get('bitcoin', 'password')
except NoSectionError:
critical("No [bitcoin] section found in configuration")
sys.exit(1)
except NoOptionError:
critical("No user and/or password in section [bitcoin] of the configuration")
sys.exit(1)
try:
bitcoinHost = config.get('bitcoin', 'host')
except NoOptionError:
bitcoinHost = 'localhost'
try:
bitcoinPort = config.get('bitcoin', 'port')
except NoOptionError:
bitcoinPort = 8332
try:
bitcoinProtocol = config.get('bitcoin', 'protocol')
except NoOptionError:
bitcoinProtocol = 'http'
from bitcoin.controller import Controller
info("Connecting to bitcoin client")
try:
version = Controller(bitcoinUser, bitcoinPass, bitcoinHost, bitcoinPort, \
bitcoinProtocol).getinfo()['version']
except IOError:
critical("Communication error with bitcoin client")
sys.exit(1)
if version < 31700:
critical("This program requires bitcoin v.0.3.18 or higher")
sys.exit(1)
# Start connection to the XMPP server
from bitcoim.component import Component
from bitcoim.jid import JID
from bitcoim.useraccount import UserAccount
try:
for jid in config.get('main program', 'admins').split():
debug("Adding %s as admin" % jid)
UserAccount(JID(jid)).isAdmin(True)
except (NoSectionError, NoOptionError):
warning("You didn't declare any administrators.")
try:
componentJid = config.get('XMPP', 'jid')
componentPass = config.get('XMPP', 'password')
componentServer = config.get('XMPP', 'server')
componentPort = config.get('XMPP', 'port')
except NoSectionError:
critical("No [XMPP] section found in configuration")
sys.exit(1)
except NoOptionError:
critical("No connection information found in [XMPP] section of the configuration")
sys.exit(1)
info("Connecting to XMPP server")
from bitcoim import i18n
try:
i18n.fallbackLangs = config.get('main program', 'languages').split() + i18n.fallbackLangs
except (NoSectionError, NoOptionError):
pass # leave module's default value
try:
bitcoIM = Component(componentJid, componentPass, componentServer,
componentPort, xmppdebug)
bitcoIM.start()
except Exception, message:
critical("Error while connecting to server: %s" % (message))
sys.exit(1)
# Start the component
info("Starting to listen to incoming XMPP communication")
try:
while bitcoIM.Process(10):
pass
except KeyboardInterrupt:
debug("Keyboard interruption. Exiting...")
bitcoIM.sayGoodbye()