#by wiebe @ QuakeNet



#help duration pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} help duration" c_duration:help:pub

proc c_duration:help:pub { nick uhost handle chan text } {
  lappend output "Usage: duration <seconds>|<date>"
  lappend output "Shows the duration for the given number of seconds or duration since/to the given date. Format for <date> is yyyy-mm-dd hh:nn:ss, not all parts are required. Without date, today is used."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#help duration msg
bind msgm fvlomn|fvlomn "help duration" c_duration:help:msg

proc c_duration:help:msg { nick uhost handle text } {
  lappend output "Usage: duration <seconds>|<date>"
  lappend output "Shows the duration for the given number of seconds or duration since/to the given date. Format for <date> is yyyy-mm-dd hh:nn:ss, not all parts are required. Without date, today is used."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#duration pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} duration" c_duration:pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} duration *" c_duration:pub

proc c_duration:pub { nick uhost handle chan text } {
  set timestamp [join [lrange [split $text] 2 end]]
  set output ""
  if { [string equal $timestamp ""] } {
    lappend output "Usage: duration <seconds>|<date>"
  } elseif { [string is digit $timestamp] } {
    set duration [expr [unixtime] + $timestamp]
    set duration [c_duration:ts $duration]
    puthelp "PRIVMSG $chan :Duration of $timestamp seconds: $duration"
  } elseif { [catch {set unixtime [clock scan $timestamp]} error] } {
    lappend output "Error converting $timestamp\017 to unixtime. Format yyyy-mm-dd hh:nn:ss, not all parts are required."
  } else {
    set duration [c_duration:ts $unixtime]
    if { $unixtime > [unixtime] } {
      puthelp "PRIVMSG $chan :Duration to $timestamp: $duration"
    } else {
      puthelp "PRIVMSG $chan :Duration since $timestamp: $duration"
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}




#duration msg
bind msg fvlomn|fvlomn duration c_duration:msg

proc c_duration:msg { nick uhost handle text } {
  set timestamp $text
  if { [string equal $timestamp ""] } {
    lappend output "Usage: duration <seconds>|<date>"
  } elseif { [string is digit $timestamp] } {
    set duration [expr [unixtime] + $timestamp]
    set duration [c_duration:ts $duration]
    lappend output "Duration of $timestamp seconds: $duration"
  } elseif { [catch {set unixtime [clock scan $timestamp]} error] } {
    lappend output "Error converting $timestamp\017 to unixtime. Format yyyy-mm-dd hh:nn:ss, not all parts are required."
  } else {
    set duration [c_duration:ts $unixtime]
    if { $unixtime > [unixtime] } {
      lappend output "Duration to $timestamp: $duration"
    } else {
      lappend output "Duration since $timestamp: $duration"
    }
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#duration dcc
bind dcc fvlomn|fvlomn duration c_duration:dcc

proc c_duration:dcc { handle idx text } {
  set timestamp $text
  if { [string equal $timestamp ""] } {
    putidx $idx "Usage: duration <seconds>|<date>"
  } elseif { [string is digit $timestamp] } {
    set duration [expr [unixtime] + $timestamp]
    set duration [c_duration:ts $duration]
    putidx $idx "Duration of $timestamp seconds: $duration"
  } elseif { [catch {set unixtime [clock scan $timestamp]} error] } {
    putidx $idx "Error converting $timestamp\017 to unixtime. Format yyyy-mm-dd hh:nn:ss, not all parts are required."
  } else {
    set duration [c_duration:ts $unixtime]
    if { $unixtime > [unixtime] } {
      putidx $idx "Duration to $timestamp: $duration"
    } else {
      putidx $idx "Duration since $timestamp: $duration"
    }
  }
  return 1
}



#give timestamp, returns duration since/to in Xy Xw Xd Xh Xm Xs format
proc c_duration:ts { ts } {
  if { ![string is digit $ts] } { return 0 }
  if { $ts > [unixtime] } {
    set ts [duration [expr $ts - [unixtime]]]
  } else {
    set ts [duration [expr [unixtime] - $ts]]
  }
  set ts [string map [list " seconds" "s" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " second" "s" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " minutes" "m" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " minute" "m" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " hours" "h" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " hour" "h" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " days" "d" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " day" "d" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " weeks" "w" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " week" "w" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " years" "y" "search" "replace" "search" "replace"] $ts]
  set ts [string map [list " year" "y" "search" "replace" "search" "replace"] $ts]
  return $ts
}

