diff --git a/conf/bnhelp.conf.in b/conf/bnhelp.conf.in index e23ce9502..ea3bba914 100644 --- a/conf/bnhelp.conf.in +++ b/conf/bnhelp.conf.in @@ -299,9 +299,9 @@ Example: /voice user Removes from the VOP list and removes temporary voice privileges. Example: /devoice user %topic -/topic [[channel] "[message]"] -Sets or displays [channel]'s topic. -Example: /topic Moderated Support "Supported is given in this channel..." +/topic [message] +Sets or displays current channel's topic. +Example: /topic Supported is given in this channel...\n(some text on a new line) %moderate /moderate Toggles the current channel's moderated status. diff --git a/src/bnetd/command.cpp b/src/bnetd/command.cpp index eca3196ae..c02cd0f45 100644 --- a/src/bnetd/command.cpp +++ b/src/bnetd/command.cpp @@ -5014,87 +5014,49 @@ namespace pvpgn static int _handle_topic_command(t_connection * c, char const * text) { char const * channel_name; - char const * topic; - char * tmp; + const char * topic; t_channel * channel; int do_save = NO_SAVE_TOPIC; - channel_name = skip_command(text); + topic = skip_command(text); - if ((topic = std::strchr(channel_name, '"'))) - { - tmp = (char *)topic; - for (tmp--; tmp[0] == ' '; tmp--); - tmp[1] = '\0'; - topic++; - tmp = std::strchr((char *)topic, '"'); - if (tmp) tmp[0] = '\0'; - } - if (!(conn_get_channel(c))) { + if (!(channel = conn_get_channel(c))) { message_send_text(c, message_type_error, c, "This command can only be used inside a channel."); return -1; } - if (channel_name[0] == '\0') - { - if (channel_get_topic(channel_get_name(conn_get_channel(c)))) - { - snprintf(msgtemp, sizeof(msgtemp), "%.64s topic: %.128s", channel_get_name(conn_get_channel(c)), channel_get_topic(channel_get_name(conn_get_channel(c)))); - } - else - { - snprintf(msgtemp, sizeof(msgtemp), "%.64s topic: no topic", channel_get_name(conn_get_channel(c))); - } - message_send_text(c, message_type_info, c, msgtemp); - - return 0; - } + channel_name = channel_get_name(channel); - if (!(topic)) + // set channel topic + if (topic[0] != '\0') { - if (channel_get_topic(channel_name)) + if (std::strlen(topic) >= MAX_TOPIC_LEN) { - snprintf(msgtemp, sizeof(msgtemp), "%.64s topic: %.128s", channel_name, channel_get_topic(channel_name)); + snprintf(msgtemp, sizeof(msgtemp), "Max topic length exceeded (max %d symbols)", MAX_TOPIC_LEN); + message_send_text(c, message_type_error, c, msgtemp); + return -1; } - else - { - snprintf(msgtemp, sizeof(msgtemp), "%.64s topic: no topic", channel_name); + + if (!(account_is_operator_or_admin(conn_get_account(c), channel_name))) { + snprintf(msgtemp, sizeof(msgtemp), "You must be at least a Channel Operator of %.64s to set the topic", channel_name); + message_send_text(c, message_type_error, c, msgtemp); + return -1; } - message_send_text(c, message_type_info, c, msgtemp); - return 0; - } - if (!(channel = channellist_find_channel_by_name(channel_name, conn_get_country(c), realm_get_name(conn_get_realm(c))))) - { - snprintf(msgtemp, sizeof(msgtemp), "There is no such channel."); - message_send_text(c, message_type_error, c, msgtemp); - return -1; - } + if (channel_get_permanent(channel)) + do_save = DO_SAVE_TOPIC; - if (std::strlen(topic) >= MAX_TOPIC_LEN) - { - snprintf(msgtemp, sizeof(msgtemp), "Max topic length exceeded (max %d symbols)", MAX_TOPIC_LEN); - message_send_text(c, message_type_error, c, msgtemp); - return -1; + channel_set_topic(channel_name, topic, do_save); } - channel_name = channel_get_name(channel); - - if (!(account_is_operator_or_admin(conn_get_account(c), channel_name))) { - snprintf(msgtemp, sizeof(msgtemp), "You must be at least a Channel Operator of %.64s to set the topic", channel_name); - message_send_text(c, message_type_error, c, msgtemp); - return -1; + // display channel topic + if (channel_display_topic(c, channel_name) < 0) + { + snprintf(msgtemp, sizeof(msgtemp), "%.64s topic: no topic", channel_name); + message_send_text(c, message_type_info, c, msgtemp); } - if (channel_get_permanent(channel)) - do_save = DO_SAVE_TOPIC; - - channel_set_topic(channel_name, topic, do_save); - - snprintf(msgtemp, sizeof(msgtemp), "%.64s topic: %.128s", channel_name, topic); - message_send_text(c, message_type_info, c, msgtemp); - return 0; } diff --git a/src/bnetd/connection.cpp b/src/bnetd/connection.cpp index 04c8c7139..ed473854c 100644 --- a/src/bnetd/connection.cpp +++ b/src/bnetd/connection.cpp @@ -2071,12 +2071,10 @@ namespace pvpgn message_send_text(c, message_type_info, c, msgtemp); } - if (channel_get_topic(channel_get_name(c->protocol.chat.channel)) && ((conn_is_irc_variant(c)) == 0)) + + if (conn_is_irc_variant(c) == 0) { - char msgtemp[MAX_MESSAGE_LEN]; - - std::sprintf(msgtemp, "%s topic: %s", channel_get_name(c->protocol.chat.channel), channel_get_topic(channel_get_name(c->protocol.chat.channel))); - message_send_text(c, message_type_info, c, msgtemp); + channel_display_topic(c, channel_get_name(c->protocol.chat.channel)); } if (c->protocol.chat.channel && (channel_get_flags(c->protocol.chat.channel) & channel_flags_moderated)) diff --git a/src/bnetd/topic.cpp b/src/bnetd/topic.cpp index 37473e078..9dcc3dda8 100644 --- a/src/bnetd/topic.cpp +++ b/src/bnetd/topic.cpp @@ -20,11 +20,15 @@ #include #include "compat/strcasecmp.h" +#include "compat/snprintf.h" + #include "common/list.h" #include "common/eventlog.h" #include "common/xalloc.h" #include "common/field_sizes.h" +#include "message.h" + #include "prefs.h" #include "common/setup_after.h" @@ -207,6 +211,50 @@ namespace pvpgn return 0; } + /* Display topic with new lines formatting (\n) */ + int channel_display_topic(t_connection * c, const char * channel_name) + { + char msgtemp[MAX_MESSAGE_LEN]; + char *topic, *tmp, *token; + char * delim = "\\"; + if (!(topic = channel_get_topic(channel_name))) + return -1; + + tmp = xstrdup(topic); + + token = strtok(tmp, delim); + bool first = true; + bool start_with_bs = (topic[0] == '\\'); + + while (token) + { + if (token[0] == 'n') + { + *token++; + if (first && start_with_bs) + { + snprintf(msgtemp, sizeof(msgtemp), "%.64s topic:", channel_name); + message_send_text(c, message_type_info, c, msgtemp); + first = false; + } + } + + if (first) + snprintf(msgtemp, sizeof(msgtemp), "%.64s topic: %.128s", channel_name, token); + else + snprintf(msgtemp, sizeof(msgtemp), token); + + message_send_text(c, message_type_info, c, msgtemp); + + token = strtok(NULL, delim); + first = false; + } + xfree((void *)tmp); + + return 0; + } + + } } diff --git a/src/bnetd/topic.h b/src/bnetd/topic.h index 07fd16c58..6352389ba 100644 --- a/src/bnetd/topic.h +++ b/src/bnetd/topic.h @@ -17,6 +17,8 @@ #ifndef INCLUDED_TOPIC_TYPES #define INCLUDED_TOPIC_TYPES +#include "connection.h" + namespace pvpgn { @@ -56,6 +58,7 @@ namespace pvpgn int topiclist_unload(void); int channel_set_topic(char const * channel_name, char const * topic_text, int do_save); char * channel_get_topic(char const * channel_name); + int channel_display_topic(t_connection * c, const char * channel_name); }