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