2021-04-08 10:29:43 -04:00
|
|
|
import os
|
2021-05-04 05:09:41 -04:00
|
|
|
import time
|
2021-04-08 10:29:43 -04:00
|
|
|
import atexit
|
|
|
|
|
2021-04-08 14:57:31 -04:00
|
|
|
import RNS
|
2021-05-04 05:09:41 -04:00
|
|
|
import LXMF
|
2021-04-08 14:57:31 -04:00
|
|
|
import nomadnet
|
|
|
|
|
2021-05-15 15:08:30 -04:00
|
|
|
import RNS.vendor.umsgpack as msgpack
|
|
|
|
|
2021-04-08 14:57:31 -04:00
|
|
|
from ._version import __version__
|
2021-04-08 10:29:43 -04:00
|
|
|
from .vendor.configobj import ConfigObj
|
|
|
|
|
|
|
|
class NomadNetworkApp:
|
2021-05-04 14:53:03 -04:00
|
|
|
time_format = "%Y-%m-%d %H:%M:%S"
|
2021-04-08 10:29:43 -04:00
|
|
|
_shared_instance = None
|
|
|
|
|
|
|
|
configdir = os.path.expanduser("~")+"/.nomadnetwork"
|
|
|
|
|
2021-04-08 14:57:31 -04:00
|
|
|
def exit_handler(self):
|
2021-05-13 10:39:50 -04:00
|
|
|
RNS.log("Nomad Network Client exit handler executing...", RNS.LOG_VERBOSE)
|
|
|
|
RNS.log("Saving directory...", RNS.LOG_VERBOSE)
|
|
|
|
self.directory.save_to_disk()
|
|
|
|
RNS.log("Nomad Network Client exiting now", RNS.LOG_VERBOSE)
|
2021-04-08 10:29:43 -04:00
|
|
|
|
|
|
|
def __init__(self, configdir = None, rnsconfigdir = None):
|
2021-04-08 14:57:31 -04:00
|
|
|
self.version = __version__
|
2021-04-08 11:08:07 -04:00
|
|
|
self.enable_client = False
|
|
|
|
self.enable_node = False
|
|
|
|
self.identity = None
|
|
|
|
|
2021-04-08 14:57:31 -04:00
|
|
|
self.uimode = None
|
|
|
|
|
2021-04-08 10:29:43 -04:00
|
|
|
if configdir == None:
|
|
|
|
self.configdir = NomadNetworkApp.configdir
|
|
|
|
else:
|
|
|
|
self.configdir = configdir
|
|
|
|
|
|
|
|
if NomadNetworkApp._shared_instance == None:
|
|
|
|
NomadNetworkApp._shared_instance = self
|
|
|
|
|
2021-05-13 10:39:50 -04:00
|
|
|
self.rns = RNS.Reticulum(configdir = rnsconfigdir)
|
|
|
|
|
2021-05-04 05:09:41 -04:00
|
|
|
self.configpath = self.configdir+"/config"
|
|
|
|
self.logfilepath = self.configdir+"/logfile"
|
|
|
|
self.storagepath = self.configdir+"/storage"
|
|
|
|
self.identitypath = self.configdir+"/storage/identity"
|
|
|
|
self.cachepath = self.configdir+"/storage/cache"
|
|
|
|
self.resourcepath = self.configdir+"/storage/resources"
|
|
|
|
self.conversationpath = self.configdir+"/storage/conversations"
|
2021-05-13 10:39:50 -04:00
|
|
|
self.directorypath = self.configdir+"/storage/directory"
|
2021-05-15 15:08:30 -04:00
|
|
|
self.peersettingspath = self.configdir+"/storage/peersettings"
|
2021-04-08 11:08:07 -04:00
|
|
|
|
|
|
|
if not os.path.isdir(self.storagepath):
|
|
|
|
os.makedirs(self.storagepath)
|
|
|
|
|
|
|
|
if not os.path.isdir(self.cachepath):
|
|
|
|
os.makedirs(self.cachepath)
|
|
|
|
|
|
|
|
if not os.path.isdir(self.resourcepath):
|
|
|
|
os.makedirs(self.resourcepath)
|
|
|
|
|
2021-05-04 05:09:41 -04:00
|
|
|
if not os.path.isdir(self.conversationpath):
|
|
|
|
os.makedirs(self.conversationpath)
|
|
|
|
|
2021-04-08 11:08:07 -04:00
|
|
|
if os.path.isfile(self.configpath):
|
|
|
|
try:
|
|
|
|
self.config = ConfigObj(self.configpath)
|
2021-04-09 16:07:38 -04:00
|
|
|
try:
|
|
|
|
self.applyConfig()
|
|
|
|
except Exception as e:
|
|
|
|
RNS.log("The configuration file is invalid. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
|
|
nomadnet.panic()
|
|
|
|
|
2021-04-08 11:08:07 -04:00
|
|
|
RNS.log("Configuration loaded from "+self.configpath)
|
|
|
|
except Exception as e:
|
|
|
|
RNS.log("Could not parse the configuration at "+self.configpath, RNS.LOG_ERROR)
|
|
|
|
RNS.log("Check your configuration file for errors!", RNS.LOG_ERROR)
|
2021-04-08 14:57:31 -04:00
|
|
|
nomadnet.panic()
|
2021-04-08 11:08:07 -04:00
|
|
|
else:
|
|
|
|
RNS.log("Could not load config file, creating default configuration file...")
|
|
|
|
self.createDefaultConfig()
|
2021-04-09 16:07:38 -04:00
|
|
|
|
2021-04-08 11:08:07 -04:00
|
|
|
|
|
|
|
if os.path.isfile(self.identitypath):
|
|
|
|
try:
|
|
|
|
self.identity = RNS.Identity.from_file(self.identitypath)
|
|
|
|
if self.identity != None:
|
|
|
|
RNS.log("Loaded Primary Identity %s from %s" % (str(self.identity), self.identitypath))
|
|
|
|
else:
|
|
|
|
RNS.log("Could not load the Primary Identity from "+self.identitypath, RNS.LOG_ERROR)
|
2021-04-08 14:57:31 -04:00
|
|
|
nomadnet.panic()
|
2021-04-08 11:08:07 -04:00
|
|
|
except Exception as e:
|
|
|
|
RNS.log("Could not load the Primary Identity from "+self.identitypath, RNS.LOG_ERROR)
|
|
|
|
RNS.log("The contained exception was: %s" % (str(e)), RNS.LOG_ERROR)
|
2021-04-08 14:57:31 -04:00
|
|
|
nomadnet.panic()
|
2021-04-08 11:08:07 -04:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
RNS.log("No Primary Identity file found, creating new...")
|
|
|
|
self.identity = RNS.Identity()
|
2021-05-16 18:07:11 -04:00
|
|
|
self.identity.to_file(self.identitypath)
|
2021-04-08 11:08:07 -04:00
|
|
|
RNS.log("Created new Primary Identity %s" % (str(self.identity)))
|
|
|
|
except Exception as e:
|
|
|
|
RNS.log("Could not create and save a new Primary Identity", RNS.LOG_ERROR)
|
|
|
|
RNS.log("The contained exception was: %s" % (str(e)), RNS.LOG_ERROR)
|
2021-04-09 16:07:38 -04:00
|
|
|
nomadnet.panic()
|
|
|
|
|
2021-05-15 15:08:30 -04:00
|
|
|
if os.path.isfile(self.peersettingspath):
|
|
|
|
try:
|
|
|
|
file = open(self.peersettingspath, "rb")
|
|
|
|
self.peer_settings = msgpack.unpackb(file.read())
|
|
|
|
file.close()
|
|
|
|
except Exception as e:
|
|
|
|
RNS.log("Could not load local peer settings from "+self.peersettingspath, RNS.LOG_ERROR)
|
|
|
|
RNS.log("The contained exception was: %s" % (str(e)), RNS.LOG_ERROR)
|
|
|
|
nomadnet.panic()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
RNS.log("No peer settings file found, creating new...")
|
|
|
|
self.peer_settings = {
|
|
|
|
"display_name": "",
|
|
|
|
"announce_interval": None,
|
|
|
|
"last_announce": None,
|
|
|
|
}
|
|
|
|
self.save_peer_settings()
|
|
|
|
RNS.log("Created new peer settings file")
|
|
|
|
except Exception as e:
|
|
|
|
RNS.log("Could not create and save a new peer settings file", RNS.LOG_ERROR)
|
|
|
|
RNS.log("The contained exception was: %s" % (str(e)), RNS.LOG_ERROR)
|
|
|
|
nomadnet.panic()
|
|
|
|
|
2021-04-08 11:08:07 -04:00
|
|
|
|
2021-04-08 10:29:43 -04:00
|
|
|
atexit.register(self.exit_handler)
|
|
|
|
|
2021-05-04 05:09:41 -04:00
|
|
|
self.message_router = LXMF.LXMRouter()
|
|
|
|
self.message_router.register_delivery_callback(self.lxmf_delivery)
|
|
|
|
|
2021-05-15 15:08:30 -04:00
|
|
|
self.lxmf_destination = self.message_router.register_delivery_identity(self.identity, display_name=self.peer_settings["display_name"])
|
2021-05-16 09:51:33 -04:00
|
|
|
self.lxmf_destination.set_default_app_data(self.get_display_name_bytes)
|
2021-05-14 11:51:38 -04:00
|
|
|
|
|
|
|
RNS.Identity.remember(
|
|
|
|
packet_hash=None,
|
|
|
|
destination_hash=self.lxmf_destination.hash,
|
2021-05-20 16:29:02 -04:00
|
|
|
public_key=self.identity.get_public_key(),
|
2021-05-14 11:51:38 -04:00
|
|
|
app_data=None
|
|
|
|
)
|
|
|
|
|
2021-05-15 15:08:30 -04:00
|
|
|
RNS.Transport.register_announce_handler(nomadnet.Conversation)
|
|
|
|
|
2021-05-04 05:09:41 -04:00
|
|
|
RNS.log("LXMF Router ready to receive on: "+RNS.prettyhexrep(self.lxmf_destination.hash))
|
|
|
|
|
2021-05-13 10:39:50 -04:00
|
|
|
self.directory = nomadnet.Directory.Directory(self)
|
|
|
|
|
2021-05-12 08:02:44 -04:00
|
|
|
nomadnet.ui.spawn(self.uimode)
|
2021-04-08 14:57:31 -04:00
|
|
|
|
2021-05-15 15:08:30 -04:00
|
|
|
def set_display_name(self, display_name):
|
|
|
|
self.peer_settings["display_name"] = display_name
|
|
|
|
self.lxmf_destination.display_name = display_name
|
|
|
|
self.save_peer_settings()
|
|
|
|
|
|
|
|
def get_display_name(self):
|
|
|
|
return self.peer_settings["display_name"]
|
|
|
|
|
2021-05-16 09:51:33 -04:00
|
|
|
def get_display_name_bytes(self):
|
|
|
|
return self.peer_settings["display_name"].encode("utf-8")
|
|
|
|
|
2021-05-15 15:08:30 -04:00
|
|
|
def announce_now(self):
|
|
|
|
self.lxmf_destination.announce()
|
|
|
|
self.peer_settings["last_announce"] = time.time()
|
|
|
|
self.save_peer_settings()
|
|
|
|
|
|
|
|
def save_peer_settings(self):
|
|
|
|
file = open(self.peersettingspath, "wb")
|
|
|
|
file.write(msgpack.packb(self.peer_settings))
|
|
|
|
file.close()
|
2021-04-08 10:29:43 -04:00
|
|
|
|
2021-05-04 05:09:41 -04:00
|
|
|
def lxmf_delivery(self, message):
|
|
|
|
time_string = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.timestamp))
|
|
|
|
signature_string = "Signature is invalid, reason undetermined"
|
|
|
|
if message.signature_validated:
|
|
|
|
signature_string = "Validated"
|
|
|
|
else:
|
|
|
|
if message.unverified_reason == LXMF.LXMessage.SIGNATURE_INVALID:
|
|
|
|
signature_string = "Invalid signature"
|
|
|
|
if message.unverified_reason == LXMF.LXMessage.SOURCE_UNKNOWN:
|
|
|
|
signature_string = "Cannot verify, source is unknown"
|
|
|
|
|
|
|
|
nomadnet.Conversation.ingest(message, self)
|
|
|
|
|
2021-05-04 09:10:21 -04:00
|
|
|
def conversations(self):
|
|
|
|
return nomadnet.Conversation.conversation_list(self)
|
2021-05-04 05:09:41 -04:00
|
|
|
|
2021-04-08 11:08:07 -04:00
|
|
|
def createDefaultConfig(self):
|
|
|
|
self.config = ConfigObj(__default_nomadnet_config__)
|
|
|
|
self.config.filename = self.configpath
|
|
|
|
|
|
|
|
if not os.path.isdir(self.configdir):
|
|
|
|
os.makedirs(self.configdir)
|
|
|
|
self.config.write()
|
|
|
|
self.applyConfig()
|
|
|
|
|
|
|
|
|
|
|
|
def applyConfig(self):
|
|
|
|
if "logging" in self.config:
|
|
|
|
for option in self.config["logging"]:
|
|
|
|
value = self.config["logging"][option]
|
|
|
|
if option == "loglevel":
|
|
|
|
RNS.loglevel = int(value)
|
|
|
|
if RNS.loglevel < 0:
|
|
|
|
RNS.loglevel = 0
|
|
|
|
if RNS.loglevel > 7:
|
|
|
|
RNS.loglevel = 7
|
2021-04-09 16:07:38 -04:00
|
|
|
if option == "destination":
|
|
|
|
if value.lower() == "file":
|
|
|
|
RNS.logdest = RNS.LOG_FILE
|
|
|
|
if "logfile" in self.config["logging"]:
|
|
|
|
self.logfilepath = self.config["logging"]["logfile"]
|
|
|
|
RNS.logfile = self.logfilepath
|
|
|
|
else:
|
|
|
|
RNS.logdest = RNS.LOG_STDOUT
|
2021-04-08 11:08:07 -04:00
|
|
|
|
|
|
|
if "client" in self.config:
|
|
|
|
for option in self.config["client"]:
|
|
|
|
value = self.config["client"][option]
|
|
|
|
|
|
|
|
if option == "enable_client":
|
|
|
|
value = self.config["client"].as_bool(option)
|
|
|
|
self.enable_client = value
|
|
|
|
|
2021-04-08 14:57:31 -04:00
|
|
|
if option == "user_interface":
|
|
|
|
value = value.lower()
|
|
|
|
if value == "none":
|
|
|
|
self.uimode = nomadnet.ui.UI_NONE
|
|
|
|
if value == "menu":
|
|
|
|
self.uimode = nomadnet.ui.UI_MENU
|
|
|
|
if value == "text":
|
|
|
|
self.uimode = nomadnet.ui.UI_TEXT
|
2021-04-09 16:07:38 -04:00
|
|
|
if "textui" in self.config:
|
|
|
|
if not "intro_time" in self.config["textui"]:
|
|
|
|
self.config["textui"]["intro_time"] = 1
|
|
|
|
else:
|
|
|
|
self.config["textui"]["intro_time"] = self.config["textui"].as_int("intro_time")
|
|
|
|
|
2021-06-30 16:17:21 -04:00
|
|
|
if not "editor" in self.config["textui"]:
|
|
|
|
self.config["textui"]["editor"] = "editor"
|
|
|
|
|
2021-07-02 07:35:10 -04:00
|
|
|
if not "glyphs" in self.config["textui"]:
|
|
|
|
self.config["textui"]["glyphs"] = "unicode"
|
|
|
|
|
2021-06-30 17:29:37 -04:00
|
|
|
if not "mouse_enabled" in self.config["textui"]:
|
|
|
|
self.config["textui"]["mouse_enabled"] = True
|
|
|
|
else:
|
2021-07-02 09:34:08 -04:00
|
|
|
self.config["textui"]["mouse_enabled"] = self.config["textui"].as_bool("mouse_enabled")
|
2021-06-30 17:29:37 -04:00
|
|
|
|
2021-07-04 19:15:02 -04:00
|
|
|
if not "hide_guide" in self.config["textui"]:
|
|
|
|
self.config["textui"]["hide_guide"] = False
|
|
|
|
else:
|
|
|
|
self.config["textui"]["hide_guide"] = self.config["textui"].as_bool("hide_guide")
|
|
|
|
|
2021-05-15 15:08:30 -04:00
|
|
|
if not "animation_interval" in self.config["textui"]:
|
|
|
|
self.config["textui"]["animation_interval"] = 1
|
|
|
|
else:
|
|
|
|
self.config["textui"]["animation_interval"] = self.config["textui"].as_int("animation_interval")
|
|
|
|
|
2021-04-09 16:07:38 -04:00
|
|
|
if not "colormode" in self.config["textui"]:
|
|
|
|
self.config["textui"]["colormode"] = nomadnet.ui.COLORMODE_16
|
|
|
|
else:
|
|
|
|
if self.config["textui"]["colormode"].lower() == "monochrome":
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["colormode"] = nomadnet.ui.TextUI.COLORMODE_MONO
|
2021-04-09 16:07:38 -04:00
|
|
|
elif self.config["textui"]["colormode"].lower() == "16":
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["colormode"] = nomadnet.ui.TextUI.COLORMODE_16
|
2021-04-09 16:07:38 -04:00
|
|
|
elif self.config["textui"]["colormode"].lower() == "88":
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["colormode"] = nomadnet.ui.TextUI.COLORMODE_88
|
2021-04-09 16:07:38 -04:00
|
|
|
elif self.config["textui"]["colormode"].lower() == "256":
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["colormode"] = nomadnet.ui.TextUI.COLORMODE_256
|
2021-04-09 16:07:38 -04:00
|
|
|
elif self.config["textui"]["colormode"].lower() == "24bit":
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["colormode"] = nomadnet.ui.TextUI.COLORMODE_TRUE
|
2021-04-09 16:07:38 -04:00
|
|
|
else:
|
|
|
|
raise ValueError("The selected Text UI color mode is invalid")
|
|
|
|
|
|
|
|
if not "theme" in self.config["textui"]:
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["theme"] = nomadnet.ui.TextUI.THEME_DARK
|
2021-04-09 16:07:38 -04:00
|
|
|
else:
|
|
|
|
if self.config["textui"]["theme"].lower() == "dark":
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["theme"] = nomadnet.ui.TextUI.THEME_DARK
|
2021-04-09 16:07:38 -04:00
|
|
|
elif self.config["textui"]["theme"].lower() == "light":
|
2021-05-04 14:53:03 -04:00
|
|
|
self.config["textui"]["theme"] = nomadnet.ui.TextUI.THEME_LIGHT
|
2021-04-09 16:07:38 -04:00
|
|
|
else:
|
|
|
|
raise ValueError("The selected Text UI theme is invalid")
|
|
|
|
else:
|
|
|
|
raise KeyError("Text UI selected in configuration file, but no [textui] section found")
|
2021-04-08 14:57:31 -04:00
|
|
|
if value == "graphical":
|
|
|
|
self.uimode = nomadnet.ui.UI_GRAPHICAL
|
|
|
|
if value == "web":
|
|
|
|
self.uimode = nomadnet.ui.UI_WEB
|
|
|
|
|
2021-04-08 11:08:07 -04:00
|
|
|
if "node" in self.config:
|
|
|
|
for option in self.config["node"]:
|
|
|
|
value = self.config["node"][option]
|
|
|
|
|
|
|
|
if option == "enable_node":
|
|
|
|
value = self.config["node"].as_bool(option)
|
|
|
|
self.enable_node = value
|
|
|
|
|
2021-04-08 10:29:43 -04:00
|
|
|
@staticmethod
|
|
|
|
def get_shared_instance():
|
|
|
|
if NomadNetworkApp._shared_instance != None:
|
|
|
|
return NomadNetworkApp._shared_instance
|
|
|
|
else:
|
|
|
|
raise UnboundLocalError("No Nomad Network applications have been instantiated yet")
|
|
|
|
|
|
|
|
|
|
|
|
def quit(self):
|
|
|
|
RNS.log("Nomad Network Client shutting down...")
|
2021-04-08 11:08:07 -04:00
|
|
|
os._exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
# Default configuration file:
|
|
|
|
__default_nomadnet_config__ = '''# This is the default Nomad Network config file.
|
|
|
|
# You should probably edit it to suit your needs and use-case,
|
|
|
|
|
|
|
|
[logging]
|
|
|
|
# Valid log levels are 0 through 7:
|
|
|
|
# 0: Log only critical information
|
|
|
|
# 1: Log errors and lower log levels
|
|
|
|
# 2: Log warnings and lower log levels
|
|
|
|
# 3: Log notices and lower log levels
|
|
|
|
# 4: Log info and lower (this is the default)
|
|
|
|
# 5: Verbose logging
|
|
|
|
# 6: Debug logging
|
|
|
|
# 7: Extreme logging
|
|
|
|
|
|
|
|
loglevel = 4
|
2021-04-09 16:07:38 -04:00
|
|
|
destination = file
|
2021-04-08 11:08:07 -04:00
|
|
|
|
|
|
|
[client]
|
|
|
|
|
2021-07-02 09:34:08 -04:00
|
|
|
enable_client = yes
|
2021-04-08 14:57:31 -04:00
|
|
|
user_interface = text
|
2021-04-08 11:08:07 -04:00
|
|
|
|
2021-04-09 16:07:38 -04:00
|
|
|
[textui]
|
|
|
|
|
2021-06-30 16:17:21 -04:00
|
|
|
# Amount of time to show intro screen
|
2021-04-09 16:07:38 -04:00
|
|
|
intro_time = 1
|
|
|
|
|
|
|
|
# Specify the number of colors to use
|
|
|
|
# valid colormodes are:
|
|
|
|
# monochrome, 16, 88, 256 and 24bit
|
|
|
|
#
|
2021-05-27 04:40:13 -04:00
|
|
|
# The default is a conservative 16 colors,
|
2021-04-09 16:07:38 -04:00
|
|
|
# but 256 colors can probably be used on
|
2021-05-27 04:40:13 -04:00
|
|
|
# most terminals. Some terminals support
|
|
|
|
# 24 bit color.
|
2021-04-09 16:07:38 -04:00
|
|
|
|
|
|
|
# colormode = monochrome
|
2021-05-27 04:39:34 -04:00
|
|
|
colormode = 16
|
|
|
|
# colormode = 88
|
2021-04-09 16:07:38 -04:00
|
|
|
# colormode = 256
|
|
|
|
# colormode = 24bit
|
|
|
|
|
2021-07-02 07:35:10 -04:00
|
|
|
# By default, unicode glyphs are used. If
|
|
|
|
# you have a Nerd Font installed, you can
|
|
|
|
# enable this for a better user interface.
|
|
|
|
# You can also enable plain text glyphs if
|
|
|
|
# your terminal doesn't support unicode.
|
|
|
|
|
|
|
|
# glyphs = plain
|
|
|
|
glyphs = unicode
|
|
|
|
# glyphs = nerdfont
|
|
|
|
|
2021-06-30 17:29:37 -04:00
|
|
|
# You can specify whether mouse events
|
|
|
|
# should be considered as input to the
|
|
|
|
# application. On by default.
|
|
|
|
mouse_enabled = True
|
|
|
|
|
2021-06-30 16:17:21 -04:00
|
|
|
# What editor to use for editing text. By
|
|
|
|
# default the operating systems "editor"
|
|
|
|
# alias will be used.
|
|
|
|
editor = editor
|
|
|
|
|
2021-07-04 19:15:02 -04:00
|
|
|
# If you don't want the Guide section to
|
|
|
|
# show up in the menu, you can disable it.
|
|
|
|
|
|
|
|
hide_guide = no
|
|
|
|
|
2021-04-08 11:08:07 -04:00
|
|
|
[node]
|
|
|
|
|
2021-07-02 09:34:08 -04:00
|
|
|
enable_node = no
|
2021-04-08 11:08:07 -04:00
|
|
|
|
|
|
|
'''.splitlines()
|