Skip to content

Instantly share code, notes, and snippets.

@but3k4
Last active September 4, 2015 11:31
Show Gist options
  • Save but3k4/8c25bca6076c545b2a34 to your computer and use it in GitHub Desktop.
Save but3k4/8c25bca6076c545b2a34 to your computer and use it in GitHub Desktop.

Revisions

  1. but3k4 revised this gist Sep 4, 2015. 1 changed file with 15 additions and 33 deletions.
    48 changes: 15 additions & 33 deletions edbot.lua
    Original file line number Diff line number Diff line change
    @@ -2,31 +2,7 @@
    local http = require("socket.http")
    local string = require("string")
    local ltn12 = require ("ltn12")

    local function encode_table(table)
    local str = ""

    local function char2hexcode(c)
    return string.format("%%%02X", string.byte(c))
    end

    local function escape(str)
    str = string.gsub(str, "\n", "\r\n")
    str = string.gsub(str, "([^0-9a-zA-Z_. -])", char2hexcode)
    str = string.gsub(str, " ", "+")
    return str
    end

    for key, vals in pairs(table) do
    if type(vals) ~= "table" then
    vals = {vals}
    end
    for k, val in pairs(vals) do
    str = str .. "&" .. escape(key) .. "=" ..escape(val)
    end
    end
    return string.sub(str,2)
    end
    local funcs = (loadfile "./libs/functions.lua")()

    local function edbot(msg)
    local params = {
    @@ -39,22 +15,26 @@ local function edbot(msg)
    ['msg'] = tostring(msg),
    }

    local data = encode_table(params)
    local body = funcs.encode_table(params)
    local response = {}

    r, c, h = http.request ({
    ok, code, headers, status = http.request ({
    method = "POST",
    url = "http://www.ed.conpet.gov.br/mod_perl/bot_gateway.cgi",
    headers = {
    ["content-type"] = "application/x-www-form-urlencoded",
    ["content-length"] = string.len(data),
    ["content-length"] = tostring(#body),
    },
    source = ltn12.source.string(data),
    source = ltn12.source.string(body),
    sink = ltn12.sink.table(response)
    })

    if code ~= 200 then
    return "Error: " .. status:gsub('HTTP/1.1', ''):gsub('^ ', '')
    end

    if response[1] ~= nil then
    return tostring(response[1]):gsub('<[^<>]*>', ''):gsub('\n', '')
    return tostring(response[1]):gsub('<[^<>]*>', ''):gsub('\n', ''):gsub('<a href="#', '')
    end
    end

    @@ -63,8 +43,10 @@ function run(msg, matches)
    end

    return {
    description = "Edbot",
    usage = "toguro: word or phrase",
    patterns = { "^toguro: (.*)$" },
    description = "Edbot plugin",
    usage = "Me explica: subject or math expression.",
    patterns = {
    "^[Mm]e explica: (.*)$",
    },
    run = run
    }
  2. but3k4 created this gist Jan 29, 2015.
    70 changes: 70 additions & 0 deletions edbot.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@

    local http = require("socket.http")
    local string = require("string")
    local ltn12 = require ("ltn12")

    local function encode_table(table)
    local str = ""

    local function char2hexcode(c)
    return string.format("%%%02X", string.byte(c))
    end

    local function escape(str)
    str = string.gsub(str, "\n", "\r\n")
    str = string.gsub(str, "([^0-9a-zA-Z_. -])", char2hexcode)
    str = string.gsub(str, " ", "+")
    return str
    end

    for key, vals in pairs(table) do
    if type(vals) ~= "table" then
    vals = {vals}
    end
    for k, val in pairs(vals) do
    str = str .. "&" .. escape(key) .. "=" ..escape(val)
    end
    end
    return string.sub(str,2)
    end

    local function edbot(msg)
    local params = {
    ['server'] = '0.0.0.0:8085',
    ['charset_post'] = "utf-8",
    ['charset'] = 'utf-8',
    ['pure'] = 1,
    ['js'] = 0,
    ['tst'] = 1,
    ['msg'] = tostring(msg),
    }

    local data = encode_table(params)
    local response = {}

    r, c, h = http.request ({
    method = "POST",
    url = "http://www.ed.conpet.gov.br/mod_perl/bot_gateway.cgi",
    headers = {
    ["content-type"] = "application/x-www-form-urlencoded",
    ["content-length"] = string.len(data),
    },
    source = ltn12.source.string(data),
    sink = ltn12.sink.table(response)
    })

    if response[1] ~= nil then
    return tostring(response[1]):gsub('<[^<>]*>', ''):gsub('\n', '')
    end
    end

    function run(msg, matches)
    return edbot(matches[1])
    end

    return {
    description = "Edbot",
    usage = "toguro: word or phrase",
    patterns = { "^toguro: (.*)$" },
    run = run
    }