The Battle for Wesnoth  1.17.14+dev
chat_events.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2022
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 "map_command_handler.hpp"
21 #include "chat_command_handler.hpp"
23 #include "preferences/general.hpp"
24 #include "preferences/game.hpp"
25 
26 static lg::log_domain log_engine("engine");
27 #define ERR_NG LOG_STREAM(err, log_engine)
28 #define LOG_NG LOG_STREAM(info, log_engine)
29 
30 namespace events {
31 
33 {
34 }
35 
37 {
38 }
39 
40 /**
41 * Change the log level of a log domain.
42 *
43 * @param data string of the form: "@<level@> @<domain@>"
44 */
45 void chat_handler::change_logging(const std::string& data) {
46  const std::string::const_iterator j =
47  std::find(data.begin(), data.end(), ' ');
48  if (j == data.end()) return;
49  const std::string level(data.begin(), j);
50  const std::string domain(j + 1, data.end());
51  int severity;
52  if (level == "error") severity = lg::err().get_severity();
53  else if (level == "warning") severity = lg::warn().get_severity();
54  else if (level == "info") severity = lg::info().get_severity();
55  else if (level == "debug") severity = lg::debug().get_severity();
56  else {
57  utils::string_map symbols;
58  symbols["level"] = level;
59  const std::string& msg =
60  VGETTEXT("Unknown debug level: '$level'.", symbols);
61  ERR_NG << msg;
62  add_chat_message(std::time(nullptr), _("error"), 0, msg);
63  return;
64  }
65  if (!lg::set_log_domain_severity(domain, severity)) {
66  utils::string_map symbols;
67  symbols["domain"] = domain;
68  const std::string& msg =
69  VGETTEXT("Unknown debug domain: '$domain'.", symbols);
70  ERR_NG << msg;
71  add_chat_message(std::time(nullptr), _("error"), 0, msg);
72  return;
73  }
74  else {
75  utils::string_map symbols;
76  symbols["level"] = level;
77  symbols["domain"] = domain;
78  const std::string& msg =
79  VGETTEXT("Switched domain: '$domain' to level: '$level'.", symbols);
80  LOG_NG << msg;
81  add_chat_message(std::time(nullptr), "log", 0, msg);
82  }
83 }
84 
85 void chat_handler::send_command(const std::string& cmd, const std::string& args /* = "" */) {
86  config data;
87  if (cmd == "muteall") {
88  data.add_child(cmd);
89  }
90  else if (cmd == "query") {
91  data.add_child(cmd)["type"] = args;
92  }
93  else if (cmd == "ban" || cmd == "unban" || cmd == "kick"
94  || cmd == "mute" || cmd == "unmute") {
95  data.add_child(cmd)["username"] = args;
96  }
97  else if (cmd == "ping") {
98  data[cmd] = std::to_string(std::time(nullptr));
99  }
100  else if (cmd == "report") {
101  data.add_child("query")["type"] = "report " + args;
102  }
103  else if (cmd == "roll") {
104  data.add_child("query")["type"] = "roll " + args;
105  }
106  send_to_server(data);
107 }
108 
109 void chat_handler::do_speak(const std::string& message, bool allies_only)
110 {
111  if (message.empty() || message == "/") {
112  return;
113  }
114  bool is_command = (message[0] == '/');
115  bool quoted_command = (is_command && message[1] == ' ');
116 
117  if (!is_command) {
118  send_chat_message(message, allies_only);
119  return;
120  }
121  else if (quoted_command) {
122  send_chat_message(std::string(message.begin() + 2, message.end()), allies_only);
123  return;
124  }
125  std::string cmd(message.begin() + 1, message.end());
126  chat_command_handler cch(*this, allies_only);
127  cch.dispatch(cmd);
128 }
129 
130 void chat_handler::user_relation_changed(const std::string& /*name*/)
131 {
132 }
133 
134 void chat_handler::send_whisper(const std::string& receiver, const std::string& message)
135 {
136  config cwhisper, data;
137  cwhisper["receiver"] = receiver;
138  cwhisper["message"] = message;
139  cwhisper["sender"] = preferences::login();
140  data.add_child("whisper", std::move(cwhisper));
141  send_to_server(data);
142 }
143 
144 void chat_handler::add_whisper_sent(const std::string& receiver, const std::string& message)
145 {
146  utils::string_map symbols;
147  symbols["receiver"] = receiver;
148  add_chat_message(std::time(nullptr), VGETTEXT("whisper to $receiver", symbols), 0, message);
149 }
150 
151 void chat_handler::add_whisper_received(const std::string& sender, const std::string& message)
152 {
153  utils::string_map symbols;
154  symbols["sender"] = sender;
155  add_chat_message(std::time(nullptr), VGETTEXT("whisper: $sender", symbols), 0, message);
156 }
157 
158 void chat_handler::send_chat_room_message(const std::string& room,
159  const std::string& message)
160 {
161  config cmsg, data;
162  cmsg["room"] = room;
163  cmsg["message"] = message;
164  cmsg["sender"] = preferences::login();
165  data.add_child("message", std::move(cmsg));
166  send_to_server(data);
167 }
168 
169 void chat_handler::add_chat_room_message_sent(const std::string &room, const std::string &message)
170 {
172 }
173 
174 void chat_handler::add_chat_room_message_received(const std::string &room,
175  const std::string &speaker, const std::string &message)
176 {
177  add_chat_message(std::time(nullptr), room + ": " + speaker, 0, message, events::chat_handler::MESSAGE_PRIVATE);
178 }
179 
180 }
std::map< std::string, t_string > string_map
virtual void add_chat_room_message_received(const std::string &room, const std::string &speaker, const std::string &message)
logger & info()
Definition: log.cpp:228
virtual void send_to_server(const config &cfg)=0
virtual void add_chat_room_message_sent(const std::string &room, const std::string &message)
#define ERR_NG
Definition: chat_events.cpp:27
std::string_view data
Definition: picture.cpp:206
void send_command(const std::string &cmd, const std::string &args="")
Definition: chat_events.cpp:85
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
static std::string _(const char *str)
Definition: gettext.hpp:93
virtual void add_whisper_received(const std::string &sender, const std::string &message)
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)
void do_speak(const std::string &message, bool allies_only=false)
logger & debug()
Definition: log.cpp:234
void change_logging(const std::string &data)
Change the log level of a log domain.
Definition: chat_events.cpp:45
#define LOG_NG
Definition: chat_events.cpp:28
severity
Definition: log.hpp:73
std::string login()
logger & err()
Definition: log.cpp:216
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
bool set_log_domain_severity(const std::string &name, int severity)
Definition: log.cpp:256
config & add_child(config_key_type key)
Definition: config.cpp:514
Handling of system events.
Definition: manager.hpp:43
logger & warn()
Definition: log.cpp:222
virtual void add_whisper_sent(const std::string &receiver, const std::string &message)
Standard logging facilities (interface).
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...
static lg::log_domain log_engine("engine")
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
int get_severity() const
Returns following values depending on the logger: error: 0 warn: 1 info: 2 debug: 3 See also the lg::...
Definition: log.hpp:180
virtual void send_chat_room_message(const std::string &room, 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