forked from Dogo6647/r2chat
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
import tkinter as tk
|
|
from tkinter import scrolledtext
|
|
import os
|
|
import time
|
|
import getpass
|
|
from json import loads
|
|
|
|
CHAT_FILE = "C:/Users/Public/r2chat/chat.txt"
|
|
REFRESH_INTERVAL = 1000
|
|
|
|
BORDER_WIDTH = 3
|
|
THEMES = [['white','white','white','black','Basic'],['#000707','#002727','#001717','white','Aqua']]
|
|
|
|
username = getpass.getuser()
|
|
CONFIG_FILE = f'C:/r2chat/legacy_config/{username}.r2cc'
|
|
|
|
if not os.path.exists(CONFIG_FILE):
|
|
CFILE_C=open(CONFIG_FILE, "a")
|
|
CFILE_C.write('0\n is now online.\n is now offline.')
|
|
CFILE_C.close()
|
|
|
|
def conf_line(n):
|
|
return((open(CONFIG_FILE).read()).split('\n')[n])
|
|
|
|
THEME = int(conf_line(0))
|
|
JOIN_MESSAGE = conf_line(1)
|
|
LEAVE_MESSAGE = conf_line(2)
|
|
|
|
class r2chat:
|
|
def __init__(self, root):
|
|
self.root = root
|
|
self.root.title(f"r2chat Legacy :: {username}")
|
|
|
|
self.last_size = 0
|
|
|
|
self.chat_area = scrolledtext.ScrolledText(root, wrap='word', font=("Lucida Console", 11), bg=THEMES[THEME][1], fg=THEMES[THEME][3], relief='groove', borderwidth=BORDER_WIDTH)
|
|
self.chat_area.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
|
|
self.chat_area.bind("<Key>", lambda e: "break")
|
|
|
|
self.entry = tk.Entry(root, font=("Lucida Console", 11), bg=THEMES[THEME][2], fg=THEMES[THEME][3], relief='groove', borderwidth=BORDER_WIDTH)
|
|
self.entry.pack(padx=10, pady=(0, 10), fill=tk.X)
|
|
self.entry.bind("<Return>", self.send_message)
|
|
|
|
if not os.path.exists(CHAT_FILE):
|
|
open(CHAT_FILE, "a").close()
|
|
|
|
self.update_chat()
|
|
self.write_system_message(f"{username}{JOIN_MESSAGE}")
|
|
|
|
def on_close(self):
|
|
self.write_system_message(f"{username}{LEAVE_MESSAGE}")
|
|
self.root.destroy()
|
|
|
|
def write_system_message(self, msg):
|
|
line = f"---- {msg} ----\n"
|
|
with open(CHAT_FILE, "a", encoding="utf-8") as f:
|
|
f.write(line)
|
|
|
|
def send_message(self, event=None):
|
|
msg = self.entry.get().strip()
|
|
if msg:
|
|
timestamp = time.strftime("%y/%m/%d %H:%M")
|
|
line = f"[{timestamp}] {username} (legacy): {msg}\n"
|
|
try:
|
|
with open(CHAT_FILE, "a", encoding="utf-8") as f:
|
|
f.write(line)
|
|
except Exception as e:
|
|
print("oh noes! ", e)
|
|
|
|
self.entry.delete(0, tk.END)
|
|
|
|
def update_chat(self):
|
|
try:
|
|
current_size = os.path.getsize(CHAT_FILE)
|
|
|
|
if current_size > self.last_size:
|
|
with open(CHAT_FILE, "r", encoding="utf-8") as f:
|
|
f.seek(self.last_size)
|
|
new_data = f.read()
|
|
self.last_size = current_size
|
|
self.chat_area.config(state='normal')
|
|
self.chat_area.insert(tk.END, new_data)
|
|
self.chat_area.config(state='disabled')
|
|
self.chat_area.yview(tk.END)
|
|
|
|
except Exception as e:
|
|
print("oh noes! ", e)
|
|
|
|
self.root.after(REFRESH_INTERVAL, self.update_chat)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
root = tk.Tk()
|
|
app = r2chat(root)
|
|
root.geometry("500x520")
|
|
root.minsize(360, 520)
|
|
root.attributes('-topmost', True)
|
|
root.protocol("WM_DELETE_WINDOW", app.on_close)
|
|
root.configure(bg=THEMES[THEME][0])
|
|
root.mainloop()
|