Asioron

imodem_2

Feb 11th, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. --imodem upgrade Asior
  2. _G.imodem = {}
  3. local internet = require('internet')
  4. local computer = require('computer')
  5. local event = require('event')
  6. local time, tm, timePong = -10, 30, true
  7. local nick, pass, server, channel, log
  8. local socket = false
  9.  
  10. local function logToFile(sms) --логирование
  11.   if log == 1 then
  12.     local f = io.open('/IMODEMLOG.txt','a')
  13.     f:write(sms..'\n')
  14.     f:close()
  15.   end
  16. end
  17.  
  18. local function readConfigFile(save)
  19.   if save then
  20.     f = io.open('/etc/imodem.cfg', 'w')
  21.     f:write('nick = "'..nick..'"\n')
  22.     f:write('pass = "'..pass..'"\n')
  23.     f:write('server = "'..server..'"\n')
  24.     f:write('channel = "'..channel..'"\n')
  25.     f:write('log = "'..tostring(log)..'"\n')
  26.     f:close()
  27.     return true
  28.   end
  29.   local f = io.open('/etc/imodem.cfg', 'r')
  30.   if not f then
  31.     f = io.open('/etc/imodem.cfg', 'w')
  32.     f:write('nick = "x'..require('component').internet.address:sub(1, 8)..'"\n')
  33.     f:write('pass = ""\n')
  34.     f:write('server = "irc.esper.net:6667"\n')
  35.     f:write('channel = "#imodem"\n')
  36.     f:write('log = "0"\n')
  37.     f:close()
  38.     f = io.open('/etc/imodem.cfg', 'r')
  39.   end
  40.   local content = f:read("*a")
  41.   f:close()
  42.   local cfg = {}
  43.   assert(load(content, "=imodem.cfg", "t", cfg))()
  44.   return cfg.nick, cfg.pass, cfg.server, cfg.channel, tonumber(cfg.log)
  45. end
  46.  
  47. local a = {...}
  48. if a[1] == 'help' then
  49.   print("Usage: [server[:port]] <nickname> <password> <channel> <save(1/0)> <log(1/0)>")
  50.   os.exit()
  51. end
  52. if a[1] and a[2] then
  53.   server = a[1]
  54.   nick = a[2]
  55.   pass = a[3] or ''
  56.   channel = a[4] or '#imodem'
  57.   log = tonumber(a[6]) or 0
  58.   if a[5] == '1' then
  59.     readConfigFile(true)
  60.   end
  61. else
  62.   nick, pass, server, channel, log = readConfigFile()
  63. end
  64. imodem.channel = channel
  65.  
  66. local function login()
  67.   if socket then socket:close() end
  68.   logToFile('LOGIN!')
  69.   socket = internet.open(server)
  70.   socket:setTimeout(0.05)
  71.   imodem.send_raw('USER '..nick..' 0 * :'..nick)
  72.   imodem.send_raw('NICK '..nick)
  73. end
  74.  
  75. imodem.send_raw = function(message)
  76.   socket:write(message..'\r\n')
  77.   socket:flush()
  78. end
  79.  
  80. imodem.broadcast = function(message)
  81.   if socket and imodem.channel then
  82.     imodem.send_raw('PRIVMSG '..imodem.channel..' :'..message)
  83.     logToFile('PRIVMSG '..imodem.channel..' :'..message)
  84.     return true
  85.   else
  86.     return false
  87.   end
  88. end
  89.  
  90. imodem.send = function(receiver, message)
  91.   if socket and receiver and message then
  92.     imodem.send_raw('PRIVMSG '..receiver..' :'..message)
  93.     logToFile('PRIVMSG '..receiver..' :'..message)
  94.     return true
  95.   else
  96.     return false
  97.   end
  98. end
  99.  
  100. imodem.stop = function()
  101.   logToFile('STOP IMODEM!')
  102.   if imodem.timer then
  103.     if socket then
  104.       imodem.send_raw('QUIT')
  105.       socket:close()
  106.     end
  107.     event.cancel(imodem.timer)
  108.     imodem = nil
  109.   end
  110. end
  111.  
  112. imodem.timer = event.timer(0.5, function()
  113.   if time >= tm and timePong then
  114.     imodem.send_raw('PING 1')
  115.     timePong = false
  116.   else
  117.     time = time + 0.5
  118.   end
  119.   if time > tm*1.5 then
  120.     logToFile('NOT PONG! RESTART!')
  121.     login()
  122.     time = 0
  123.     timePong = true
  124.   end
  125.   if not socket then login() end
  126.   repeat
  127.     local ok, line = pcall(socket.read, socket)
  128.     if ok then
  129.       logToFile(line)
  130.       local match2 = line:match('PRIVMSG[^PRIVMSG]%S*[^ :]')
  131.       if match2 then responser,_ = match2:gsub("%PRIVMSG ", "") end
  132.       if not line then login() end
  133.       local match, prefix = line:match('^(:(%S+) )')
  134.       if prefix then prefix = prefix:match('^[^!]+') end
  135.       if match then line = line:sub(#match+1) end
  136.       local match, command = line:match('^(([^:]%S*))')
  137.       if match then line = line:sub(#match+1) end
  138.       repeat
  139.         local match = line:match('^( ([^:]%S*))')
  140.         if match then
  141.           line = line:sub(#match+1)
  142.         end
  143.       until not match
  144.       local message = line:match('^ :(.*)$')
  145.       if command == '001' or command == '404' then
  146.         imodem.send_raw('JOIN '..imodem.channel)
  147.         imodem.send('NickServ','identify '..pass)
  148.       elseif command == '433' or command == '436' then
  149.         nick = nick..string.char(math.random(97,122))
  150.         imodem.send_raw('NICK '..nick)
  151.       elseif command == 'PING' then
  152.         imodem.send_raw('PONG :'..message)
  153.       elseif command == 'PONG' then
  154.         time = 0
  155.         timePong = true
  156.       elseif command == 'PRIVMSG' then
  157.         computer.pushSignal('modem_message', nick, prefix, 0, responser, message)
  158.       end
  159.     end
  160.   until not ok
  161. end, math.huge)
Advertisement
Add Comment
Please, Sign In to add comment