## this line makes sure other scripts won't interfere
catch {unset webfile}

## for each channel you want a webfile for, do this:
set webfile(#UC) "/home/eric/public_html/uc/whoson.html"


## define these for channels you want alternate (lynx-friendly) web pages
## for:  (the 'lynxfilerf' needs to be relative to your html directory)
set lynxfile(#UC) "/home/eric/public_html/uc/whoson-lynx.html"
set lynxfilerf(#UC) "whoson-lynx.html"
set pic(#UC) "white.gif"


## how often should these html files get updated?
## (1 means once every minute, 5 means once every 5 minutes, etc)
set web_update 1

## this will help people figure out what timezone you're in :)
set webtz "CET"


bind pub - !web do_ques
proc do_ques {  global webfile lynxfile web_update botnick webtz lynxfilerf pic} {
  foreach chan [array names webfile] {
    if {[lsearch -exact [string tolower [channels]] [string tolower $chan]] == -1} {continue}
    set fd [open $webfile($chan) w]
    if {[info exists lynxfile($chan)]} {
      set fdl [open $lynxfile($chan) w]
    } else {
      set fdl [open "/dev/null" w]
    }
    puts $fd "<meta http-equiv=\"Refresh\" content=[expr $web_update *60]>"
    if {![onchan $botnick $chan]} {
      puts $fd "<html><head><title>People on $chan right now!</title></head>"
      puts $fd "<body><h1>Oops!</h1><p>"
      puts $fd "The bot is not on $chan right now for some strange reason.<br>"
      puts $fd "IRC isn't a very stable place these days... Please try again"
      puts $fd "later!<p></body></html>"
      puts $fdl "<html><head><title>People on $chan right now!</title></head>"
      puts $fdl "<body><h1>Oops!</h1><p>"
      puts $fdl "The bot is not on $chan right now for some strange reason.<br>"
      puts $fdl "IRC isn't a very stable place these days... Please try again"
      puts $fdl "later!<p></body></html>"
      close $fd
      close $fdl
    } {
      puts $fd "<html><head><title>People on $chan right now!</title></head>"
      puts $fd "<body><center><A HREF=irc.html><IMG SRC=$pic($chan) ALT=$chan BORDER=0></A>"
      puts $fdl "<html><head><title>People on $chan right now!</title></head>"
      puts $fdl "<body><h1>$chan</h1>"
      if {[info exists lynxfile($chan)]} {
        puts $fd "<p>If this page looks strange on your browser, try"
        puts $fd "<a href=\"$lynxfilerf($chan)\">this page</a>.<p>"
      }
      puts $fd "People on $chan as of [date] [time] $webtz<P>"
      puts $fd "<table border=1 cellpadding=4>"
      puts $fd "<th align=left>Nickname<th align=left>Status"
      puts $fd "<th align=left>User@Host"
      puts $fdl "<em>People on $chan as of [date] [time] $webtz</em><pre>"
      puts $fdl "Nickname  Status           User@Host"
      foreach i [chanlist $chan] {
        puts $fd "<tr>"
        if {[isop $i $chan]} { set chop "op"} { set chop "" }
        if {[getchanidle $i $chan] > 10} {
          if {$chop == ""} { set chop "idle" } { set chop "${chop}, idle" }
        }
        set handle [finduser $i![getchanhost $i $chan]]
        if {[onchansplit $i $chan]} {
          if {$chop == ""} { 
            set chop "<strong>(split)</strong>"
          } {
            set chop "${chop} <strong>(split)</strong>"
          }
        }
        set url [geturl $handle]
        set info [getinfo $handle]
        if {"" == $chop} { set chop "-" }
        set link ""
        if {"" != $url} {
          set link "<a href=\"$url\">"
        }
        if {("" == $link) &&
            ([string compare [string range $info 0 6] "http://"] == 0)} {
          set link "<a href=\"[lindex [split $info] 0]\">"
        }
        if {"" == $link} {
          set email [getemail $handle]
          if {("" != $email) && ([string first @ $email] > 0)} {
            set link "<a href=\"mailto:$email\">"
          }
        }
        if {"" != $link} { set elink "</a>" } { set elink "" }
        puts $fd "<td align=left>${link}${i}${elink}</td>"
        puts $fd "<td align=left>${chop}</td>"
        set chost [getchanhost $i $chan]
        if {[string compare [string tolower $i] [string tolower $botnick]] == 0} {
          set chost "<em>I'm a Believer and the bot on this channel!</em>"
          set info ""
        } {
          if {[matchattr $handle b]} {
            set chost "<em>This is another channel bot.</em>"
            set info ""
          }
        }
        puts $fd "<td align=left>${chost}</td>"
        puts $fdl "${link}[format \%-9s $i]${elink} [format \%-16s $chop] $chost"
        if {"" != $info} {
          puts $fd "<tr><td></td><td colspan=2><strong>Info:</strong> $info</td>"
          puts $fdl "   <strong>Info:</strong> $info"
        }
      }
      puts $fd "</table>"
      puts $fdl "</pre>"
      puts $fd "<P><address>This page will reload automatically every minute.</address></center>"
      puts $fd "</body></html>"
      puts $fdl "</body></html>"
      close $fd
      close $fdl
    }
}

set xxchans ""
foreach i [array names webfile] {
  set xxchans "$xxchans $i"
  if {![info exists lynxfile($i)]} {
    set xxchans "$xxchans (no lynx)"
  }
  set xxchans "${xxchans},"
}
set xxchans [string trimright $xxchans ","]
putlog "Quesedilla v3:$xxchans"
unset xxchans
