Onionr/onionr/onionrutils.py

50 lines
2.2 KiB
Python
Raw Normal View History

2018-01-09 22:58:12 +00:00
'''
Onionr - P2P Microblogging Platform & Social network
OnionrUtils offers various useful functions to Onionr. Relatively misc.
'''
'''
2018-01-09 22:58:12 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
# Misc functions that do not fit in the main api, but are useful
import getpass, sys, requests, configparser, os
2018-01-09 22:58:12 +00:00
class OnionrUtils():
'''Various useful functions'''
2018-01-09 22:58:12 +00:00
def __init__(self):
return
2018-01-14 04:30:10 +00:00
def printErr(self, text='an error occured'):
'''Print an error message to stderr with a new line'''
2018-01-14 04:30:10 +00:00
sys.stderr.write(text + '\n')
def localCommand(self, command):
'''Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.'''
config = configparser.ConfigParser()
if os.path.exists('data/config.ini'):
config.read('data/config.ini')
else:
return
requests.get('http://' + open('data/host.txt', 'r').read() + ':' + str(config['CLIENT']['PORT']) + '/client/?action=' + command + '&token=' + config['CLIENT']['CLIENT HMAC'])
2018-01-09 22:58:12 +00:00
def getPassword(self, message='Enter password: '):
'''Get a password without showing the users typing and confirm the input'''
2018-01-09 22:58:12 +00:00
# Get a password safely with confirmation and return it
while True:
print(message)
pass1 = getpass.getpass()
print('Confirm password: ')
pass2 = getpass.getpass()
if pass1 != pass2:
print("Passwords do not match.")
input()
else:
break
return pass1