split messages by newline
This commit is contained in:
parent
5eae90751d
commit
15d057e92b
@ -9,6 +9,8 @@
|
|||||||
#include <libirc_rfcnumeric.h>
|
#include <libirc_rfcnumeric.h>
|
||||||
#include <libircclient.h>
|
#include <libircclient.h>
|
||||||
|
|
||||||
|
#include "./string_view_split.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -120,20 +122,27 @@ bool IRCClientMessageManager::sendText(const Contact3 c, std::string_view messag
|
|||||||
}
|
}
|
||||||
|
|
||||||
{ // actually send
|
{ // actually send
|
||||||
std::string tmp_message{message}; // string_view might not be nul terminated
|
// split message by line
|
||||||
if (action) {
|
for (const auto& inner_str : MM::std_utils::split(message, "\n")) {
|
||||||
if (irc_cmd_me(_ircc.getSession(), to_str.c_str(), tmp_message.c_str()) != 0) {
|
if (inner_str.empty()) {
|
||||||
std::cerr << "IRCCMM error: failed to send action\n";
|
continue;
|
||||||
|
|
||||||
// we dont have offline messaging in irc
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (irc_cmd_msg(_ircc.getSession(), to_str.c_str(), tmp_message.c_str()) != 0) {
|
|
||||||
std::cerr << "IRCCMM error: failed to send message\n";
|
|
||||||
|
|
||||||
// we dont have offline messaging in irc
|
std::string tmp_message{inner_str}; // string_view might not be nul terminated
|
||||||
return false;
|
if (action) {
|
||||||
|
if (irc_cmd_me(_ircc.getSession(), to_str.c_str(), tmp_message.c_str()) != 0) {
|
||||||
|
std::cerr << "IRCCMM error: failed to send action\n";
|
||||||
|
|
||||||
|
// we dont have offline messaging in irc
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (irc_cmd_msg(_ircc.getSession(), to_str.c_str(), tmp_message.c_str()) != 0) {
|
||||||
|
std::cerr << "IRCCMM error: failed to send message\n";
|
||||||
|
|
||||||
|
// we dont have offline messaging in irc
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
49
src/solanaceae/ircclient_messages/string_view_split.hpp
Normal file
49
src/solanaceae/ircclient_messages/string_view_split.hpp
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
namespace MM::std_utils {
|
||||||
|
|
||||||
|
inline std::string_view trim_prefix(std::string_view sv) {
|
||||||
|
while (!sv.empty() && std::isspace(sv.front())) {
|
||||||
|
sv.remove_prefix(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sv;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::string_view trim_suffix(std::string_view sv) {
|
||||||
|
while (!sv.empty() && std::isspace(sv.back())) {
|
||||||
|
sv.remove_suffix(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sv;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::string_view trim(std::string_view sv) {
|
||||||
|
return trim_suffix(trim_prefix(sv));
|
||||||
|
}
|
||||||
|
|
||||||
|
// src : https://marcoarena.wordpress.com/2017/01/03/string_view-odi-et-amo/
|
||||||
|
inline std::vector<std::string_view> split(std::string_view str, const char* delims) {
|
||||||
|
std::vector<std::string_view> ret;
|
||||||
|
|
||||||
|
std::string_view::size_type start = 0;
|
||||||
|
auto pos = str.find_first_of(delims, start);
|
||||||
|
while (pos != std::string_view::npos) {
|
||||||
|
if (pos != start) {
|
||||||
|
ret.push_back(str.substr(start, pos - start));
|
||||||
|
}
|
||||||
|
start = pos + 1;
|
||||||
|
pos = str.find_first_of(delims, start);
|
||||||
|
}
|
||||||
|
if (start < str.length())
|
||||||
|
ret.push_back(str.substr(start, str.length() - start));
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // MM::std_utils
|
||||||
|
|
Loading…
Reference in New Issue
Block a user