From 6c71c3045435221ea91a6b75d4df835320496a44 Mon Sep 17 00:00:00 2001
From: Gabriel Wilson <gabriel@libresolutions.network>
Date: Wed, 18 May 2022 03:59:32 -0400
Subject: [PATCH] Revamp

---
 README.md         |  6 ++++
 commands.py       | 24 +++++++++-----
 data-example.json |  4 ++-
 features.py       | 82 +++++++++++++++++++++++++++++++++++++++++++++++
 main.py           |  4 ++-
 5 files changed, 110 insertions(+), 10 deletions(-)
 create mode 100644 features.py

diff --git a/README.md b/README.md
index 491740e..1a18073 100644
--- a/README.md
+++ b/README.md
@@ -26,5 +26,11 @@ You may want to set data.json as a volume to be able to make changes on the fly
 
 ![matrix room screenshot](screenshots/example2.png)
 
+## Features in the works
+* Alerts
+* Dynamic configuration
+* Meeting requests
+* Configurable API calls
+
 
 
diff --git a/commands.py b/commands.py
index 1aa057e..804f8c6 100644
--- a/commands.py
+++ b/commands.py
@@ -1,18 +1,19 @@
 import random
+import features
 jokes = [
     ["Testing","Testing you!"],
     ["singing pokemon","Jiggalypuff! 🎙️"]
 ]
 #jokes = []
 
-def handle_command(state,message):
+def handle_command(state,sender,message):
     for command in commands:
             if message.find(command)==0:
-                result = commands[command](state,message)           
+                result = commands[command](state,message,sender)           
                 return result
     if len(state) > 0:
         try:
-            result = commands[state[0]](state,message)
+            result = commands[state[0]](state,message,sender)
             return result
         except Exception as e:
             return {
@@ -39,13 +40,13 @@ def handle_command(state,message):
     bot: Nice one! -> bot saves knock-knock joke
     """
     
-def nevermind(state,message):
+def nevermind(state,message,sender):
     return {
         "response":"starting over.",
         "state":[]
     }
 
-def knock_knock(state,message):
+def knock_knock(state,message,sender):
     #return {response:"",state:""}
     try:
         if state[0] != "knock knock":
@@ -93,7 +94,7 @@ def knock_knock(state,message):
     bot: 
     
     """
-def joke(state,message):
+def joke(state,message,sender):
     if len(jokes) == 0:
         return {
             "response":"I don't know any jokes...",
@@ -134,8 +135,16 @@ def joke(state,message):
         "response":None,
         "state":[]
     }
-        
+    
+    """
 
+    """  
+    def appointments():
+        
+        return {
+            "response":None,
+            state:[]
+        }
 commands = {
     "nevermind":nevermind,
     "knock knock":knock_knock,
@@ -144,7 +153,6 @@ commands = {
 
 
 
-
 if __name__ == '__main__':
     msg = ""
     while msg != "exit":
diff --git a/data-example.json b/data-example.json
index f5846bc..67fbcaf 100644
--- a/data-example.json
+++ b/data-example.json
@@ -23,5 +23,7 @@
         "footer": "Brought to you by Nerve\nSee the [code](https://codeberg.org/gabe/Nerve)"
 
     },
-    "users": {}
+    "users": {},
+    "admins": [],
+    "meetings": []
 }
\ No newline at end of file
diff --git a/features.py b/features.py
new file mode 100644
index 0000000..6f4a12f
--- /dev/null
+++ b/features.py
@@ -0,0 +1,82 @@
+import json
+import requests
+"""
+Features 
+This is the main API for executing functions with data
+"""
+
+
+class API:
+    def __init__(self):
+        with open('data.json') as f:
+            self.data = json.loads(f.read())
+            # Only refresh data when a change is made
+            
+    def save(self):
+        with open('data.json','w') as f:
+            f.write(json.dumps(self.data,indent=2))
+            return True
+        return False
+
+    #Administration
+    
+
+    def is_admin(self,handle):
+        #return bool
+        pass
+    def add_admin(self,handle):
+        #return bool
+        if self.is_admin(handle):
+            return True
+        else:
+            self.data["admins"].append(handle)
+            self.save()
+            return True
+    def remove_admin(self,handle):
+        #return bool
+        if self.is_admin(handle):
+            i = self.data["admins"].index(handle)
+            self.data["admins"].pop(i)
+            self.save()
+            return True
+        else:
+            return True
+    
+    #FAQ
+    def get_header(self):
+        return self.data["faq"]["header"]
+    def set_header(self,hdr):
+        #return bool
+        self.data["faq"]["header"] = hdr
+        self.save()
+        return True
+    def get_questions(self):
+        #return questions
+        return self.data["faq"]["questions"]
+    def add_question(self,qtn):
+        #return bool
+        self.data["faq"]["questions"].append(qtn)
+        self.save()
+        return True
+    def remove_question(self,qtn_i):
+        #return bool
+        self.data["faq"]["questions"].pop(qtn_i)
+        self.save()
+        return True
+    def update_question(self,qtn_i,qtn):
+        #return bool
+        self.data["faq"]["questions"][qtn_i]=qtn
+        return True
+    
+    #Meetings
+    def get_meetings():
+        pass
+    def request_meeting():
+        pass
+    def accept_meeting():
+        pass
+    
+    
+if __name__ == '__main__':
+    storeAPI = API()
+    print("Storage loaded.\n",storeAPI.data['username'])
diff --git a/main.py b/main.py
index 9b4da96..b721f89 100644
--- a/main.py
+++ b/main.py
@@ -3,6 +3,8 @@ import nio
 import simplematrixbotlib as botlib
 import commands
 
+#TODO move state functionality entirely to features.py
+
 data = json.loads(open('data.json').read())
 dfaq = data["faq"]
 userstate = data['users']
@@ -34,7 +36,7 @@ async def faq(room, message):
     
     
     state = userstate[message.sender]['state']
-    result = commands.handle_command(state,message.body)
+    result = commands.handle_command(state,message.sender,message.body)
     if  result['response']!= None:
         state = result['state']
         userstate[message.sender]['state'] = result['state']