You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mod_roster_command.lua 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. -----------------------------------------------------------
  2. -- mod_roster_command: Manage rosters through prosodyctl
  3. -- version 0.02
  4. -----------------------------------------------------------
  5. -- Copyright (C) 2011 Matthew Wild
  6. -- Copyright (C) 2011 Adam Nielsen
  7. --
  8. -- This project is MIT/X11 licensed. Please see the
  9. -- COPYING file in the source package for more information.
  10. -----------------------------------------------------------
  11. if module.host ~= "*" then
  12. module:log("error", "Do not load this module in Prosody, for correct usage see: https://modules.prosody.im/mod_roster_command.html");
  13. return;
  14. end
  15. -- Workaround for lack of util.startup...
  16. local prosody = _G.prosody;
  17. local hosts = prosody.hosts;
  18. prosody.bare_sessions = prosody.bare_sessions or {};
  19. _G.bare_sessions = _G.bare_sessions or prosody.bare_sessions;
  20. local usermanager = require "core.usermanager";
  21. local rostermanager = require "core.rostermanager";
  22. local storagemanager = require "core.storagemanager";
  23. local jid = require "util.jid";
  24. local warn = require"util.prosodyctl".show_warning;
  25. -- Make a *one-way* subscription. User will see when contact is online,
  26. -- contact will not see when user is online.
  27. function subscribe(user_jid, contact_jid)
  28. local user_username, user_host = jid.split(user_jid);
  29. local contact_username, contact_host = jid.split(contact_jid);
  30. if not hosts[user_host] then
  31. warn("The host '%s' is not configured for this server.", user_host);
  32. return;
  33. end
  34. if hosts[user_host].users.name == "null" then
  35. storagemanager.initialize_host(user_host);
  36. usermanager.initialize_host(user_host);
  37. end
  38. -- Update user's roster to say subscription request is pending. Bare hosts (e.g. components) don't have rosters.
  39. if user_username ~= nil then
  40. rostermanager.set_contact_pending_out(user_username, user_host, contact_jid);
  41. end
  42. if hosts[contact_host] then
  43. if contact_host ~= user_host and hosts[contact_host].users.name == "null" then
  44. storagemanager.initialize_host(contact_host);
  45. usermanager.initialize_host(contact_host);
  46. end
  47. -- Update contact's roster to say subscription request is pending...
  48. rostermanager.set_contact_pending_in(contact_username, contact_host, user_jid);
  49. -- Update contact's roster to say subscription request approved...
  50. rostermanager.subscribed(contact_username, contact_host, user_jid);
  51. -- Update user's roster to say subscription request approved. Bare hosts (e.g. components) don't have rosters.
  52. if user_username ~= nil then
  53. rostermanager.process_inbound_subscription_approval(user_username, user_host, contact_jid);
  54. end
  55. end
  56. end
  57. -- Make a mutual subscription between jid1 and jid2. Each JID will see
  58. -- when the other one is online.
  59. function subscribe_both(jid1, jid2)
  60. subscribe(jid1, jid2);
  61. subscribe(jid2, jid1);
  62. end
  63. -- Unsubscribes user from contact (not contact from user, if subscribed).
  64. function unsubscribe(user_jid, contact_jid)
  65. local user_username, user_host = jid.split(user_jid);
  66. local contact_username, contact_host = jid.split(contact_jid);
  67. if not hosts[user_host] then
  68. warn("The host '%s' is not configured for this server.", user_host);
  69. return;
  70. end
  71. if hosts[user_host].users.name == "null" then
  72. storagemanager.initialize_host(user_host);
  73. usermanager.initialize_host(user_host);
  74. end
  75. -- Update user's roster to say subscription is cancelled...
  76. rostermanager.unsubscribe(user_username, user_host, contact_jid);
  77. if hosts[contact_host] then
  78. if contact_host ~= user_host and hosts[contact_host].users.name == "null" then
  79. storagemanager.initialize_host(contact_host);
  80. usermanager.initialize_host(contact_host);
  81. end
  82. -- Update contact's roster to say subscription is cancelled...
  83. rostermanager.unsubscribed(contact_username, contact_host, user_jid);
  84. end
  85. end
  86. -- Cancel any subscription in either direction.
  87. function unsubscribe_both(jid1, jid2)
  88. unsubscribe(jid1, jid2);
  89. unsubscribe(jid2, jid1);
  90. end
  91. -- Set the name shown and group used in the contact list
  92. function rename(user_jid, contact_jid, contact_nick, contact_group)
  93. local user_username, user_host = jid.split(user_jid);
  94. if not hosts[user_host] then
  95. warn("The host '%s' is not configured for this server.", user_host);
  96. return;
  97. end
  98. if hosts[user_host].users.name == "null" then
  99. storagemanager.initialize_host(user_host);
  100. usermanager.initialize_host(user_host);
  101. end
  102. -- Load user's roster and find the contact
  103. local roster = rostermanager.load_roster(user_username, user_host);
  104. local item = roster[contact_jid];
  105. if item then
  106. if contact_nick then
  107. item.name = contact_nick;
  108. end
  109. if contact_group then
  110. item.groups = {}; -- Remove from all current groups
  111. item.groups[contact_group] = true;
  112. end
  113. rostermanager.save_roster(user_username, user_host, roster);
  114. end
  115. end
  116. function remove(user_jid, contact_jid)
  117. unsubscribe_both(user_jid, contact_jid);
  118. local user_username, user_host = jid.split(user_jid);
  119. local roster = rostermanager.load_roster(user_username, user_host);
  120. roster[contact_jid] = nil;
  121. rostermanager.save_roster(user_username, user_host, roster);
  122. end
  123. function module.command(arg)
  124. local command = arg[1];
  125. if not command then
  126. warn("Valid subcommands: (un)subscribe(_both) | rename");
  127. return 0;
  128. end
  129. table.remove(arg, 1);
  130. if command == "subscribe" then
  131. subscribe(arg[1], arg[2]);
  132. return 0;
  133. elseif command == "subscribe_both" then
  134. subscribe_both(arg[1], arg[2]);
  135. return 0;
  136. elseif command == "unsubscribe" then
  137. unsubscribe(arg[1], arg[2]);
  138. return 0;
  139. elseif command == "unsubscribe_both" then
  140. unsubscribe_both(arg[1], arg[2]);
  141. return 0;
  142. elseif command == "remove" then
  143. remove(arg[1], arg[2]);
  144. return 0;
  145. elseif command == "rename" then
  146. rename(arg[1], arg[2], arg[3], arg[4]);
  147. return 0;
  148. else
  149. warn("Unknown command: %s", command);
  150. return 1;
  151. end
  152. return 0;
  153. end