Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Trying to convert plugin to Lua need help with URI encoding

Trying to convert plugin to Lua need help with URI encoding

Posting of new messages is disabled at present.

Refresh page


Posted by Cage_fire_2000   USA  (119 posts)  Bio
Date Fri 05 Sep 2008 02:09 PM (UTC)

Amended on Fri 05 Sep 2008 02:10 PM (UTC) by Cage_fire_2000

Message
Hi, I have a plugin which allows you to launch web searches from MUSHclient, right now it's written in JScript, and I use encodeURIComponent() to encode the search parameters, is there anything equivilant in Lua? I'd rather not code it from scratch since I'm not that familiar with Lua yet, and it just seems like a lot of work for little benefit.

Edit: Oh, and before I forget I'd also need something like encodeURI().
Top

Posted by Beale   (35 posts)  Bio
Date Reply #1 on Fri 05 Sep 2008 02:50 PM (UTC)
Message
No - the Lua libraries are somewhat sparse for various reasons, while Javascripts are somewhat large for various other reasons.

With the usefulness that is the added utils functions in MUSHClient, though, you can throw together something like this fairly easily:



-- Modes: 1 - escape, 2 - encodeURI, 3 - encodeURIComponent
function escapeURIs(sIn, mode)
  find=string.find -- Shortcut to avoid repeat lookups
  sOut=""

  -- Characters to omit for escape, encodeURI 
  --   and encodeURIcomponent respectively
  searchStrings={[[@*/+]], [[~!@#$&*()=:/,;?+']], [[~!*()']]}
  search = searchStrings[mode]

  -- Iterate over each character in the string
  for char in string.gmatch(sIn, ".") do

    -- Check character against omissions and convert
    if find(search, char, 1, true) == nil then 
      sOut=sOut..'%'..utils.tohex(char) 
    else 
      sOut=sOut..char 
    end
  end

  return sOut
end


This should serve your needs, but be aware it's not comprehensively tested. (I checked it against a few strings in each mode, but that's it.)
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #2 on Fri 05 Sep 2008 04:44 PM (UTC)
Message
You could probably lift this function from one of the various Lua web modules, like Kepler (www.keplerproject.org) -- that would be likely to be quite tested and handle the various subtle issues that this kind of thing runs into. Furthermore, the license is very liberal (MIT) so you wouldn't have issues with that.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #3 on Sat 06 Sep 2008 03:35 AM (UTC)
Message
See:

http://www.gammon.com.au/forum/?id=8319

There are a few other pages that describe connecting to the Internet with Lua using luasocket.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,849 views.

Posting of new messages is disabled at present.

Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.