Improved channel support.

This commit is contained in:
Nicolás Ortega Froysa 2017-11-17 17:18:33 +01:00
parent c6eb1d6b09
commit 909a5ff740
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
2 changed files with 32 additions and 5 deletions

View File

@ -137,8 +137,11 @@ int NeoComm_join_channel(const char *channel_name);
* @brief Leave a channel.
*
* @param channel_name Name of the channel to disconnect from.
*
* @return 1 upon success, 0 upon failure. Use NeoComm_get_last_error for a
* text description of the error.
*/
void NeoComm_leave_channel(const char *channel_name);
int NeoComm_leave_channel(const char *channel_name);
/**
* @brief Get the next available message (in chronological order) for
@ -189,7 +192,8 @@ int NeoComm_check_message(const time_t token);
/**
* @brief Get the last error that occurred in text.
*
* @return A string with the last error occurred.
* @return A string with the last error occurred, if there are no errors it
* return NULL.
*/
const char *NeoComm_get_last_error();

View File

@ -34,6 +34,11 @@ int NeoComm_join_channel(const char *channel_name) {
add_error("NeoComm must be initialized.");
return 0;
}
else if(channel_name[0] != '#')
{
add_error("Channel name must start with `#'.");
return 0;
}
const std::string s_chan_name(channel_name);
if(s_chan_name.empty())
@ -66,17 +71,35 @@ int NeoComm_join_channel(const char *channel_name) {
return 1;
}
void NeoComm_leave_channel(const char *channel_name) {
int NeoComm_leave_channel(const char *channel_name) {
if(not node.isRunning())
return;
{
add_error("NeoComm must be initialized.");
return 0;
}
else if(channel_name[0] != '#')
{
add_error("Channel name must start with `#'.");
return 0;
}
const std::string s_chan_name(channel_name);
if(channels.find(s_chan_name) == channels.end())
return;
{
add_error("Channel does not exist.");
return 0;
}
else if(s_chan_name.empty())
{
add_error("Chan name is empty.");
return 0;
}
struct channel_info *channel = &channels[s_chan_name];
channel->token.wait();
node.cancelListen(channel->hash, channel->token.get());
channels.erase(s_chan_name);
return 1;
}
struct message *NeoComm_get_next_message(const char *channel_name) {