diff --git a/include/neocomm.h b/include/neocomm.h index a8928a2..8f19501 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -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(); diff --git a/src/channel.cpp b/src/channel.cpp index f906527..08da9b5 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -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) {