kopia lustrzana https://github.com/mate-dev/meshtastic-matrix-relay
First work for shortnames
rodzic
aa84c2a77e
commit
e3818a5bc7
30
db_utils.py
30
db_utils.py
|
@ -9,6 +9,9 @@ def initialize_database():
|
|||
cursor.execute(
|
||||
"CREATE TABLE IF NOT EXISTS longnames (meshtastic_id TEXT PRIMARY KEY, longname TEXT)"
|
||||
)
|
||||
cursor.execute(
|
||||
"CREATE TABLE IF NOT EXISTS shortnames (meshtastic_id TEXT PRIMARY KEY, shortname TEXT)"
|
||||
)
|
||||
cursor.execute(
|
||||
"CREATE TABLE IF NOT EXISTS plugin_data (plugin_name TEXT, meshtastic_id TEXT, data TEXT, PRIMARY KEY (plugin_name, meshtastic_id))"
|
||||
)
|
||||
|
@ -81,7 +84,6 @@ def save_longname(meshtastic_id, longname):
|
|||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def update_longnames(nodes):
|
||||
if nodes:
|
||||
for node in nodes.values():
|
||||
|
@ -90,3 +92,29 @@ def update_longnames(nodes):
|
|||
meshtastic_id = user["id"]
|
||||
longname = user.get("longName", "N/A")
|
||||
save_longname(meshtastic_id, longname)
|
||||
|
||||
def get_shortname(meshtastic_id):
|
||||
with sqlite3.connect("meshtastic.sqlite") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT shortname FROM shortnames WHERE meshtastic_id=?", (meshtastic_id,))
|
||||
result = cursor.fetchone()
|
||||
return result[0] if result else None
|
||||
|
||||
def save_shortname(meshtastic_id, shortname):
|
||||
with sqlite3.connect("meshtastic.sqlite") as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"INSERT OR REPLACE INTO shortnames (meshtastic_id, shortname) VALUES (?, ?)",
|
||||
(meshtastic_id, shortname),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def update_shortnames():
|
||||
if meshtastic_interface.nodes:
|
||||
for node in meshtastic_interface.nodes.values():
|
||||
user = node.get("user")
|
||||
if user:
|
||||
meshtastic_id = user["id"]
|
||||
shortname = user.get("shortName", "N/A")
|
||||
save_shortname(meshtastic_id, shortname)
|
|
@ -93,13 +93,14 @@ async def join_matrix_room(matrix_client, room_id_or_alias: str) -> None:
|
|||
|
||||
|
||||
# Send message to the Matrix room
|
||||
async def matrix_relay(room_id, message, longname, meshnet_name):
|
||||
async def matrix_relay(room_id, message, longname, shortname, meshnet_name):
|
||||
matrix_client = await connect_matrix()
|
||||
try:
|
||||
content = {
|
||||
"msgtype": "m.text",
|
||||
"body": message,
|
||||
"meshtastic_longname": longname,
|
||||
"meshtastic_shortname": shortname,
|
||||
"meshtastic_meshnet": meshnet_name,
|
||||
}
|
||||
await asyncio.wait_for(
|
||||
|
@ -156,6 +157,7 @@ async def on_room_message(
|
|||
text = event.body.strip()
|
||||
|
||||
longname = event.source["content"].get("meshtastic_longname")
|
||||
shortname = event.source["content"].get("meshtastic_shortname", None)
|
||||
meshnet_name = event.source["content"].get("meshtastic_meshnet")
|
||||
suppress = event.source["content"].get("mmrelay_suppress")
|
||||
local_meshnet_name = relay_config["meshtastic"]["meshnet_name"]
|
||||
|
@ -168,9 +170,11 @@ async def on_room_message(
|
|||
full_display_name = f"{longname}/{meshnet_name}"
|
||||
if meshnet_name != local_meshnet_name:
|
||||
logger.info(f"Processing message from remote meshnet: {text}")
|
||||
short_longname = longname[:3]
|
||||
short_meshnet_name = meshnet_name[:4]
|
||||
prefix = f"{short_longname}/{short_meshnet_name}: "
|
||||
# If shortname is None, truncate the longname to 3 characters
|
||||
if shortname is None:
|
||||
shortname = longname[:3]
|
||||
prefix = f"{shortname}/{short_meshnet_name}: "
|
||||
text = re.sub(
|
||||
rf"^\[{full_display_name}\]: ", "", text
|
||||
) # Remove the original prefix from the text
|
||||
|
|
Ładowanie…
Reference in New Issue