Skip to content

Commit

Permalink
Command /topic modifications:
Browse files Browse the repository at this point in the history
* new line symbol "\n" is available
* remove required channel name from arguments: /topic [message]
#6
  • Loading branch information
HarpyWar committed Apr 21, 2014
1 parent 8657c02 commit 0e72ec9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 69 deletions.
6 changes: 3 additions & 3 deletions conf/bnhelp.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ Example: /voice user
Removes <username> 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.
Expand Down
84 changes: 23 additions & 61 deletions src/bnetd/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 3 additions & 5 deletions src/bnetd/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
48 changes: 48 additions & 0 deletions src/bnetd/topic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
#include <cstdio>

#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"

Expand Down Expand Up @@ -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;
}


}

}
3 changes: 3 additions & 0 deletions src/bnetd/topic.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef INCLUDED_TOPIC_TYPES
#define INCLUDED_TOPIC_TYPES

#include "connection.h"

namespace pvpgn
{

Expand Down Expand Up @@ -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);

}

Expand Down

0 comments on commit 0e72ec9

Please sign in to comment.