Merge pull request 'Scan for new messages' (#13) from newscan into master

This seems to be working as intended, so I am going to merge it in now.
master
cmccabe 3 years ago
commit 86d00d381d

@ -15,6 +15,12 @@ If `gab msg` is run without a message then your `$EDITOR` will be opened for you
`gab -m "This is a message"`<br>
`gab -m`
**-n, --new, new**<br>
This will report any channels that have new messages since you last viewed the channel.<br>
This only shows channels you have previously contributed to, i.e. where you have a ~/.gab-channelname file in your home directory.<br>
Put this line in your shell startup file and you will be notified on each login if there are new messages.<br>
`gab -n`
**-l, --list, list**<br>
View the chat log. Pass an integer for the number of recent posts you would like to view (defaults to 5)<br>
`gab -l 20`

74
gab

@ -8,6 +8,7 @@ from subprocess import call
import tempfile
chan_dir = ".config/gab/channels" ## not used yet. todo: move all gab channel logs to this dir
current_user = os.environ.get('USER')
log = {}
help_text = """
@ -25,6 +26,7 @@ flag value
-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.
-n, --new, new Check for new, unread messages.
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
@ -43,7 +45,6 @@ Examples:
title = "\033[1mGAB v2.2\033[0m"
def get_chan():
current_user = os.environ.get('USER')
fp = "/home/{}/.gab_chan".format(current_user)
if not os.path.isfile(fp):
@ -56,7 +57,6 @@ def get_chan():
return channel
def blocked_users():
current_user = os.environ.get('USER')
fp = "/home/{}/.gab_block".format(current_user)
if not os.path.isfile(fp):
@ -68,14 +68,26 @@ def 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() and os.access("/home/"+x, os.R_OK)]
def list_channels():
user_dirs = ["/home/{}".format(x) for x in os.listdir("/home/") if os.access("/home/"+x, os.R_OK)]
def get_user_channels(user_dir):
chans = {"gab"}
for y in os.listdir(user_dir):
if len(y) > 5 and y[:5] == ".gab-" and y[-1] != "!":
chans.add(y[5:])
return chans
def get_chan_list():
chans = {"gab"}
for y in os.listdir("/home/" + current_user):
if len(y) > 5 and y[:5] == ".gab-" and y[-1] != "!":
chans.add(y[5:])
return chans
def list_channels():
chans = get_chan_list()
chan = get_chan()
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:
@ -85,9 +97,12 @@ def list_channels():
x = x + " *"
print(x)
def read_file(user, path, num_lines):
global log
if os.path.isfile(path):
if "/home/" + current_user + "/" in path:
os.utime(path, None) ## update file's timestamp on each read
with open(path, "r") as f:
for line in range(num_lines):
msg = f.readline().split("|", 1)
@ -134,8 +149,8 @@ def list_messages(count="5"):
print("There are no messages to display")
def diff_time_from_now(t):
diff = time.time() - t
def diff_time_from_now(t, n=time.time()):
diff = n - t
diff = int(diff)
out = ""
@ -162,10 +177,9 @@ def diff_time_from_now(t):
def add_message(msg):
user = os.environ.get('USER')
timestamp = str(time.time())
output = "{}|{}\n".format(timestamp, msg.strip())
fp = "/home/{}/.gab{}".format(user, get_chan())
fp = "/home/{}/.gab{}".format(current_user, get_chan())
try:
with open(fp, 'r') as original:
data = original.read(12000)
@ -190,7 +204,6 @@ def add_message(msg):
def block_user(user_to_block):
current_user = os.environ.get('USER')
fp = "/home/{}/.gab_block".format(current_user)
block_fp = "/home/{}/".format(user_to_block)
if not os.path.isdir(block_fp):
@ -206,7 +219,6 @@ def block_user(user_to_block):
def unblock_user(user_to_unblock):
current_user = os.environ.get('USER')
fp = "/home/{}/.gab_block".format(current_user)
unblock_fp = "/home/{}/".format(user_to_unblock)
@ -230,7 +242,7 @@ 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:
@ -240,6 +252,35 @@ def switch_channel(newchan):
list_messages()
def new_scan(silent=True):
unread_chans = []
chans = get_user_channels("/home/" + current_user)
for c in chans:
my_timestamp = chan_timestamp = 0.0
if c == "gab":
files = get_files("")
else:
files = get_files("-" + c)
for f in files:
if os.path.isfile(f[1]):
with open(f[1]) as fh:
timestamp = float(fh.readline().split("|")[0])
if timestamp > float(chan_timestamp):
chan_timestamp = timestamp
if f[0] == current_user:
atime= time.ctime(os.stat(f[1]).st_atime)
my_timestamp = time.mktime(datetime.strptime(atime, "%a %b %d %H:%M:%S %Y").timetuple())
if my_timestamp - chan_timestamp < 0:
unread_chans.append(c)
if len(unread_chans):
print("New gab messages in: " + ", ".join(unread_chans))
elif silent == False:
print("gab has no unread messages.")
def parse_command():
args = sys.argv[1:]
if not len(args):
@ -251,6 +292,8 @@ def parse_command():
list_messages(args[1])
else:
print("Invalid command input to 'list'")
elif args[0] in ["new", "--new", "-n"]:
new_scan()
elif args[0] in ["-m", "--msg", "msg"]:
if len(args) < 2:
# code from https://stackoverflow.com/a/6309753
@ -298,4 +341,3 @@ def parse_command():
if __name__ == '__main__':
parse_command()

Loading…
Cancel
Save