Debian Codegraph @

src/bnetd/topic.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2015 xboi209
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 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "common/setup_before.h"
19 #include "topic.h"
20
21 #include <fstream>
22 #include <memory>
23 #include <new>
24 #include <regex>
25 #include <string>
26 #include <utility>
27
28 #include "compat/strcasecmp.h"
29
30 #include "common/eventlog.h"
31 #include "common/field_sizes.h"
32
33 #include "message.h"
34 #include "prefs.h"
35
36 #include "common/setup_after.h"
37
38 namespace pvpgn
39 {
40
41 namespace bnetd
42 {
43 std::vector<std::shared_ptr<class_topic::t_topic>> class_topic::class_topiclist::Head;
44
45 bool class_topic::class_topiclist::IsHeadLoaded = false;
46
47 class_topic::class_topic()
48 {
49 //Already loaded, do not load again
50 if (this->topiclist.IsHeadLoaded == true)
51 return;
52
53 std::ifstream topicfile_stream(prefs_get_topicfile());
54 if (!topicfile_stream)
55 {
56 eventlog(eventlog_level_error, __FUNCTION__, "couldn't open topic file");
57 return;
58 }
59
60 std::string strLine;
61 std::smatch match;
62 const std::regex rgx("(.*)\t(.*)"); // tab character separates the channel name and topic
63
64 //loop through each line in topic file
65 while (std::getline(topicfile_stream, strLine))
66 {
67 //skip empty lines
68 if (strLine.empty() == true)
69 continue;
70
71 if (!std::regex_search(strLine, match, rgx))
72 {
73 eventlog(eventlog_level_error, __FUNCTION__, "Invalid line in topic file ({})", strLine.c_str());
74 continue;
75 }
76
77 //check if current line in file already exists in Head
78 if (this->topiclist.get(match[1].str()) != nullptr)
79 {
80 eventlog(eventlog_level_error, __FUNCTION__, "Duplicate line for channel {} in topic file", match[1].str().c_str());
81 continue;
82 }
83
84 //save current line to Head
85 this->topiclist.add(match[1].str(), match[2].str(), true);
86 }
87
88 this->topiclist.IsHeadLoaded = true;
89 }
90
91 //Get channel_name's topic string
92 std::string class_topic::get(const std::string channel_name)
93 {
94 if ((channel_name.size() + 1) > MAX_CHANNELNAME_LEN || channel_name.empty() == true)
95 {
96 eventlog(eventlog_level_error, __FUNCTION__, "got invalid channel name length");
97 return std::string();
98 }
99
100 auto topic = this->topiclist.get(channel_name);
101 if (topic == nullptr)
102 return std::string();
103
104 return topic->topicstr;
105 }
106
107 //Sets channel_name's topic
108 bool class_topic::set(const std::string channel_name, const std::string topic_text, bool do_save)
109 {
110 if ((channel_name.size() + 1) > MAX_CHANNELNAME_LEN || channel_name.empty() == true)
111 {
112 eventlog(eventlog_level_error, __FUNCTION__, "got invalid channel name length");
113 return false;
114 }
115
116 if ((topic_text.size() + 1) > MAX_TOPIC_LEN || topic_text.empty() == true)
117 {
118 eventlog(eventlog_level_error, __FUNCTION__, "got invalid topic length");
119 return false;
120 }
121
122 auto topic = this->topiclist.get(channel_name);
123
124 if (topic != nullptr)
125 {
126 eventlog(eventlog_level_trace, __FUNCTION__, "Setting <{}>'s topic to <{}>", channel_name.c_str(), topic_text.c_str());
127 topic->topicstr = topic_text;
128 }
129 else
130 {
131 eventlog(eventlog_level_trace, __FUNCTION__, "Adding <{}:{}> to topiclist", channel_name.c_str(), topic_text.c_str());
132 this->topiclist.add(channel_name, topic_text, do_save);
133 }
134
135 if (do_save == true)
136 {
137 if (this->topiclist.save() == false)
138 {
139 eventlog(eventlog_level_error, __FUNCTION__, "error saving topic list");
140 return false;
141 }
142 }
143
144 return true;
145 }
146
147 // Displays channel_name's topic to connection c
148 bool class_topic::display(t_connection * c, const std::string channel_name)
149 {
150 if ((channel_name.size() + 1) > MAX_CHANNELNAME_LEN || channel_name.empty() == true)
151 {
152 eventlog(eventlog_level_error, __FUNCTION__, "got invalid channel name length");
153 return false;
154 }
155
156 auto topic = this->topiclist.get(channel_name);
157 if (topic == nullptr)
158 return false;
159
160 auto topicstr = topic->topicstr;
161
162 if (topicstr.empty())
163 {
164 eventlog(eventlog_level_error, __FUNCTION__, "topic is empty");
165 return false;
166 }
167
168 //send parts of topic string as separate message if there's a newline character
169 std::regex rgx("\\\\n+");
170 std::sregex_token_iterator iter(topicstr.begin(), topicstr.end(), rgx, -1), end;
171 for (bool first = true; iter != end; ++iter)
172 {
173 std::string msg(iter->str());
174 if (first == true)
175 {
176 msg.insert(0, topic->channel_name + " topic: ");
177 first = false;
178 }
179 message_send_text(c, message_type_info, c, msg);
180 }
181
182 return true;
183 }
184
185
186 //Get t_topic pointer of channel_name
187 std::shared_ptr<class_topic::t_topic> class_topic::class_topiclist::get(const std::string channel_name)
188 {
189 for (auto topic : this->Head)
190 {
191 if (strcasecmp(channel_name.c_str(), topic->channel_name.c_str()) == 0)
192 return topic;
193 }
194 eventlog(eventlog_level_debug, __FUNCTION__, "returning nullptr");
195
196 return nullptr;
197 }
198
199 //Saves data from Head vector to topic file
200 bool class_topic::class_topiclist::save()
201 {
202 std::fstream topicfile_stream(prefs_get_topicfile(), std::ofstream::app);
203 if (!topicfile_stream)
204 {
205 eventlog(eventlog_level_error, __FUNCTION__, "couldn't open topic file");
206 return false;
207 }
208
209 std::string strLine;
210 std::smatch match;
211 const std::regex rgx("(.*)\t(.*)"); // tab character separates the channel name and topic
212
213 //Check if data in Head vector already exists in topic file
214 while (std::getline(topicfile_stream, strLine))
215 {
216 if (!std::regex_search(strLine, match, rgx))
217 {
218 eventlog(eventlog_level_error, __FUNCTION__, "Invalid line in topic file ({})", strLine.c_str());
219 continue;
220 }
221
222 for (auto topic : this->Head)
223 {
224 if (topic->save == true)
225 {
226 if (match[1].str() == topic->channel_name)
227 {
228 break;
229 }
230 else
231 {
232 topicfile_stream << topic->channel_name << "\t" << topic->topicstr << std::endl;
233 break;
234 }
235 }
236 }
237 }
238 return true;
239 }
240
241 //Adds a new pointer to the Head vector
242 void class_topic::class_topiclist::add(std::string channel_name, std::string topic_text, bool do_save)
243 {
244 auto topic = std::make_shared<class_topic::t_topic>(class_topic::t_topic{ channel_name, topic_text, do_save });
245 this->Head.push_back(std::move(topic));
246 }
247
248 }
249
250 }