unbind dcc - note *note
bind dcc - note botnetnoteredir

proc botnetnoteredir {hand idx arg} {
global botnick notekey noteincr
  set target [lindex $arg 0]
  set text [lrange $arg 1 end]
  if {$text != ""} {
    if {[bots] == ""} {
      if {[getdccaway $idx] == "" } {
        if {[echo $idx] == 1} {
          putdcc $idx "-> $target $text"
        }
      }
      resendnote $hand $target $text
      return 0
    } {
      set handle [split $target @]
      set rbot [lindex $handle 1]
      set to [lindex $handle 0]
      if {$to == ""} {
        putdcc $idx "USAGE: .note <to-whom> OR .note <to-whom>@<bot)"
        return 0
      } {
        if {$rbot != ""} {
          resendnote $hand $target $text
          return 0
        } {
          if {[validuser $to]} {
            set onbot 0
            if {[set bottime [user-get $to botlaston]] != ""} {
              foreach person [dcclist] {
                if {[string tolower $to] == [string tolower [lindex $person 1]]} {
                  set targidx [lindex $person 0]
                  set onbot 1
                  if {[getdccaway $targidx] == ""} {
                     resendnote $hand $to $text
                     putdcc $idx "-> $to $text"
                     return 0
                     break
                  } {
                    if {$noteincr == 5 } {
                      set notekey [rand 999]
                      set noteincr 0
                    } {
                      if {![info exists notekey]} {
                        set notekey [rand 999]
                      }
                    }
                    set text [encrypt $notekey $text]
                    putallbots "redirawychk $hand $to $text"
                  }
                }
              }
            } 
            if {$onbot == 0} {
              if {$noteincr == 5} {
                set notekey [rand 999]
                set noteincr 0
              } {
                if {![info exists notekey]} {
                  set notekey [rand 999]
                }
              }
            set text [encrypt $notekey $text]
            putallbots "redironchk $hand $to $text"
            }
          } {
            putdcc $idx "Sorry but I don't know anyone by that name"
          }
        }
      }
    }
  } {
    putdcc $idx "Usage: .note <nick> <text>"
  }
}


bind bot - redirawychk bot_redirawychk

proc bot_redirawychk {bot cmd arg} {
  global botnick
    set hand [lindex $arg 0]
    set to [lindex $arg 1]
    set text [lrange $arg 2 end]
    if {[validuser $to]} {  
      set onbot 0
      if {[set bottime [user-get $to botlaston]] != ""} {
        foreach person [dcclist] {
          if {[string tolower $to] == [string tolower [lindex $person 1]]} {
            set targidx [lindex $person 0]
            if {[getdccaway $targidx]  == ""} {
              putbot $bot "noterediravl $hand $to $text"
              return 0
              break
            } {
              putbot $bot "storenote $hand $to $text"
              return 0 
            }
          }
        }
      }
    }
    if {$onbot == 0} {
      putbot $bot "storenote $hand $to $text"
    }
}


bind bot - redironchk bot_redironchk
            
proc bot_redironchk {bot cmd arg} {
  global botnick
    set hand [lindex $arg 0]
    set to [lindex $arg 1]
    set text [lrange $arg 2 end]
    set onbot 0
    if {[validuser $to]} {
      if {[set bottime [user-get $to botlaston]] != ""} {
        foreach person [dcclist] {
          if {[string tolower $to] == [string tolower [lindex $person 1]]} {
            set targidx [lindex $person 0]
            set onbot 1
            break
          }
        }
      }
      if {$onbot == 1} {
        putbot $bot "noterediravl $hand $to $text"
      } {
        putbot $bot "storenote $hand $to $text"
        return 0
      }
    } {
      putbot $bot "storenote $hand $to $text"
      return 0
    }
}
            

bind bot - storenote bot_storenote

proc bot_storenote {bot cmd arg} {
  global botnick lastnoteauth notekey noteincr storewait
    if {![info exists lastnoteauth]} {
      set lastnoteauth "none none none"
    }
    if {![info exists storewait]} {
      set storewait 5
    }
    set to [lindex $arg 1]
    if {[validuser $to]} {
      if {[string tolower $arg] != [string tolower $lastnoteauth]} {
        utimer $storewait "storenotelocal $arg"
      }
    } {
      if {$idx != -1} {
        if {[getdccaway $idx] == "" } {
          putdcc $idx "I don't know anyone by that name."
          return 0
        }
      }
    }
}

proc storenotelocal {hand to text} {
  global botnick lastnoteauth noteincr notekey
    set text [decrypt $notekey $text]
    set idx [getidx $hand]
    set targidx [getidx $to]
    set channel [lindex [console $idx] 0]
    set arg "$hand $to $text"
    if {[string tolower $arg] != [string tolower $lastnoteauth]} {
      set lastnoteauth $arg
      incr noteincr
      if {$targidx != -1} {
        if {[getdccaway $targidx] == "" } {
          if {[getdccaway $idx] == ""} {
            if {[echo $idx] == 1} {
              putdcc $idx "-> $to $text"
            }
          resendnote $hand $to $text
          return 0
          }
        } {
          putdcc $idx "$to is away: [getdccaway $targidx]"
        }
        putdcc $idx "Note Stored."
      }
      if {[onchan $to $channel]} {
        putserv "NOTICE [hand2nick $to $channel] :Note arrived for you."
      }
      resendnote $hand $to $text
      return 0
    }
}


bind bot - noterediravl bot_noterediravl

proc bot_noterediravl {bot cmd arg} {
  global botnick lastnoteauth notekey noteincr
    if {![info exists lastnoteauth]} {
      set lastnoteauth "none none none"
    }
    set hand [lindex $arg 0]
    set to [lindex $arg 1]
    set text [lrange $arg 2 end]
    set idx [getidx $hand]
    set onbot 0
    if {$lastnoteauth != $arg} {
      putbot $bot "noteredirauth $hand $to $notekey $text"
      set lastnoteauth $arg
      incr noteincr
    }
}


bind bot - noteredirauth bot_noteredirauth

proc bot_noteredirauth {bot cmd arg} {
  global botnick
    set hand [lindex $arg 0]
    set to [lindex $arg 1]
    set notekey [lindex $arg 2]
    set text [lrange $arg 3 end]
    set text [decrypt $notekey $text]
    putbot $bot "note_delivered $hand $to $text"
    resendnote "$hand@$bot" $to $text 
}


proc resendnote {hand to text} {
  global botnick lastauthnote
    set arg "$hand $to $text"
    set lastauthnote $arg
    sendnote $hand $to $text
}

bind bot - note_delivered note_delivered

proc note_delivered {bot cmd arg} {
  global botnick
    set hand [lindex $arg 0]
    set to [lindex $arg 1]
    set text [lrange $arg 2 end]
    set idx [getidx $hand]
    set target "$to@$bot"
    if {[getdccaway $idx] == "" } {
      if {[echo $idx] == 1} {
        putdcc $idx "-> $target $text"
      }
    }
    putdcc $idx "$bot: Note delivered to $to."
}


bind bot - note_stored note_stored

proc note_stored {bot cmd arg} {
  global botnick
    set hand [lindex $arg 0]
    set to [lindex $arg 1]
    set away [lrange $arg 2 end]
    set idx [getidx $hand]
    if {[getdccaway $idx] == "" } {
      putdcc $idx "$bot: $to is on $bot away: $away."
    }
    putdcc $idx "Note Stored on $bot."
 
}     


proc ask_storewait {} {
  global botnick
    putallbots "ping $botnick [unixtime]"
    set storewait_timer [timer 20 ask_storewait]
}


unbind bot - pong bot_pong
bind bot - pong bot_set_storewait

proc bot_set_storewait {bot cmd arg} {
  global botnick
    set hand [lindex $arg 0]
    if {$hand == $botnick} {
      set pingtime [expr [unixtime] - [lindex $arg 1]]
      if {!($pingtime > 3)} {
        set storewait [expr 1 + ($pingtime * 2)]
      }
    } {
      bot_pong $bot botpong $arg
    }
}


if {![info exists lastnoteauth]} {
  set lastauthnote "none none none"
}

if {![info exists notekey]} {
  set notekey [rand 999]
}

if {![info exists noteincr]} {
  set noteincr "0"
}

if {![info exists storewait_timer]} {
  set storewait_timer [timer 20 ask_storewait]
}

putlog "Botnet Note Re-Direction System v1.2.0 by Lucien loaded."
