Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This Program requires my Socket API: www.computercraft.info/forums2/index.php?/topic/3624-mc-125cc-141-socketapi-now-with-smp/
- -- Command Syntax <side> <server> <port> <nick> <channel> [<side_monitor>]
- --[[
- Changelog:
- v1.01:
- Fixed: ping timeouts are past now (as long as you don't pause minecraft)
- v1.10:
- Fixed: Text after a colon now displays properly
- Added: ping support(optical and acustical)
- Added: monitor support
- v1.20:
- Added: PM support (/msg command, /r for quickreply to last incoming pm)
- --]]
- noteblockside = "left"
- tArgs = {...}
- local x, y = 1, 2
- local lastuser = "" --author of last incoming pm
- function split(inp, char)
- local ret = {}
- local retid = 1
- local tmpret = ""
- for i = 1, string.len(inp) do
- local tmp = string.sub(inp, i, i)
- if tmp == char then
- ret[retid] = tmpret
- tmpret=""
- retid = retid + 1
- else
- tmpret = tmpret..tmp
- end
- end
- ret[retid] = tmpret
- tmpret=""
- return ret
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function echo(s)
- term.setCursorPos(x, y)
- print(s)
- x, y = term.getCursorPos()
- term.setCursorPos(1,1)
- print("> "..string.rep(" ",getTermSize("x")-2))
- term.setCursorPos(3,1)
- if tArgs[6] then
- local mon = peripheral.wrap(tArgs[6])
- term.redirect(mon)
- print(s)
- term.restore()
- end
- end
- function getTermSize(axis)
- local x1, y1 = term.getSize()
- if axis == "x" then
- return x1
- else
- return y1
- end
- end
- function input()
- term.setCursorPos(1,1)
- print("> ")
- term.setCursorPos(3,1)
- local rd = read()
- term.setCursorPos(1,1)
- print(string.rep(" ",getTermSize("x")))
- return rd
- end
- function connect()
- echo("Connecting to "..tArgs[2]..":"..tArgs[3].." on "..tArgs[1].." WebInterface...")
- suc, msg = socket.connect(tArgs[1], tArgs[2], tArgs[3])
- if suc then
- echo("Connection successful.")
- return true
- else
- echo("Connection failed: "..msg)
- return false
- end
- end
- function sockWrite(s)
- return socket.writeLine(tArgs[1], s)
- end
- function privmsg(to, text)
- sockWrite("PRIVMSG "..to.." :"..text)
- end
- function logIn()
- echo("Logging in as "..tArgs[4].."...")
- sockWrite("NICK "..tArgs[4])
- sockWrite("USER SockCraftIrc 0 "..tArgs[2].." :The ComputerCraft IRC Client by Stiepen WITH SOCKETS!!")
- local tmp2 = ""
- local tmp = ""
- while string.upper(tmp2) ~= "PING" do
- tmp = socket.readLine(tArgs[1])
- tmp2 = string.sub(tmp, 1, 4)
- end
- sockWrite("PONG "..string.sub(tmp, 6))
- echo("Joining "..tArgs[5].."...")
- sockWrite("JOIN "..tArgs[5])
- end
- function threadWrite()
- while true do
- local tmp = input()
- if string.sub(tmp, 1, 1) == "/" then
- if string.sub(tmp, 1, 4) == "/msg" then
- local name = split(tmp, " ")[2]
- local msg = string.sub(tmp, string.len(name) + 6)
- privmsg(name, msg)
- echo("[me->"..name.."] "..msg)
- elseif string.sub(tmp, 1, 2) == "/r" then
- local msg = string.sub(tmp, 4)
- privmsg(lastuser, msg)
- echo("[me->"..lastuser.."] "..msg)
- else
- sockWrite(string.sub(tmp, 2))
- echo("[COMMAND]: "..string.sub(tmp, 2))
- end
- else
- privmsg(tArgs[5], tmp)
- echo("<"..tArgs[4].."> "..tmp)
- end
- end
- end
- function threadRead()
- while true do
- local tmp = socket.readLine(tArgs[1])
- if tmp == nil then return false end
- data = split(tmp, ":")
- cmd = split(data[2], " ")
- if string.lower(data[1]) == "ping" then
- sockWrite(data[2])
- elseif string.lower(cmd[2]) == "privmsg" then
- if string.lower(cmd[3]) == string.lower(tArgs[5]) then
- local text = string.sub(tmp, string.len(data[1]..data[2].."..")+1)
- local tmpmsg = "<"..split(cmd[1],"!")[1].."> "..text
- if string.find(text, tArgs[4]) then
- tmpmsg = "!!! "..tmpmsg
- rs.setOutput(noteblockside, true)
- sleep(0.2)
- rs.setOutput(noteblockside, false)
- end
- echo (tmpmsg)
- elseif string.sub(cmd[3],1,1) ~= "#" then
- echo("["..split(cmd[1],"!")[1].."->me] "..string.sub(tmp, string.len(data[1]..data[2].."..")+1))
- lastuser = split(cmd[1],"!")[1]
- rs.setOutput(noteblockside, true)
- sleep(0.2)
- rs.setOutput(noteblockside, false)
- sleep(0.2)
- rs.setOutput(noteblockside, true)
- sleep(0.2)
- rs.setOutput(noteblockside, false)
- end
- end
- end
- end
- function pingThread()
- while true do
- sleep(30)
- sockWrite("PING randomdata")
- end
- end
- clear()
- if connect() then
- logIn()
- parallel.waitForAny(threadWrite, threadRead, pingThread)
- --sockWrite("QUIT Bye")
- socket.disconnect(tArgs[1])
- echo("disconnected.")
- term.setCursorPos(x, y)
- print()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement