# Announces new Trump posts via trumpstruth.org RSS
# Feed: https://www.trumpstruth.org/feed
catch {setudef flag enabletruth}
catch {setudef flag enableTruth}
namespace eval ::trumptruthrss {
variable cfg
array set cfg {
feed_url "https://www.trumpstruth.org/feed"
poll_seconds 3600
curl "/usr/bin/curl"
curl_timeout 15
user_agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"
state_file "trumptruthrss.state"
max_chars 420
strip_non_ascii 1
exclude_retruths 1
max_new_announce 5
debug 0
}
variable last_guid ""
variable in_progress 0
variable timer_id ""
}
if {[info commands lreverse] eq ""} {
proc lreverse {lst} {
set out {}
foreach x $lst { set out [linsert $out 0 $x] }
return $out
}
}
proc ::trumptruthrss::dlog {msg} {
variable cfg
if {$cfg(debug)} { putlog "trumptruthrss: $msg" }
}
proc ::trumptruthrss::chan_enabled {chan} {
if {![catch {set v [channel get $chan enabletruth]}]} { return $v }
if {![catch {set v [channel get $chan enableTruth]}]} { return $v }
putlog "trumptruthrss: channel flag enabletruth/enableTruth not defined"
return 0
}
proc ::trumptruthrss::enabled_channels {} {
set out {}
foreach c [channels] {
if {[::trumptruthrss::chan_enabled $c]} { lappend out $c }
}
return $out
}
proc ::trumptruthrss::load_state {} {
variable cfg
variable last_guid
if {![file exists $cfg(state_file)]} { set last_guid ""; return }
catch {
set f [open $cfg(state_file) r]
set last_guid [string trim [read $f]]
close $f
}
}
proc ::trumptruthrss::save_state {} {
variable cfg
variable last_guid
catch {
set f [open $cfg(state_file) w]
puts $f $last_guid
close $f
}
}
proc ::trumptruthrss::is_retruth {item} {
# look for "ReTruth", "RT @user ..."
# Sometimes the link points away from @realDonaldTrump
set raw [string tolower [dict get $item text]]
if {[string first "retruth" $raw] >= 0} { return 1 }
set clean [string tolower [::trumptruthrss::strip_html [dict get $item text]]]
if {[string first "rt @" $clean] == 0} { return 1 }
if {[dict exists $item link]} {
set link [string tolower [dict get $item link]]
if {$link ne "" && [string first "%40realdonaldtrump" $link] < 0 && [string first "/@realdonaldtrump" $link] < 0} {
return 1
}
}
return 0
}
proc ::trumptruthrss::curl_fetch {url} {
variable cfg
set cmd [list $cfg(curl) -s -L --compressed -m $cfg(curl_timeout) \
-H "Accept: application/rss+xml, application/xml;q=0.9, */*;q=0.8" \
-H "User-Agent: $cfg(user_agent)" \
-w "\n__CURLMETA__:%{http_code}:%{content_type}\n" \
$url \
]
dlog "Fetching: $url"
if {[catch {set out [eval exec $cmd]} err]} {
putlog "trumptruthrss: curl failed: $err"
return [list 0 "" ""]
}
set code 0
set ctype ""
if {[regexp {__CURLMETA__:(\d+):([^\r\n]+)} $out -> code ctype]} {
regsub {(\r?\n)?__CURLMETA__:[0-9]+:[^\r\n]+(\r?\n)?$} $out "" body
} else {
set body $out
}
dlog "HTTP $code (ctype=$ctype) bytes=[string length $body]"
return [list $code $ctype $body]
}
proc ::trumptruthrss::html_decode {s} {
set s [string map {
"&" "&"
"<" "<"
">" ">"
""" "\""
""" "\""
"'" "'"
"'" "'"
" " " "
} $s]
return $s
}
proc ::trumptruthrss::strip_html {html} {
set s $html
regsub -all -nocase {
} $s "\n" s
regsub -all -nocase {
} $s "\n" s
regsub -all {<[^>]+>} $s "" s
set s [::trumptruthrss::html_decode $s]
regsub -all {[[:space:]]+} $s " " s
return [string trim $s]
}
proc ::trumptruthrss::rss_gettag {block tag} {
set open "<$tag"
set close "$tag>"
set i [string first $open $block]
if {$i < 0} { return "" }
set gt [string first ">" $block $i]
if {$gt < 0} { return "" }
set j [string first $close $block $gt]
if {$j < 0} { return "" }
set val [string range $block [expr {$gt+1}] [expr {$j-1}]]
set val [string trim $val]
if {[string first {} $val]
if {$end > 9} {
set val [string range $val 9 [expr {$end-1}]]
set val [string trim $val]
}
}
}
proc ::trumptruthrss::parse_feed {xml} {
set head [string tolower [string range $xml 0 200]]
if {[string first "= 0 || [string first "= 0} {
putlog "trumptruthrss: Got HTML instead of RSS/XML."
return {}
}
if {([string first "" $xml $s]
if {$e < 0} break
lappend items [string range $xml $s [expr {$e+6}]]
set pos [expr {$e+7}]
}
if {[llength $items] == 0} {
putlog "trumptruthrss: No