forked from TheCommsChannel/TC2-BBS-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_operations.py
More file actions
149 lines (129 loc) · 5.65 KB
/
db_operations.py
File metadata and controls
149 lines (129 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import logging
import sqlite3
import threading
import uuid
from datetime import datetime
from utils import (
send_bulletin_to_bbs_nodes,
send_delete_bulletin_to_bbs_nodes,
send_delete_mail_to_bbs_nodes,
send_mail_to_bbs_nodes, send_message
)
thread_local = threading.local()
def get_db_connection():
if not hasattr(thread_local, 'connection'):
thread_local.connection = sqlite3.connect('bulletins.db')
return thread_local.connection
def initialize_database():
conn = get_db_connection()
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS bulletins (
id INTEGER PRIMARY KEY AUTOINCREMENT,
board TEXT NOT NULL,
sender_short_name TEXT NOT NULL,
date TEXT NOT NULL,
subject TEXT NOT NULL,
content TEXT NOT NULL,
unique_id TEXT NOT NULL
)''')
c.execute('''CREATE TABLE IF NOT EXISTS mail (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sender TEXT NOT NULL,
sender_short_name TEXT NOT NULL,
recipient TEXT NOT NULL,
date TEXT NOT NULL,
subject TEXT NOT NULL,
content TEXT NOT NULL,
unique_id TEXT NOT NULL
);''')
c.execute('''CREATE TABLE IF NOT EXISTS channels (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
url TEXT NOT NULL
);''')
conn.commit()
print("Database schema initialized.")
def add_channel(name, url):
conn = get_db_connection()
c = conn.cursor()
c.execute("INSERT INTO channels (name, url) VALUES (?, ?)", (name, url))
conn.commit()
def get_channels():
conn = get_db_connection()
c = conn.cursor()
c.execute("SELECT name, url FROM channels")
return c.fetchall()
def add_bulletin(board, sender_short_name, subject, content, bbs_nodes, interface, unique_id=None):
conn = get_db_connection()
c = conn.cursor()
date = datetime.now().strftime('%m/%d/%Y %H:%M')
if not unique_id:
unique_id = str(uuid.uuid4())
c.execute(
"INSERT INTO bulletins (board, sender_short_name, date, subject, content, unique_id) VALUES (?, ?, ?, ?, ?, ?)",
(board, sender_short_name, date, subject, content, unique_id))
conn.commit()
if bbs_nodes and interface:
send_bulletin_to_bbs_nodes(board, sender_short_name, subject, content, unique_id, bbs_nodes, interface)
# New logic to send group chat notification for urgent bulletins
if board.lower() == "urgent":
group_chat_id = 4294967295 # Default group chat ID (0xFFFFFFFF)
notification_message = f"💥NEW URGENT BULLETIN💥\nFrom: {sender_short_name}\nTitle: {subject}"
send_message(notification_message, group_chat_id, interface)
return unique_id
def get_bulletins(board):
conn = get_db_connection()
c = conn.cursor()
c.execute("SELECT id, subject, sender_short_name, date, unique_id FROM bulletins WHERE board = ?", (board,))
return c.fetchall()
def get_bulletin_content(bulletin_id):
conn = get_db_connection()
c = conn.cursor()
c.execute("SELECT sender_short_name, date, subject, content, unique_id FROM bulletins WHERE id = ?", (bulletin_id,))
return c.fetchone()
def delete_bulletin(bulletin_id, bbs_nodes, interface):
conn = get_db_connection()
c = conn.cursor()
c.execute("DELETE FROM bulletins WHERE id = ?", (bulletin_id,))
conn.commit()
send_delete_bulletin_to_bbs_nodes(bulletin_id, bbs_nodes, interface)
def add_mail(sender_id, sender_short_name, recipient_id, subject, content, bbs_nodes, interface, unique_id=None):
conn = get_db_connection()
c = conn.cursor()
date = datetime.now().strftime('%m/%d/%Y %H:%M')
if not unique_id:
unique_id = str(uuid.uuid4())
c.execute("INSERT INTO mail (sender, sender_short_name, recipient, date, subject, content, unique_id) VALUES (?, ?, ?, ?, ?, ?, ?)",
(sender_id, sender_short_name, recipient_id, date, subject, content, unique_id))
conn.commit()
if bbs_nodes and interface:
send_mail_to_bbs_nodes(sender_id, sender_short_name, recipient_id, subject, content, unique_id, bbs_nodes, interface)
return unique_id
def get_mail(recipient_id):
conn = get_db_connection()
c = conn.cursor()
c.execute("SELECT id, sender_short_name, subject, date, unique_id FROM mail WHERE recipient = ?", (recipient_id,))
return c.fetchall()
def get_mail_content(mail_id):
conn = get_db_connection()
c = conn.cursor()
c.execute("SELECT sender_short_name, date, subject, content, unique_id FROM mail WHERE id = ?", (mail_id,))
return c.fetchone()
def delete_mail(unique_id, bbs_nodes, interface):
logging.info(f"Attempting to delete mail with unique_id: {unique_id}")
conn = get_db_connection()
c = conn.cursor()
try:
c.execute("SELECT unique_id FROM mail WHERE unique_id = ?", (unique_id,))
result = c.fetchone()
logging.debug(f"Fetch result for unique_id {unique_id}: {result}")
if result is None:
logging.error(f"No mail found with unique_id: {unique_id}")
return # Early exit if no matching mail found
c.execute("DELETE FROM mail WHERE unique_id = ?", (unique_id,))
conn.commit()
send_delete_mail_to_bbs_nodes(unique_id, bbs_nodes, interface)
logging.info(f"Mail with unique_id: {unique_id} deleted and sync message sent.")
except Exception as e:
logging.error(f"Error deleting mail with unique_id {unique_id}: {e}")
raise