split messages by newline

This commit is contained in:
Green Sky 2023-12-18 00:38:55 +01:00
parent 5eae90751d
commit 15d057e92b
No known key found for this signature in database
2 changed files with 70 additions and 12 deletions

View File

@ -9,6 +9,8 @@
#include <libirc_rfcnumeric.h>
#include <libircclient.h>
#include "./string_view_split.hpp"
#include <cstdint>
#include <string_view>
#include <vector>
@ -120,20 +122,27 @@ bool IRCClientMessageManager::sendText(const Contact3 c, std::string_view messag
}
{ // actually send
std::string tmp_message{message}; // string_view might not be nul terminated
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;
// split message by line
for (const auto& inner_str : MM::std_utils::split(message, "\n")) {
if (inner_str.empty()) {
continue;
}
} 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;
std::string tmp_message{inner_str}; // string_view might not be nul terminated
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;
}
}
}
}

View 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