-#!/usr/bin/env perl
+#!/usr/bin/env python3
# Copyright (C) 2019 Alex Schroeder <alex@gnu.org>
# This program is free software: you can redistribute it and/or modify it under
# with this program. If not, see <https://www.gnu.org/licenses/>.
import diaspy
+import subprocess
import argparse
+import shutil
import cmd
import sys
import os
# Command abbreviations
_ABBREVS = {
"q": "quit",
+ "p": "print",
+ "c": "comments",
}
-# Config file finder
+_RC_PATHS = (
+ "~/.config/jan-pona-mute/login",
+ "~/.config/.jan-pona-mute",
+ "~/.jan-pona-mute"
+)
+
+_PAGERS = (
+ "mdcat",
+ "fold",
+ "cat"
+)
+
+# Init file finder
def get_rcfile():
- rc_paths = ("~/.config/jan-pona-mute/login", "~/.config/.jan-pona-mute", "~/.jan-pona-mute")
- for rc_path in rc_paths:
+ for rc_path in _RC_PATHS:
rcfile = os.path.expanduser(rc_path)
if os.path.exists(rcfile):
return rcfile
return None
+# Pager finder
+def get_pager():
+ for cmd in _PAGERS:
+ pager = shutil.which(cmd)
+ if pager != None:
+ return pager
+
class DiasporaClient(cmd.Cmd):
prompt = "\x1b[38;5;255m" + "> " + "\x1b[0m"
- intro = "Welcome to Diaspora!"
+ intro = "Welcome to Diaspora! Use the intro command for a quick introduction."
+
+ header_format = "\x1b[1;38;5;255m" + "%s" + "\x1b[0m"
username = None
pod = None
password = None
+ pager = None
connection = None
- notifications = None
+ notifications = []
+ index = None
+ post = None
+ post_cache = {} # key is self.post.uid
+
+ undo = []
+
# dict mapping user ids to usernames
users = {}
self.users[guid] = user.handle()
return self.users[guid]
- def __init__(self, account):
- cmd.Cmd.__init__(self)
- self.onecmd("account " + account)
+ def do_intro(self, line):
+ """Start here."""
+ print("""
+Use the account and password commands to set up your connection, then
+use the login command to log in. If everything works as intended, use
+the save command to save these commands to an init file.
+
+Once you've listed things such as notifications, enter a number to
+select the corresponding item. Use the print command to see more.
+""")
def do_account(self, account):
"""Set username and pod using the format username@pod."""
try:
(self.username, self.pod) = account.split('@')
+ print("Username and pod set: %s@%s" % (self.username, self.pod))
except ValueError:
print("The account must contain an @ character, e.g. kensanata@pluspora.com.")
print("Use the account comand to set the account.")
def do_info(self, line):
"""Get some info about things. By default, it is info about yourself."""
print("Info about yourself:")
- print("Username: " + self.username)
- print("Password: " + ("unset" if self.password == None else "set"))
- print("Pod: " + self.pod)
+ print("Username: %s" % self.username)
+ print("Password: %s" % ("None" if self.password == None else "set"))
+ print("Pod: %s" % self.pod)
+ print("Pager: %s" % self.pager)
def do_password(self, password):
"""Set the password."""
self.password = (None if self.password == "" else password)
- print("Password " + ("unset" if self.password == "" else "set"))
+ print("Password %s" % ("unset" if self.password == "" else "set"))
+
+ def do_save(self, line):
+ """Save your login information to the init file."""
+ if self.username == None or self.pod == None:
+ print("Use the account command to set username and pod")
+ elif self.password == None:
+ print("Use the password command")
+ else:
+ rcfile = get_rcfile()
+ if rcfile == None:
+ rfile = first(_RC_PATHS)
+ seen_account = False
+ seen_password = False
+ seen_login = False
+ changed = False
+ file = []
+ with open(rcfile, "r") as fp:
+ for line in fp:
+ words = line.strip().split()
+ if words:
+ if words[0] == "account":
+ seen_account = True
+ account = "%s@%s" % (self.username, self.pod)
+ if len(words) > 1 and words[1] != account:
+ line = "account %s\n" % account
+ changed = True
+ elif words[0] == "password":
+ seen_password = True
+ if len(words) > 1 and words[1] != self.password:
+ line = "password %s\n" % self.password
+ changed = True
+ elif words[0] == "login":
+ if seen_account and seen_password:
+ seen_login = True
+ else:
+ # skip login if no account or no password given
+ line = None
+ changed = True
+ if line != None:
+ file.append(line)
+ if not seen_account:
+ file.append("account %s@%s\n" % (self.username, self.pod))
+ changed = True
+ if not seen_password:
+ file.append("password %s\n" % self.password)
+ changed = True
+ if not seen_login:
+ file.append("login\n")
+ changed = True
+ if changed:
+ if os.path.isfile(rcfile):
+ os.rename(rcfile, rcfile + "~")
+ if not os.path.isdir(os.path.dirname(rcfile)):
+ os.makedirs(os.path.dirname(rcfile))
+ with open(rcfile, "w") as fp:
+ fp.write("".join(file))
+ print("Wrote %s" % rcfile)
+ else:
+ print("No changes made, %s left unchanged" % rcfile)
def do_login(self, line):
"""Login."""
if line != "":
- self.onecmd("account " + line)
- if self.password == "":
- print("Use the password command to set a password for " + self.username)
- return
- self.connection = diaspy.connection.Connection(
- pod = "https://" + self.pod, username = self.username, password = self.password)
- self.connection.login()
- self.onecmd("notifications")
+ self.onecmd("account %s" % line)
+ if self.username == None or self.pod == None:
+ print("Use the account command to set username and pod")
+ elif self.password == None:
+ print("Use the password command")
+ else:
+ self.connection = diaspy.connection.Connection(
+ pod = "https://%s" % self.pod, username = self.username, password = self.password)
+ try:
+ self.connection.login()
+ self.onecmd("notifications")
+ except diaspy.errors.LoginError:
+ print("Login failed")
+
+ def do_pager(self, pager):
+ """Set the pager, e.g. to cat"""
+ self.pager = pager
+ print("Pager set: %s" % self.pager)
+
+ def header(self, line):
+ """Wrap line in header format."""
+ return self.header_format % line
def do_notifications(self, line):
- """Show notifications."""
+ """List notifications."""
if self.connection == None:
print("Use the login command, first.")
return
self.notifications = diaspy.notifications.Notifications(self.connection).last()
for n, notification in enumerate(self.notifications):
- print("%2d. %s %s" % (n+1, notification.when(), notification))
+ print(self.header("%2d. %s %s") % (n+1, notification.when(), notification))
+ print("Enter a number to select the notification.")
### The end!
def do_quit(self, *args):
expanded = line.replace(first_word, full_cmd, 1)
return self.onecmd(expanded)
+ try:
+ n = int(line.strip())
+ # Finally, see if it's a notification and show it
+ self.do_show(n)
+ except ValueError:
+ print("Use the help command to show available commands")
+
+ def do_show(self, n):
+ """Show the post given by the index number.
+The index number must refer to the current list of notifications."""
+ try:
+ notification = self.notifications[n-1]
+ self.index = n
+ except IndexError:
+ print("Index too high!")
+ return
+
+ self.show(notification)
+
+ print("Loading...")
+ self.post = diaspy.models.Post(connection = self.connection, id = notification.about())
+ self.post_cache[self.post.guid] = self.post
+
+ print()
+ self.show(self.post)
+
+ def show(self, item):
+ """Show the current item."""
+ if self.pager:
+ subprocess.run(self.pager, input = repr(item), text = True)
+ else:
+ print(repr(item))
+
+ def do_comments(self, line):
+ """Show the comments for the current post."""
+ if self.post == None:
+ print("Use the show command to show a post, first.")
+ return
+ if self.post.comments == None:
+ print("The current post has no comments.")
+ return
+
+ n = 5
+ comments = self.post.comments
+
+ if line == "all":
+ n = None
+ elif line != "":
+ try:
+ n = int(line.strip())
+ except ValueError:
+ print("The comments command takes a number as its argument, or 'all'")
+ print("The default is to show the last 5 comments")
+ return
+
+ if n != None:
+ comments = comments[-n:]
+
+ for n, comment in enumerate(comments):
+ print()
+ print(self.header("%2d. %s %s") % (n+1, comment.when(), comment.author()))
+ print()
+ self.show(comment)
+
+ def do_comment(self, line):
+ """Leave a comment on the current post."""
+ if self.post == None:
+ print("Use the show command to show a post, first.")
+ return
+ comment = self.post.comment(line)
+ self.post.comments.add(comment)
+ self.undo.append("delete comment %s from %s" % (comment.id, self.post.guid))
+ print("Comment posted.")
+
+ def do_delete(self, line):
+ """Delete a comment."""
+ words = line.strip().split()
+ if words:
+ if words[0] == "comment":
+ if self.post == None:
+ print("Use the show command to show a post, first.")
+ return
+ if len(words) != 4:
+ print("Deleting a comment requires a comment id and a post guid.")
+ print("delete comment <id> from <guid>")
+ return
+ self.post_cache[words[3]].delete_comment(words[1])
+ print("Comment deleted.")
+ else:
+ print("Deleting '%s' is not supported." % words[0])
+ return
+ else:
+ print("Delete what?")
+
+ def do_undo(self, line):
+ """Undo an action."""
+ if line != "":
+ print("Undo does not take an argument.")
+ return
+ if not self.undo:
+ print("There is nothing to undo.")
+ return
+ return self.onecmd(self.undo.pop())
+
# Main function
def main():
# Parse args
parser = argparse.ArgumentParser(description='A command line Diaspora client.')
- parser.add_argument('account',
- help='your account, e.g. kensanata@pluspora.com')
+ parser.add_argument('--no-init-file', dest='init_file', action='store_const',
+ const=False, default=True, help='Do not load a init file')
args = parser.parse_args()
# Instantiate client
- c = DiasporaClient(args.account)
-
- # Process config file
- rcfile = get_rcfile()
- if rcfile:
- print("Using config %s" % rcfile)
- with open(rcfile, "r") as fp:
- for line in fp:
- line = line.strip()
- c.cmdqueue.append(line)
+ c = DiasporaClient()
+
+ # Process init file
+ seen_pager = False
+ if args.init_file:
+ rcfile = get_rcfile()
+ if rcfile:
+ print("Using init file %s" % rcfile)
+ with open(rcfile, "r") as fp:
+ for line in fp:
+ line = line.strip()
+ if line != "":
+ c.cmdqueue.append(line)
+ if not seen_pager:
+ seen_pager = line.startswith("pager ");
+ else:
+ print("Use the save command to save your login sequence to an init file")
+
+ if not seen_pager:
+ # prepend
+ c.cmdqueue.insert(0, "pager %s" % get_pager())
# Endless interpret loop
while True: