Browse Source

Merge branch 'channels' of sloum/gab2 into master

channel-indicator
Sloom Sloum Sluom IV 3 years ago committed by Gitea
parent
commit
cdba3d876f
  1. 100
      gab

100
gab

@ -4,9 +4,53 @@ import sys
import time
from datetime import datetime
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"
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
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():
current_user = os.environ.get('USER')
fp = "/home/{}/.gab_chan".format(current_user)
if not os.path.isfile(fp):
return ""
with open(fp, 'r') as chanfile:
channel = chanfile.read().split("\n")[0].strip()
if channel:
channel = "-" + channel
return channel
def blocked_users():
current_user = os.environ.get('USER')
@ -17,10 +61,21 @@ def blocked_users():
with open(fp, 'r') as blockfile:
return blockfile.read().split("\n")
files = [[x, "/home/{}/.gab".format(x)] for x in os.listdir("/home/") if x not in blocked_users()]
log = {}
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
@ -39,7 +94,8 @@ def read_file(user, path, num_lines):
def list_messages(count="5"):
global log
global files
chan = get_chan()
files = get_files(chan)
try:
count = int(count)
@ -57,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))):
@ -64,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")
@ -99,7 +157,7 @@ def add_message(msg):
user = os.environ.get('USER')
timestamp = str(time.time())
output = "{}|{}\n".format(timestamp, msg.strip())
fp = "/home/{}/.gab".format(user)
fp = "/home/{}/.gab{}".format(user, get_chan())
try:
with open(fp, 'r') as original:
data = original.read(12000)
@ -160,6 +218,19 @@ def unblock_user(user_to_unblock):
print("User '{}' has been removed from your block list".format(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(chan))
list_messages()
def parse_command():
args = sys.argv[1:]
@ -176,23 +247,32 @@ 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")
list_channels()
elif len(args) > 2:
print("Expected a channel to join, but received too many arguments")
else:
switch_channel(args[1])
else:
print("Unknown command: {}".format(args[0]))

Loading…
Cancel
Save