2013-06-21 00:55:04 +02:00
import ConfigParser
2017-01-11 14:27:19 +01:00
from configparser import BMConfigParser
2017-02-08 20:37:42 +01:00
import defaults
2013-06-22 16:55:15 +02:00
import sys
import os
2013-08-21 16:08:22 +02:00
import locale
2013-11-02 00:25:24 +01:00
import random
import string
2014-01-20 21:25:02 +01:00
import platform
from distutils . version import StrictVersion
2013-06-22 16:55:15 +02:00
2013-07-05 20:08:19 +02:00
from namecoin import ensureNamecoinOptions
2017-01-11 17:00:00 +01:00
import paths
import state
2013-07-05 20:08:19 +02:00
2013-06-22 16:55:15 +02:00
storeConfigFilesInSameDirectoryAsProgramByDefault = False # The user may de-select Portable Mode in the settings if they want the config files to stay in the application data folder.
2013-06-21 00:55:04 +02:00
2014-02-06 14:16:07 +01:00
def _loadTrustedPeer ( ) :
try :
2017-01-11 14:27:19 +01:00
trustedPeer = BMConfigParser ( ) . get ( ' bitmessagesettings ' , ' trustedpeer ' )
2014-02-06 14:16:07 +01:00
except ConfigParser . Error :
# This probably means the trusted peer wasn't specified so we
# can just leave it as None
return
host , port = trustedPeer . split ( ' : ' )
2017-01-12 06:58:35 +01:00
state . trustedPeer = state . Peer ( host , int ( port ) )
2014-02-06 14:16:07 +01:00
2013-06-21 00:55:04 +02:00
def loadConfig ( ) :
2017-01-11 17:00:00 +01:00
if state . appdata :
BMConfigParser ( ) . read ( state . appdata + ' keys.dat ' )
#state.appdata must have been specified as a startup option.
2013-06-21 00:55:04 +02:00
try :
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . get ( ' bitmessagesettings ' , ' settingsversion ' )
2017-01-11 17:00:00 +01:00
print ' Loading config files from directory specified on startup: ' + state . appdata
2013-08-26 00:55:53 +02:00
needToCreateKeysFile = False
2013-06-21 00:55:04 +02:00
except :
2013-08-26 00:55:53 +02:00
needToCreateKeysFile = True
else :
2017-01-11 17:00:00 +01:00
BMConfigParser ( ) . read ( paths . lookupExeFolder ( ) + ' keys.dat ' )
2013-08-26 00:55:53 +02:00
try :
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . get ( ' bitmessagesettings ' , ' settingsversion ' )
2013-08-26 00:55:53 +02:00
print ' Loading config files from same directory as program. '
needToCreateKeysFile = False
2017-01-11 17:00:00 +01:00
state . appdata = paths . lookupExeFolder ( )
2013-08-26 00:55:53 +02:00
except :
# Could not load the keys.dat file in the program directory. Perhaps it
# is in the appdata directory.
2017-01-11 17:00:00 +01:00
state . appdata = paths . lookupAppdataFolder ( )
BMConfigParser ( ) . read ( state . appdata + ' keys.dat ' )
2013-08-26 00:55:53 +02:00
try :
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . get ( ' bitmessagesettings ' , ' settingsversion ' )
2017-01-11 17:00:00 +01:00
print ' Loading existing config files from ' , state . appdata
2013-08-26 00:55:53 +02:00
needToCreateKeysFile = False
except :
needToCreateKeysFile = True
if needToCreateKeysFile :
# This appears to be the first time running the program; there is
# no config file (or it cannot be accessed). Create config file.
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . add_section ( ' bitmessagesettings ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' settingsversion ' , ' 10 ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' port ' , ' 8444 ' )
BMConfigParser ( ) . set (
2016-06-18 14:19:23 +02:00
' bitmessagesettings ' , ' timeformat ' , ' %% c ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' blackwhitelist ' , ' black ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' startonlogon ' , ' false ' )
2013-08-26 00:55:53 +02:00
if ' linux ' in sys . platform :
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' minimizetotray ' , ' false ' )
# This isn't implimented yet and when True on
# Ubuntu causes Bitmessage to disappear while
# running when minimized.
else :
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' minimizetotray ' , ' true ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' showtraynotifications ' , ' true ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' startintray ' , ' false ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' socksproxytype ' , ' none ' )
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' sockshostname ' , ' localhost ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' socksport ' , ' 9050 ' )
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' socksauthentication ' , ' false ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' sockslisten ' , ' false ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' socksusername ' , ' ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' sockspassword ' , ' ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' keysencrypted ' , ' false ' )
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' messagesencrypted ' , ' false ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' defaultnoncetrialsperbyte ' , str (
2017-02-08 20:37:42 +01:00
defaults . networkDefaultProofOfWorkNonceTrialsPerByte ) )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' defaultpayloadlengthextrabytes ' , str (
2017-02-08 20:37:42 +01:00
defaults . networkDefaultPayloadLengthExtraBytes ) )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' minimizeonclose ' , ' false ' )
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' maxacceptablenoncetrialsperbyte ' , ' 0 ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set (
2013-08-26 00:55:53 +02:00
' bitmessagesettings ' , ' maxacceptablepayloadlengthextrabytes ' , ' 0 ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' dontconnect ' , ' true ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' userlocale ' , ' system ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' useidenticons ' , ' True ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' identiconsuffix ' , ' ' . join ( random . choice ( " 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz " ) for x in range ( 12 ) ) ) # a twelve character pseudo-password to salt the identicons
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' replybelow ' , ' False ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' maxdownloadrate ' , ' 0 ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' maxuploadrate ' , ' 0 ' )
BMConfigParser ( ) . set ( ' bitmessagesettings ' , ' ttl ' , ' 367200 ' )
2013-09-28 02:47:16 +02:00
2013-11-04 08:05:07 +01:00
#start:UI setting to stop trying to send messages after X days/months
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set (
2013-11-04 08:05:07 +01:00
' bitmessagesettings ' , ' stopresendingafterxdays ' , ' ' )
2017-01-11 14:27:19 +01:00
BMConfigParser ( ) . set (
2013-11-04 08:05:07 +01:00
' bitmessagesettings ' , ' stopresendingafterxmonths ' , ' ' )
2017-01-11 14:27:19 +01:00
#BMConfigParser().set(
2013-11-04 08:05:07 +01:00
# 'bitmessagesettings', 'timeperiod', '-1')
2013-10-10 07:10:46 +02:00
#end
2013-08-28 04:29:39 +02:00
# Are you hoping to add a new option to the keys.dat file? You're in
# the right place for adding it to users who install the software for
# the first time. But you must also add it to the keys.dat file of
# existing users. To do that, search the class_sqlThread.py file for the
# text: "right above this line!"
2013-08-26 00:55:53 +02:00
ensureNamecoinOptions ( )
2013-06-21 00:55:04 +02:00
2013-08-26 00:55:53 +02:00
if storeConfigFilesInSameDirectoryAsProgramByDefault :
# Just use the same directory as the program and forget about
# the appdata folder
2017-01-11 17:00:00 +01:00
state . appdata = ' '
2013-08-26 00:55:53 +02:00
print ' Creating new config files in same directory as program. '
else :
2017-01-11 17:00:00 +01:00
print ' Creating new config files in ' , state . appdata
if not os . path . exists ( state . appdata ) :
os . makedirs ( state . appdata )
2013-08-26 00:55:53 +02:00
if not sys . platform . startswith ( ' win ' ) :
os . umask ( 0o077 )
2017-01-15 10:50:02 +01:00
BMConfigParser ( ) . save ( )
2014-01-20 21:25:02 +01:00
2014-02-06 14:16:07 +01:00
_loadTrustedPeer ( )
2014-01-20 21:25:02 +01:00
def isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections ( ) :
try :
if sys . platform [ 0 : 3 ] == " win " :
2016-11-13 08:50:09 +01:00
VER_THIS = StrictVersion ( platform . version ( ) )
2014-01-21 07:17:36 +01:00
return StrictVersion ( " 5.1.2600 " ) < = VER_THIS and StrictVersion ( " 6.0.6000 " ) > = VER_THIS
2014-01-20 21:25:02 +01:00
return False
except Exception as err :
2014-01-28 20:57:01 +01:00
return False