1
0

kick_users.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. from argparse import ArgumentParser
  3. import json
  4. import requests
  5. import sys
  6. import urllib
  7. def _mkurl(template, kws):
  8. for key in kws:
  9. template = template.replace(key, kws[key])
  10. return template
  11. def main(hs, room_id, access_token, user_id_prefix, why):
  12. if not why:
  13. why = "Automated kick."
  14. print "Kicking members on %s in room %s matching %s" % (hs, room_id, user_id_prefix)
  15. room_state_url = _mkurl(
  16. "$HS/_matrix/client/api/v1/rooms/$ROOM/state?access_token=$TOKEN",
  17. {
  18. "$HS": hs,
  19. "$ROOM": room_id,
  20. "$TOKEN": access_token
  21. }
  22. )
  23. print "Getting room state => %s" % room_state_url
  24. res = requests.get(room_state_url)
  25. print "HTTP %s" % res.status_code
  26. state_events = res.json()
  27. if "error" in state_events:
  28. print "FATAL"
  29. print state_events
  30. return
  31. kick_list = []
  32. room_name = room_id
  33. for event in state_events:
  34. if not event["type"] == "m.room.member":
  35. if event["type"] == "m.room.name":
  36. room_name = event["content"].get("name")
  37. continue
  38. if not event["content"].get("membership") == "join":
  39. continue
  40. if event["state_key"].startswith(user_id_prefix):
  41. kick_list.append(event["state_key"])
  42. if len(kick_list) == 0:
  43. print "No user IDs match the prefix '%s'" % user_id_prefix
  44. return
  45. print "The following user IDs will be kicked from %s" % room_name
  46. for uid in kick_list:
  47. print uid
  48. doit = raw_input("Continue? [Y]es\n")
  49. if len(doit) > 0 and doit.lower() == 'y':
  50. print "Kicking members..."
  51. # encode them all
  52. kick_list = [urllib.quote(uid) for uid in kick_list]
  53. for uid in kick_list:
  54. kick_url = _mkurl(
  55. "$HS/_matrix/client/api/v1/rooms/$ROOM/state/m.room.member/$UID?access_token=$TOKEN",
  56. {
  57. "$HS": hs,
  58. "$UID": uid,
  59. "$ROOM": room_id,
  60. "$TOKEN": access_token
  61. }
  62. )
  63. kick_body = {
  64. "membership": "leave",
  65. "reason": why
  66. }
  67. print "Kicking %s" % uid
  68. res = requests.put(kick_url, data=json.dumps(kick_body))
  69. if res.status_code != 200:
  70. print "ERROR: HTTP %s" % res.status_code
  71. if res.json().get("error"):
  72. print "ERROR: JSON %s" % res.json()
  73. if __name__ == "__main__":
  74. parser = ArgumentParser("Kick members in a room matching a certain user ID prefix.")
  75. parser.add_argument("-u","--user-id",help="The user ID prefix e.g. '@irc_'")
  76. parser.add_argument("-t","--token",help="Your access_token")
  77. parser.add_argument("-r","--room",help="The room ID to kick members in")
  78. parser.add_argument("-s","--homeserver",help="The base HS url e.g. http://matrix.org")
  79. parser.add_argument("-w","--why",help="Reason for the kick. Optional.")
  80. args = parser.parse_args()
  81. if not args.room or not args.token or not args.user_id or not args.homeserver:
  82. parser.print_help()
  83. sys.exit(1)
  84. else:
  85. main(args.homeserver, args.room, args.token, args.user_id, args.why)