Reworking of some of cmccabe's ideas to mesh with some of sloum's ideas

channels-redux
sloum 3 years ago
parent a61828cbea
commit df938b8fef

81
gab

@ -7,7 +7,36 @@ from datetime import datetime
chan_dir = ".config/gab/channels" ## not used yet. todo: move all gab channel logs to this dir
log = {}
help_text = "GAB - A simple chat interface\n\nsyntax: gab [flag] [value]\n\nflag value\n-------------------- ---------------\n-h, --help, help None\n-m, --msg, msg Quoted text with the msg being added to chat\n-l, --list, list An integer representing the number of rows you\'d like to view, default 5\n-b, --block, block A username to block/ignore\n-u, --unblock, unblock A username to unblock/unignore\n-c, --channel, channel Name of another channel you would like to load"
help_text = """
GAB - A simple chat interface
syntax: gab [flag] [value]
flag value
-------------------- ---------------
-h, --help, help
-m, --msg, msg Quoted text with the msg being added to chat
-l, --list, list An integer representing the number of rows you'd like
to view, default 5
-b, --block, block A username to block/ignore
-u, --unblock, unblock A username to unblock/unignore
-c, --channel, channel Name of the channel you would like to join or create.
An empty value lists the available channels.
Channels names ending in ! are private. They can be joined by anyone that
knows they exist, but will not be listed in the channel listings. To return
to the main/default channel, simply switch channels to 'gab':
gab channel gab
Examples:
gab msg "Good afternoon!"
gab list 20
gab block annoyingperson
gab channel my-secret-channel!
gab channel a-public-channel
gab unblock somedude
"""
title = "\033[1mGAB v2.1\033[0m"
def get_chan():
@ -15,10 +44,13 @@ def get_chan():
fp = "/home/{}/.gab_chan".format(current_user)
if not os.path.isfile(fp):
return "gab"
return ""
with open(fp, 'r') as chanfile:
return chanfile.read().split("\n")[0]
channel = chanfile.read().split("\n")[0].strip()
if channel:
channel = "-" + channel
return channel
def blocked_users():
current_user = os.environ.get('USER')
@ -30,8 +62,20 @@ def blocked_users():
with open(fp, 'r') as blockfile:
return blockfile.read().split("\n")
def get_files():
return [[x, "/home/{}/.{}".format(x, get_chan())] for x in os.listdir("/home/") if x not in blocked_users()]
def get_files(channel):
return [[x, "/home/{}/.gab{}".format(x, channel)] for x in os.listdir("/home/") if x not in blocked_users()]
def list_channels():
user_dirs = ["/home/{}".format(x) for x in os.listdir("/home/")]
chans = {"gab"}
for x in user_dirs:
for y in os.listdir(x):
if len(y) > 5 and y[:5] == ".gab-" and y[-1] != "!":
chans.add(y[5:])
print(title)
print("\n\033[1mAvailable channels\033[0m:")
for x in chans:
print(x)
def read_file(user, path, num_lines):
global log
@ -50,7 +94,8 @@ def read_file(user, path, num_lines):
def list_messages(count="5"):
global log
files = get_files()
chan = get_chan()
files = get_files(chan)
try:
count = int(count)
@ -68,6 +113,7 @@ def list_messages(count="5"):
sorted_keys = sorted_keys[:min(len(sorted_keys), count)]
sorted_keys.sort(reverse=False)
print(title)
print("Current channel: \033[1m{}\033[0m".format(chan[1:] if chan else "gab"))
print("Last message: {}".format(diff_time_from_now(last_time)))
print("\n- - -")
for key in range(min(count, len(sorted_keys))):
@ -75,6 +121,7 @@ def list_messages(count="5"):
print("- - -")
else:
print(title)
print("Current channel: \033[1m{}\033[0m".format(chan[1:] if chan else "gab"))
print()
print("There are no messages to display")
@ -110,7 +157,7 @@ def add_message(msg):
user = os.environ.get('USER')
timestamp = str(time.time())
output = "{}|{}\n".format(timestamp, msg.strip())
fp = "/home/{}/.{}".format(user, get_chan())
fp = "/home/{}/.gab{}".format(user, get_chan())
try:
with open(fp, 'r') as original:
data = original.read(12000)
@ -172,13 +219,16 @@ def unblock_user(user_to_unblock):
def switch_channel(newchan):
chan = newchan
if newchan == "gab":
newchan = ""
current_user = os.environ.get('USER')
fp = "/home/{}/.gab_chan".format(current_user)
with open(fp, 'w') as chanfile:
chanfile.write(newchan)
print("You are now viewing the '{}' channel.".format(newchan))
print("You are now viewing the '{}' channel.".format(chan))
list_messages()
@ -197,29 +247,30 @@ def parse_command():
if len(args) < 2:
print("Expected a message, but one was not received")
elif len(args) > 2:
print("Received too many arguments")
print("Expected a message, but received too many arguments. Did you put your message in quotes?")
else:
add_message(args[1])
elif args[0] in ["-b", "--block", "block"]:
if len(args) < 2:
print("Expected a user to block, but one was not received")
elif len(args) > 2:
print("Received too many arguments")
print("Expected a user to block, but received too many arguments")
else:
block_user(args[1])
elif args[0] in ["-u", "--unblock", "unblock"]:
if len(args) < 2:
print("Expected a user to block, but one was not received")
print("Expected a user to unblock, but one was not received")
elif len(args) > 2:
print("Received too many arguments")
print("Expected a user to unblock, but received too many arguments")
else:
unblock_user(args[1])
elif args[0] in ["-c", "--channel", "channel"]:
if len(args) < 2:
print("Returning to default channel.")
switch_channel("gab")
# print("Returning to default channel.")
# switch_channel("gab")
list_channels()
elif len(args) > 2:
print("Expected a channel to join, but one was not received.")
print("Expected a channel to join, but received too many arguments")
else:
switch_channel(args[1])
else:

Loading…
Cancel
Save