mfn

RSS Reader for Eggdrop

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