The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
chat_events.cpp
Go to the documentation of this file.
1 #include "chat_events.hpp"
2 #include "global.hpp"
3 
4 #include "config_assign.hpp"
5 #include "formatter.hpp"
7 #include "gettext.hpp"
8 #include "log.hpp"
10 #include "chat_command_handler.hpp"
11 #include "preferences.hpp"
12 #include "game_preferences.hpp"
13 
14 #include <boost/range/algorithm/find_if.hpp>
15 
16 static lg::log_domain log_engine("engine");
17 #define ERR_NG LOG_STREAM(err, log_engine)
18 #define LOG_NG LOG_STREAM(info, log_engine)
19 
20 namespace events {
21 
23 {
24 }
25 
27 {
28 }
29 
30 /**
31 * Change the log level of a log domain.
32 *
33 * @param data string of the form: "@<level@> @<domain@>"
34 */
36  const std::string::const_iterator j =
37  std::find(data.begin(), data.end(), ' ');
38  if (j == data.end()) return;
39  const std::string level(data.begin(), j);
40  const std::string domain(j + 1, data.end());
41  int severity;
42  if (level == "error") severity = lg::err().get_severity();
43  else if (level == "warning") severity = lg::warn().get_severity();
44  else if (level == "info") severity = lg::info().get_severity();
45  else if (level == "debug") severity = lg::debug().get_severity();
46  else {
47  utils::string_map symbols;
48  symbols["level"] = level;
49  const std::string& msg =
50  vgettext("Unknown debug level: '$level'.", symbols);
51  ERR_NG << msg << std::endl;
52  add_chat_message(time(nullptr), _("error"), 0, msg);
53  return;
54  }
55  if (!lg::set_log_domain_severity(domain, severity)) {
56  utils::string_map symbols;
57  symbols["domain"] = domain;
58  const std::string& msg =
59  vgettext("Unknown debug domain: '$domain'.", symbols);
60  ERR_NG << msg << std::endl;
61  add_chat_message(time(nullptr), _("error"), 0, msg);
62  return;
63  }
64  else {
65  utils::string_map symbols;
66  symbols["level"] = level;
67  symbols["domain"] = domain;
68  const std::string& msg =
69  vgettext("Switched domain: '$domain' to level: '$level'.", symbols);
70  LOG_NG << msg << "\n";
71  add_chat_message(time(nullptr), "log", 0, msg);
72  }
73 }
74 
75 void chat_handler::send_command(const std::string& cmd, const std::string& args /* = "" */) {
76  config data;
77  if (cmd == "muteall") {
78  data.add_child(cmd);
79  }
80  else if (cmd == "query") {
81  data.add_child(cmd)["type"] = args;
82  }
83  else if (cmd == "ban" || cmd == "unban" || cmd == "kick"
84  || cmd == "mute" || cmd == "unmute") {
85  data.add_child(cmd)["username"] = args;
86  }
87  else if (cmd == "ping") {
88  data[cmd] = std::to_string(time(nullptr));
89  }
90  else if (cmd == "green") {
91  data.add_child("query")["type"] = "lobbymsg @" + args;
92  }
93  else if (cmd == "red") {
94  data.add_child("query")["type"] = "lobbymsg #" + args;
95  }
96  else if (cmd == "yellow") {
97  data.add_child("query")["type"] = "lobbymsg <255,255,0>" + args;
98  }
99  else if (cmd == "report") {
100  data.add_child("query")["type"] = "report " + args;
101  }
102  else if (cmd == "join") {
103  data.add_child("room_join")["room"] = args;
104  }
105  else if (cmd == "part") {
106  data.add_child("room_part")["room"] = args;
107  }
108  send_to_server(data);
109 }
110 
111 void chat_handler::do_speak(const std::string& message, bool allies_only)
112 {
113  if (message == "" || message == "/") {
114  return;
115  }
116  bool is_command = (message[0] == '/');
117  bool quoted_command = (is_command && message[1] == ' ');
118 
119  if (!is_command) {
120  send_chat_message(message, allies_only);
121  return;
122  }
123  else if (quoted_command) {
124  send_chat_message(std::string(message.begin() + 2, message.end()), allies_only);
125  return;
126  }
127  std::string cmd(message.begin() + 1, message.end());
128  chat_command_handler cch(*this, allies_only);
129  cch.dispatch(cmd);
130 }
131 
133 {
134 }
135 
137 {
138  config cwhisper, data;
139  cwhisper["receiver"] = receiver;
140  cwhisper["message"] = message;
141  cwhisper["sender"] = preferences::login();
142  data.add_child("whisper", cwhisper);
143  send_to_server(data);
144 }
145 
147 {
148  utils::string_map symbols;
149  symbols["receiver"] = receiver;
150  add_chat_message(time(nullptr), VGETTEXT("whisper to $receiver", symbols), 0, message);
151 }
152 
154 {
155  utils::string_map symbols;
156  symbols["sender"] = sender;
157  add_chat_message(time(nullptr), VGETTEXT("whisper: $sender", symbols), 0, message);
158 }
159 
161  const std::string& message)
162 {
163  config cmsg, data;
164  cmsg["room"] = room;
165  cmsg["message"] = message;
166  cmsg["sender"] = preferences::login();
167  data.add_child("message", cmsg);
168  send_to_server(data);
169 }
170 
172 {
174 }
175 
177  const std::string &speaker, const std::string &message)
178 {
179  add_chat_message(time(nullptr), room + ": " + speaker, 0, message, events::chat_handler::MESSAGE_PRIVATE);
180 }
181 
182 }
GLint level
Definition: glew.h:1220
virtual void add_chat_room_message_received(const std::string &room, const std::string &speaker, const std::string &message)
logger & info()
Definition: log.cpp:91
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:17
void send_command(const std::string &cmd, const std::string &args="")
Definition: chat_events.cpp:75
int get_severity() const
Definition: log.hpp:127
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
virtual void add_whisper_received(const std::string &sender, const std::string &message)
bool set_log_domain_severity(std::string const &name, int severity)
Definition: log.cpp:117
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
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)
std::map< std::string, t_string > string_map
config & add_child(const std::string &key)
Definition: config.cpp:743
void do_speak(const std::string &message, bool allies_only=false)
logger & debug()
Definition: log.cpp:97
void change_logging(const std::string &data)
Change the log level of a log domain.
Definition: chat_events.cpp:35
#define LOG_NG
Definition: chat_events.cpp:18
GLenum severity
Definition: glew.h:2497
std::string login()
logger & err()
Definition: log.cpp:79
#define VGETTEXT(msgid, symbols)
virtual void add_chat_message(const time_t &time, const std::string &speaker, int side, const std::string &message, MESSAGE_TYPE type=MESSAGE_PRIVATE)=0
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
std::string vgettext(const char *msgid, const utils::string_map &symbols)
Handling of system events.
Definition: manager.hpp:42
logger & warn()
Definition: log.cpp:85
bool find(E event, F functor)
Tests whether an event handler is available.
virtual void add_whisper_sent(const std::string &receiver, const std::string &message)
Standard logging facilities (interface).
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
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:83
virtual void send_chat_room_message(const std::string &room, const std::string &message)
GLsizei const GLcharARB ** string
Definition: glew.h:4503