Advertisement
Wojbie

#Computercraft Irc Monitor

Nov 7th, 2013 (edited)
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.47 KB | None | 0 0
  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. --   Copyright (c) 2013-2021 Wojbie (wojbie@wojbie.net)
  5. --   Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
  6. --   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. --   2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. --   3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. --   4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. --   5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. --   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY.
  12.  
  13. local zone={Hour=1,Min=0,Sec=0} --provide offset from time thats printed.
  14.  
  15. local user=nil --Username to send redstone signal if mentioned (String).
  16. local userSide=nil --Side to send signal(top, bottom, left, right, front and back)
  17.  
  18. local tArgs={...}
  19. local i=1
  20. local filter=false
  21.  
  22. while #tArgs>0 do
  23.     if tArgs[1] == "-f" then
  24.         filter=true
  25.         i=i+1
  26.         table.remove(tArgs,1)
  27.     elseif tArgs[1] and tArgs[2] then
  28.         local nope=true
  29.         for i,k in pairs(redstone.getSides()) do
  30.             if tArgs[2]==k then
  31.                 user=table.remove(tArgs,1)
  32.                 userSide=table.remove(tArgs,1)
  33.                 nope=false
  34.                 break
  35.             end
  36.         end
  37.         if nope then table.remove(tArgs,1) end
  38.     else table.remove(tArgs,1) end
  39. end
  40.  
  41. if  user then user=string.lower(user) end--easyer to work on that later
  42.  
  43. if filter then print("Filter on") end
  44. if user then print("On pings for ",user," emits redstone signal on ",userSide," side") rs.setOutput(userSide,false) end
  45. sleep(1)
  46.  
  47. local Startup="http://sbnc.khobbits.co.uk/log/logs/100/computercraft.html"
  48. local Update="http://sbnc.khobbits.co.uk/log/logs/10/computercraft.html"
  49. local UpdateRate=1
  50.  
  51. --patterns
  52. local patcutoff="<p>If you want your channel logged here, contact KHobbits on #lain @ Esper.<br />()"
  53. local patmaincut="<span class='(.-)'><span class='timestamp'>(.-)</span><br /></span>"
  54. local patsystem="%[(%d-):(%d-):(%d-)%] </span><span style=\"color:(.-);\">(.*)"
  55. local patsystem2="%[(%d-):(%d-):(%d-)%] (.*)"
  56. local patuser="%[(%d-):(%d-):(%d-)%] </span><span>&lt;</span><span style=\"color:(.-);\">(.-)</span><span>&gt;(.*)"
  57.  
  58. --html unescaping http://stackoverflow.com/a/14899740
  59. local gsub, char = string.gsub, string.char
  60. local entityMap  = {["lt"]="<",["gt"]=">",["amp"]="&",["quot"]='"',["apos"]="'",["nbsp"]=" "}
  61. local entitySwap = function(orig,n,s)
  62.   return entityMap[s] or n=="#" and char(s) or orig
  63. end
  64. function unescape(str)
  65.   return gsub( str, '(&(#?)([%d%a]+);)', entitySwap )
  66. end
  67.  
  68. local All={}
  69.  
  70. local colors, Standard, bgColor
  71. if term.isColour() then
  72.     colors={green=colours.lime,purple=colours.purple,teal=colours.lightBlue,navy=colours.blue,fuchsia=colours.pink,red=colours.red,gray=colours.lightGrey}
  73.     Standard = colours.white
  74.     bgColor = colours.black
  75. else
  76.     colors={green=colours.white,purple=colours.white,teal=colours.white,navy=colours.white,fuchsia=colours.white,red=colours.white,gray=colours.white}
  77.     Standard = colours.white
  78.     bgColor = colours.black
  79. end
  80.  
  81.  
  82. local function download(A)
  83. handle=http.get(A)
  84. if not handle then return nil end
  85. local out=handle.readAll()
  86. handle.close()
  87. return out
  88. end
  89.  
  90. local function inlines(A)
  91. local out={}
  92. local cut=string.match(A,patcutoff)
  93.  
  94. --Cutting in lines
  95. A=string.sub(A,cut or 1)
  96. for i,k in string.gmatch(A,patmaincut)do --getting in lines
  97. table.insert(out,{["system"]=i=="chatline-hideable",["content"]=k})
  98. end
  99.  
  100. return out
  101. end
  102.  
  103. local function timezone(Hour,Min,Sec) --add time correction onto timestamp
  104.     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
  105.     local p=0
  106.     Sec,p=loop(tonumber(Sec)+zone.Sec,60)
  107.     Min,p=loop(tonumber(Min)+zone.Min+p,60)
  108.     Hour=loop(tonumber(Hour)+zone.Hour+p,24)
  109.     return string.format ("[%02d:%02d]",Hour,Min)
  110.     --return string.format ("[%02d:%02d:%02d]",Hour,Min,Sec) --"["..Hour..":"..Min..":"..Sec.."]"
  111. end
  112.  
  113. local function process(A) --process one table line into better one
  114. local ta,tb,tc
  115.     ta,tb,tc,A.color,A.user,A.data = string.match(A.content,patuser)
  116.     if not ta then ta,tb,tc,A.color,A.data = string.match(A.content,patsystem) end
  117.     if not ta then ta,tb,tc,A.data = string.match(A.content,patsystem2) end
  118. A.mtime=timezone(ta,tb,tc)
  119. A.data=unescape(A.data)
  120. return A
  121. end
  122.  
  123. local function findlast(A,B)
  124. if not B then B="impossible" end
  125. local out={}
  126. local fin=A[#A].content
  127. for i=#A,1,-1 do
  128. if A[i].content==B then return out,fin end
  129. table.insert(out,1,process(A[i]))
  130. end
  131. table.insert(out,1,{["system"]=true,["mtime"]=out[1].mtime,["color"]="red",["data"]="* Possible loss of few lines of log before this message."})
  132. return out,fin
  133. end
  134.  
  135. local function printline(A)
  136.     write( "\n" )
  137.     term.setTextColor(Standard)
  138.     write(A.mtime)
  139.     if A.user then write(" <") end
  140.     if A.color then term.setTextColor(colors[A.color]) end
  141.     if A.user then write(A.user) term.setTextColor(Standard) write(">") else write(" ") end
  142.     write(A.data)
  143. end
  144.  
  145. local function printAll(A,filter,ding)
  146.     for i,k in pairs(A) do
  147.         if not (filter and k.system) then
  148.             printline(k)
  149.             if ding and user then
  150.                 local data=string.lower(k.data)
  151.                 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
  152.             end
  153.         end
  154.     end
  155. end
  156.  
  157. local function AddTab(A,B)
  158.     for i,k in pairs(B) do table.insert(A,k) end
  159. end
  160.  
  161. local file,new=nil
  162.  
  163. while true do
  164. file=download(Startup)
  165. if file then break end
  166. sleep(1)
  167. end
  168.  
  169. local All,last =  findlast(inlines(file),last)
  170. printAll(All,filter)
  171.  
  172. local tick=os.startTimer(UpdateRate)
  173. while true do
  174.     local sEvent, param = os.pullEvent()
  175.     if sEvent =="timer" then
  176.         if param==tick then
  177.             if user then rs.setOutput(userSide,false) end
  178.             while true do
  179.                 file=download(Update)
  180.                 if file then break end
  181.                 sleep(1)
  182.             end
  183.             new,last =  findlast(inlines(file),last)
  184.             AddTab(All,new)
  185.             printAll(new,filter,true)
  186.             tick=os.startTimer(UpdateRate)
  187.         end
  188.     end
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement