Advertisement
Stiepen

CC IrcClient v1.20

Aug 29th, 2012
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.79 KB | None | 0 0
  1. -- This Program requires my Socket API: www.computercraft.info/forums2/index.php?/topic/3624-mc-125cc-141-socketapi-now-with-smp/
  2. -- Command Syntax <side> <server> <port> <nick> <channel> [<side_monitor>]
  3. --[[
  4.   Changelog:
  5.   v1.01:
  6.     Fixed: ping timeouts are past now (as long as you don't pause minecraft)
  7.   v1.10:
  8.     Fixed: Text after a colon now displays properly
  9.     Added: ping support(optical and acustical)
  10.     Added: monitor support
  11.   v1.20:
  12.     Added: PM support (/msg command, /r for quickreply to last incoming pm)
  13. --]]
  14. noteblockside = "left"
  15. tArgs = {...}
  16. local x, y = 1, 2
  17. local lastuser = "" --author of last incoming pm
  18.  
  19. function split(inp, char)
  20.   local ret = {}
  21.   local retid = 1
  22.   local tmpret = ""
  23.   for i = 1, string.len(inp) do
  24.     local tmp = string.sub(inp, i, i)
  25.     if tmp == char then
  26.       ret[retid] = tmpret
  27.       tmpret=""
  28.       retid = retid + 1
  29.     else
  30.       tmpret = tmpret..tmp
  31.     end
  32.   end
  33.   ret[retid] = tmpret
  34.   tmpret=""
  35.   return ret
  36. end
  37.  
  38. function clear()
  39.   term.clear()
  40.   term.setCursorPos(1,1)
  41. end
  42.  
  43. function echo(s)
  44.   term.setCursorPos(x, y)
  45.   print(s)
  46.   x, y = term.getCursorPos()
  47.  
  48.   term.setCursorPos(1,1)
  49.   print("> "..string.rep(" ",getTermSize("x")-2))
  50.   term.setCursorPos(3,1)
  51.   if tArgs[6] then
  52.     local mon = peripheral.wrap(tArgs[6])
  53.     term.redirect(mon)
  54.     print(s)
  55.     term.restore()
  56.   end
  57. end
  58.  
  59. function getTermSize(axis)
  60.   local x1, y1 = term.getSize()
  61.   if axis == "x" then
  62.     return x1
  63.   else
  64.     return y1
  65.   end
  66. end
  67.  
  68. function input()
  69.   term.setCursorPos(1,1)
  70.   print("> ")
  71.   term.setCursorPos(3,1)
  72.   local rd = read()
  73.   term.setCursorPos(1,1)
  74.   print(string.rep(" ",getTermSize("x")))
  75.   return rd
  76. end
  77.  
  78. function connect()
  79.   echo("Connecting to "..tArgs[2]..":"..tArgs[3].." on "..tArgs[1].." WebInterface...")
  80.   suc, msg = socket.connect(tArgs[1], tArgs[2], tArgs[3])
  81.   if suc then
  82.     echo("Connection successful.")
  83.     return true
  84.   else
  85.     echo("Connection failed: "..msg)
  86.     return false
  87.   end
  88. end
  89.  
  90. function sockWrite(s)
  91.   return socket.writeLine(tArgs[1], s)
  92. end
  93.  
  94. function privmsg(to, text)
  95.   sockWrite("PRIVMSG "..to.." :"..text)
  96. end
  97.  
  98. function logIn()
  99.   echo("Logging in as "..tArgs[4].."...")
  100.   sockWrite("NICK "..tArgs[4])
  101.   sockWrite("USER SockCraftIrc 0 "..tArgs[2].." :The ComputerCraft IRC Client by Stiepen WITH SOCKETS!!")
  102.   local tmp2 = ""
  103.   local tmp = ""
  104.   while string.upper(tmp2) ~= "PING" do
  105.     tmp = socket.readLine(tArgs[1])
  106.     tmp2 = string.sub(tmp, 1, 4)
  107.   end
  108.   sockWrite("PONG "..string.sub(tmp, 6))
  109.   echo("Joining "..tArgs[5].."...")
  110.   sockWrite("JOIN "..tArgs[5])
  111. end
  112.  
  113. function threadWrite()
  114.   while true do
  115.     local tmp = input()
  116.     if string.sub(tmp, 1, 1) == "/" then
  117.       if string.sub(tmp, 1, 4) == "/msg" then
  118.         local name = split(tmp, " ")[2]
  119.         local msg = string.sub(tmp, string.len(name) + 6)
  120.         privmsg(name, msg)
  121.         echo("[me->"..name.."] "..msg)
  122.       elseif string.sub(tmp, 1, 2) == "/r" then
  123.         local msg = string.sub(tmp, 4)
  124.     privmsg(lastuser, msg)
  125.         echo("[me->"..lastuser.."] "..msg)
  126.       else
  127.         sockWrite(string.sub(tmp, 2))
  128.         echo("[COMMAND]: "..string.sub(tmp, 2))
  129.       end
  130.     else
  131.       privmsg(tArgs[5], tmp)
  132.       echo("<"..tArgs[4].."> "..tmp)
  133.     end
  134.   end
  135. end
  136.  
  137. function threadRead()
  138.   while true do
  139.     local tmp = socket.readLine(tArgs[1])
  140.     if tmp == nil then return false end
  141.     data = split(tmp, ":")
  142.     cmd = split(data[2], " ")
  143.     if string.lower(data[1]) == "ping" then
  144.       sockWrite(data[2])
  145.     elseif string.lower(cmd[2]) == "privmsg" then
  146.       if string.lower(cmd[3]) == string.lower(tArgs[5]) then
  147.         local text = string.sub(tmp, string.len(data[1]..data[2].."..")+1)
  148.         local tmpmsg = "<"..split(cmd[1],"!")[1].."> "..text
  149.         if string.find(text, tArgs[4]) then
  150.           tmpmsg = "!!! "..tmpmsg
  151.           rs.setOutput(noteblockside, true)
  152.           sleep(0.2)
  153.           rs.setOutput(noteblockside, false)
  154.         end
  155.         echo (tmpmsg)
  156.       elseif string.sub(cmd[3],1,1) ~= "#" then
  157.         echo("["..split(cmd[1],"!")[1].."->me] "..string.sub(tmp, string.len(data[1]..data[2].."..")+1))
  158.         lastuser = split(cmd[1],"!")[1]
  159.         rs.setOutput(noteblockside, true)
  160.     sleep(0.2)
  161.         rs.setOutput(noteblockside, false)
  162.         sleep(0.2)
  163.         rs.setOutput(noteblockside, true)
  164.         sleep(0.2)
  165.         rs.setOutput(noteblockside, false)
  166.       end
  167.     end
  168.    
  169.   end
  170. end
  171.  
  172. function pingThread()
  173.   while true do
  174.     sleep(30)
  175.     sockWrite("PING randomdata")
  176.   end
  177. end
  178.  
  179. clear()
  180. if connect() then
  181.   logIn()
  182.   parallel.waitForAny(threadWrite, threadRead, pingThread)
  183.   --sockWrite("QUIT Bye")
  184.   socket.disconnect(tArgs[1])
  185.   echo("disconnected.")
  186.   term.setCursorPos(x, y)
  187.   print()
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement