forked from Arceliar/bmwrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bminterface.py
207 lines (176 loc) · 7.53 KB
/
bminterface.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import ConfigParser
import xmlrpclib
import json
import datetime
import time
import email.utils
import os
import logging
purgeList = []
allMessages = []
def _getKeyLocation(): #make this not suck later
return '~/.config/PyBitmessage/keys.dat'
def _getConfig(keys):
return apiData()
#TODO make this work, so the above can be removed
config = ConfigParser.SafeConfigParser()
config.read(keys)
try:
api_port = config.getint('bitmessagesettings', 'apiport')
api_iface = config.get('bitmessagesettings', 'apiinterface')
api_uname = config.get('bitmessagesettings', 'apiusername')
api_passwd = config.get('bitmessagesettings', 'apipassword')
except:
logging.warning("Could not load keys.dat config")
return 0
return "http://"+api_uname+":"+api_passwd+"@"+api_iface+":"+str(api_port)+"/"
def _makeApi(keys):
return xmlrpclib.ServerProxy(_getConfig(keys))
def _sendMessage(toAddress, fromAddress, subject, body):
api = _makeApi(_getKeyLocation())
try:
return api.sendMessage(toAddress, fromAddress, subject, body)
except:
return 0
def _sendBroadcast(fromAddress, subject, body):
api = _makeApi(_getKeyLocation())
try:
return api.sendBroadcast(fromAddress, subject, body)
except:
return 0
def _stripAddress(address):
if 'broadcast' in address.lower():
return 'broadcast'
orig = address
alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
retstring = ''
while address:
if address[:3] == 'BM-':
retstring = 'BM-'
address = address[3:]
while address[0] in alphabet:
retstring += address[0]
address = address[1:]
else:
address = address[1:]
logging.info("converted address " + orig + " to " + retstring)
return retstring
def send(toAddress, fromAddress, subject, body):
toAddress = _stripAddress(toAddress)
fromAddress = _stripAddress(fromAddress)
subject = subject.encode('base64')
body = body.encode('base64')
if toAddress == 'broadcast':
return _sendBroadcast(fromAddress, subject, body)
else:
return _sendMessage(toAddress, fromAddress, subject, body)
def _getAll():
global allMessages
if not allMessages:
api = _makeApi(_getKeyLocation())
allMessages = json.loads(api.getAllInboxMessages())
return allMessages
def get(msgID):
inboxMessages = _getAll()
dateTime = email.utils.formatdate(time.mktime(datetime.datetime.fromtimestamp(float(inboxMessages['inboxMessages'][msgID]['receivedTime'])).timetuple()))
toAddress = inboxMessages['inboxMessages'][msgID]['toAddress'] + '@bm.addr'
fromAddress = inboxMessages['inboxMessages'][msgID]['fromAddress'] + '@bm.addr'
##Disabled to support new chan format
#if 'Broadcast' in toAddress:
# toAddress = fromAddress
subject = inboxMessages['inboxMessages'][msgID]['subject'].decode('base64')
body = inboxMessages['inboxMessages'][msgID]['message'].decode('base64')
return dateTime, toAddress, fromAddress, subject, body
def listMsgs():
inboxMessages = _getAll()
return len(inboxMessages['inboxMessages'])
def markForDelete(msgID):
global purgeList
inboxMessages = _getAll()
msgRef = str(inboxMessages['inboxMessages'][msgID]['msgid'])
purgeList.append(msgRef)
return 0
def cleanup():
global allMessages
global purgeList
while len(purgeList):
_deleteMessage(purgeList.pop())
allMessages = []
return 0
def _deleteMessage(msgRef):
api = _makeApi(_getKeyLocation())
api.trashMessage(msgRef) #TODO uncomment this to allow deletion
return 0
def getUIDLforAll():
api = _makeApi(_getKeyLocation())
inboxMessages = json.loads(api.getAllInboxMessages())
refdata = []
for msgID in range(len(inboxMessages['inboxMessages'])):
msgRef = inboxMessages['inboxMessages'][msgID]['msgid'] #gets the message Ref via the message index number
refdata.append(str(msgRef))
return refdata #api.trashMessage(msgRef) #TODO uncomment this to allow deletion
def getUIDLforSingle(msgID):
api = _makeApi(_getKeyLocation())
inboxMessages = json.loads(api.getAllInboxMessages())
msgRef = inboxMessages['inboxMessages'][msgID]['msgid'] #gets the message Ref via the message index number
return [str(msgRef)]
##############################################################################
def lookupAppdataFolder(): #gets the appropriate folders for the .dat files depending on the OS. Taken from bitmessagemain.py
import sys
APPNAME = "PyBitmessage"
from os import path, environ
if sys.platform == 'darwin':
if "HOME" in environ:
dataFolder = path.join(os.environ["HOME"], "Library/Application support/", APPNAME) + '/'
else:
logging.warning('Could not find home folder, please report this message and your OS X version to the Daemon Github.')
os.exit()
elif 'win32' in sys.platform or 'win64' in sys.platform:
dataFolder = path.join(environ['APPDATA'], APPNAME) + '\\'
else:
dataFolder = path.expanduser(path.join("~", "." + "config", APPNAME + "/"))
return dataFolder
def apiData():
global keysPath
config = ConfigParser.SafeConfigParser()
keysPath = 'keys.dat'
config.read(keysPath) #First try to load the config file (the keys.dat file) from the program directory
try:
config.get('bitmessagesettings','settingsversion')
appDataFolder = ''
except:
#Could not load the keys.dat file in the program directory. Perhaps it is in the appdata directory.
appDataFolder = lookupAppdataFolder()
keysPath = appDataFolder + 'keys.dat'
config = ConfigParser.SafeConfigParser()
config.read(keysPath)
try:
config.get('bitmessagesettings','settingsversion')
except:
#keys.dat was not there either, something is wrong.
print ' '
print '******************************************************************'
print 'There was a problem trying to access the Bitmessage keys.dat file.'
print 'Make sure that daemon is in the same directory as Bitmessage.'
print '******************************************************************'
print ' '
print config
print ' '
try:
apiConfigured = config.getboolean('bitmessagesettings','apienabled') #Look for 'apienabled'
apiEnabled = apiConfigured
except:
apiConfigured = False #If not found, set to false since it still needs to be configured
print "You need to edit your keys.dat file and enable bitmessage's API"
print "See for more details: https://bitmessage.org/wiki/API"
print "Will now crash..."
raise
#if (apiConfigured == False):#If the apienabled == false or is not present in the keys.dat file, notify the user and set it up
#apiInit(apiEnabled) #Initalize the keys.dat file with API information
#keys.dat file was found or appropriately configured, allow information retrieval
apiEnabled = config.getboolean('bitmessagesettings','apienabled')
apiPort = config.getint('bitmessagesettings', 'apiport')
apiInterface = config.get('bitmessagesettings', 'apiinterface')
apiUsername = config.get('bitmessagesettings', 'apiusername')
apiPassword = config.get('bitmessagesettings', 'apipassword')
return "http://" + apiUsername + ":" + apiPassword + "@" + apiInterface+ ":" + str(apiPort) + "/" #Build the api credentials