biboumi_adhoc_commands.cpp 3.45 KB
Newer Older
1
#include <xmpp/biboumi_adhoc_commands.hpp>
2
#include <xmpp/biboumi_component.hpp>
3
#include <bridge/bridge.hpp>
4

5
void DisconnectUserStep1(XmppComponent* xmpp_component, AdhocSession&, XmlNode& command_node)
6
{
7 8
  auto biboumi_component = static_cast<BiboumiComponent*>(xmpp_component);

9 10 11 12 13 14 15 16 17 18 19 20 21 22
  XmlNode x("jabber:x:data:x");
  x["type"] = "form";
  XmlNode title("title");
  title.set_inner("Disconnect a user from the gateway");
  x.add_child(std::move(title));
  XmlNode instructions("instructions");
  instructions.set_inner("Choose a user JID and a quit message");
  x.add_child(std::move(instructions));
  XmlNode jids_field("field");
  jids_field["var"] = "jids";
  jids_field["type"] = "list-multi";
  jids_field["label"] = "The JIDs to disconnect";
  XmlNode required("required");
  jids_field.add_child(std::move(required));
23
  for (Bridge* bridge: biboumi_component->get_bridges())
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    {
      XmlNode option("option");
      option["label"] = bridge->get_jid();
      XmlNode value("value");
      value.set_inner(bridge->get_jid());
      option.add_child(std::move(value));
      jids_field.add_child(std::move(option));
    }
  x.add_child(std::move(jids_field));

  XmlNode message_field("field");
  message_field["var"] = "quit-message";
  message_field["type"] = "text-single";
  message_field["label"] = "Quit message";
  XmlNode message_value("value");
  message_value.set_inner("Disconnected by admin");
  message_field.add_child(std::move(message_value));
  x.add_child(std::move(message_field));
  command_node.add_child(std::move(x));
}

void DisconnectUserStep2(XmppComponent* xmpp_component, AdhocSession& session, XmlNode& command_node)
{
47 48
  auto biboumi_component = static_cast<BiboumiComponent*>(xmpp_component);

49 50
  // Find out if the jids, and the quit message are provided in the form.
  std::string quit_message;
51
  const XmlNode* x = command_node.get_child("x", "jabber:x:data");
52 53
  if (x)
    {
54 55 56
      const XmlNode* message_field = nullptr;
      const XmlNode* jids_field = nullptr;
      for (const XmlNode* field: x->get_children("field", "jabber:x:data"))
57 58 59 60 61 62
        if (field->get_tag("var") == "jids")
          jids_field = field;
        else if (field->get_tag("var") == "quit-message")
          message_field = field;
      if (message_field)
        {
63
          const XmlNode* value = message_field->get_child("value", "jabber:x:data");
64 65 66 67 68 69
          if (value)
            quit_message = value->get_inner();
        }
      if (jids_field)
        {
          std::size_t num = 0;
70
          for (const XmlNode* value: jids_field->get_children("value", "jabber:x:data"))
71
            {
72
              Bridge* bridge = biboumi_component->find_user_bridge(value->get_inner());
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
              if (bridge)
                {
                  bridge->shutdown(quit_message);
                  num++;
                }
            }
          command_node.delete_all_children();

          XmlNode note("note");
          note["type"] = "info";
          if (num == 0)
            note.set_inner("No user were disconnected.");
          else if (num == 1)
            note.set_inner("1 user has been disconnected.");
          else
            note.set_inner(std::to_string(num) + " users have been disconnected.");
          command_node.add_child(std::move(note));
90
          return;
91 92
        }
    }
93 94 95 96 97
  XmlNode error(ADHOC_NS":error");
  error["type"] = "modify";
  XmlNode condition(STANZA_NS":bad-request");
  error.add_child(std::move(condition));
  command_node.add_child(std::move(error));
98 99
  session.terminate();
}