Advertisement
MrSnake20_15

IRC_ROBOT

Feb 18th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.70 KB | None | 0 0
  1. local component = require("component")
  2. local computer = require("computer")
  3.  
  4. if not component.isAvailable("internet") then
  5.   io.stderr:write("OpenIRC requires an Internet Card to run!\n")
  6.   return
  7. end
  8.  
  9. local event = require("event")
  10. local internet = require("internet")
  11. local shell = require("shell")
  12. local term = require("term")
  13. local text = require("text")
  14. local modem=component.modem
  15.  
  16. --local chatbox = component.chat_box
  17. local cfg = {}--chan,nick,pass
  18. local file=io.open("cfg", "r")
  19. for line in file:lines() do table.insert(cfg,line) end
  20. file:close()
  21.  
  22.  
  23. local nick = cfg[2]
  24. local pass = cfg[3]
  25. target = cfg[1]
  26. host = "irc.esper.net:6667"
  27. local port = 54321
  28. modem.open(port)
  29.  
  30. function send(target,...)
  31. sock:write("PRIVMSG "..target.." :".. ... .."\r\n")sock:flush()
  32. end
  33.  
  34. function topic(target,...)
  35.   sock:write("TOPIC "..target.." :".. ... .."\r\n") sock:flush()
  36. end
  37.  
  38. function robot(msg,n)--244 str
  39.   local f = string.sub(msg,1,1)
  40.   if f == "#" then
  41.     --print("[MSG-IRC] " .. n .. ": " .. string.sub(msg,2))
  42.     modem.broadcast(port,"["..n.."]:"..string.sub(msg,2))
  43.   elseif f=='.' then
  44.     pcall(load(string.sub(msg,2), _, _, _ENV))
  45.   elseif f=='~' then
  46.     os.execute(string.sub(msg,2))
  47.   elseif f=="!" then
  48.     if not target then
  49.       target="#control"
  50.     end
  51.     send(target,string.sub(msg,2))
  52.   elseif f=="*" then
  53.     local cmd = string.sub(msg,2)
  54.     if cmd == 'help' then
  55.       send(target,"'.'' - это pcall(load());'~'-это os.execute();'!'-send();'*help'-эта подсказка; '*topic'-сменить topic")
  56.     elseif cmd == 'topic' then
  57.       local t = string.sub(cmd,7)
  58.       if not target then
  59.       target="#control"
  60.       end
  61.       topic(target,t)
  62.     end
  63.   else
  64.     --print("[IRC] " .. n .. ": " .. msg)
  65.     modem.broadcast(port,"["..n.."]:"..msg)
  66.   end
  67. end
  68.  
  69.  
  70.  
  71.  
  72. --chatbox.setName("§r§3IRC§7§o")
  73.  
  74. -- try to connect to server.
  75. sock, reason = internet.open(host)
  76. if not sock then
  77.   io.stderr:write(reason .. "\n")
  78.   return
  79. end
  80. --[[
  81. local function print(message, overwrite)
  82.   local w, h = component.gpu.getResolution()
  83.   local line
  84.   repeat
  85.     line, message = text.wrap(text.trim(message), w, w)
  86.     if not overwrite then
  87.       component.gpu.copy(1, 1, w, h - 1, 0, -1)
  88.     end
  89.     overwrite = false
  90.     component.gpu.fill(1, h - 1, w, 1, " ")
  91.     component.gpu.set(1, h - 1, line)
  92.   until not message or message == ""
  93. end]]
  94. function autocreate(table, key)
  95.   table[key] = {}
  96.   return table[key]
  97. end
  98. local function name(identity)
  99.   return identity and identity:match("^[^!]+") or identity or "Anonymous"
  100. end
  101.  
  102. local callback = nil
  103. local messages = {}
  104. local init = 0
  105. local whois = setmetatable({}, {__index=autocreate})
  106. local names = setmetatable({}, {__index=autocreate})
  107. local timer
  108. local ignore = {
  109.   [213]=true, [214]=true, [215]=true, [216]=true, [217]=true,
  110.   [218]=true, [231]=true, [232]=true, [233]=true, [240]=true,
  111.   [241]=true, [244]=true, [244]=true, [246]=true, [247]=true,
  112.   [250]=true, [300]=true, [316]=true, [361]=true, [362]=true,
  113.   [363]=true, [373]=true, [384]=true, [492]=true,
  114.   -- custom ignored responses.
  115.   [265]=true, [266]=true, [330]=true
  116. }
  117.  
  118. local commands = {
  119. --Replys
  120.   RPL_WELCOME = "001",
  121.   RPL_YOURHOST = "002",
  122.   RPL_CREATED = "003",
  123.   RPL_MYINFO = "004",
  124.   RPL_BOUNCE = "005",
  125.   RPL_LUSERCLIENT = "251",
  126.   RPL_LUSEROP = "252",
  127.   RPL_LUSERUNKNOWN = "253",
  128.   RPL_LUSERCHANNELS = "254",
  129.   RPL_LUSERME = "255",
  130.   RPL_AWAY = "301",
  131.   RPL_UNAWAY = "305",
  132.   RPL_NOWAWAY = "306",
  133.   RPL_WHOISUSER = "311",
  134.   RPL_WHOISSERVER = "312",
  135.   RPL_WHOISOPERATOR = "313",
  136.   RPL_WHOISIDLE = "317",
  137.   RPL_ENDOFWHOIS = "318",
  138.   RPL_WHOISCHANNELS = "319",
  139.   RPL_CHANNELMODEIS = "324",
  140.   RPL_NOTOPIC = "331",
  141.   RPL_TOPIC = "332",
  142.   RPL_NAMREPLY = "353",
  143.   RPL_ENDOFNAMES = "366",
  144.   RPL_MOTDSTART = "375",
  145.   RPL_MOTD = "372",
  146.   RPL_ENDOFMOTD = "376",
  147.   RPL_WHOISSECURE = "671",
  148.   RPL_HELPSTART = "704",
  149.   RPL_HELPTXT = "705",
  150.   RPL_ENDOFHELP = "706",
  151.   RPL_UMODEGMSG = "718",
  152.  
  153. --Errors
  154.   ERR_BANLISTFULL = "478",
  155.   ERR_CHANNELISFULL = "471",
  156.   ERR_UNKNOWNMODE = "472",
  157.   ERR_INVITEONLYCHAN = "473",
  158.   ERR_BANNEDFROMCHAN = "474",
  159.   ERR_CHANOPRIVSNEEDED = "482",
  160.   ERR_UNIQOPRIVSNEEDED = "485",
  161.   ERR_USERNOTINCHANNEL = "441",
  162.   ERR_NOTONCHANNEL = "442",
  163.   ERR_NICKCOLLISION = "436",
  164.   ERR_NICKNAMEINUSE = "433",
  165.   ERR_ERRONEUSNICKNAME = "432",
  166.   ERR_WASNOSUCHNICK = "406",
  167.   ERR_TOOMANYCHANNELS = "405",
  168.   ERR_CANNOTSENDTOCHAN = "404",
  169.   ERR_NOSUCHCHANNEL = "403",
  170.   ERR_NOSUCHNICK = "401",
  171.   ERR_MODELOCK = "742"
  172. }
  173.  
  174. local function msgListener(evt, addr, user, msg)
  175.   table.insert(messages, {user = user, msg = msg})
  176. end
  177.  
  178. local function handleCommand(prefix, command, args, message)
  179.   if command == "PING" then
  180.     sock:write(string.format("PONG :%s\r\n", message))
  181.     sock:flush()
  182.   elseif command == "NICK" then
  183.     local oldNick, newNick = name(prefix), tostring(args[1] or message)
  184.     if oldNick == nick then
  185.       nick = newNick
  186.     end
  187.     --print(oldNick .. " is now known as " .. newNick .. ".")
  188.   elseif command == "MODE" then
  189.     if #args == 2 then
  190.       --print("[" .. args[1] .. "] " .. name(prefix) .. " set mode".. ( #args[2] > 2 and "s" or "" ) .. " " .. tostring(args[2] or message) .. ".")
  191.     else
  192.       local setmode = {}
  193.       local cumode = "+"
  194.       if args[2] then
  195.         args[2]:gsub(".", function(char)
  196.           if char == "-" or char == "+" then
  197.             cumode = char
  198.           else
  199.             table.insert(setmode, {cumode, char})
  200.           end
  201.         end)
  202.         local d = {}
  203.         local users = {}
  204.         for i = 3, #args do
  205.           users[i-2] = args[i]
  206.         end
  207.         users[#users+1] = message
  208.         local last
  209.         local ctxt = ""
  210.         for c = 1, #users do
  211.           if not setmode[c] then
  212.             break
  213.           end
  214.           local mode = setmode[c][2]
  215.           local pfx = setmode[c][1]=="+"
  216.           local key = mode == "o" and (pfx and "opped" or "deoped") or
  217.             mode == "v" and (pfx and "voiced" or "devoiced") or
  218.             mode == "q" and (pfx and "quieted" or "unquieted") or
  219.             mode == "b" and (pfx and "banned" or "unbanned") or
  220.             "set " .. setmode[c][1] .. mode .. " on"
  221.           if last ~= key then
  222.             if last then
  223.               --print(ctxt)
  224.             end
  225.             ctxt = "[" .. args[1] .. "] " .. name(prefix) .. " " .. key
  226.             last = key
  227.           end
  228.           ctxt = ctxt .. " " .. users[c]
  229.         end
  230.         if #ctxt > 0 then
  231.           --print(ctxt)
  232.         end
  233.       end
  234.     end
  235.   elseif command == "QUIT" then
  236.     --print(name(prefix) .. " quit (" .. (message or "Quit") .. ").")
  237.   elseif command == "JOIN" then
  238.     --print("[" .. args[1] .. "] " .. name(prefix) .. " entered the room.")
  239.   elseif command == "PART" then
  240.     --print("[" .. args[1] .. "] " .. name(prefix) .. " has left the room (quit: " .. (message or "Quit") .. ").")
  241.   elseif command == "TOPIC" then
  242.     --print("[" .. args[1] .. "] " .. name(prefix) .. " has changed the topic to: " .. message)
  243.   elseif command == "KICK" then
  244.     --print("[" .. args[1] .. "] " .. name(prefix) .. " kicked " .. args[2])
  245.   elseif command == "PRIVMSG" then
  246.     local ctcp = message:match("^\1(.-)\1$")
  247.     if ctcp then
  248.       --print("[" .. name(prefix) .. "] CTCP " .. ctcp)
  249.       local ctcp, param = ctcp:match("^(%S+) ?(.-)$")
  250.       ctcp = ctcp:upper()
  251.       if ctcp == "TIME" then
  252.         sock:write("NOTICE " .. name(prefix) .. " :\001TIME " .. os.date() .. "\001\r\n")
  253.         sock:flush()
  254.       elseif ctcp == "VERSION" then
  255.         sock:write("NOTICE " .. name(prefix) .. " :\001VERSION Minecraft/OpenComputers Lua 5.2\001\r\n")
  256.         sock:flush()
  257.       elseif ctcp == "PING" then
  258.         sock:write("NOTICE " .. name(prefix) .. " :\001PING " .. param .. "\001\r\n")
  259.         sock:flush()
  260.       end
  261.     else
  262.       if string.find(message, "\001ACTION") then
  263.         --print("[" .. args[1] .. "] " .. name(prefix) .. string.gsub(string.gsub(message, "\001ACTION", ""), "\001", ""))
  264.       else
  265.         --print("[" .. args[1] .. "] " .. name(prefix) .. ": " .. message)
  266.         --chatbox.say("§7" .. name(prefix) .. " §3? §f" .. message:gsub("§[0-9a-fA-Fklmno]", ""))
  267.         robot(message,name(prefix))
  268.       end
  269.     end
  270.   elseif command == "NOTICE" then
  271.     --print("[NOTICE] " .. message)
  272.   elseif command == "ERROR" then
  273.     --print("[ERROR] " .. message)
  274.   elseif tonumber(command) and ignore[tonumber(command)] then
  275.   elseif command == commands.RPL_WELCOME then
  276.     --print(message)
  277.   elseif command == commands.RPL_YOURHOST then -- ignore
  278.   elseif command == commands.RPL_CREATED then -- ignore
  279.   elseif command == commands.RPL_MYINFO then -- ignore
  280.   elseif command == commands.RPL_BOUNCE then -- ignore
  281.   elseif command == commands.RPL_LUSERCLIENT then
  282.     --print(message)
  283.   elseif command == commands.RPL_LUSEROP then -- ignore
  284.   elseif command == commands.RPL_LUSERUNKNOWN then -- ignore
  285.   elseif command == commands.RPL_LUSERCHANNELS then -- ignore
  286.   elseif command == commands.RPL_LUSERME then
  287.     --print(message)
  288.   elseif command == commands.RPL_AWAY then
  289.     --print(string.format("%s is away: %s", name(args[1]), message))
  290.   elseif command == commands.RPL_UNAWAY or command == commands.RPL_NOWAWAY then
  291.     --print(message)
  292.   elseif command == commands.RPL_WHOISUSER then
  293.     local nick = args[2]:lower()
  294.     whois[nick].nick = args[2]
  295.     whois[nick].user = args[3]
  296.     whois[nick].host = args[4]
  297.     whois[nick].realName = message
  298.   elseif command == commands.RPL_WHOISSERVER then
  299.     local nick = args[2]:lower()
  300.     whois[nick].server = args[3]
  301.     whois[nick].serverInfo = message
  302.   elseif command == commands.RPL_WHOISOPERATOR then
  303.     local nick = args[2]:lower()
  304.     whois[nick].isOperator = true
  305.   elseif command == commands.RPL_WHOISIDLE then
  306.     local nick = args[2]:lower()
  307.     whois[nick].idle = tonumber(args[3])
  308.   elseif command == commands.RPL_WHOISSECURE then
  309.     local nick = args[2]:lower()
  310.     whois[nick].secureconn = "Is using a secure connection"
  311.   elseif command == commands.RPL_ENDOFWHOIS then
  312.     local nick = args[2]:lower()
  313.     local info = whois[nick]
  314.     if info.nick then --print("Nick: " .. info.nick)
  315.     end
  316.     if info.user then --print("User name: " .. info.user)
  317.     end
  318.     if info.realName then --print("Real name: " .. info.realName)
  319.     end
  320.     if info.host then --print("Host: " .. info.host)
  321.     end
  322.     if info.server then --print("Server: " .. info.server .. (info.serverInfo and (" (" .. info.serverInfo .. ")") or ""))
  323.     end
  324.     if info.secureconn then --print(info.secureconn)
  325.     end
  326.     if info.channels then --print("Channels: " .. info.channels)
  327.     end
  328.     if info.idle then --print("Idle for: " .. info.idle)
  329.     end
  330.     whois[nick] = nil
  331.   elseif command == commands.RPL_WHOISCHANNELS then
  332.     local nick = args[2]:lower()
  333.     whois[nick].channels = message
  334.   elseif command == commands.RPL_CHANNELMODEIS then
  335.     --print("Channel mode for " .. args[1] .. ": " .. args[2] .. " (" .. args[3] .. ")")
  336.   elseif command == commands.RPL_NOTOPIC then
  337.     --print("No topic is set for " .. args[1] .. ".")
  338.   elseif command == commands.RPL_TOPIC then
  339.     --print("Topic for " .. args[1] .. ": " .. message)
  340.   elseif command == commands.RPL_NAMREPLY then
  341.     local channel = args[3]
  342.     table.insert(names[channel], message)
  343.   elseif command == commands.RPL_ENDOFNAMES then
  344.     local channel = args[2]
  345.     --print("Users on " .. channel .. ": " .. (#names[channel] > 0 and table.concat(names[channel], " ") or "none"))
  346.     names[channel] = nil
  347.   elseif command == commands.RPL_MOTDSTART then
  348.     --print(message .. args[1])
  349.   elseif command == commands.RPL_MOTD then
  350.     --print(message)
  351.   elseif command == commands.RPL_ENDOFMOTD then -- ignore
  352.     init = 1
  353.   elseif command == commands.RPL_HELPSTART or
  354.   command == commands.RPL_HELPTXT or
  355.   command == commands.RPL_ENDOFHELP then
  356.     --print(message)
  357.   elseif command == commands.ERR_BANLISTFULL or
  358.   command == commands.ERR_BANNEDFROMCHAN or
  359.   command == commands.ERR_CANNOTSENDTOCHAN or
  360.   command == commands.ERR_CHANNELISFULL or
  361.   command == commands.ERR_CHANOPRIVSNEEDED or
  362.   command == commands.ERR_ERRONEUSNICKNAME or
  363.   command == commands.ERR_INVITEONLYCHAN or
  364.   command == commands.ERR_NICKCOLLISION or
  365.   command == commands.ERR_NOSUCHNICK or
  366.   command == commands.ERR_NOTONCHANNEL or
  367.   command == commands.ERR_UNIQOPRIVSNEEDED or
  368.   command == commands.ERR_UNKNOWNMODE or
  369.   command == commands.ERR_USERNOTINCHANNEL or
  370.   command == commands.ERR_WASNOSUCHNICK or
  371.   command == commands.ERR_MODELOCK then
  372.     --print("[ERROR]: " .. message)
  373.   elseif tonumber(command) and (tonumber(command) >= 200 and tonumber(command) < 400) then
  374.     --print("[Response " .. command .. "] " .. table.concat(args, ", ") .. ": " .. tostring(message))
  375.  
  376.   ---------------------------------------------------
  377.   -- Error messages. No real point in handling those manually.
  378.   -- http://tools.ietf.org/html/rfc2812#section-5.2
  379.  
  380.   elseif tonumber(command) and (tonumber(command) >= 400 and tonumber(command) < 600) then
  381.     --print("[Error] " .. table.concat(args, ", ") .. ": " .. message)
  382.  
  383.   ---------------------------------------------------
  384.   -- Unhandled message.
  385.  
  386.   else
  387.     --print("Unhandled command: " .. command .. ": " .. message)
  388.   end
  389. end
  390. local result, reason = pcall(function()
  391.   term.clear()
  392.   --print("Welcome to OpenIRC!")
  393.   sock:setTimeout(0.05)
  394.   sock:write(string.format("NICK %s\r\n", nick))
  395.   sock:write(string.format("USER %s 0 * :%s [OpenComputers bridge]\r\n", nick:lower(), nick))
  396.   sock:flush()
  397.   timer = event.timer(0.5, function()
  398.     if not sock then
  399.       return false
  400.     end
  401.     repeat
  402.       local ok, line = pcall(sock.read, sock)
  403.       if ok then
  404.         if not line then
  405.           --print("Connection lost.")
  406.           sock:close()
  407.           sock = nil
  408.           return false
  409.         end
  410.         line = text.trim(line) -- get rid of trailing \r
  411.         local match, prefix = line:match("^(:(%S+) )")
  412.         if match then line = line:sub(#match + 1) end
  413.         local match, command = line:match("^(([^:]%S*))")
  414.         if match then line = line:sub(#match + 1) end
  415.         local args = {}
  416.         repeat
  417.           local match, arg = line:match("^( ([^:]%S*))")
  418.           if match then
  419.             line = line:sub(#match + 1)
  420.             table.insert(args, arg)
  421.           end
  422.         until not match
  423.         local message = line:match("^ :(.*)$")
  424.  
  425.         if callback then
  426.           local result, reason = pcall(callback, prefix, command, args, message)
  427.           if not result then
  428.             --print("Error in callback: " .. tostring(reason))
  429.           end
  430.         end
  431.         handleCommand(prefix, command, args, message)
  432.       end
  433.     until not ok
  434.   end, math.huge)
  435.  
  436.   local history = {}
  437.  
  438.   repeat
  439.     local w, h = component.gpu.getResolution()
  440.     term.setCursor(1, h)
  441.     term.write((target or "?") .. "> ")
  442.     if sock then
  443.       if init == 1 then
  444.         sock:write("JOIN "..target.."\r\n")
  445.        
  446.         sock:write("PRIVMSG NICKSERV :IDENTIFY " ..pass.."\r\n")
  447.         sock:flush()
  448.         send(target,"Я тут!")
  449.         init = 2
  450.       end
  451.       if init == 2 then
  452.         for i = #messages, 1, -1 do
  453.           local msg = messages[i]
  454.           --print(msg.user .. ": " .. msg.msg)
  455.           sock:write("PRIVMSG "..target.." :\x0315" .. msg.user .. "\x03: " .. msg.msg .. "\r\n")
  456.           sock:flush()
  457.           table.remove(messages, i)
  458.         end
  459.       end
  460.     end
  461.     os.sleep(.25)
  462.   until not sock
  463. end)
  464.  
  465. if sock then
  466.   sock:write("QUIT\r\n")
  467.   sock:close()
  468. end
  469. if timer then
  470.   event.cancel(timer)
  471. end
  472. if not result then
  473.   error(reason, 0)
  474. end
  475. --return reason
  476. os.shutdown(true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement