Advertisement
Ford

RSS Feed Reader for eggdrop

Feb 16th, 2013
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 13.04 KB | None | 0 0
  1. ##############################################################################################
  2. ##  ##     rssreader.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help    ##  ##
  3. ##############################################################################################
  4. ## .rss in the party line for commands and syntax. Example Feed Add Below:                  ##
  5. ##.rss add #Hawkee hawkee Hawkee http://www.hawkee.com/comment.rss.php?tool_type=snippet_id ##
  6. ##############################################################################################
  7. ##      ____                __                 ###########################################  ##
  8. ##     / __/___ _ ___ _ ___/ /____ ___   ___   ###########################################  ##
  9. ##    / _/ / _ `// _ `// _  // __// _ \ / _ \  ###########################################  ##
  10. ##   /___/ \_, / \_, / \_,_//_/   \___// .__/  ###########################################  ##
  11. ##        /___/ /___/                 /_/      ###########################################  ##
  12. ##                                             ###########################################  ##
  13. ##############################################################################################
  14. ##  ##                             Start Setup.                                         ##  ##
  15. ##############################################################################################
  16. namespace eval rssreader {
  17. ## Edit textf to change the color/state of the text shown                               ##  ##
  18.   variable textf "\017\00304"
  19. ## Edit linkf to change the color/state of the links                                    ##  ##
  20.   variable linkf "\017\037"
  21. ## Change usetiny to shorten links with tinyurl. 1 = on , 0 = off                       ##  ##  
  22.   variable usetiny 1
  23. ## Edit maxresults to the amount of results you want per query. This will not cause     ##  ##
  24. ## You to lose results. It will only spread them out over several querys.               ##  ##
  25.   variable maxresults 2
  26. ## Edit checkdelay to change the frequency feed pages are checked. Delay is in minutes. ##  ##
  27.   variable checkdelay 3
  28. ## Edit startupdelay to add delay to startup/restart. Delay is in minutes.              ##  ##
  29.   variable startupdelay 2
  30. ##############################################################################################
  31. ##  ##                           End Setup.                                              ## ##
  32. ##############################################################################################  
  33.   bind dcc - rss rssreader::settings
  34.   bind evnt -|- init-server rssreader::loadhash
  35.   bind evnt -|- prerehash rssreader::loadhash
  36.   proc settings {hand idx text} {
  37.     set choice [lindex $text 0]; set channel [lindex $text 1]
  38.     set name [lindex $text 2]; set logo [lindex $text 3]
  39.     set link [lindex $text 4]
  40.     if {[string equal -nocase "add" $choice] && $link != ""} {
  41.       set deleteinfo [hget "rssreader" $channel]
  42.       if {[set idx [lsearch -glob [timers] "*$deleteinfo*"]] != -1 && $deleteinfo != ""} {
  43.         killtimer [lindex [lindex [timers] $idx] 2]
  44.       }
  45.       hadd "rssreader" "$name" "rssreader::main {$channel $logo $link}"
  46.       savehash
  47.       putlog "Added feed $name to $channel as $logo $link"
  48.       rssreader::main "$channel $logo $link"
  49.     } elseif {[string equal -nocase "addxml" $choice] && $link != ""} {
  50.       set deleteinfo [hget "rssreader" $channel]
  51.       if {[set idx [lsearch -glob [timers] "*$deleteinfo*"]] != -1 && $deleteinfo != ""} {
  52.         killtimer [lindex [lindex [timers] $idx] 2]
  53.       }
  54.       hadd "rssreader" "$name" "rssreader::type2 {$channel $logo $link}"
  55.       savehash
  56.       putlog "Added feed $name to $channel as $logo $link"
  57.       rssreader::type2 "$channel $logo $link"
  58.     } elseif {[string equal -nocase "list" $choice]} {
  59.       putlog "\[RSS list\]"
  60.       set count [hfind "rssreader" "*" 0]; set counter 1
  61.       while {$count >= $counter} {
  62.         putlog "[hfind "rssreader" "*" $counter]"
  63.         incr counter
  64.       }
  65.     } elseif {[string equal -nocase "info" $choice] && $channel != ""} {
  66.       putlog "[hget "rssreader" $channel]"
  67.     } elseif {[string equal -nocase "delete" $choice] && $channel != ""} {
  68.       set deleteinfo [hget "rssreader" $channel]
  69.       hdel "rssreader" $channel
  70.       savehash
  71.       if {[set idx [lsearch -glob [timers] "*$deleteinfo*"]] != -1} {
  72.         killtimer [lindex [lindex [timers] $idx] 2]
  73.         putlog "Removed $channel from RSS"
  74.       }
  75.     } elseif {[string equal -nocase "timers" $choice]} {
  76.       putlog [timers]
  77.     } else {
  78.       putlog "\[RSS Syntax\]"
  79.       putlog "Add Feed: .rss add <#chan1,#chan2,#chanetc> <name> <logo> <link>"
  80.       putlog "Add Xml Feed: .rss addxml <#chan1,#chan2,#chanetc> <name> <logo> <link>"
  81.       putlog "Delete Feed: .rss delete <name>"
  82.       putlog "Info Feed: .rss info <name>"
  83.       putlog "List Feeds: .rss list"
  84.       putlog "Help Feeds: .rss"
  85.     }  
  86.   }
  87.   proc main {text} {
  88.     set chan [lindex $text 0]; set logo [lindex $text 1]; set linker [lindex $text 2]
  89.     set title ""; set link ""; set description ""; set maxcount 1; set json ""
  90.     if {[set idx [lsearch -glob [timers] "*rssreader::main {$chan $logo ${linker}}*"]] != -1} {
  91.       killtimer [lindex [lindex [timers] $idx] 2]
  92.     }
  93.     timer $rssreader::checkdelay "rssreader::main {$chan $logo $linker}"
  94.     set rssreaderurl "/ajax/services/feed/load?v=1.0&q=${linker}"
  95.     set rssreadersite "ajax.googleapis.com"; set rssout ""
  96.     if {[catch {set rssreadersock [socket -async $rssreadersite 80]} sockerr]} {
  97.       return 0
  98.     } else {
  99.       puts $rssreadersock "GET $rssreaderurl HTTP/1.0"
  100.       puts $rssreadersock "Host: $rssreadersite"
  101.       puts $rssreadersock "User-Agent: Opera 9.6"
  102.       puts $rssreadersock ""
  103.       flush $rssreadersock
  104.       while {![eof $rssreadersock]} {
  105.         set rssreadervar " [gets $rssreadersock] "
  106.         if {[regexp {\"responseStatus\":\s?400} $rssreadervar]} {
  107.           if {[set idx [lsearch -glob [timers] "*rssreader::main {$chan $logo ${linker}}*"]] != -1} {
  108.            killtimer [lindex [lindex [timers] $idx] 2]
  109.          }
  110.           type2 "$chan $logo $linker"
  111.           close $rssreadersock
  112.           return
  113.         } else {
  114.           regexp {\:\[(\{.*)$} $rssreadervar match rssout
  115.           set rssout [regexp -all -inline {\{(.*?)\}} $rssout]
  116.           if {$rssout != ""} {
  117.             set count 0
  118.             foreach {match matched} $rssout {
  119.               incr count
  120.               set matched [regexp -all -inline {(".*?":".*?"\,)} $match]
  121.              foreach {innermatch innermatched} $matched {
  122.                 regexp  {\"(.*?)\":\".*?\"\,} $innermatch match varname
  123.                regexp  {\".*?\":\"(.*?)\"\,} $innermatch match value
  124.                 set value [string map {\$ \002\$\002 \] \002\]\002 \[ \002\[\002} $value]
  125.                 set $varname $value
  126.               }
  127.               if {[hfind "rsstempold" "*${link}*" 1] != $link} {
  128.                 if {$title == ""} { set title $description }
  129.                 set linked $link
  130.                 if {$rssreader::usetiny} { set linked [tiny [urlencode $link]] }
  131.                 if {$maxcount <= $rssreader::maxresults} {
  132.                   putserv "PRIVMSG $chan :${logo} ${rssreader::textf}[dehex $title] ${rssreader::linkf}$linked"
  133.                   incr maxcount
  134.                   hadd "rsstempnew" $link 1
  135.                 }
  136.               }
  137.            }
  138.             hfree rsstempold
  139.             hcopy rsstempnew rsstempold
  140.          }
  141.         }
  142.      }
  143.       close $rssreadersock
  144.    }
  145.  }
  146.  proc type2 {text} {
  147.    set chan [lindex $text 0]; set logo [lindex $text 1]; set linker [lindex $text 2]
  148.    set title ""; set link ""; set description ""
  149.    if {[set idx [lsearch -glob [timers] "*rssreader::type2 {$chan $logo ${linker}}*"]] != -1} {
  150.      killtimer [lindex [lindex [timers] $idx] 2]
  151.    }
  152.    timer $rssreader::checkdelay "rssreader::type2 {$chan $logo $linker}"
  153.     regexp -- {https?\:\/\/(.*?)(\/.*)$} $linker wholematch rsstype2site rsstype2url
  154.     set itemfound 0 ; set maxcount 1
  155.    if {[catch {set rsstype2sock [socket -async $rsstype2site 80]} sockerr]} {
  156.      return 0
  157.    } else {
  158.      puts $rsstype2sock "GET $rsstype2url HTTP/1.0"
  159.      puts $rsstype2sock "Host: $rsstype2site"
  160.      puts $rsstype2sock "User-Agent: Opera 9.6"
  161.      puts $rsstype2sock ""
  162.      flush $rsstype2sock
  163.      while {![eof $rsstype2sock]} {
  164.         set rsstype2var " [gets $rsstype2sock] "   
  165.         if {[string match {*<item>*} $rsstype2var]} { set itemfound 1   }
  166.         if {[regexp {<title>(.*?)(?:<\/title>|$)} $rsstype2var match title]} { }
  167.         if {[regexp {<link>(.*?)(?:<\/link>|$)} $rsstype2var match link]} {
  168.           if {[hfind "rsstempold" "*${link}*" 1] != $link} {
  169.             if {$itemfound} {
  170.               set linked $link
  171.               if {$rssreader::usetiny} { set linked [tiny [urlencode $link]] }
  172.               if {$maxcount <= $rssreader::maxresults} {
  173.                 set title [string map {\$ \002\$\002 \] \002\]\002 \[ \002\[\002} $title]
  174.                 putserv "PRIVMSG $chan :${logo} ${rssreader::textf}[dehex $title] ${rssreader::linkf}$linked"
  175.                 incr maxcount
  176.                 hadd "rsstempnew" $link 1
  177.               }
  178.             }
  179.           }
  180.         }
  181.      }
  182.       set itemfound 0
  183.      hfree rsstempold
  184.       hcopy rsstempnew rsstempold
  185.       close $rsstype2sock
  186.    }
  187.  }
  188.  proc tiny {link} {
  189.    set tinysite tinyurl.com
  190.    set tinyurl /api-create.php?url=${link}
  191.    if {[catch {set tinysock [socket -async $tinysite 80]} sockerr]} {
  192.      putlog "$tinysite $tinyurl $sockerr error"
  193.      return $link
  194.    } else {
  195.      puts $tinysock "GET $tinyurl HTTP/1.0"
  196.      puts $tinysock "Host: $tinysite"
  197.      puts $tinysock "User-Agent: Opera 9.6"
  198.      puts $tinysock ""
  199.      flush $tinysock
  200.      while {![eof $tinysock]} {
  201.        set tinyvar " [gets $tinysock] "
  202.        if {[regexp {(http:\/\/.*)} $tinyvar match tinyresult]} {
  203.          close $tinysock
  204.          return $tinyresult
  205.        }
  206.      }
  207.      close $tinysock
  208.      return $link
  209.    }
  210.  }
  211.  proc hex {decimal} { return [format %x $decimal] }
  212.  proc decimal {hex} { return [expr 0x$hex] }
  213.  proc dehex {string} {
  214.    regsub -all {^\{|\}$} $string "" string
  215.    set string [subst [regsub -nocase -all {\\u([a-f0-9]{4})} $string {[format %c [decimal \1]]}]]
  216.    set string [subst [regsub -nocase -all {\%([a-f0-9]{2})} $string {[format %c [decimal \1]]}]]
  217.    set string [subst [regsub -nocase -all {\&#([0-9]{2});} $string {[format %c \1]}]]
  218.    set string [subst [regsub -nocase -all {\&#x([0-9]{2});} $string {[format %c [decimal \1]]}]]
  219.    set string [string map {&quot; \" &middot; · &amp; & <b> \002 </b> \002 &ndash; – &raquo; \
  220.    » &laquo; « &Uuml; Ü &uuml; ü &Aacute; Á &aacute; á &Eacute; É &eacute; é &Iacute; Í &iacute; \
  221.    í &Oacute; Ó &oacute; ó &Ntilde; Ñ &ntilde; ñ &Uacute; Ú &uacute; ú &aelig; æ &nbsp; " "\
  222.     \( \002\(\002 \) \002\)\002 \{ \002\{\002 \} \002\}\002} $string]
  223.    return $string
  224.  }
  225.  proc urlencode {string} {
  226.    regsub -all {^\{|\}$} $string "" string
  227.    return [subst [regsub -nocase -all {([^a-z0-9\+])} $string {%[format %x [scan "\\&" %c]]}]]
  228.  }
  229.  proc hadd {hashname hashitem hashdata } {
  230.    global $hashname
  231.    set ${hashname}($hashitem) $hashdata
  232.    #putlog "[subst $[subst $hashname]($hashitem)]"
  233.  }
  234.  proc hget {hashname hashitem} {
  235.    upvar #0 $hashname hgethashname
  236.     if {[info exists hgethashname($hashitem)]} {
  237.       return $hgethashname($hashitem)
  238.     } else {
  239.       return 0
  240.     }
  241.  }
  242.  proc hfind {hashname search value} {
  243.    upvar #0 $hashname hfindhashname
  244.     if {[array exists hfindhashname]} {
  245.       if {$value == 0} {
  246.         return [llength [array names hfindhashname $search]]
  247.      } else {
  248.        set value [expr $value - 1]
  249.         return [lindex [array names hfindhashname $search] $value]
  250.       }
  251.     }
  252.  }
  253.  proc hsave {hashname filename} {
  254.    upvar #0 $hashname hsavehashname
  255.     if {[array exists hsavehashname]} {
  256.       set hsavefile [open $filename w]
  257.       foreach {key value} [array get hsavehashname] {
  258.         puts $hsavefile "${key}=${value}"
  259.       }
  260.       close $hsavefile
  261.     }
  262.  }
  263.  proc hload {hashname filename} {
  264.    upvar #0 $hashname hloadhashname
  265.     hfree $hashname
  266.     set hloadfile [open $filename]
  267.     set linenum 0
  268.     while {[gets $hloadfile line] >= 0} {
  269.       if {[regexp -- {([^\s]+)=(.*)$} $line wholematch item data]} {
  270.         set hloadhashname($item) $data
  271.       }
  272.    }
  273.     close $hloadfile
  274.  }
  275.  proc hfree {hashname} {
  276.    upvar #0 $hashname hfreehashname
  277.     if {[array exists hfreehashname]} {
  278.      foreach key [array names hfreehashname] {
  279.         unset hfreehashname($key)
  280.       }
  281.     }
  282.  }
  283.  proc hdel {hashname hashitem} {
  284.    upvar #0 $hashname hdelhashname
  285.     if {[info exists hdelhashname($hashitem)]} {
  286.       unset hdelhashname($hashitem)
  287.     }
  288.  }
  289.  proc hcopy {hashfrom hashto} {
  290.     upvar #0 $hashfrom hashfromlocal $hashto hashtolocal
  291.     array set hashtolocal [array get hashfromlocal]
  292.  }
  293.  proc savehash {} {
  294.    hsave "rssreader" "${::network}rssreader.hsh"
  295.  }
  296.  proc loadhash {type} {
  297.    if {[file exists "${::network}rssreader.hsh"]} {
  298.       rssreader::hload "rssreader" "${::network}rssreader.hsh"
  299.     }
  300.    set count $rssreader::startupdelay
  301.    foreach {key value} [array get ::rssreader] {
  302.      if {[set idx [lsearch -glob [timers] "*$value*"]] != -1} {
  303.         killtimer [lindex [lindex [timers] $idx] 2]
  304.      }
  305.      timer $count $value
  306.      incr count
  307.    }
  308.  }  
  309. }
  310. putlog "\002*Loaded* \017\00304\002RSS Reader\017 \002by \
  311. Ford_Lawnmower irc.GeekShed.net #Script-Help .rss for help"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement