Skip to content

Commit

Permalink
new feature: --joined-dm-rooms to list all joined DM rooms of current…
Browse files Browse the repository at this point in the history
… user

- see also issue #140
  • Loading branch information
8go committed Oct 16, 2023
1 parent fe676b9 commit 79a5d0c
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 17 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,12 @@ $ matrix-commander --joined-rooms
$ # list all the members of 2 specific rooms
$ matrix-commander --joined-members '!someroomId1:example.com' \
'!someroomId2:example.com'
$ # list all the members of all rooms that I am member of
$ # list all the members of all rooms that I am member of
$ matrix-commander --joined-members '*'
$ # list all the joined DM rooms of 1 specific user
$ matrix-commander --joined-dm-rooms '@user1:example.com'
$ # list all the joined DM rooms that I am member of
$ matrix-commander --joined-dm-rooms '*'
$ # set presence
$ matrix-commander --set-presence "unavailable"
$ # get presence of matrix-commander itself
Expand Down Expand Up @@ -810,6 +814,7 @@ usage: matrix-commander [--usage] [-h] [--manual] [--readme] [-d]
[--delete-mxc MXC_URI [MXC_URI ...]]
[--delete-mxc-before TIMESTAMP [TIMESTAMP ...]]
[--joined-rooms] [--joined-members ROOM [ROOM ...]]
[--joined-dm-rooms USER [USER ...]]
[--mxc-to-http MXC_URI [MXC_URI ...]] [--devices]
[--discovery-info] [--login-info]
[--content-repository-config]
Expand Down Expand Up @@ -1510,6 +1515,15 @@ options:
rooms. Details:: If you want to print the joined
members of all rooms that you are member of, then use
the special character '*'.
--joined-dm-rooms USER [USER ...]
Print the list of joined DM rooms for one or multiple
users. Details:: For each user specified, it prints
all DM rooms that you share with the specified user.
There might be 0, 1, or multiple DM rooms for a given
user. Short user names like 'john' can be also be
given. If you want to print all DM rooms that you are
member of, then use the special character '*'. For
each DM room found a single line of output is printed.
--mxc-to-http MXC_URI [MXC_URI ...]
Convert MXC URIs to HTTP URLs. Details:: Convert one
or more matrix content URIs to the corresponding HTTP
Expand Down Expand Up @@ -1920,7 +1934,7 @@ options:
the program will continue to run. This is useful for
having version number in the log files.
You are running version 7.5.0 2023-10-15. Enjoy, star on Github and contribute
You are running version 7.6.0 2023-10-16. Enjoy, star on Github and contribute
by submitting a Pull Request. Also have a look at matrix-commander-tui.
```

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.5.0
7.6.0
Binary file removed dist/matrix-commander-7.5.0.tar.gz
Binary file not shown.
Binary file added dist/matrix-commander-7.6.0.tar.gz
Binary file not shown.
Binary file removed dist/matrix_commander-7.5.0-py3-none-any.whl
Binary file not shown.
Binary file added dist/matrix_commander-7.6.0-py3-none-any.whl
Binary file not shown.
4 changes: 3 additions & 1 deletion help.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ matrix-commander supports these arguments:
Print the list of joined rooms.
--joined-members ROOM [ROOM ...]
Print the list of joined members for one or multiple rooms.
--joined-dm-rooms USER [USER ...]
Print the list of joined DM rooms for one or multiple users.
--mxc-to-http MXC_URI [MXC_URI ...]
Convert MXC URIs to HTTP URLs.
--devices, --get-devices
Expand Down Expand Up @@ -204,5 +206,5 @@ delete-content ROOM_ID EVENT_ID REASON [ROOM_ID EVENT_ID REASON ...]
-v [PRINT|CHECK], -V [PRINT|CHECK], --version [PRINT|CHECK]
Print version information or check for updates.

You are running version 7.5.0 2023-10-15. Enjoy, star on Github and contribute
You are running version 7.6.0 2023-10-16. Enjoy, star on Github and contribute
by submitting a Pull Request. Also have a look at matrix-commander-tui.
12 changes: 11 additions & 1 deletion help.manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ usage: matrix-commander [--usage] [-h] [--manual] [--readme] [-d]
[--delete-mxc MXC_URI [MXC_URI ...]]
[--delete-mxc-before TIMESTAMP [TIMESTAMP ...]]
[--joined-rooms] [--joined-members ROOM [ROOM ...]]
[--joined-dm-rooms USER [USER ...]]
[--mxc-to-http MXC_URI [MXC_URI ...]] [--devices]
[--discovery-info] [--login-info]
[--content-repository-config]
Expand Down Expand Up @@ -754,6 +755,15 @@ options:
rooms. Details:: If you want to print the joined
members of all rooms that you are member of, then use
the special character '*'.
--joined-dm-rooms USER [USER ...]
Print the list of joined DM rooms for one or multiple
users. Details:: For each user specified, it prints
all DM rooms that you share with the specified user.
There might be 0, 1, or multiple DM rooms for a given
user. Short user names like 'john' can be also be
given. If you want to print all DM rooms that you are
member of, then use the special character '*'. For
each DM room found a single line of output is printed.
--mxc-to-http MXC_URI [MXC_URI ...]
Convert MXC URIs to HTTP URLs. Details:: Convert one
or more matrix content URIs to the corresponding HTTP
Expand Down Expand Up @@ -1164,5 +1174,5 @@ options:
the program will continue to run. This is useful for
having version number in the log files.

You are running version 7.5.0 2023-10-15. Enjoy, star on Github and contribute
You are running version 7.6.0 2023-10-16. Enjoy, star on Github and contribute
by submitting a Pull Request. Also have a look at matrix-commander-tui.
3 changes: 2 additions & 1 deletion help.usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ usage: matrix-commander [--usage] [-h] [--manual] [--readme] [-d]
[--delete-mxc MXC_URI [MXC_URI ...]]
[--delete-mxc-before TIMESTAMP [TIMESTAMP ...]]
[--joined-rooms] [--joined-members ROOM [ROOM ...]]
[--joined-dm-rooms USER [USER ...]]
[--mxc-to-http MXC_URI [MXC_URI ...]] [--devices]
[--discovery-info] [--login-info]
[--content-repository-config]
Expand Down Expand Up @@ -65,5 +66,5 @@ usage: matrix-commander [--usage] [-h] [--manual] [--readme] [-d]
[--room-invites [LIST|JOIN|LIST+JOIN]]
[-v [PRINT|CHECK]]

You are running version 7.5.0 2023-10-15. Enjoy, star on Github and contribute
You are running version 7.6.0 2023-10-16. Enjoy, star on Github and contribute
by submitting a Pull Request. Also have a look at matrix-commander-tui.
172 changes: 162 additions & 10 deletions matrix_commander/matrix_commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
HAVE_OPENID = False

# version number
VERSION = "2023-10-15"
VERSIONNR = "7.5.0"
VERSION = "2023-10-16"
VERSIONNR = "7.6.0"
# matrix-commander; for backwards compitability replace _ with -
PROG_WITHOUT_EXT = os.path.splitext(os.path.basename(__file__))[0].replace(
"_", "-"
Expand Down Expand Up @@ -211,9 +211,9 @@
)

# increment this number and use new incremented number for next warning
# last unique Wxxx warning number used: W112:
# last unique Wxxx warning number used: W113:
# increment this number and use new incremented number for next error
# last unique Exxx error number used: E251
# last unique Exxx error number used: E254:


class LooseVersion:
Expand Down Expand Up @@ -1671,9 +1671,8 @@ async def determine_dm_rooms(
)
gs.err_count += 1
for user in users:
if (
rcvr
and user == rcvr.user_id
if rcvr and (
user == rcvr.user_id
or short_user_name_to_user_id(user, credentials)
== rcvr.user_id
or user == rcvr.display_name
Expand Down Expand Up @@ -1799,9 +1798,8 @@ async def determine_dm_rooms_for_user(
f"Sender does not match {privacy_filter(str(resp))}"
)
gs.err_count += 1
if (
rcvr
and user == rcvr.user_id
if rcvr and (
user == rcvr.user_id
or short_user_name_to_user_id(user, credentials)
== rcvr.user_id
or user == rcvr.display_name
Expand Down Expand Up @@ -4747,6 +4745,138 @@ async def action_joined_members(
)


async def action_joined_dm_rooms(
client: AsyncClient, credentials: dict
) -> None:
"""Get and list my DM rooms while already being logged in."""
senderrooms = []
usersdict = {} # empty dict
users = gs.pa.joined_dm_rooms
userslong = [] # short user ids are converted into full user ids
if "*" in users:
userslong = list("*")
else:
for user in users:
if is_short_user_id(user):
userslong.append(short_user_name_to_user_id(user, credentials))
else:
userslong.append(user)
if not users:
gs.log.warning(
"W113: "
"No membership action(s) were performed because no users "
"were specified. Use --joined-dm-rooms option and specify users."
)
gs.warn_count += 1
return
sender = credentials["user_id"] # who am i
gs.log.debug(
f"Trying to get DM rooms for these users: {users}, " f"{userslong}"
)
resp = await client.joined_rooms()
if isinstance(resp, JoinedRoomsError):
gs.log.error(
"E252: "
"joined_rooms failed with "
f"{privacy_filter(str(resp))}. Not able to "
"get all rooms as specified by '*'. "
"The DM room listing will be missing."
)
gs.err_count += 1
return
else:
gs.log.debug(
f"joined_rooms successful with {privacy_filter(str(resp))}"
)
senderrooms = resp.rooms
for room in senderrooms:
room = room.replace(r"\!", "!") # remove possible escape
resp = await client.joined_members(room)
if isinstance(resp, JoinedMembersError):
gs.log.error(
"E253: "
f"joined_members failed with {privacy_filter(str(resp))}"
)
gs.err_count += 1
else:
gs.log.debug(
f"joined_members successful with {privacy_filter(str(resp))}"
)
if resp.members and len(resp.members) == 2:
if resp.members[0].user_id == sender:
# sndr = resp.members[0]
rcvr = resp.members[1]
elif resp.members[1].user_id == sender:
# sndr = resp.members[1]
rcvr = resp.members[0]
else:
# sndr = None
rcvr = None
gs.log.error(
"E254: "
f"Sender does not match {privacy_filter(str(resp))}"
)
gs.err_count += 1
if rcvr and (
"*" in userslong
or rcvr.user_id in userslong
# displayname does not work like this code:
# display name would be considered short user id
# and converted to full user id.
# or (
# rcvr.display_name in userslong
# and not is_user_id(rcvr.display_name)
# )
):
if rcvr.user_id in usersdict:
usersdict[rcvr.user_id].append(
{
"room_id": resp.room_id,
"members": resp.members.copy(),
}
)
else:
usersdict[rcvr.user_id] = [
{
"room_id": resp.room_id,
"members": resp.members.copy(),
}
]
gs.log.debug(f"usersdict is {usersdict}")
for user in usersdict:
gs.log.debug(f"user is {user}")
for room in usersdict[user]:
gs.log.debug(f"room is {room}")
# members = List[RoomMember] ; RoomMember
# output format controlled via --output flag
text = user + SEP + room["room_id"]
for member in room["members"]:
# convert None to ''
text += (
SEP
+ zn(member.user_id)
+ SEP
+ zn(member.display_name)
+ SEP
+ zn(member.avatar_url)
)
text = text.strip()
# Object of type xxxResponse is not JSON
# serializable, hence we use the dictionary.
json_max = room
# json_max.update({"key": value}) # add dict items
json_max.update({"user_id": user}) # add dict items
json_ = json_max.copy()
json_spec = None
print_output(
gs.pa.output,
text=text,
json_=json_,
json_max=json_max,
json_spec=json_spec,
)


async def action_mxc_to_http(client: AsyncClient, credentials: dict) -> None:
"""Convert MXC URI to HTTP URL while already logged in."""
for mxc in gs.pa.mxc_to_http:
Expand Down Expand Up @@ -5926,6 +6056,8 @@ async def action_roomsetget() -> None:
await action_joined_rooms(gs.client, gs.credentials)
if gs.pa.joined_members:
await action_joined_members(gs.client, gs.credentials)
if gs.pa.joined_dm_rooms:
await action_joined_dm_rooms(gs.client, gs.credentials)
if gs.pa.mxc_to_http:
await action_mxc_to_http(gs.client, gs.credentials)
if gs.pa.devices:
Expand Down Expand Up @@ -6788,6 +6920,7 @@ def initial_check_of_args() -> None: # noqa: C901
or gs.pa.download
or gs.pa.joined_rooms
or gs.pa.joined_members
or gs.pa.joined_dm_rooms
or gs.pa.mxc_to_http
or gs.pa.devices
or gs.pa.discovery_info
Expand Down Expand Up @@ -8272,6 +8405,23 @@ def main_inner(
"Details:: If you want to print the joined members of all rooms that "
"you are member of, then use the special character '*'.",
)
ap.add_argument(
# no single char flag
"--joined-dm-rooms",
required=False,
action="extend",
nargs="+",
type=str,
metavar="USER",
help="Print the list of joined DM rooms for one or multiple users. "
"Details:: For each user specified, it prints all DM rooms that you "
"share with the specified user. There might be 0, 1, or multiple "
"DM rooms for a given user. "
"Short user names like 'john' can be also be given. "
"If you want to print all DM rooms that "
"you are member of, then use the special character '*'. "
"For each DM room found a single line of output is printed. ",
)
ap.add_argument(
"--mxc-to-http",
required=False,
Expand Down Expand Up @@ -9106,6 +9256,8 @@ def main_inner(
Print the list of joined rooms.
<--joined-members> ROOM [ROOM ...]
Print the list of joined members for one or multiple rooms.
<--joined-dm-rooms> USER [USER ...]
Print the list of joined DM rooms for one or multiple users.
<--mxc-to-http> MXC_URI [MXC_URI ...]
Convert MXC URIs to HTTP URLs.
<--devices,> <--get-devices>
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://packaging.python.org/en/latest/tutorials/packaging-projects/
# https://setuptools.pypa.io/en/latest/userguide/
name = matrix-commander
version = 7.5.0
version = 7.6.0
author = 8go
description = A simple command-line Matrix client
long_description = file: PyPi-Instructions.md, README.md
Expand Down

0 comments on commit 79a5d0c

Please sign in to comment.