2 # Copyright (C) 2019 Alex Schroeder <alex@gnu.org>
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 # You should have received a copy of the GNU Affero General Public License along
15 # with this program. If not, see <https://www.gnu.org/licenses/>.
23 # Command abbreviations
29 "~/.config/jan-pona-mute/login",
30 "~/.config/.jan-pona-mute",
36 for rc_path
in _RC_PATHS
:
37 rcfile
= os
.path
.expanduser(rc_path
)
38 if os
.path
.exists(rcfile
):
42 class DiasporaClient(cmd
.Cmd
):
44 prompt
= "\x1b[38;5;255m" + "> " + "\x1b[0m"
45 intro
= "Welcome to Diaspora!"
55 # dict mapping user ids to usernames
58 def get_username(self
, guid
):
59 if guid
in self
.users
:
60 return self
.users
[guid
]
62 user
= diaspy
.people
.User(connection
= self
.connection
, guid
= guid
)
63 self
.users
[guid
] = user
.handle()
64 return self
.users
[guid
]
66 def do_account(self
, account
):
67 """Set username and pod using the format username@pod."""
69 (self
.username
, self
.pod
) = account
.split('@')
70 print("Username and pod set: %s@%s" % (self
.username
, self
.pod
))
72 print("The account must contain an @ character, e.g. kensanata@pluspora.com.")
73 print("Use the account comand to set the account.")
75 def do_info(self
, line
):
76 """Get some info about things. By default, it is info about yourself."""
77 print("Info about yourself:")
78 print("Username: %s" % self
.username
)
79 print("Password: %s" % ("None" if self
.password
== None else "set"))
80 print("Pod: %s" % self
.pod
)
82 def do_password(self
, password
):
83 """Set the password."""
84 self
.password
= (None if self
.password
== "" else password
)
85 print("Password %s" % ("unset" if self
.password
== "" else "set"))
87 def do_save(self
, line
):
88 if self
.username
== None or self
.pod
== None:
89 print("Use the account command to set username and pod")
90 elif self
.password
== None:
91 print("Use the password command")
95 rfile
= first(_RC_PATHS
)
101 with
open(rcfile
, "r") as fp
:
103 words
= line
.strip().split()
105 if words
[0] == "account":
107 account
= "%s@%s" % (self
.username
, self
.pod
)
108 if len(words
) > 1 and words
[1] != account
:
109 line
= "account %s\n" % account
111 elif words
[0] == "password":
113 if len(words
) > 1 and words
[1] != self
.password
:
114 line
= "password %s\n" % self
.password
116 elif words
[0] == "login":
117 if seen_account
and seen_password
:
120 # skip login if no account or no password given
126 file.append("account %s@%s\n" % (self
.username
, self
.pod
))
128 if not seen_password
:
129 file.append("password %s\n" % self
.password
)
132 file.append("login\n")
135 if os
.path
.isfile(rcfile
):
136 os
.rename(rcfile
, rcfile
+ "~")
137 if not os
.path
.isdir(os
.path
.dirname(rcfile
)):
138 os
.makedirs(os
.path
.dirname(rcfile
))
139 with
open(rcfile
, "w") as fp
:
140 fp
.write("".join(file))
141 print("Wrote %s" % rcfile
)
143 print("No changes made, %s left unchanged" % rcfile
)
145 def do_login(self
, line
):
148 self
.onecmd("account %s" % line
)
149 if self
.username
== None or self
.pod
== None:
150 print("Use the account command to set username and pod")
151 elif self
.password
== None:
152 print("Use the password command")
154 self
.connection
= diaspy
.connection
.Connection(
155 pod
= "https://%s" % self
.pod
, username
= self
.username
, password
= self
.password
)
157 self
.connection
.login()
158 self
.onecmd("notifications")
159 except diaspy
.errors
.LoginError
:
160 print("Login failed")
162 def do_notifications(self
, line
):
163 """List notifications."""
164 if self
.connection
== None:
165 print("Use the login command, first.")
167 self
.things
= diaspy
.notifications
.Notifications(self
.connection
).last()
168 for n
, notification
in enumerate(self
.things
):
169 print("%2d. %s %s" % (n
+1, notification
.when(), notification
))
172 def do_quit(self
, *args
):
173 """Exit jan-pona-mute."""
177 def default(self
, line
):
178 if line
.strip() == "EOF":
179 return self
.onecmd("quit")
181 # Expand abbreviated commands
182 first_word
= line
.split()[0].strip()
183 if first_word
in _ABBREVS
:
184 full_cmd
= _ABBREVS
[first_word
]
185 expanded
= line
.replace(first_word
, full_cmd
, 1)
186 return self
.onecmd(expanded
)
188 # Try to parse numerical index for lookup table
190 n
= int(line
.strip())
192 print("Use the help command")
196 item
= self
.things
[n
-1]
198 print ("Index too high!")
204 def show(self
, item
):
205 """Show the current item."""
212 parser
= argparse
.ArgumentParser(description
='A command line Diaspora client.')
213 parser
.add_argument('--no-init-file', dest
='init_file', action
='store_const',
214 const
=False, default
=True, help='Do not load a init file')
215 args
= parser
.parse_args()
222 rcfile
= get_rcfile()
224 print("Using init file %s" % rcfile
)
225 with
open(rcfile
, "r") as fp
:
228 c
.cmdqueue
.append(line
)
230 print("Use the save command to save your login sequence to an init file")
232 # Endless interpret loop
236 except KeyboardInterrupt:
239 if __name__
== '__main__':