Advertisement
Guest User

disttwitch.lua

a guest
Apr 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. -- Twitch chat display
  2. local serverhost = "irc.chat.twitch.tv" -- Server to connect to
  3. local serverport = 6667 -- Server port
  4. local serveruser = "username" -- Server username
  5. local serverpass = "oauth:xxxxxxxxxxxxxxxxxxxxxxxx" -- Twitch oauth token (http://twitchapps.com/tmi/)
  6. local term = require("term")
  7. local fs = require("filesystem")
  8. local net = require("internet")
  9.  
  10. print("Starting...")
  11.  
  12. function getAuth()
  13.   print("No auth file found! Please visit http://twitchapps.com/tmi/.")
  14.   print("Once you've logged in, please input the entire string you received (including the beginning 'oauth:')")
  15.   local oauth = term.read()
  16.   net.request("https://api.twitch.tv/kraken/user", nil, { Authorization = "OAuth " .. oauth:sub(7) })
  17.  
  18. end
  19.  
  20. print("Which channel should be joined?")
  21. local serverchan = term.read()
  22.  
  23. if serverchan:sub(1,1) ~= "#" then
  24.   serverchan = "#" .. serverchan
  25. end
  26.  
  27. function split(data, pat)
  28.   local ret = {}
  29.   for i in string.gmatch(data,pat) do
  30.     table.insert(ret,i)
  31.   end
  32.   return ret
  33. end
  34.  
  35. print("Starting connection...")
  36.  
  37. local event = require("event")
  38. local con = net.open(serverhost, serverport)
  39. local serialization = require("serialization")
  40. local keyboard = require("keyboard")
  41. local keys = keyboard.keys
  42. local component = require("component")
  43. local gpu = component.getPrimary("gpu")
  44. local keyPressed
  45.  
  46. print("Testing connection...")
  47.  
  48. function getKeyPress()
  49.   local _, _, _, keycode = event.pull(1/20, "key_down")
  50.   return keys[keycode]
  51. end
  52.  
  53. if(con) then
  54.   print("Connected!")
  55.   local line,png,linesplit,msgfrom = ""
  56.   local running = true
  57.   con:write("PASS " .. serverpass .. "\r\n")
  58.   con:write("NICK " .. serveruser .. "\r\n")
  59.   con:write("JOIN " .. serverchan .. "\r\n")
  60.   print("Joined " .. serverchan)
  61.   function exitProgram()
  62.     running = false
  63.   end
  64.   while(true) do
  65.     local id, _, _, _ = event.pull(0.2, "interrupted")
  66.     if id == "interrupted" then
  67.       break
  68.     end
  69.     line = con:read()
  70.     -- print(line)
  71.     linesplit = split(line, "[^:]+")
  72.     if linesplit[1] == "PING" or linesplit[1] == "PING " then
  73.       png = split(line,"[^:]+")
  74.       con:write("PONG :" .. png[#png] .. "\r\n")
  75.     elseif string.find(linesplit[1], "PRIVMSG #") ~= nil then
  76.       msgfrom = split(linesplit[1], "[^!]+")
  77.       msgfrom = msgfrom[1]
  78.       msg = linesplit
  79.       msg[0], msg[1] = nil, nil
  80.       print(msgfrom .. ": " .. linesplit[2])
  81.     elseif linesplit[1] == "NOTICE" or linesplit[1] == "NOTICE " then
  82.       if linesplit[2] == "Login authentication failed" then
  83.         print("Invalid login information. Exiting.")
  84.       end
  85.     end
  86.   end
  87. else
  88.   print("Connection failed.")
  89. end
  90. print("Goodbye!")
  91. os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement