Wojbie

#Computercraft Irc Monitor

Nov 7th, 2013
876
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Wojbie's #ComputerCraft Irc Channel Monitor
  2. --Simply run and watch chat go by
  3. --change user and userSide below to set settings
  4.  
  5. local zone={Hour=1,Min=0,Sec=0} --provide offset from time thats printed.
  6.  
  7. local user=nil --Username to send redstone signal if mentioned (String).
  8. local userSide=nil --Side to send signal(top, bottom, left, right, front and back)
  9.  
  10. local tArgs={...}
  11. local i=1
  12. local filter=false
  13.  
  14. while #tArgs>0 do
  15.     if tArgs[1] == "-f" then
  16.         filter=true
  17.         i=i+1
  18.         table.remove(tArgs,1)
  19.     elseif tArgs[1] and tArgs[2] then
  20.         local nope=true
  21.         for i,k in pairs(redstone.getSides()) do
  22.             if tArgs[2]==k then
  23.                 user=table.remove(tArgs,1)
  24.                 userSide=table.remove(tArgs,1)
  25.                 nope=false
  26.                 break
  27.             end
  28.         end
  29.         if nope then table.remove(tArgs,1) end
  30.     else table.remove(tArgs,1) end
  31. end
  32.  
  33. if  user then user=string.lower(user) end--easyer to work on that later
  34.  
  35. if filter then print("Filter on") end
  36. if user then print("On pings for ",user," emits redstone signal on ",userSide," side") rs.setOutput(userSide,false) end
  37. sleep(1)
  38.  
  39. local Startup="http://sbnc.khobbits.co.uk/log/logs/100/computercraft.html"
  40. local Update="http://sbnc.khobbits.co.uk/log/logs/10/computercraft.html"
  41. local UpdateRate=1
  42.  
  43. --patterns
  44. local patcutoff="<p>If you want your channel logged here, contact KHobbits on #lain @ Esper.<br />()"
  45. local patmaincut="<span class='(.-)'><span class='timestamp'>(.-)</span><br /></span>"
  46. local patsystem="%[(%d-):(%d-):(%d-)%] </span><span style=\"color:(.-);\">(.*)"
  47. local patsystem2="%[(%d-):(%d-):(%d-)%] (.*)"
  48. local patuser="%[(%d-):(%d-):(%d-)%] </span><span>&lt;</span><span style=\"color:(.-);\">(.-)</span><span>&gt;(.*)"
  49.  
  50. --html unescaping http://stackoverflow.com/a/14899740
  51. local gsub, char = string.gsub, string.char
  52. local entityMap  = {["lt"]="<",["gt"]=">",["amp"]="&",["quot"]='"',["apos"]="'",["nbsp"]=" "}
  53. local entitySwap = function(orig,n,s)
  54.   return entityMap[s] or n=="#" and char(s) or orig
  55. end
  56. function unescape(str)
  57.   return gsub( str, '(&(#?)([%d%a]+);)', entitySwap )
  58. end
  59.  
  60. local All={}
  61.  
  62. local colors, Standard, bgColor
  63. if term.isColour() then
  64.     colors={green=colours.lime,purple=colours.purple,teal=colours.lightBlue,navy=colours.blue,fuchsia=colours.pink,red=colours.red,gray=colours.lightGrey}
  65.     Standard = colours.white
  66.     bgColor = colours.black
  67. else
  68.     colors={green=colours.white,purple=colours.white,teal=colours.white,navy=colours.white,fuchsia=colours.white,red=colours.white,gray=colours.white}
  69.     Standard = colours.white
  70.     bgColor = colours.black
  71. end
  72.  
  73.  
  74. local function download(A)
  75. handle=http.get(A)
  76. if not handle then return nil end
  77. local out=handle.readAll()
  78. handle.close()
  79. return out
  80. end
  81.  
  82. local function inlines(A)
  83. local out={}
  84. local cut=string.match(A,patcutoff)
  85.  
  86. --Cutting in lines
  87. A=string.sub(A,cut or 1)
  88. for i,k in string.gmatch(A,patmaincut)do --getting in lines
  89. table.insert(out,{["system"]=i=="chatline-hideable",["content"]=k})
  90. end
  91.  
  92. return out
  93. end
  94.  
  95. local function timezone(Hour,Min,Sec) --add time correction onto timestamp
  96.     local function loop(A,B) local p=0 while A<0 do A=A+B p=p-1 end while A>B-1 do A=A-B p=p+1 end return A,p end
  97.     local p=0
  98.     Sec,p=loop(tonumber(Sec)+zone.Sec,60)
  99.     Min,p=loop(tonumber(Min)+zone.Min+p,60)
  100.     Hour=loop(tonumber(Hour)+zone.Hour+p,24)
  101.     return string.format ("[%02d:%02d]",Hour,Min)
  102.     --return string.format ("[%02d:%02d:%02d]",Hour,Min,Sec) --"["..Hour..":"..Min..":"..Sec.."]"
  103. end
  104.  
  105. local function process(A) --process one table line into better one
  106. local ta,tb,tc
  107.     ta,tb,tc,A.color,A.user,A.data = string.match(A.content,patuser)
  108.     if not ta then ta,tb,tc,A.color,A.data = string.match(A.content,patsystem) end
  109.     if not ta then ta,tb,tc,A.data = string.match(A.content,patsystem2) end
  110. A.mtime=timezone(ta,tb,tc)
  111. A.data=unescape(A.data)
  112. return A
  113. end
  114.  
  115. local function findlast(A,B)
  116. if not B then B="impossible" end
  117. local out={}
  118. local fin=A[#A].content
  119. for i=#A,1,-1 do
  120. if A[i].content==B then return out,fin end
  121. table.insert(out,1,process(A[i]))
  122. end
  123. table.insert(out,1,{["system"]=true,["mtime"]=out[1].mtime,["color"]="red",["data"]="* Possible loss of few lines of log before this message."})
  124. return out,fin
  125. end
  126.  
  127. local function printline(A)
  128.     write( "\n" )
  129.     term.setTextColor(Standard)
  130.     write(A.mtime)
  131.     if A.user then write(" <") end
  132.     if A.color then term.setTextColor(colors[A.color]) end
  133.     if A.user then write(A.user) term.setTextColor(Standard) write(">") else write(" ") end
  134.     write(A.data)
  135. end
  136.  
  137. local function printAll(A,filter,ding)
  138.     for i,k in pairs(A) do
  139.         if not (filter and k.system) then
  140.             printline(k)
  141.             if ding and user then
  142.                 local data=string.lower(k.data)
  143.                 if (k.user and user~=string.lower(k.user)) and not string.match(data,"* "..user) and string.match(data,user) then rs.setOutput(userSide, true) end
  144.             end
  145.         end
  146.     end
  147. end
  148.  
  149. local function AddTab(A,B)
  150.     for i,k in pairs(B) do table.insert(A,k) end
  151. end
  152.  
  153. local file,new=nil
  154.  
  155. while true do
  156. file=download(Startup)
  157. if file then break end
  158. sleep(1)
  159. end
  160.  
  161. local All,last =  findlast(inlines(file),last)
  162. printAll(All,filter)
  163.  
  164. local tick=os.startTimer(UpdateRate)
  165. while true do
  166.     local sEvent, param = os.pullEvent()
  167.     if sEvent =="timer" then
  168.         if param==tick then
  169.             if user then rs.setOutput(userSide,false) end
  170.             while true do
  171.                 file=download(Update)
  172.                 if file then break end
  173.                 sleep(1)
  174.             end
  175.             new,last =  findlast(inlines(file),last)
  176.             AddTab(All,new)
  177.             printAll(new,filter,true)
  178.             tick=os.startTimer(UpdateRate)
  179.         end
  180.     end
  181. end
RAW Paste Data