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