## adresse rss du site
set rss(010feedurl) "http://www.afp.com/francais/rss/stories.xml"
## fichier de back-up
set rss(010file) "data/rsslog_010"
## salon ou sont affiché les infos
set rss(010chans) "#shell-fr"
## triggers reconnus ( 1er chan, 2e pv )
set rss(010pubtriggers) {!afp}
set rss(010msgtriggers) {afp}
## nb de news postés
set rss(010newspost) "1"
## nombre de news postées en même temps
set rss(010newspostlimit) "1"
## nombre de news postées en même temps quand un trigger est utilisé
set rss(010publimit) "3"
set rss(010msglimit) "10"
## Affichage de la news
## Les triggers possibles <publisher> <news> <link>
## Les codes couleurs
## \002 gras
## \003 [00-15] couleurs
## \017 remise a zéro
## \026 reverse
## \037 souligné
set rss(010layout) "\002\[AFP\]:\017 <news> \00302<link>"
set rss(010triggerlayout) "\002\[AFP\]:\017 <news> \00302<link>"
## mode debug
set rss(010debug) "1"

##### Pas toucher apres sinon ca marche plus #####

foreach trigger [split $rss(010pubtriggers) { }] {bind pub - $trigger pub_rss-010}
foreach trigger [split $rss(010msgtriggers) { }] {bind msg - $trigger msg_rss-010}
catch {unset trigger}

bind time - "?0 * * * *" rsscheck-010

proc rsscheck-010 {min hour day week year} {
  global rss
  set counter 0
  if {$rss(010newspost) == 1 } {
    regexp -nocase -- {^(http://)?(\S*\.[a-zA-Z]{2,3})(/.+?)(\:([0-9]+))?$} $rss(010feedurl) tmp pro rss(010url)  rss(010feed) tmp rss(010port)
    if {$rss(010port) == ""} {set rss(010port) 80}
    if {$rss(010debug) == 1} {
      putlog "RSS: searching news on $rss(010url) (port: $rss(010port))"
    }
    set rss(010lastnews) "none"
    regexp -- {(\S+/)?} $rss(010file) tmp rss(010dir)
    if {$rss(010dir) != ""} {
      if {![file isdirectory $rss(010dir)]} {
	file mkdir $rss(010dir)
	if {$rss(010debug) == 1} {
	  putlog "RSS: created directory: $rss(010dir)"
	}
      }
    }
    if {[file exists $rss(010file)]} {
      set temp [open "$rss(010file)" r]
      set rss(010lastnews) "[gets $temp]"
      if {$rss(010lastnews) == ""} {set rss(010lastnews) "none"}
      close $temp
    }
    if {[catch {set sock [socket $rss(010url) $rss(010port)]} sockerror]} {
      if {$rss(010debug) == 1} {
	putlog "RSS: error: $sockerror"
      }
      return
    } else {
      set limit $rss(010msglimit)
      if {$limit < $rss(010newspostlimit)} {set limit $rss(010newspostlimit)}
      if {$limit < $rss(010publimit)} {set limit $rss(010publimit)}
      set temp [open "$rss(010file)" w+]
      puts $sock "GET $rss(010feed) HTTP/1.1"
      puts $sock "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
      puts $sock "Host: $rss(010url)"
      puts $sock ""
      flush $sock
      set counter 0 ; set getfeed 0
      while {(![eof $sock])} {
	if {$counter == $limit} {break}
	set bl [gets $sock]
	if {[regexp -nocase -- {<item\s.+?>|<item>} $bl]} {set getfeed 1}
	if {$getfeed == 0} {
	  if {[regexp -nocase -- {<title>.+</title>} $bl]} {set publisher [cleanstring-010 $bl 1]}
	} elseif {$getfeed == 1} {
	  if {[regexp -nocase -- {<link>.+</link>} $bl]} {set newslink [cleanstring-010 $bl 1]}
	  if {[regexp -nocase -- {<title>.+</title>} $bl]} {set newsline [cleanstring-010 $bl 1]}
	  if {[regexp -nocase -- {</item>} $bl]} {
	    regsub -all -- {<publisher>} $rss(010layout) $publisher rss(010output)
	    regsub -all -- {<news>} $rss(010output) $newsline rss(010output)
	    regsub -all -- {<link>} $rss(010output) $newslink rss(010output)
	    puts $temp "$rss(010output)"
	    set getfeed 0 ; incr counter
	  }
	}
      }
    }
    close $sock ; close $temp ; set counter 0
    set temp [open "$rss(010file)" r]
    while {[eof $temp] != 1} {
      set rss(010newline) [gets $temp]
      if {($rss(010lastnews) == $rss(010newline)) || ($counter == $rss(010newspostlimit))} {
	if {$rss(010debug) == 1} {
	  putlog "RSS 010 Plus de news trouvées"
	}
	close $temp; return
      }
      incr counter
      foreach chan [channels] {
	if {$rss(010chans) == ""} {
	  if {[regexp -- {c} [getchanmode $chan]]} {
	    regsub -all -- {\003[\d]{0,2}(,[\d]{0,2})?|\002|\037|\017|\026|\006|\007} $rss(010newline) "" rss(010outline)
	  } else {
	    set rss(010outline) $rss(010newline)
	  }
	  putserv "PRIVMSG $chan :$rss(010outline)"
	} else {
	  if {[lsearch -exact [string tolower $rss(010chans)] [string tolower $chan]] != -1} {
	    if {[regexp -- {c} [getchanmode $chan]]} {
	      regsub -all -- {\003[\d]{0,2}(,[\d]{0,2})?|\002|\037|\017|\026|\006|\007} $rss(010newline) "" rss(010outline)
	    } else {
	      set rss(010outline) $rss(010newline)
	    }
	    putserv "PRIVMSG $chan :$rss(010outline)"
	  }
	}
      }
    }
    close $temp
  }
}

proc rssnews-010 {target limit} {
  global rss
  regexp -nocase -- {^(http://)?(\S*\.[a-zA-Z]{2,3})(/.+?)(\:([0-9]+))?$} $rss(010feedurl) tmp pro rss(010url) rss(010feed) tmp rss(010port)
  if {$rss(010port) == ""} {set rss(010port) 80}
  if {[catch {set sock [socket $rss(010url) $rss(010port)] } sockerror]} {
    if {$rss(010debug) == 1} {
      putlog "RSS: error: $sockerror"
    }
    return
  } else {
    puts $sock "GET $rss(010feed) HTTP/1.1"
    puts $sock "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    puts $sock "Host: $rss(010url)"
    puts $sock ""
    flush $sock
    set counter 0 ; set getfeed 0
    while {(![eof $sock])} {
      set bl [gets $sock]
      if {[regexp -nocase -- {<item\s.+?>|<item>} $bl]} {set getfeed 1}
      if {$getfeed == 0} {
	if {[regexp -nocase -- {<title>.+</title>} $bl]} {set publisher [cleanstring-010 $bl 1]}
      } elseif {$getfeed == 1} {
	if {[regexp -nocase -- {<link>.+</link>} $bl]} {set newslink [cleanstring-010 $bl 1]}
	if {[regexp -nocase -- {<title>.+</title>} $bl]} {set newsline [cleanstring-010 $bl 1]}
	if {[regexp -nocase -- {</item>} $bl]} {
	  set getfeed 0 ; incr counter
	  regsub -all -- {<publisher>} $rss(010triggerlayout) $publisher rss(010output)
	  regsub -all -- {<news>} $rss(010output) $newsline rss(010output)
	  regsub -all -- {<link>} $rss(010output) $newslink rss(010output)
	  regsub -all -- {<id>} $rss(010output) $counter rss(010output)
	  putserv "PRIVMSG $target :$rss(010output)"
	}
      }
      if {$counter == $limit} {break}
    }
    close $sock
  }
}

proc cleanstring-010 {string method} {
  regsub -all -- {<.+?>} $string {} string ; regsub -all -- {\t} $string {} string
# if {($method == 1)} {regsub -all -- {\&*.+;} $string {} string}
  if {($method == 1)} {regsub -all -- {\&} $string {\\\&} string}
  set string [encoding convertfrom identity $string]
  return $string
}

proc msg_rss-010 {nick uhost hand arg} {
  global rss
  rssnews-010 $nick $rss(010msglimit)
}

proc pub_rss-010 {nick uhost hand chan arg} {
  global rss
  if {([lsearch -exact [string tolower $rss(010chans)] [string tolower $chan]] != -1) || ($rss(010chans) == "")} {
    rssnews-010 $chan $rss(010publimit)
  }
}
