add commands to add/drop access
This commit is contained in:
		| @@ -86,7 +86,11 @@ static auto start_irc( | |||||||
|     bot->sig_command.connect([webhook, connection](const Bot::Command &cmd) { |     bot->sig_command.connect([webhook, connection](const Bot::Command &cmd) { | ||||||
|         auto cursor = webhook_commands.find(std::string{cmd.command}); |         auto cursor = webhook_commands.find(std::string{cmd.command}); | ||||||
|         if (cursor != webhook_commands.end()) { |         if (cursor != webhook_commands.end()) { | ||||||
|             cursor->second(webhook, cmd); |             try { | ||||||
|  |                 cursor->second(webhook, cmd); | ||||||
|  |             } catch (const std::exception &e) { | ||||||
|  |                 BOOST_LOG_TRIVIAL(error) << "Command handler failed: " << e.what(); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -484,49 +484,77 @@ std::map<std::string, void (*)(std::shared_ptr<Webhooks>, const myirc::Bot::Comm | |||||||
|          std::string name; |          std::string name; | ||||||
|          if (iss >> name) |          if (iss >> name) | ||||||
|          { |          { | ||||||
|              auto cursor = webhooks->settings_.projects.find(name); |              auto &project = webhooks->settings_.projects.at(name); | ||||||
|              if (cursor == webhooks->settings_.projects.end()) |  | ||||||
|              { |  | ||||||
|                  return; |  | ||||||
|              } |  | ||||||
|  |  | ||||||
|              auto &project = cursor->second; |  | ||||||
|  |  | ||||||
|              if (not authorized_for_project(cmd, project, cmd.account)) |              if (not authorized_for_project(cmd, project, cmd.account)) | ||||||
|              { |              { | ||||||
|                  return; |                  return; | ||||||
|              } |              } | ||||||
|  |  | ||||||
|              project.enabled = true; |              project.enabled = true; | ||||||
|              webhooks->save_settings(); |              webhooks->save_settings(); | ||||||
|              reply_to(webhooks, cmd, "Enabled project " + name); |              reply_to(webhooks, cmd, "Enabled project " + name); | ||||||
|          } |          } | ||||||
|      }}, |      }}, | ||||||
|      {"disable-project", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) { |      {"disable-project", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) { | ||||||
|          //if (cmd.oper.empty()) |  | ||||||
|          //{ |  | ||||||
|          //    return; |  | ||||||
|          //} |  | ||||||
|          std::istringstream iss{std::string{cmd.arguments}}; |          std::istringstream iss{std::string{cmd.arguments}}; | ||||||
|          std::string name; |          std::string name; | ||||||
|          if (iss >> name) |          if (iss >> name) | ||||||
|          { |          { | ||||||
|              auto cursor = webhooks->settings_.projects.find(name); |              auto &project = webhooks->settings_.projects.at(name); | ||||||
|              if (cursor == webhooks->settings_.projects.end()) |  | ||||||
|              { |  | ||||||
|                  return; |  | ||||||
|              } |  | ||||||
|  |  | ||||||
|              auto &project = cursor->second; |  | ||||||
|  |  | ||||||
|              if (not authorized_for_project(cmd, project, cmd.account)) |              if (not authorized_for_project(cmd, project, cmd.account)) | ||||||
|              { |              { | ||||||
|                  return; |                  return; | ||||||
|              } |              } | ||||||
|  |  | ||||||
|              project.enabled = false; |              project.enabled = false; | ||||||
|              webhooks->save_settings(); |              webhooks->save_settings(); | ||||||
|              reply_to(webhooks, cmd, "Disabled project " + name); |              reply_to(webhooks, cmd, "Disabled project " + name); | ||||||
|          } |          } | ||||||
|      }}, |      }}, | ||||||
|  |      {"add-access", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) { | ||||||
|  |          if (cmd.oper.empty()) | ||||||
|  |          { | ||||||
|  |              return; | ||||||
|  |          } | ||||||
|  |          std::istringstream iss{std::string{cmd.arguments}}; | ||||||
|  |          std::string name, account; | ||||||
|  |          if (iss >> name >> account) | ||||||
|  |          { | ||||||
|  |              auto &project = webhooks->settings_.projects.at(name); | ||||||
|  |              if (not authorized_for_project(cmd, project, cmd.account)) | ||||||
|  |              { | ||||||
|  |                  return; | ||||||
|  |              } | ||||||
|  |              auto [_, inserted] = project.authorized_accounts.insert(account); | ||||||
|  |              if (inserted) { | ||||||
|  |                  webhooks->save_settings(); | ||||||
|  |                  reply_to(webhooks, cmd, "Access added"); | ||||||
|  |              } | ||||||
|  |              else | ||||||
|  |              { | ||||||
|  |                  reply_to(webhooks, cmd, "Access already set"); | ||||||
|  |              } | ||||||
|  |          } | ||||||
|  |      }}, | ||||||
|  |      {"drop-access", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) { | ||||||
|  |          if (cmd.oper.empty()) | ||||||
|  |          { | ||||||
|  |              return; | ||||||
|  |          } | ||||||
|  |          std::istringstream iss{std::string{cmd.arguments}}; | ||||||
|  |          std::string name, account; | ||||||
|  |          if (iss >> name >> account) | ||||||
|  |          { | ||||||
|  |              auto &project = webhooks->settings_.projects.at(name); | ||||||
|  |              if (not authorized_for_project(cmd, project, cmd.account)) | ||||||
|  |              { | ||||||
|  |                  return; | ||||||
|  |              } | ||||||
|  |              auto removed = project.authorized_accounts.erase(account); | ||||||
|  |              if (removed) {  | ||||||
|  |                 webhooks->save_settings(); | ||||||
|  |                 reply_to(webhooks, cmd, "Access dropped"); | ||||||
|  |              } else { | ||||||
|  |                 reply_to(webhooks, cmd, "Access not found"); | ||||||
|  |              } | ||||||
|  |          } | ||||||
|  |      }}, | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user