added get by id and last added with -1 for clipboard

This commit is contained in:
Djalim Simaila 2023-03-22 18:35:09 +01:00
parent 7eeba7090d
commit 4447468b77
2 changed files with 48 additions and 28 deletions

View File

@ -1,5 +1,6 @@
import pyperclip
import socketio
import reauests
import os
import sys
import subprocess
@ -10,28 +11,17 @@ import zc.lockfile
ip = 'simailadjalim.fr'
port = "9564"
hostname = "WarMachine"
logfilepath = "./logs.txt"
username = "neotaku67"
password = "un bon mot de passe de prefererance mais en sah tant qu'il est hashe ca passe"
"""
This script is a daemon that, on event, send and sync the clipboard with a distant one
"""
def dechiffreCePutainDeMessage(message : str) -> str:
"""
"""
ip = f"http://{ip}:{port}/"
print(ip)
sio = socketio.Client()
sio.connect(ip)
try:
print("[Debug] Trying to lockfile")
zc.lockfile.LockFile('/tmp/notifysync.lock')
except:
print("[Debug] Failed to lock file, another instance running, exiting")
sys.exit()
print("[Debug] Connected to Server .w.")
@ -43,14 +33,14 @@ def sendSystemNotification(title:str,content:str):
subprocess.run(["notify-send",title,content])
@sio.event
def clip(data):
def NotificationUpdate(data):
content = data["content"]
clipCmd = f'echo {content} | xclip'
print(f"[ClipEvent] received data from ")
os.system(clipCmd)
@sio.event
def notify(data):
def ClipboardUpdate(data):
title, content = data["title"], data["content"]
command = f'notify-send "{title}" "{content}"'
print(command)

View File

@ -1,9 +1,9 @@
#!/bin/python
import flask_socketio
import sqlite3
import secrets
import time
from flask import Flask, request, render_template, json, jsonify
import flask_socketio
from flask import Flask, request
# base socket
app = Flask(__name__)
@ -24,7 +24,7 @@ def first_run():
token)")
cur.execute("CREATE TABLE NOTIFICATION(\
id,\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
date,\
machine,\
token,\
@ -32,7 +32,7 @@ def first_run():
content)")
cur.execute("CREATE TABLE CLIPBOARD(\
id,\
id INTEGER PRIMARY KEY AUTOINCREMENT,\
date,\
machine,\
token,\
@ -43,6 +43,7 @@ def first_run():
###############################################################################
@app.route("/user", methods=['POST'])
def auth():
"""
@ -111,11 +112,11 @@ def add_notification():
return {"status": "Error, no user"}, 500
try:
notification = (date, device_name, token, title, content)
cur.execute("INSERT INTO notification VALUES (0,?,?,?,?,?)",
cur.execute("INSERT INTO notification VALUES (null,?,?,?,?,?)",
notification)
con.commit()
con.close()
socketio.emit("update", broadcast=True)
socketio.emit("NotificationUpdate", broadcast=True)
return {"status": "ok"}, 200
except:
return {"status": "error"}, 500
@ -155,10 +156,10 @@ def add_clipboard():
return {"status": "Error, no user"}, 500
try:
clipboard = (date, device_name, token, content)
cur.execute("INSERT INTO CLIPBOARD VALUES (0,?,?,?,?)", clipboard)
cur.execute("INSERT INTO CLIPBOARD VALUES (null,?,?,?,?)", clipboard)
con.commit()
con.close()
socketio.emit("update", broadcast=True)
socketio.emit("ClipboardUpdate", broadcast=True)
return {"status": "ok"}, 200
except:
return {"status": "error"}, 500
@ -168,18 +169,47 @@ def add_clipboard():
def get_clipboard():
"""
Le but de cet app se resume a cette fonction, elle recoit une requete
http et renvoie via le websocket le contenu de la requette a tout les
http et renvoie via le websocket le contenu de la requette a tout les
client.
"""
token = request.values.get("token")
con = sqlite3.connect("database.db")
cur = con.cursor()
clipboard = cur.execute("SELECT content FROM CLIPBOARD WHERE \
clipboard = cur.execute("SELECT content, id FROM CLIPBOARD WHERE \
token=?", (token, )).fetchall()
return {"status": "ok", "notifications": clipboard}, 200
return {"status": "ok", "clipboard": clipboard}, 200
@app.route("/clipboard/<clipid>", methods=['GET'])
def get_clipboard_by_id(clipid):
"""
Le but de cet app se resume a cette fonction, elle recoit une requete
http et renvoie via le websocket le contenu de la requette a tout les
client.
"""
clipid = int(clipid)
token = request.values.get("token")
con = sqlite3.connect("database.db")
cur = con.cursor()
if clipid == -1:
clipboard = cur.execute("SELECT content,id \
FROM CLIPBOARD \
WHERE token=? \
ORDER BY id DESC \
LIMIT 1",
(token,)).fetchall()
else:
clipboard = cur.execute("SELECT content,id FROM CLIPBOARD WHERE \
token=? AND\
id=?",
(token, clipid)).fetchone()
return {"status": "ok", "clipboard": clipboard}, 200
if __name__ == '__main__':
if True:
try:
f=open("database.db","r")
f.close()
except :
first_run()
socketio.run(app, host="0.0.0.0", port=9564)