ColourNote called from within OnPluginTelnetSubnegotiation

Posted by Kevnuke on Sat 07 Jan 2012 05:26 AM — 9 posts, 35,993 views.

USA #0
I got a basic GMCP plugin working that simply notes the output of messages to the output window. It works as expected, except for one thing. The background color of ColourNote I use to show the GMCP messages is getting applied to some of the text that it shouldn't be.

Here is the relevant piece of the plugin..

Version 4.80


<script>
<![CDATA[

require("json")
require("tprint")

-- GMCP and Telnet Request/Subnegotiation info
local CLIENT_ID = {client="MUSHclient", version=Version()}
local codes = {
  IAC_SB_GMCP = "\255\250\201", -- begins a GMCP packet
  IAC_SE      = "\255\240",     -- ends a GMCP packet
  GMCP        = 201,            -- GMCP protocol number
}
local SB_GMCP = codes.IAC_SB_GMCP .. "%s" .. codes.IAC_SE

-- GMCP modules supported by this plugin
local GMCP_options = {
  "Core 1",
  "Char 1",
  "Char.Skills 1",
  "Char.Items 1",
  "Comm.Channel 1",
  "Room 1",
  "Redirect 1",
  "IRE.Rift 1",
  "IRE.Composer 1",
}

function SendGMCP(message, content)
  if type(message) ~= "string" then
    return nil, "Message name must be a string."
  end
  
  if content ~= nil then
    content = json.encode({content})
    if content == nil then
      return nil, "Invalid input."
    end
    message = ("%s %s"):format(message, content:sub(2, #content-1))
  end
  
  SendPkt(SB_GMCP:format(message))
  return true
end

function OnPluginTelnetRequest (opt, data)
  if opt ~= 201 then
    return false
  end
  
  if data == "SENT_DO" then
    Note("GMCP enabled.\n")
    
    SendGMCP("Core.Hello ", CLIENT_ID)
    SendGMCP("Core.Supports.Set ", GMCP_options)
  end
  
  return true
end

function OnPluginTelnetSubnegotiation (opt, data)
  if opt ~= codes.GMCP then
    return
  end

ColourNote ("blue", "white", data)
  
  local msg, content = unpack(utils.split(data, " ", 1))
  if not content or content:len() == 0 then
    content = nil
  else
    -- Not every JSON parser allows any top-level value to be valid.
    -- Ensuring that a non-object non-array value is at least within
    -- an array makes this code parser-agnostic.
    local err
    content, err = json.decode(("[%s]"):format(content))
    if content ~= nil then
      content = content[1]
    else
      error("Invalid message: " .. err)
    end
  end
  
function toggle_debug()
  gmcpdebug = not gmcpdebug
  Note(("GMCP Debugging is %s"):format(gmcpdebug and "Enabled" or "Disabled"))
end

]]>
</script>



..how do I upload screenshots?
USA #1
Kevnuke said:
..how do I upload screenshots?

Upload them to a service like imageshack.us, and post the link.

I have to ask, why are you modifying the original GMCP plugin? It's meant to emit data for other plugins to use, not to be directly modified itself. Also, there's a GMCP DEBUG alias in the original that toggles debug output, displaying GMCP data as it comes in.
USA #2
Because I'm going to have everything done in a single plugin, it doesn't need to broadcast. I wanted to see the unmodified message from the server. It's more of a personal project to improve my Lua skills. Such as how to take the server output and put it into useable tables and other variables. I saw you do it in the plugin but I wanted to understand it well enough to recreate it myself.

I'll post screenshots when I'm on my computer.
USA #3
Kevnuke said:
Because I'm going to have everything done in a single plugin, it doesn't need to broadcast. I wanted to see the unmodified message from the server. It's more of a personal project to improve my Lua skills. Such as how to take the server output and put it into useable tables and other variables. I saw you do it in the plugin but I wanted to understand it well enough to recreate it myself.

Fair enough. Just keep in mind it probably won't cooperate other plugins that need GMCP, whether you release yours for other people to use, or you install other peoples' plugins.
USA #4
Here are the screenshots for what I was talking about.

http://imageshack.us/g/502/achaea03.jpg/

What's the forum code for a hyperlink?.. I didn't see it in the link.
Amended on Sun 08 Jan 2012 09:01 PM by Kevnuke
USA #5
Links and images are tags reserved for forum admins, which basically just means Nick.

I'm not really sure why ColourNote is doing that, to be honest...
USA #6
Twisol said:

Links and images are tags reserved for forum admins, which basically just means Nick.


Thanks, good to know.

Twisol said:

I'm not really sure why ColourNote is doing that, to be honest...


Well if anything my little experiment maybe discovered a bug?
Australia Forum Administrator #7
Kevnuke said:

I got a basic GMCP plugin working that simply notes the output of messages to the output window. It works as expected, except for one thing. The background color of ColourNote I use to show the GMCP messages is getting applied to some of the text that it shouldn't be.


Doing a ColourNote during telnet negotiation may lead to strange results. You are injecting a new line into what might be a partial line received from the MUD.

If you must do this, I would queue it up (eg. with DoAfterNote) and have it appear in a more orderly way.
USA #8
Thanks for the explanation, Nick. I just got the memo because I forgot to subscribe to this thread lol.

Holy crap! Working on a Mac really screwed me up! I got used to using the scroll wheel on my mouse in the reverse direction in the 4 days that I couldn't use my laptop..