Convert gui.py into a default plugin

This commit is contained in:
Arinerron 2018-04-21 17:09:48 -07:00
parent bd0a175dfc
commit 9cf07355ce
No known key found for this signature in database
GPG Key ID: 99383627861C62F0
3 changed files with 128 additions and 123 deletions

View File

@ -0,0 +1,119 @@
#!/usr/bin/python
'''
Onionr - P2P Microblogging Platform & Social network
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/>.
'''
# Imports some useful libraries
import logger, config
from tkinter import *
import os, sqlite3, core
def sendMessage():
global sendEntry
messageToAdd = '-txt-' + sendEntry.get()
addedHash = pluginapi.get_core().setData(messageToAdd)
pluginapi.get_core().addToBlockDB(addedHash, selfInsert=True)
pluginapi.get_core().setBlockType(addedHash, 'txt')
sendEntry.delete(0, END)
def update():
global listedBlocks, listbox, runningCheckDelayCount, runningCheckDelay, root, daemonStatus
for i in pluginapi.get_core().getBlocksByType('txt'):
if i.strip() == '' or i in listedBlocks:
continue
blockFile = open('./data/blocks/' + i + '.dat')
listbox.insert(END, str(blockFile.read().replace('-txt-', '')))
blockFile.close()
listedBlocks.append(i)
listbox.see(END)
blocksList = os.listdir('./data/blocks/') # dir is your directory path
number_blocks = len(blocksList)
runningCheckDelayCount += 1
if runningCheckDelayCount == runningCheckDelay:
resp = pluginapi.get_core()._utils.localCommand('ping')
if resp == 'pong':
daemonStatus.config(text="Onionr Daemon Status: Running")
else:
daemonStatus.config(text="Onionr Daemon Status: Not Running")
runningCheckDelayCount = 0
root.after(10000, update)
def openGUI():
global root, runningCheckDelay, runningCheckDelayCount, scrollbar, listedBlocks, nodeInfo, keyInfo, idText, idEntry, pubKeyEntry, listbox, daemonStatus, sendEntry
root = Tk()
root.title("Onionr GUI")
runningCheckDelay = 5
runningCheckDelayCount = 4
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
listedBlocks = []
nodeInfo = Frame(root)
keyInfo = Frame(root)
idText = pluginapi.get_onionr().get_hostname()
idEntry = Entry(nodeInfo)
Label(nodeInfo, text="Node Address: ").pack(side=LEFT)
idEntry.pack()
idEntry.insert(0, idText.strip())
idEntry.configure(state="readonly")
nodeInfo.pack()
pubKeyEntry = Entry(keyInfo)
Label(keyInfo, text="Public key: ").pack(side=LEFT)
pubKeyEntry.pack()
pubKeyEntry.insert(0, pluginapi.get_core()._crypto.pubKey)
pubKeyEntry.configure(state="readonly")
keyInfo.pack()
sendEntry = Entry(root)
sendBtn = Button(root, text='Send Message', command=sendMessage)
sendEntry.pack(side=TOP, pady=5)
sendBtn.pack(side=TOP)
listbox = Listbox(root, yscrollcommand=scrollbar.set, height=15)
listbox.pack(fill=BOTH, pady=25)
daemonStatus = Label(root, text="Onionr Daemon Status: unknown")
daemonStatus.pack()
scrollbar.config(command=listbox.yview)
root.after(2000, update)
root.mainloop()
def on_init(api, data = None):
global pluginapi
pluginapi = api
api.commands.register(['gui', 'launch-gui', 'open-gui'], openGUI)
api.commands.register_help('gui', 'Opens a graphical interface for Onionr')
return

View File

@ -1,104 +0,0 @@
#!/usr/bin/python
'''
Onionr - P2P Microblogging Platform & Social network
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/>.
'''
from tkinter import *
import os, sqlite3, core
class OnionrGUI:
def __init__(self, myCore):
self.root = Tk()
self.myCore = myCore # onionr core
self.root.title("PyOnionr")
self.runningCheckDelay = 5
self.runningCheckDelayCount = 4
scrollbar = Scrollbar(self.root)
scrollbar.pack(side=RIGHT, fill=Y)
self.listedBlocks = []
self.nodeInfo = Frame(self.root)
self.keyInfo = Frame(self.root)
idText = open('./data/hs/hostname', 'r').read()
#idLabel = Label(self.info, text="Node Address: " + idText)
#idLabel.pack(pady=5)
idEntry = Entry(self.nodeInfo)
Label(self.nodeInfo, text="Node Address: ").pack(side=LEFT)
idEntry.pack()
idEntry.insert(0, idText.strip())
idEntry.configure(state="readonly")
self.nodeInfo.pack()
pubKeyEntry = Entry(self.keyInfo)
Label(self.keyInfo, text="Public key: ").pack(side=LEFT)
pubKeyEntry.pack()
pubKeyEntry.insert(0, self.myCore._crypto.pubKey)
pubKeyEntry.configure(state="readonly")
self.keyInfo.pack()
self.sendEntry = Entry(self.root)
sendBtn = Button(self.root, text='Send Message', command=self.sendMessage)
self.sendEntry.pack(side=TOP, pady=5)
sendBtn.pack(side=TOP)
self.listbox = Listbox(self.root, yscrollcommand=scrollbar.set, height=15)
#listbox.insert(END, str(i))
self.listbox.pack(fill=BOTH, pady=25)
self.daemonStatus = Label(self.root, text="Onionr Daemon Status: unknown")
self.daemonStatus.pack()
scrollbar.config(command=self.listbox.yview)
self.root.after(2000, self.update)
self.root.mainloop()
def sendMessage(self):
messageToAdd = '-txt-' + self.sendEntry.get()
addedHash = self.myCore.setData(messageToAdd)
self.myCore.addToBlockDB(addedHash, selfInsert=True)
self.myCore.setBlockType(addedHash, 'txt')
self.sendEntry.delete(0, END)
def update(self):
for i in self.myCore.getBlocksByType('txt'):
if i.strip() == '' or i in self.listedBlocks:
continue
blockFile = open('./data/blocks/' + i + '.dat')
self.listbox.insert(END, str(blockFile.read().replace('-txt-', '')))
blockFile.close()
self.listedBlocks.append(i)
self.listbox.see(END)
blocksList = os.listdir('./data/blocks/') # dir is your directory path
number_blocks = len(blocksList)
self.runningCheckDelayCount += 1
if self.runningCheckDelayCount == self.runningCheckDelay:
if self.myCore._utils.localCommand('ping') == 'pong':
self.daemonStatus.config(text="Onionr Daemon Status: Running")
else:
self.daemonStatus.config(text="Onionr Daemon Status: Not Running")
self.runningCheckDelayCount = 0
self.root.after(10000, self.update)

View File

@ -22,6 +22,7 @@
'''
import sys, os, base64, random, getpass, shutil, subprocess, requests, time, platform, datetime, re
from threading import Thread
import api, core, config, logger, onionrplugins as plugins, onionrevents as events
from onionrutils import OnionrUtils
from netcontroller import NetController
@ -31,12 +32,6 @@ try:
except ImportError:
raise Exception("You need the PySocks module (for use with socks5 proxy to use Tor)")
try:
import gui
except ImportError:
logger.error('You need python3 tkinter and tk installed to use Onionr.')
sys.exit(1)
ONIONR_TAGLINE = 'Anonymous P2P Platform - GPLv3 - https://Onionr.VoidNet.Tech'
ONIONR_VERSION = '0.0.0' # for debugging and stuff
API_VERSION = '1' # increments of 1; only change when something fundemental about how the API works changes. This way other nodes knows how to communicate without learning too much information about you.
@ -171,8 +166,6 @@ class Onionr:
'getpms': self.getPMs,
'get-pms': self.getPMs,
'gui': self.openGUI,
'addpeer': self.addPeer,
'add-peer': self.addPeer,
'add-address': self.addAddress,
@ -200,7 +193,6 @@ class Onionr:
'add-msg': 'Broadcasts a message to the Onionr network',
'pm': 'Adds a private message to block',
'get-pms': 'Shows private messages sent to you',
'gui': 'Opens a graphical interface for Onionr',
'introduce': 'Introduce your node to the public Onionr network (DAEMON MUST BE RUNNING)',
}
@ -319,13 +311,6 @@ class Onionr:
self.onionrUtils.sendPM(peer, message)
def openGUI(self):
'''
Opens a graphical interface for Onionr
'''
gui.OnionrGUI(self.onionrCore)
def listKeys(self):
'''
Displays a list of keys (used to be called peers) (?)
@ -476,7 +461,7 @@ class Onionr:
logger.info('Do ' + logger.colors.bold + sys.argv[0] + ' --help' + logger.colors.reset + logger.colors.fg.green + ' for Onionr help.')
def start(self):
def start(self, input = False):
'''
Starts the Onionr daemon
'''
@ -488,7 +473,9 @@ class Onionr:
lockFile = open('.onionr-lock', 'w')
lockFile.write('')
lockFile.close()
self.running = True
self.daemon()
self.running = False
if not self.debug and not self._developmentMode:
os.remove('.onionr-lock')
@ -550,13 +537,16 @@ class Onionr:
self.showHelp(cmd)
elif not command is None:
if command.lower() in helpmenu:
logger.info(logger.colors.bold + command + logger.colors.reset + logger.colors.fg.blue + ' : ' + logger.colors.reset + helpmenu[command.lower()])
logger.info(logger.colors.bold + command + logger.colors.reset + logger.colors.fg.blue + ' : ' + logger.colors.reset + helpmenu[command.lower()], timestamp = False)
else:
logger.warn(logger.colors.bold + command + logger.colors.reset + logger.colors.fg.blue + ' : ' + logger.colors.reset + 'No help menu entry was found')
logger.warn(logger.colors.bold + command + logger.colors.reset + logger.colors.fg.blue + ' : ' + logger.colors.reset + 'No help menu entry was found', timestamp = False)
else:
self.version(0)
for command, helpmessage in helpmenu.items():
self.showHelp(command)
return
def get_hostname(self):
return open('./data/hs/hostname', 'r').read()
Onionr()