Reworks the way items are listed and improves keeping file permissions on chat logs

action-support
sloumdrone 4 years ago
parent a3a3b7d87c
commit db7fbd9b5a

3
.gitignore vendored

@ -0,0 +1,3 @@
.oneliner
oneliner
.DS*

29
gab

@ -9,11 +9,12 @@ title = "\033[1mGAB v2.0\033[0m"
files = [[x, "/home/{}/.gab".format(x)] for x in os.listdir("/home/")]
log = {}
def read_file(user, path):
def read_file(user, path, num_lines):
global log
if os.path.isfile(path):
with open(path, "r") as f:
for line in f:
lines = f.read().split("\n")[-num_lines:]
for line in lines:
msg = line.split("|", 1)
if len(msg) == 2:
log[msg[0].strip()] = "\033[7m {} \033[0m {}".format(user, msg[1].strip())
@ -21,11 +22,17 @@ def read_file(user, path):
def list_messages(count="5"):
global log
global files
try:
count = int(count)
except ValueError:
print("Error parsing list number, using default: 5")
count = 5
for x in files:
read_file(x[0], x[1], count)
sorted_keys = list(log.keys())
if len(sorted_keys) > 0:
sorted_keys.sort(reverse=True)
@ -61,33 +68,33 @@ def diff_time_from_now(t):
return out
def generate_output():
global files
for x in files:
read_file(x[0], x[1])
def add_message(msg):
user = os.environ.get('USER')
timestamp = str(time.time())
fp = "/home/{}/.gab".format(user)
try:
with open("/home/{}/.gab".format(user), "a") as f:
with open(fp, "a") as f:
f.write("{}|{}\n".format(timestamp, msg.strip()))
print("Successfully added text to chatlog")
except:
print("Error adding text to chatlog")
try:
os.chmod(fp, 0o644)
except PermissionError:
print("You do not have permission to modify the chatlog")
except FileNotFoundError:
print("The chatlog does not exist")
def parse_command():
args = sys.argv[1:]
if not len(args):
generate_output()
list_messages()
elif args[0] in ["-h", "--help", "help"]:
print(help_text)
elif args[0] in ["list", "--list", "-l"]:
if len(args) == 2:
generate_output()
list_messages(args[1])
else:
print("Invalid command input to 'list'")

Loading…
Cancel
Save