tvsource $tcl_library/tv/mfslib.tcl
proc action_mfsstream {chan path env} {
global header
global content
global footer
global exepath
global port
global source_dir
global dateformat
global startdateformat
global stopdateformat
global cdateformat
global ddateformat
global airdateformat
global expdateformat
global versionnum
global tivoswversion
global db
# there MUST be a better way to do this.. oh well
#set test [exec ifconfig]
#regexp {eth.*inet addr:(.*) Bc} $test junk address
#set address [string trim $address]
if {[string index $path 0] == "/"} {
set path [string range $path 1 end]
}
puts $chan [html_start "MFSStream"]
set ret [ readConfig $chan ]
if {$path == ""} {
if {$ret == 1} {
puts $chan "Can't read .cfg file."
} elseif { $header == "" } {
puts $chan "Error in .cfg file!"
} else {
printJavaScript $chan
regsub -all "%tivoswversion%" $header "$tivoswversion" header
puts $chan $header
set db [dbopen]
#set the version and proper timezone
GetOSVersion $chan
set_timezone
if { $versionnum == "2" } {
ForeachMfsFile fsid name type "/Recording/NowShowing" "" {
printFsidInfo $fsid $type $chan
}
} elseif { $versionnum == "3" } {
ForeachMfsFile fsid name type "/Recording/NowShowingByTitle" "" {
printFsidInfo $fsid $type $chan
}
} else {
puts $chan "Unsupported Tivo Version!"
}
puts $chan $footer
dbclose $db
}
puts $chan [html_end]
} else {
#puts $chan [html_start "MFSStream"]
regexp {([^/]*)/([^/]*)/(.*)} $path junk action fsid val
if { $val != "" && $fsid != "" && $action != "" } {
set db [dbopen]
if { $action == "delete" } {
fsidDelete $fsid $chan
} else {
fsidEdit $fsid $action $val $chan
}
dbclose $db
}
#puts $chan [html_end]
}
catch { flush $chan }
close $chan
}
proc fsidDelete { id chan } {
global db
ForeachMfsFile fsid name type "/Recording/NowShowing" "" {
if { $fsid == $id } {
RetryTransaction {
set rec [db $db openid $fsid]
set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
set title [strim [dbobj $program get Title]]
}
puts $chan "
Deleting: $fsid - $title
"
puts $chan "
Signaling Tivo to redraw list within the next 60 seconds...
"
RubbishObjectByFsId $fsid
event send $TmkEvent::EVT_DATA_CHANGED $TmkDataChanged::RECORDINGS_ON_DISK 0
}
}
}
proc fsidEdit { newfsid action newval chan } {
global tzoffset
global db
RetryTransaction {
set rec [db $db openid $newfsid]
set showing [dbobj $rec get Showing]
set program [dbobj $showing get Program]
if { $action == "title" } {
puts $chan "
Changing $newfsid's title to : $newval
"
dbobj $program set Title $newval
}
if { $action == "description" } {
puts $chan "
Changing $newfsid's description to : $newval
"
dbobj $program set Description $newval
}
if { $action == "episode" } {
puts $chan "
Changing $newfsid's episode to : $newval
"
dbobj $program set EpisodeTitle $newval
}
if { $action == "startdate" } {
puts $chan "Changing StartDate/StartTime to: $newval
"
set newstart2 [clock scan "$newval"]
set newdate [clock format $newstart2 -format "%m/%d/%Y"]
set newdate [expr [clock scan $newdate] / 86400]
set newtime [clock format $newstart2 -format "%I:%M %p"]
set newtime [expr [clock scan "1/1/70 $newtime"] - $tzoffset]
dbobj $rec set StartDate $newdate
dbobj $rec set StartTime $newtime
}
if { $action == "stopdate" } {
puts $chan "Changing StartDate/StartTime to: $newval
"
set newstop2 [clock scan "$newval"]
set newdate [clock format $newstop2 -format "%m/%d/%Y"]
set newdate [expr [clock scan $newdate] / 86400]
set newtime [clock format $newstop2 -format "%I:%M %p"]
set newtime [expr [clock scan "1/1/70 $newtime"] - $tzoffset]
dbobj $rec set StopDate $newdate
dbobj $rec set StopTime $newtime
}
#if { $newtype != "" } { dbobj $rec set SelectionType $newtype }
puts $chan "
Signaling Tivo to redraw list within the next 60 seconds...
"
puts $chan "Click here to close this window and refresh mfsstream."
event send $TmkEvent::EVT_DATA_CHANGED $TmkDataChanged::RECORDINGS_ON_DISK 0
}
}
proc printFsidInfo { fsid type chan } {
global tzoffset
global db
global header
global content
global footer
global exepath
global port
global dateformat
global startdateformat
global stopdateformat
global cdateformat
global ddateformat
global airdateformat
global expdateformat
global tivoswversion
global versionnum
# set the sw version
set fsidInfo(tivoswversion) $tivoswversion
set fsidInfo(version) $versionnum
RetryTransaction {
set fsidInfo(fsid) $fsid
try { set fsidInfo(rec) [db $db openid $fsid] } catch errCode {set fsidInfo(rec) "N/A"}
try { set fsidInfo(parts) [dbobj $fsidInfo(rec) get Part] } catch errCode {set fsidInfo(parts) "N/A"}
try { set fsidInfo(showing) [dbobj $fsidInfo(rec) get Showing] } catch errCode {set fsidInfo(showing) "N/A"}
try { set fsidInfo(program) [dbobj $fsidInfo(showing) get Program] } catch errCode {set fsidInfo(program) "N/A"}
try { set fsidInfo(title) [strim [dbobj $fsidInfo(program) get Title]] } catch errCode {set fsidInfo(title) "N/A"}
try { set fsidInfo(description) [strim [dbobj $fsidInfo(program) get Description]] } catch errCode {set fsidInfo(description) "N/A"}
try { set fsidInfo(episode) [strim [dbobj $fsidInfo(program) get EpisodeTitle]] } catch errCode {set fsidInfo(episode) "N/A"}
try { set fsidInfo(startdate) [dbobj $fsidInfo(rec) get StartDate] } catch errCode {set fsidInfo(startdate) "N/A"}
try { set fsidInfo(stopdate) [dbobj $fsidInfo(rec) get StopDate] } catch errCode {set fsidInfo(stopdate) "N/A"}
try { set fsidInfo(seltype) [dbobj $fsidInfo(rec) get SelectionType] } catch errCode {set fsidInfo(seltype) "N/A"}
try { set fsidInfo(expdate) [dbobj $fsidInfo(rec) get ExpirationDate] } catch errCode {set fsidInfo(expdate) "N/A"}
try { set fsidInfo(exptime) [dbobj $fsidInfo(rec) get ExpirationTime] } catch errCode {set fsidInfo(exptime) "N/A"}
try { set fsidInfo(score) [dbobj $fsidInfo(rec) get Score] } catch errCode {set fsidInfo(score) "N/A"}
try { set fsidInfo(starttime) [dbobj $fsidInfo(rec) get StartTime] } catch errCode {set fsidInfo(starttime) "N/A"}
try { set fsidInfo(stoptime) [dbobj $fsidInfo(rec) get StopTime] } catch errCode {set fsidInfo(stoptime) "N/A"}
try { set fsidInfo(creason) [dbobj $fsidInfo(rec) get CancelReason] } catch errCode {set fsidInfo(creason) "N/A"}
try { set fsidInfo(cdate) [dbobj $fsidInfo(rec) get CancelDate] } catch errCode {set fsidInfo(cdate) "N/A"}
try { set fsidInfo(ctime) [dbobj $fsidInfo(rec) get CancelDate] } catch errCode {set fsidInfo(ctime) "N/A"}
try { set fsidInfo(ddate) [dbobj $fsidInfo(rec) get DeletionDate] } catch errCode {set fsidInfo(ddate) "N/A"}
try { set fsidInfo(dtime) [dbobj $fsidInfo(rec) get DeletionTime] } catch errCode {set fsidInfo(dtime) "N/A"}
try { set fsidInfo(estring) [dbobj $fsidInfo(rec) get ErrorString] } catch errCode {set fsidInfo(estring) "N/A"}
try { set fsidInfo(state) [dbobj $fsidInfo(rec) get State] } catch errCode {set fsidInfo(state) "N/A"}
try { set fsidInfo(ipath) [dbobj $fsidInfo(rec) get IndexPath] } catch errCode {set fsidInfo(ipath) "N/A"}
try { set fsidInfo(dur) [dbobj $fsidInfo(showing) get Duration] } catch errCode {set fsidInfo(dur) "N/A"}
try { set fsidInfo(airdate) [dbobj $fsidInfo(program) get OriginalAirDate] } catch errCode {set fsidInfo(airdate) "N/A"}
try { set fsidInfo(series) [dbobj $fsidInfo(program) get Series] } catch errCode {set fsidInfo(series"N/A"}
try { set fsidInfo(thumb) [dbobj $fsidInfo(series) get ThumbData] } catch errCode {set fsidInfo(thumb) "N/A"}
try { set fsidInfo(showtype) [dbobj $fsidInfo(program) get ShowType] } catch errCode {set fsidInfo(showtype) "N/A"}
try { set fsidInfo(stream) [dbobj $fsidInfo(rec) get StreamFileSize] } catch errCode {set fsidInfo(stream) "N/A"}
try { set fsidInfo(actor) [dbobj $fsidInfo(program) get Actor] } catch errCode {set fsidInfo(actor) "N/A"}
try { set fsidInfo(writer) [dbobj $fsidInfo(program) get Writer] } catch errCode {set fsidInfo(writer"N/A"}
try { set fsidInfo(dir) [dbobj $fsidInfo(program) get Director] } catch errCode {set fsidInfo(dir) "N/A"}
try { set fsidInfo(year) [dbobj $fsidInfo(program) get MovieYear] } catch errCode {set fsidInfo(year) "N/A"}
try {
foreach part $fsidInfo(parts) {
set file [dbobj $part get File]
append fsidInfo(allparts) "$file "
}
} catch errCode {set fsidInfo(allparts) "N/A"}
set fsidInfo(port) $port
regsub -all "\/" $exepath "\.2F\." exepath
set fsidInfo(path) $exepath
# set defrecquality [dbobj $setup get RecordQuality]
#showing date
set seconds [expr [dbobj $fsidInfo(showing) get Date] * 86400 + [dbobj $fsidInfo(showing) get Time] + $tzoffset]
set fsidInfo(date) [clock format $seconds -format $dateformat]
#startdate
if { ! [isInt $fsidInfo(startdate)] } { set fsidInfo(startdate) 0 }
if { ! [isInt $fsidInfo(starttime)] } { set fsidInfo(starttime) 0 }
set seconds [expr $fsidInfo(startdate) * 86400 + $fsidInfo(starttime) + $tzoffset]
set fsidInfo(startdate) [clock format $seconds -format $startdateformat]
#stopdate
if { ! [isInt $fsidInfo(stopdate)] } { set fsidInfo(stopdate) 0 }
if { ! [isInt $fsidInfo(stoptime)] } { set fsidInfo(stoptime) 0 }
set seconds [expr $fsidInfo(stopdate) * 86400 + $fsidInfo(stoptime) + $tzoffset]
set fsidInfo(stopdate) [clock format $seconds -format $stopdateformat]
#expdate
if { ! [isInt $fsidInfo(exptime)] } { set fsidInfo(exptime) 0 }
if { ! [isInt $fsidInfo(expdate)] } { set fsidInfo(expdate) 0 }
set seconds [expr $fsidInfo(expdate) * 86400 + $fsidInfo(exptime) + $tzoffset]
set fsidInfo(expdate) [clock format $seconds -format $expdateformat]
#airdate
if { ! [isInt $fsidInfo(airdate)] } { set fsidInfo(airdate) 0 }
set seconds [expr $fsidInfo(airdate) * 86400]
set fsidInfo(airdate) [clock format $seconds -format $airdateformat]
#ddate
if { ! [isInt $fsidInfo(ddate)] } { set fsidInfo(ddate) 0 }
if { ! [isInt $fsidInfo(dtime)] } { set fsidInfo(dtime) 0 }
set seconds [expr $fsidInfo(ddate) * 86400 + $fsidInfo(dtime) + $tzoffset]
set fsidInfo(ddate) [clock format $seconds -format $ddateformat]
#cdate
if { ! [isInt $fsidInfo(cdate)] } { set fsidInfo(cdate) 0 }
if { ! [isInt $fsidInfo(ctime)] } { set fsidInfo(ctime) 0 }
set fsidInfo(cdate) [expr $fsidInfo(cdate) * 86400 + $fsidInfo(ctime) + $tzoffset]
#set fsidInfo(cdate) [clock format $seconds -format $cdateformat]
set size $fsidInfo(stream)
#set fsidInfo(dispsize) [expr $fsidInfo(stream) / 1024]
if { $fsidInfo(stream) > 1024 } {
set fsidInfo(dispsize) [expr $fsidInfo(stream) / 1024]
} else {
set fsidInfo(dispsize) $fsidInfo(stream)
}
try {
set size [expr { double( $size ) * double (1024) }]
if { $size > 2147483648.0 } {
set size 0
}
regexp {(.*)\..*} $size junk size
set fsidInfo(size) $size
} catch errCode {set fsidInfo(size) 0}
#set the parts
try {
foreach part $fsidInfo(parts) {
set file [dbobj $part get File]
append fsidInfo(hrefparts) "$file "
}
} catch errCode {set fsidInfo(hrefparts) "N/A"}
#parse out the content field
set newcontent $content
foreach name [array names fsidInfo] {
regsub -all {&} $fsidInfo($name) {.38.amp;} fsidInfo($name)
regsub -all {\(} $fsidInfo($name) {.38.#40;} fsidInfo($name)
regsub -all {\)} $fsidInfo($name) {.38.#41;} fsidInfo($name)
regsub -all {'} $fsidInfo($name) {.38.acute;} fsidInfo($name)
regsub -all {"} $fsidInfo($name) {.38.quot;} fsidInfo($name)
regsub -all "%$name%" $newcontent "$fsidInfo($name)" newcontent
regsub -all {\.38\.} $newcontent "\\\&" newcontent
}
puts $chan $newcontent
}
}
proc isInt { val } {
return [regexp {^-?[1-9][0-9]*$|^-?0[xX][0-9a-fA-F]+$|^-?0[0-7]*$} $val]
}
proc readConfig { chan } {
#available variables
global header
global content
global footer
global exepath
global port
global source_dir
global dateformat
global startdateformat
global stopdateformat
global cdateformat
global ddateformat
global airdateformat
global expdateformat
set header ""
global fsidInfo
# Open the .cfg file
set retcd [ catch { set fp [open $source_dir/modules/mfsstream.cfg r] } ]
if { $retcd == 1 } {
return 1;
}
# Read the entire file one line at a time
# Slurp up the data file
set data [read $fp]
close $fp
set data [split $data "\n"]
foreach inline $data {
if { [regexp {^#} $inline] == 0 } {
if { [regexp {^header::} $inline] } { regexp "^header::(.*)" $inline junk header }
if { [regexp {^content::} $inline] } { regexp "^content::(.*)" $inline junk content }
if { [regexp {^footer::} $inline] } { regexp "^footer::(.*)" $inline junk footer }
if { [regexp {^path::} $inline] } { regexp "^path::(.*)" $inline junk exepath }
if { [regexp {^port::} $inline] } { regexp "^port::(.*)" $inline junk port }
if { [regexp {^dateformat::} $inline] } { regexp "^dateformat::(.*)" $inline junk dateformat }
if { [regexp {^startdateformat::} $inline] } { regexp "^startdateformat::(.*)" $inline junk startdateformat }
if { [regexp {^stopdateformat::} $inline] } { regexp "^stopdateformat::(.*)" $inline junk stopdateformat }
if { [regexp {^cdateformat::} $inline] } { regexp "^cdateformat::(.*)" $inline junk cdateformat }
if { [regexp {^ddateformat::} $inline] } { regexp "^ddateformat::(.*)" $inline junk ddateformat }
if { [regexp {^airdateformat::} $inline] } { regexp "^airdateformat::(.*)" $inline junk airdateformat }
if { [regexp {^expdateformat::} $inline] } { regexp "^expdateformat::(.*)" $inline junk expdateformat }
}
}
}
proc GetOSVersion { chan } {
global db
global tivoswversion
global versionnum
set versionnum "0"
RetryTransaction {
set sws [db $db open /SwSystem/ACTIVE]
set tivoswversion [dbobj $sws get Name]
if { [PrefixMatches "3." $tivoswversion] } {
set versionnum "3"
} else {
set versionnum "2"
}
}
}
#sets the timezone
proc set_timezone { } {
global db
global tzoffset
global tivoswversion
global versionnum
RetryTransaction {
if { $versionnum == "2" } {
set setup [db $db open /Setup]
set setuptz [dbobj $setup get TimeZone]
} elseif { $versionnum == "3" } {
set setup [db $db open /State/LocationConfig]
set setuptz [dbobj $setup get TimeZoneOld]
}
set daylightsavings [dbobj $setup get DaylightSavingsPolicy]
set tzoffset [get_tzoffset $setuptz $daylightsavings]
}
}
proc get_tzoffset {mfstz dst} {
if { $mfstz < 0 } {
set tz $mfstz
} else {
set tzlist "-5 -6 -7 -8 -9 -10 0 1 2 3 4 5 6 7 8 9 10 11 12 -1 -2 -3 -4 -1
1 -12"
set tz [lindex $tzlist [expr $mfstz - 1]]
}
if { $tz == "" } { set tz 0 }
if { $dst == 2 || $dst == "" } {
set date [clock format [clock seconds] -format "%1d %u %1m %1H %1M"]
scan $date "%d %d %d %d %d" dom dow month hour min
if {$month > 4 && $month < 10} {
set dlsval 1
} elseif {$month == 4 && $dom > 7} {
set dlsval 1
} elseif {$month == 4 && $dom <= 7 && $dow == 0 && $hour >= 2} {
set dlsval 1
} elseif {$month == 4 && $dom <= 7 && $dow != 0 && ($dom-$dow > 0)} {
set dlsval 1
} elseif {$month == 10 && $dom < 25} {
set dlsval 1
} elseif {$month == 10 && $dom >= 25 && $dow == 0 && $hour < 2} {
set dlsval 1
} elseif {$month == 10 && $dom >= 25 && $dow != 0 && ($dom-24-$dow < 1) } {
set dlsval 1
} else {
set dlsval 0
}
if {$dlsval == 1} {
return [expr ($tz+1)*60*60]
} else {
return [expr $tz*60*60]
}
} else {
return [expr $tz*60*60]
}
}
proc printJavaScript {chan} {
puts $chan {
}
}
register_module "mfsstream" "MfsStream" "Download TY Streams"