####################################################
# by wiebe @ QuakeNet
#
####################################################


####################################################
# duration:help:pubm
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} help duration" duration:help:pubm
proc duration:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "duration: usage duration <seconds>|<date><time>"
  lappend o "duration: shows the duration for the given number of seconds, or the time since/left to a given time or date."
  lappend o "duration: <time> a time of day, which is of the form: hh?:mm?:ss?? ?meridian? ?zone? or hhmm ?meridian? ?zone?. if no meridian is specified, hh is interpreted on a 24-hour clock."
  lappend o "duration: <date> a specific month and day with optional year. the acceptable formats are mm/dd?/yy?, monthname dd ?, yy?, dd monthname ?yy?, day, dd monthname yy, ?CC?yymmdd, ?CC?yy-mm-dd, dd-monthname-?CC?yy. the default year is the current year. if the year is less than 100, we treat the years 00-68 as 2000-2068 and the years 69-99 as 1969-1999."
relative time
  lappend o "duration: a specification relative to the current time. the format is number unit acceptable units are year, fortnight, month, week, day, hour, minute (or min), and second (or sec). the unit can be specified as a singular or plural, as in 3 weeks. these modifiers may also be specified: tomorrow, yesterday, today, now, last, this, next, ago."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "duration: " } }
  putloglev c $c "help: $n $u $h $c duration"
  return 1
}


####################################################
# duration:help:msg
####################################################
bind msgm fvlomn|fvlomn "help duration" duration:help:msgm
proc duration:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "duration: usage duration <seconds>|<date><time>"
  lappend o "duration: shows the duration for the given number of seconds, or the time since/left to a given time or date."
  lappend o "duration: <time> a time of day, which is of the form: hh?:mm?:ss?? ?meridian? ?zone? or hhmm ?meridian? ?zone?. if no meridian is specified, hh is interpreted on a 24-hour clock."
  lappend o "duration: <date> a specific month and day with optional year. the acceptable formats are mm/dd?/yy?, monthname dd ?, yy?, dd monthname ?yy?, day, dd monthname yy, ?CC?yymmdd, ?CC?yy-mm-dd, dd-monthname-?CC?yy. the default year is the current year. if the year is less than 100, we treat the years 00-68 as 2000-2068 and the years 69-99 as 1969-1999."
  lappend o "duration: a specification relative to the current time. the format is number unit acceptable units are year, fortnight, month, week, day, hour, minute (or min), and second (or sec). the unit can be specified as a singular or plural, as in 3 weeks. these modifiers may also be specified: tomorrow, yesterday, today, now, last, this, next, ago."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "duration: " } }
  putcmdlog "($n!$u) !$h! help duration"
  return 1
}


####################################################
# duration:pub
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} duration" duration:pubm
bind pubm fvlomn|fvlomn "% ${botnet-nick} duration *" duration:pubm
proc duration:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "duration: usage duration <seconds>|<date><time>"
  } else {
    set o [duration:duration $t]; set m [lindex [split [getchanmode $c]] 0]
    if {![string match *m* $m] || [botisop $c] || [botishalfop $c] || [botisvoice $c]} {
      if {[string match *c* $m] } { set o [stripcodes bcru $o] }
      if {[string equal [info procs privmsg] ""]} { foreach l $o { puthelp "PRIVMSG $c :$l" }
      } else { foreach l $o { privmsg $c $l puthelp "duration: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "duration: " } }
  putloglev c $c "duration: $n $u $h $c $t"
  return 1
}


####################################################
# duration:msg
####################################################
bind msg fvlomn|fvlomn duration duration:msg
proc duration:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} { lappend o "duration: usage duration <seconds>|<date><time>"
  } else { set o [duration:duration $t] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "duration: " } }
  return 1
}


####################################################
# duration:dcc
####################################################
bind dcc -|- duration duration:dcc
proc duration:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "duration: usage duration <seconds>|<date><time>"
    lappend o "duration: shows the duration for the given number of seconds, or the time since/left to a given time or date."
    lappend o "duration: <time> a time of day, which is of the form: hh?:mm?:ss?? ?meridian? ?zone? or hhmm ?meridian? ?zone?. if no meridian is specified, hh is interpreted on a 24-hour clock."
    lappend o "duration: <date> a specific month and day with optional year. the acceptable formats are mm/dd?/yy?, monthname dd ?, yy?, dd monthname ?yy?, day, dd monthname yy, ?CC?yymmdd, ?CC?yy-mm-dd, dd-monthname-?CC?yy. the default year is the current year. if the year is less than 100, we treat the years 00-68 as 2000-2068 and the years 69-99 as 1969-1999."
    lappend o "duration: a specification relative to the current time. the format is number unit acceptable units are year, fortnight, month, week, day, hour, minute (or min), and second (or sec). the unit can be specified as a singular or plural, as in 3 weeks. these modifiers may also be specified: tomorrow, yesterday, today, now, last, this, next, ago."
  } else { set o [duration:duration $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# duration:duration
####################################################
proc duration:duration { t } {
  set n [clock seconds]; set f $t
  if {[string is digit $f]} { set f [ctime $f] }
  if {[string is digit $t] && $t > [expr pow(2,32)]} {
    lappend o "duration: $t is greater than 2^32"
  } elseif {[catch {set m [clock scan $f]} e]} {
    lappend o "duration: $e"
  } else {
    set d [duration:ts $m]
    if {![string equal $f $t]} { set f "unixtime $t, date $f" }
    if {$m > $n} { lappend o "duration: duration to '$f' is '$d'"
    } else { lappend o "duration: duration since '$f' is '$d'" }
  }
  return $o
}


####################################################
# duration:ts
####################################################
proc duration:ts { t } {
  if {![string is digit $t]} { return 0 }
  set n [clock seconds]; set t [expr $t - $n]; if {$t < 0} { set t [expr $t * -1] }
  set t [duration $t]
  set t [string map "seconds s second s minutes m minute m hours h hour h" $t]
  set t [string map "days d day d weeks w week w years y year y" $t]
  return [join [lrange [split $t] 0 3] ""]
}


set scriptdb(duration) {
  "provides duration command for looking up unixtimes, dates and duration to since the given input"
}

