Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --imodem upgrade Asior
- _G.imodem = {}
- local internet = require('internet')
- local computer = require('computer')
- local event = require('event')
- local time, tm, timePong = -10, 30, true
- local nick, pass, server, channel, log
- local socket = false
- local function logToFile(sms) --логирование
- if log == 1 then
- local f = io.open('/IMODEMLOG.txt','a')
- f:write(sms..'\n')
- f:close()
- end
- end
- local function readConfigFile(save)
- if save then
- f = io.open('/etc/imodem.cfg', 'w')
- f:write('nick = "'..nick..'"\n')
- f:write('pass = "'..pass..'"\n')
- f:write('server = "'..server..'"\n')
- f:write('channel = "'..channel..'"\n')
- f:write('log = "'..tostring(log)..'"\n')
- f:close()
- return true
- end
- local f = io.open('/etc/imodem.cfg', 'r')
- if not f then
- f = io.open('/etc/imodem.cfg', 'w')
- f:write('nick = "x'..require('component').internet.address:sub(1, 8)..'"\n')
- f:write('pass = ""\n')
- f:write('server = "irc.esper.net:6667"\n')
- f:write('channel = "#imodem"\n')
- f:write('log = "0"\n')
- f:close()
- f = io.open('/etc/imodem.cfg', 'r')
- end
- local content = f:read("*a")
- f:close()
- local cfg = {}
- assert(load(content, "=imodem.cfg", "t", cfg))()
- return cfg.nick, cfg.pass, cfg.server, cfg.channel, tonumber(cfg.log)
- end
- local a = {...}
- if a[1] == 'help' then
- print("Usage: [server[:port]] <nickname> <password> <channel> <save(1/0)> <log(1/0)>")
- os.exit()
- end
- if a[1] and a[2] then
- server = a[1]
- nick = a[2]
- pass = a[3] or ''
- channel = a[4] or '#imodem'
- log = tonumber(a[6]) or 0
- if a[5] == '1' then
- readConfigFile(true)
- end
- else
- nick, pass, server, channel, log = readConfigFile()
- end
- imodem.channel = channel
- local function login()
- if socket then socket:close() end
- logToFile('LOGIN!')
- socket = internet.open(server)
- socket:setTimeout(0.05)
- imodem.send_raw('USER '..nick..' 0 * :'..nick)
- imodem.send_raw('NICK '..nick)
- end
- imodem.send_raw = function(message)
- socket:write(message..'\r\n')
- socket:flush()
- end
- imodem.broadcast = function(message)
- if socket and imodem.channel then
- imodem.send_raw('PRIVMSG '..imodem.channel..' :'..message)
- logToFile('PRIVMSG '..imodem.channel..' :'..message)
- return true
- else
- return false
- end
- end
- imodem.send = function(receiver, message)
- if socket and receiver and message then
- imodem.send_raw('PRIVMSG '..receiver..' :'..message)
- logToFile('PRIVMSG '..receiver..' :'..message)
- return true
- else
- return false
- end
- end
- imodem.stop = function()
- logToFile('STOP IMODEM!')
- if imodem.timer then
- if socket then
- imodem.send_raw('QUIT')
- socket:close()
- end
- event.cancel(imodem.timer)
- imodem = nil
- end
- end
- imodem.timer = event.timer(0.5, function()
- if time >= tm and timePong then
- imodem.send_raw('PING 1')
- timePong = false
- else
- time = time + 0.5
- end
- if time > tm*1.5 then
- logToFile('NOT PONG! RESTART!')
- login()
- time = 0
- timePong = true
- end
- if not socket then login() end
- repeat
- local ok, line = pcall(socket.read, socket)
- if ok then
- logToFile(line)
- local match2 = line:match('PRIVMSG[^PRIVMSG]%S*[^ :]')
- if match2 then responser,_ = match2:gsub("%PRIVMSG ", "") end
- if not line then login() end
- local match, prefix = line:match('^(:(%S+) )')
- if prefix then prefix = prefix:match('^[^!]+') end
- if match then line = line:sub(#match+1) end
- local match, command = line:match('^(([^:]%S*))')
- if match then line = line:sub(#match+1) end
- repeat
- local match = line:match('^( ([^:]%S*))')
- if match then
- line = line:sub(#match+1)
- end
- until not match
- local message = line:match('^ :(.*)$')
- if command == '001' or command == '404' then
- imodem.send_raw('JOIN '..imodem.channel)
- imodem.send('NickServ','identify '..pass)
- elseif command == '433' or command == '436' then
- nick = nick..string.char(math.random(97,122))
- imodem.send_raw('NICK '..nick)
- elseif command == 'PING' then
- imodem.send_raw('PONG :'..message)
- elseif command == 'PONG' then
- time = 0
- timePong = true
- elseif command == 'PRIVMSG' then
- computer.pushSignal('modem_message', nick, prefix, 0, responser, message)
- end
- end
- until not ok
- end, math.huge)
Advertisement
Add Comment
Please, Sign In to add comment