diff --git a/README.md b/README.md index 8801d02..9df8b27 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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] @@ -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 @@ -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. ``` diff --git a/VERSION b/VERSION index 18bb418..93c8dda 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -7.5.0 +7.6.0 diff --git a/dist/matrix-commander-7.5.0.tar.gz b/dist/matrix-commander-7.5.0.tar.gz deleted file mode 100644 index 52a3fef..0000000 Binary files a/dist/matrix-commander-7.5.0.tar.gz and /dev/null differ diff --git a/dist/matrix-commander-7.6.0.tar.gz b/dist/matrix-commander-7.6.0.tar.gz new file mode 100644 index 0000000..32873df Binary files /dev/null and b/dist/matrix-commander-7.6.0.tar.gz differ diff --git a/dist/matrix_commander-7.5.0-py3-none-any.whl b/dist/matrix_commander-7.5.0-py3-none-any.whl deleted file mode 100644 index 9236b67..0000000 Binary files a/dist/matrix_commander-7.5.0-py3-none-any.whl and /dev/null differ diff --git a/dist/matrix_commander-7.6.0-py3-none-any.whl b/dist/matrix_commander-7.6.0-py3-none-any.whl new file mode 100644 index 0000000..8cd6ede Binary files /dev/null and b/dist/matrix_commander-7.6.0-py3-none-any.whl differ diff --git a/help.help.txt b/help.help.txt index 8de2842..0023821 100644 --- a/help.help.txt +++ b/help.help.txt @@ -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 @@ -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. diff --git a/help.manual.txt b/help.manual.txt index 9971534..bd2f4d8 100644 --- a/help.manual.txt +++ b/help.manual.txt @@ -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] @@ -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 @@ -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. diff --git a/help.usage.txt b/help.usage.txt index d4424c6..137a2fb 100644 --- a/help.usage.txt +++ b/help.usage.txt @@ -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] @@ -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. diff --git a/matrix_commander/matrix_commander.py b/matrix_commander/matrix_commander.py index 0715f40..9c3f223 100755 --- a/matrix_commander/matrix_commander.py +++ b/matrix_commander/matrix_commander.py @@ -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( "_", "-" @@ -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: @@ -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 @@ -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 @@ -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: @@ -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: @@ -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 @@ -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, @@ -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> diff --git a/setup.cfg b/setup.cfg index b71a63e..378c1c7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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