/alert command with messagebox_show function

https://github.com/HarpyWar/pvpgn/issues/15
This commit is contained in:
HarpyWar 2014-05-24 20:55:52 +04:00
parent af2baccdb8
commit d4428c1971
10 changed files with 120 additions and 1 deletions

View file

@ -211,6 +211,13 @@
Example: /realmann Hello everyone!
%alert
--------------------------------------------------------
/alert <message>
Show MessageBox with <message> to everyone. Use \n as a new line symbol.
Example: /alert Hello\neveryone!
%news
--------------------------------------------------------
/news

View file

@ -150,6 +150,7 @@
2 /realmann
2 /ann /announce
2 /alert
3 /serverban /ipban
3 /ipscan

View file

@ -371,6 +371,7 @@ namespace pvpgn
static int _handle_moderate_command(t_connection * c, char const * text);
static int _handle_clearstats_command(t_connection * c, char const * text);
static int _handle_tos_command(t_connection * c, char const * text);
static int _handle_alert_command(t_connection * c, char const * text);
static const t_command_table_row standard_command_table[] =
{
@ -487,6 +488,7 @@ namespace pvpgn
{ "/moderate", _handle_moderate_command },
{ "/clearstats", _handle_clearstats_command },
{ "/icon", handle_icon_command },
{ "/alert", _handle_alert_command },
{ NULL, NULL }
@ -5062,5 +5064,57 @@ namespace pvpgn
return 0;
}
/* Send message to all clients (similar to announce, but in messagebox) */
static int _handle_alert_command(t_connection * c, char const * text)
{
char const * channel_name;
const char * topic;
t_clienttag clienttag;
t_clienttag clienttag_dest;
clienttag = conn_get_clienttag(c);
// disallow clients that doesn't support SID_MESSAGEBOX
if (clienttag != CLIENTTAG_STARCRAFT_UINT && clienttag != CLIENTTAG_BROODWARS_UINT && clienttag != CLIENTTAG_STARJAPAN_UINT && clienttag != CLIENTTAG_SHAREWARE_UINT &&
clienttag != CLIENTTAG_DIABLORTL_UINT && clienttag != CLIENTTAG_DIABLOSHR_UINT &&
clienttag != CLIENTTAG_WARCIIBNE_UINT && clienttag != CLIENTTAG_BNCHATBOT_UINT)
{
message_send_text(c, message_type_error, c, "Your game client doesn't support MessageBox.");
return -1;
}
std::vector<std::string> args = split_command(text, 1);
if (args[1].empty())
{
describe_command(c, args[0].c_str());
return 0;
}
// reduntant line - it adds a caption to message box
std::string goodtext = args[1] + "\n\n***************************\nBy " + conn_get_username(c);
// caption
snprintf(msgtemp, sizeof(msgtemp), "Information from %.64s", prefs_get_servername());
t_connection * conn;
t_elem const * curr;
// send to online users
LIST_TRAVERSE_CONST(connlist(), curr)
{
if (conn = (t_connection*)elem_get_data(curr))
{
clienttag_dest = conn_get_clienttag(conn);
if (clienttag_dest != CLIENTTAG_STARCRAFT_UINT && clienttag_dest != CLIENTTAG_BROODWARS_UINT && clienttag_dest != CLIENTTAG_STARJAPAN_UINT && clienttag_dest != CLIENTTAG_SHAREWARE_UINT &&
clienttag_dest != CLIENTTAG_DIABLORTL_UINT && clienttag_dest != CLIENTTAG_DIABLOSHR_UINT &&
clienttag_dest != CLIENTTAG_WARCIIBNE_UINT && clienttag_dest != CLIENTTAG_BNCHATBOT_UINT) {
continue;
}
messagebox_show(conn, goodtext.c_str(), msgtemp);
}
}
return 0;
}
}
}

View file

@ -22,6 +22,7 @@
#include <cstring>
#include <cerrno>
#include <string>
#include "compat/gethostname.h"
#include "common/xalloc.h"
@ -33,6 +34,7 @@
#include "common/bn_type.h"
#include "common/list.h"
#include "common/util.h"
#include "common/xstring.h"
#include "account.h"
#include "account_wrap.h"
@ -1712,6 +1714,26 @@ namespace pvpgn
return 0;
}
/* Show message box on a client side (https://github.com/HarpyWar/pvpgn/issues/15) */
extern int messagebox_show(t_connection * dst, char const * text, char const * caption, int type)
{
t_packet *rpacket;
std::string newtext = str_replace_nl(text);
if ((rpacket = packet_create(packet_class_bnet)))
{
packet_set_size(rpacket, sizeof(t_server_messagebox));
packet_set_type(rpacket, SERVER_MESSAGEBOX);
bn_int_set(&rpacket->u.server_messagebox.style, type);
packet_append_string(rpacket, newtext.c_str());
packet_append_string(rpacket, caption);
conn_push_outqueue(dst, rpacket);
packet_del_ref(rpacket);
}
return 0;
}
}
}

View file

@ -121,6 +121,7 @@ namespace pvpgn
#include <cstdio>
#define JUST_NEED_TYPES
#include "connection.h"
#include "common/bnet_protocol.h"
#undef JUST_NEED_TYPES
namespace pvpgn
@ -140,6 +141,7 @@ namespace pvpgn
extern int message_send_text(t_connection * dst, t_message_type type, t_connection * src, char const * text);
extern int message_send_formatted(t_connection * dst, char const * text);
extern int message_send_file(t_connection * dst, std::FILE * fd);
extern int messagebox_show(t_connection * dst, char const * text, char const * caption = "", int type = SERVER_MESSAGEBOX_OK);
}

View file

@ -4052,7 +4052,21 @@ namespace pvpgn
*/
} PACKED_ATTR() t_server_clanmemberupdate;
#define SERVER_MESSAGEBOX 0x19ff
typedef struct
{
t_bnet_header h;
bn_int style;
/* Text */
/* Caption */
} PACKED_ATTR() t_server_messagebox;
#define SERVER_MESSAGEBOX_OK 0x00000000
#define SERVER_MESSAGEBOX_OKCANCEL 0x00000001
#define SERVER_MESSAGEBOX_YESNO 0x00000004
}
#endif

View file

@ -746,6 +746,8 @@ namespace pvpgn
return "SERVER_CLANMEMBER_REMOVED_NOTIFY";
case SERVER_CLANMEMBERUPDATE:
return "SERVER_CLANMEMBERUPDATE";
case SERVER_MESSAGEBOX:
return "SERVER_MESSAGEBOX";
}
return "unknown";

View file

@ -365,6 +365,8 @@ namespace pvpgn
t_server_claninforeply server_claninforeply;
t_client_findanongame_profile_clan client_findanongame_profile_clan;
t_server_findanongame_profile_clan server_findanongame_profile_clan;
t_server_messagebox server_messagebox;
} u;
} t_packet;

View file

@ -24,6 +24,7 @@
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include "compat/strdup.h"
#include "common/xalloc.h"
@ -365,4 +366,17 @@ namespace pvpgn
return result;
}
/* Replace "\n" in string to a new line character '\n' */
extern std::string str_replace_nl(char const * text)
{
std::string s(text);
size_t pos = 0;
while ((pos = s.find("\\n", pos)) != std::string::npos) {
s.replace(pos, 2, "\n");
}
return s;
}
}

View file

@ -34,6 +34,7 @@ namespace pvpgn
extern char * str_strip_affix(char * str, char const * affix);
extern const char *str_replace(char *orig, char *rep, char *with);
extern std::vector<std::string> split_command(char const * text, int args_count);
extern std::string str_replace_nl(char const * text);
/*
Fix for std::string for some unix compilers