0

I am working on a simple plugin for a game emulator in C++. The purpose of the plugin is to detect if message posted by user contains more than 3 spaces or message with these strings: : or ;

The code looks like this so far:

#include "common/hercules.h"
#include "common/memmgr.h"
#include "common/mmo.h"
#include "common/socket.h"
#include "common/strlib.h"
#include "map/clif.h"
#include "map/pc.h"

#include "plugins/HPMHooking.h"
#include "common/HPMDataCheck.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

HPExport struct hplugin_info pinfo = {
    "GM Impersonate",   // Plugin name  
    SERVER_TYPE_MAP,    // Which server types this plugin works with?
    "1.0",              // Plugin version
    HPM_VERSION,        // HPM Version (don't change, macro is automatically updated)
};

bool my_pc_process_chat_message(bool retVal___, struct map_session_data *sd, const char *message) {
    if (retVal___ == true) {
        if (stristr(message, "    ")) {
            clif->messagecolor_self(sd->fd, COLOR_RED, "Possible GM Impersonation Detected - you cannot use more than 3 spaces in chat.");
            return false;
        }
        if (stristr(message, " : ") || stristr(message, " ; ")) {
            clif->messagecolor_self(sd->fd, COLOR_RED, "Possible GM Impersonation Detected - you cannot use : or ; in chat.");
            return false;
        }
    }
    return true;
}

HPExport void plugin_init(void) {
    addHookPost(pc, process_chat_message, my_pc_process_chat_message);
}

I am not a C++ programmer, I used a sample plugin to work this code out, which appears to be working miraculously. I am now trying to improve this little plugin to detect invisible glyphs, as if they were spaces, in the message char array.

How can I achieve this?

I found this post - https://stackoverflow.com/a/15813530/2332336 but this appears to be for string, not char array. Any ideas?

Community
  • 1
  • 1
Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • When sanitizing strings it is usually better to whitelist than to blacklist. – stark Jul 11 '16 at 19:50
  • What does "invisible / Non-ASCII" mean? Do you want to disallow all non-ASCII characters, like € or é? Do you want to disallow all "invisible" characters (whatever that might mean)? – anatolyg Jul 11 '16 at 19:54
  • 1
    Have a look at `std::isprint `: http://en.cppreference.com/w/cpp/string/byte/isprint – Richard Critten Jul 11 '16 at 19:57
  • [`std::remove_if`](http://en.cppreference.com/w/cpp/algorithm/remove) will work on your byte array. You just need a replacement for string's `erase`. Hint: Just write `'\0'` at the location returned by `std::remove`. – davidbak Jul 11 '16 at 20:00
  • I based my code on someone else's old submission. It looked something like this: http://pastebin.com/rkbTh3ry - this code doesnt work in the new plugin system i am using, so I have re-written it as above. But if you see in this code, it appears the person is making a new string and replacing all invisible chars `xA0` into space before checking. I am not sure what `xA0` is - I assumed there were more than one of these types of invisible characters. I wanted a all-in-one solution to replace all invisible characters into space before checking if space count > 3. – Latheesan Jul 11 '16 at 20:01
  • BTW - write/test your code in some simple environment, not the plugin environment you're using. E.g., a simple command line/console program. – davidbak Jul 11 '16 at 20:01
  • @anatolyg - sorry I think I worded it wrong, I meant detect invisible characters and replace them into space, and then check if total consecutive space count > 3. – Latheesan Jul 11 '16 at 20:04
  • Become a C++ programmer, or hire a C++ programmer to program your C++. – Lightness Races in Orbit Jul 11 '16 at 21:58

1 Answers1

0

I'm not sure the solution you linked to is the same as "invisible/Non-ASCII" as it seems only to deal with stripping out 8-bit ASCII codes. However if you want to try the solution you linked to, there is no reason to let the difference in char arrays and strings stand in your way. The String class has a constructor that will work to convert for you.

string mystring(message);

Then you can feel free to use the "stripUnicode" method from the linked solution to see if that gets what you were expecting.

Brian Cross
  • 142
  • 1
  • 7
  • is `mystring` a c++ function? – Latheesan Jul 11 '16 at 20:09
  • mystring (or whatever you want to name it) would be a new object variable of type string. It looks like the method stripUnicode would take it as a parameter and work to remove the unicode bits from it. You do not have to name it "mystring" and I would recommend naming it something that makes more sense for what you are doing. – Brian Cross Jul 11 '16 at 20:11
  • Looks like I don't have access to `string` in this C++ project I am using, i get this error: `identifier "string" is undefined` – Latheesan Jul 11 '16 at 20:13
  • We can't teach all the basics of C++ here, @Latheesan. Pick up a good C++ book or hire a C++ programmer to complete the work! – Lightness Races in Orbit Jul 11 '16 at 21:59