The Battle for Wesnoth  1.19.7+dev
chat_events.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "chat_events.hpp"
16 
17 #include "formula/string_utils.hpp"
18 #include "gettext.hpp"
19 #include "log.hpp"
20 #include "chat_command_handler.hpp"
22 
23 static lg::log_domain log_engine("engine");
24 #define ERR_NG LOG_STREAM(err, log_engine)
25 #define LOG_NG LOG_STREAM(info, log_engine)
26 
27 namespace events {
28 
30 {
31 }
32 
34 {
35 }
36 
37 /**
38 * Change the log level of a log domain.
39 *
40 * @param data string of the form: "@<level@> @<domain@>"
41 */
42 void chat_handler::change_logging(const std::string& data) {
43  const std::string::const_iterator j =
44  std::find(data.begin(), data.end(), ' ');
45  if (j == data.end()) return;
46  const std::string level(data.begin(), j);
47  const std::string domain(j + 1, data.end());
49  if (level == "error") severity = lg::err().get_severity();
50  else if (level == "warning") severity = lg::warn().get_severity();
51  else if (level == "info") severity = lg::info().get_severity();
52  else if (level == "debug") severity = lg::debug().get_severity();
53  else {
54  utils::string_map symbols;
55  symbols["level"] = level;
56  const std::string& msg =
57  VGETTEXT("Unknown debug level: '$level'.", symbols);
58  ERR_NG << msg;
59  add_chat_message(std::time(nullptr), _("error"), 0, msg);
60  return;
61  }
62  if (!lg::set_log_domain_severity(domain, severity)) {
63  utils::string_map symbols;
64  symbols["domain"] = domain;
65  const std::string& msg =
66  VGETTEXT("Unknown debug domain: '$domain'.", symbols);
67  ERR_NG << msg;
68  add_chat_message(std::time(nullptr), _("error"), 0, msg);
69  return;
70  }
71  else {
72  utils::string_map symbols;
73  symbols["level"] = level;
74  symbols["domain"] = domain;
75  const std::string& msg =
76  VGETTEXT("Switched domain: '$domain' to level: '$level'.", symbols);
77  LOG_NG << msg;
78  add_chat_message(std::time(nullptr), "log", 0, msg);
79  }
80 }
81 
82 void chat_handler::send_command(const std::string& cmd, const std::string& args /* = "" */) {
83  config data;
84  if (cmd == "muteall") {
85  data.add_child(cmd);
86  }
87  else if (cmd == "query") {
88  data.add_child(cmd)["type"] = args;
89  }
90  else if (cmd == "ban" || cmd == "unban" || cmd == "kick"
91  || cmd == "mute" || cmd == "unmute") {
92  data.add_child(cmd)["username"] = args;
93  }
94  else if (cmd == "ping") {
95  data[cmd] = std::to_string(std::time(nullptr));
96  }
97  else if (cmd == "report") {
98  data.add_child("query")["type"] = "report " + args;
99  }
100  else if (cmd == "roll") {
101  data.add_child("query")["type"] = "roll " + args;
102  }
104 }
105 
106 void chat_handler::do_speak(const std::string& message, bool allies_only)
107 {
108  if (message.empty() || message == "/") {
109  return;
110  }
111  bool is_command = (message[0] == '/');
112  bool quoted_command = (is_command && message[1] == ' ');
113 
114  if (!is_command) {
115  send_chat_message(message, allies_only);
116  return;
117  }
118  else if (quoted_command) {
119  send_chat_message(std::string(message.begin() + 2, message.end()), allies_only);
120  return;
121  }
122  std::string cmd(message.begin() + 1, message.end());
123  chat_command_handler cch(*this, allies_only);
124  cch.dispatch(cmd);
125 }
126 
127 void chat_handler::user_relation_changed(const std::string& /*name*/)
128 {
129 }
130 
131 void chat_handler::send_whisper(const std::string& receiver, const std::string& message)
132 {
133  config cwhisper, data;
134  cwhisper["receiver"] = receiver;
135  cwhisper["message"] = message;
136  cwhisper["sender"] = prefs::get().login();
137  data.add_child("whisper", std::move(cwhisper));
139 }
140 
141 void chat_handler::add_whisper_sent(const std::string& receiver, const std::string& message)
142 {
143  utils::string_map symbols;
144  symbols["receiver"] = receiver;
145  add_chat_message(std::time(nullptr), VGETTEXT("whisper to $receiver", symbols), 0, message);
146 }
147 
148 void chat_handler::add_whisper_received(const std::string& sender, const std::string& message)
149 {
150  utils::string_map symbols;
151  symbols["sender"] = sender;
152  add_chat_message(std::time(nullptr), VGETTEXT("whisper: $sender", symbols), 0, message);
153 }
154 
155 void chat_handler::send_chat_room_message(const std::string& room,
156  const std::string& message)
157 {
158  config cmsg, data;
159  cmsg["room"] = room;
160  cmsg["message"] = message;
161  cmsg["sender"] = prefs::get().login();
162  data.add_child("message", std::move(cmsg));
164 }
165 
166 void chat_handler::add_chat_room_message_sent(const std::string &room, const std::string &message)
167 {
168  add_chat_room_message_received(room, prefs::get().login(), message);
169 }
170 
171 void chat_handler::add_chat_room_message_received(const std::string &room,
172  const std::string &speaker, const std::string &message)
173 {
174  add_chat_message(std::time(nullptr), room + ": " + speaker, 0, message, events::chat_handler::MESSAGE_PRIVATE);
175 }
176 
177 }
static lg::log_domain log_engine("engine")
#define ERR_NG
Definition: chat_events.cpp:24
#define LOG_NG
Definition: chat_events.cpp:25
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
virtual void add_chat_room_message_received(const std::string &room, const std::string &speaker, const std::string &message)
virtual void send_chat_room_message(const std::string &room, const std::string &message)
virtual void add_chat_room_message_sent(const std::string &room, const std::string &message)
virtual void user_relation_changed(const std::string &name)
Called when a processed command results in a relation (friend/ignore) change for a user whose name is...
virtual void add_whisper_received(const std::string &sender, const std::string &message)
virtual void add_whisper_sent(const std::string &receiver, const std::string &message)
void do_speak(const std::string &message, bool allies_only=false)
void change_logging(const std::string &data)
Change the log level of a log domain.
Definition: chat_events.cpp:42
virtual void send_chat_message(const std::string &message, bool allies_only=false)=0
virtual void send_whisper(const std::string &receiver, const std::string &message)
virtual void add_chat_message(const std::time_t &time, const std::string &speaker, int side, const std::string &message, MESSAGE_TYPE type=MESSAGE_PRIVATE)=0
virtual void send_to_server(const config &cfg)=0
void send_command(const std::string &cmd, const std::string &args="")
Definition: chat_events.cpp:82
severity get_severity() const
Definition: log.hpp:220
static prefs & get()
std::string login()
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static std::string _(const char *str)
Definition: gettext.hpp:93
Standard logging facilities (interface).
Handling of system events.
logger & err()
Definition: log.cpp:307
severity
Definition: log.hpp:83
logger & debug()
Definition: log.cpp:325
logger & warn()
Definition: log.cpp:313
logger & info()
Definition: log.cpp:319
bool set_log_domain_severity(const std::string &name, severity severity)
Definition: log.cpp:347
std::map< std::string, t_string > string_map
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Definition: general.hpp:140
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
std::string_view data
Definition: picture.cpp:178