]> Repos - abc-messenger/commitdiff
db
authorAndrew Gundersen <gundersena@xavier.edu>
Mon, 21 Dec 2020 18:00:32 +0000 (12:00 -0600)
committerAndrew Gundersen <gundersena@xavier.edu>
Mon, 21 Dec 2020 18:00:32 +0000 (12:00 -0600)
__pycache__/agent.cpython-38.pyc
__pycache__/db.cpython-38.pyc [new file with mode: 0644]
__pycache__/mail.cpython-38.pyc
__pycache__/message.cpython-38.pyc
__pycache__/session.cpython-38.pyc
agent.py
db.py [new file with mode: 0644]
mail.py
message.py
session.py

index e0a1799db4ca3f40d59a7aee0d7f3d0449e448bf..8fdd51c9e7be8b5a0ab8412a3b3a116f7676a72a 100644 (file)
Binary files a/__pycache__/agent.cpython-38.pyc and b/__pycache__/agent.cpython-38.pyc differ
diff --git a/__pycache__/db.cpython-38.pyc b/__pycache__/db.cpython-38.pyc
new file mode 100644 (file)
index 0000000..0050954
Binary files /dev/null and b/__pycache__/db.cpython-38.pyc differ
index 8c36803d8715be1039d470665db7d04a08ce119a..39159837f5deebec8a7cae3296bffb2262dd48c5 100644 (file)
Binary files a/__pycache__/mail.cpython-38.pyc and b/__pycache__/mail.cpython-38.pyc differ
index 2380ee2de6a5b1526a1ccf1dd99ae6ba803b93ac..c458305b80c4e6e0923a355acda1129932ff882b 100644 (file)
Binary files a/__pycache__/message.cpython-38.pyc and b/__pycache__/message.cpython-38.pyc differ
index 61f4152d83645ed2d05f4ffa34366514e9903f9f..bf6a6503ef5344a75aca5a19f17ad5e842dd521c 100644 (file)
Binary files a/__pycache__/session.cpython-38.pyc and b/__pycache__/session.cpython-38.pyc differ
index 2d0fbc34fd289d78700536326a1eaaaef92c7197..1ff4d86a25a42481dd98ce1df21afbe89ab6784f 100644 (file)
--- a/agent.py
+++ b/agent.py
@@ -16,12 +16,12 @@ class Agent:
         self.service = None
         self.client_connection = connection
 
-    async def recv_agent_intent(self):
+    async def recv(self):
         package = await self.client_connection.recv()
         jpackage = self.__decode(package)
         return jpackage
 
-    async def send_agent_intent(self, jpackage):
+    async def send(self, jpackage):
         package = self.__encode(jpackage)
         await self.client_connection.send(package)
 
diff --git a/db.py b/db.py
new file mode 100644 (file)
index 0000000..9b72d8e
--- /dev/null
+++ b/db.py
@@ -0,0 +1,26 @@
+# db.py
+
+import mysql.connector
+
+# database settings
+DBUSER = "remoteAccess"
+DBPASSWORD = "remoteAccess2020"
+DBHOST = "75.86.178.105"
+
+# People Schema
+# -------------
+
+people_cnx = mysql.connector.connect(user=DBUSER, password=DBPASSWORD, host=DBHOST, database="People")
+people_cursor = people_cnx.cursor(buffered=True)
+
+# Verify that an email is a valid crimata_id.
+def verify(crimata_id, get=False):
+    query = (f"SELECT * FROM People.users WHERE crimata_id='{crimata_id}';")
+
+    people_cursor.execute(query)
+
+    verified = False
+    for (_, crimata_id, _) in people_cursor:
+        verified = True
+
+    return verified
\ No newline at end of file
diff --git a/mail.py b/mail.py
index 7ca46d73107cc43fe244499d43a5ad4ae9f63876..cb5b6f9172825820017799516c693a5818062f09 100644 (file)
--- a/mail.py
+++ b/mail.py
@@ -20,25 +20,25 @@ class Mail:
                return intent
 
        # Send intent.
-       def mailbox_send(self, jpackage):
+       async def mailbox_send(self, jpackage):
                mail = self.__encode(jpackage)
                print(f"Putting message into outbox {self.crimata_id}")
                mailroom.put_mail(self.crimata_id, mail)
+               await self.send(1) # agent confirmation.
 
        def __encode(self, jpackage) -> typs.Mail:
                j = jpackage
                owner = self.crimata_id
                target = j.get("target")
-               print(target)
                text = j.get("text")
                audio = j.get("audio")
                mail = typs.Mail(owner, target, text, audio)
                return mail
 
-       @staticmethod
-       def __decode(mail: typs.Mail):
+       def __decode(self, mail: typs.Mail):
                jpackage = {
                        "owner": mail.owner,
+                       "target": self.crimata_id,
                        "text": mail.text, #dev only
                        "audio": mail.audio
                }
index 9f803b55a9dc41ad0109be012b9ef9c9c3ba394d..73fc394e4131f7e46bc7e591316467696e007f1c 100644 (file)
@@ -33,7 +33,7 @@ async def messenger():
 
         # Put the mailbox back
         mailroom.que.put(mailbox)
-        await asyncio.sleep(1)
+        await asyncio.sleep(0.1)
 
 async def run():
     await messenger()
\ No newline at end of file
index 2e1f806314d8af2f0c8696b0a9086553f3cd7c0e..ec4f0b93d7c9734bd6dc2386114e007d846dddb8 100644 (file)
@@ -4,6 +4,7 @@ import time
 import asyncio
 import websockets
 
+import db
 import mail
 import agent
 
@@ -29,15 +30,23 @@ class Session(agent.Agent, mail.Mail):
 
             try:
                 jpackage = await asyncio.wait_for(
-                    self.recv_agent_intent(), timeout=0.2)
+                    self.recv(), timeout=0.1)
             except asyncio.TimeoutError:
                 continue
             except websockets.exceptions.ConnectionClosed:
                 await self.shutdown()
-                continue                
+                continue   
+
+            # Check if target is verified.
+            verified = db.verify(jpackage.get("target"))
+
+            # Send error code back to agent.
+            if not verified:
+                await self.send(2)
+                continue
 
             # Call corresponding endpoint.
-            self.mailbox_send(jpackage)
+            await self.mailbox_send(jpackage)
 
     # Send mail back to agent.
     #! Pulling message from backend.
@@ -51,13 +60,13 @@ class Session(agent.Agent, mail.Mail):
             # Try recv.
             try:
                 jpackage = await asyncio.wait_for(
-                    self.mailbox_recv(), timeout=0.2)
+                    self.mailbox_recv(), timeout=0.1)
             except asyncio.TimeoutError:
                 continue
 
             # Send message to Agent.
             print("Sending message")
-            await self.send_agent_intent(jpackage)
+            await self.send(jpackage)
 
     # Shutdown protocol.
     async def shutdown(self):