## Note Routing procs.
## by Butchie: danny@dune.applicad.co.il
## This script routes notes to a central bot.

## New Commands:
## .note <-- this remains the same.
## .ln <text> = Sends <text> to the last person you sent a note to.
## .rln <handle> = Sends the contents of your last note to <handle>
## .re <text> = Sends <text> to the last person that sent you a note.
## NB: All cmds handle handle@bot as well.  
##     This script replaces and unbinds the original note procedure in eggdrop.
##     .note proc will replace all " chars with ' (avoids certain TCL problems)

## Sets ##
##------##  
### Just make sure you set the line below.
### You put there the name of the bot that will collect the notes.
### Usually it's the HUB bot of your botnet.
### This var will reset to "" if the bot is not +s (shares userlist),
### or if the name here is not a valid bot on the botnet.
### I also recommend increasing the number of notes that bot can keep per-user.
### (see src\eggdrop.h)
set note_keeper_bot "mybot" 

##################################################################
########## NO NEED TO TOUCH ANYTHING BELOW THIS LINE! ############
##################################################################

## Binds ##
##-------##
unbind dcc p note *note
bind dcc p note new_note
bind dcc p rln resend_last_note
bind dcc p ln send_last_note
bind dcc p re re_last_note
bind bot b setnotefrom update_note_from
bind chof p * kill_note_vars 

## Procs ##
##-------##
### note modules
## ver 3.0
## by Butchie

## this replaces the regular proc (note).
proc new_note {hand idx args} {
  global notes_last_to notes_last_text notes_last_from note_keeper_bot
  regsub -all \" [lindex $args 0] \' rest
  set dest [lindex $rest 0]
  set content [lrange $rest 1 end]
  if {$content==""} {
    putdcc $idx "Usage: .note <to> <text> (Missing <text>)"
    return 0}
  if {$dest==""} {
    putdcc $idx "Usage: .note <to> <text> (Missing destination)"
    return 0}
  if {[note_keeper_check] && ([llength [split $dest @]]==1) && ([hand2idx $dest]=="-1")} {
    putdcc $idx "$dest is not present on this bot. Re-routing note to \002Note-Keeper\002 bot ($note_keeper_bot)."
    append dest "@$note_keeper_bot"}
  set result [sendnote $hand $dest $content]
  switch $result {
    "0" {
      putdcc $idx "Failed to send note!"
      return 0}
    "1" {
      putdcc $idx "-> $dest $content"
      putdcc $idx "Note Sent."
      set notes_last_to($hand) $dest
      set notes_last_text($hand) $content
      set dest [split $dest @]
      if {[llength $dest] == 1} {
        set notes_last_from([string tolower $dest]) $hand} else {
          putbot [lindex $dest 1] "setnotefrom [lindex $dest 0] $hand"}
      return 0}
    "2" {
      putdcc $idx "Note Stored."
      set notes_last_to($hand) $dest
      set notes_last_text($hand) $content
      return 0}
    "3" {
      putdcc $idx "Failed to send note. (notebox full). contents and destination cached."
      set notes_last_to($hand) $dest
      set notes_last_text($hand) $content
      return 0}
    "4" {
      set notes_last_to($hand) $dest
      set notes_last_text($hand) $content
      return 0}
    "5" {
      putdcc $idx "User is away. Note Stored. (and cached)"
      set notes_last_to($hand) $dest
      set notes_last_text($hand) $content
      set dest [split $dest @]
      if {[llength $dest] == 1} {
        set notes_last_from([string tolower $dest]) $hand} else {
          putbot [lindex $dest 1] "setnotefrom [lindex $dest 0] $hand"}
      return 0}
    "default" {
      putdcc $idx "Failed."
      return 0}
  }
return 2
}
                    
## this returns 0 when the note shouldn't be sent to the note_keeper.
proc note_keeper_check {} {
  global note_keeper_bot              
# no note-keeper-bot (a.k.a NKB)
  if {$note_keeper_bot==""} {return 0}
# NKB exists but doesn't share users - might not have that user on his lists
# or that user can be someone else.
  if {![matchattr $note_keeper_bot s]} {
    putlog ">>>\002Note-Keeper\002 Bot is \002not\002 sharing users with this bot!<<<"
    set note_keeper_bot ""
    return 0}                                                                     
# NKB is not connected right now.
  if {[lsearch [string tolower [bots]] [string tolower $note_keeper_bot]]=="-1"} {return 0}
# else... return 1
  return 1}

## resend my last note.
proc resend_last_note {hand idx rest} {
  global notes_last_text
  set newdest [lindex $rest 0]
  if {$rest==""} {
    putdcc $idx "Usage: .rln <new-destination> (will resend your last sent note)"
    return 0}
  if ![info exists notes_last_text($hand)] {
    putdcc $idx "You haven't sent a note to anyone yet. use .note"
    return 0}
  new_note $hand $idx "$newdest $notes_last_text($hand)"
  return 0}

## send a note to the last person you sent a note to
proc send_last_note {hand idx rest} {
  global notes_last_to
  if {$rest==""} {
    putdcc $idx "Usage: .ln <text> (will send text to the last person you sent note to)"
    return 0}
  if ![info exists notes_last_to($hand)] {
    putdcc $idx "You haven't sent a note to anyone yet. use .note"
    return 0}
  new_note $hand $idx "$notes_last_to($hand) $rest"
  return 0}
             
## send a note to the last person that sent a note to you.
proc re_last_note {hand idx rest} {
  global notes_last_from
  if {$rest==""} {
    putdcc $idx "Usage: .re <text> (will send <text> to the last person that sent you a note)"
    return 0}
  if ![info exists notes_last_from([string tolower $hand])] {
    putdcc $idx "You haven't recieved any notes yet!"
    return 0}
  new_note $hand $idx "$notes_last_from([string tolower $hand]) $rest"
  return 0}

## kills your last notes reference
proc kill_note_vars {hand idx} {
  global notes_last_to notes_last_text
  catch {unset notes_last_to($hand) notes_last_text($hand) notes_last_from([string tolower $hand])}
return 0}  

## updates notes_last_from over the botnet
proc update_note_from {from cmd rest} {
  global notes_last_from
  if {$cmd!="setnotefrom"} {return 0}
  set dest [string tolower [lindex $rest 0]]
  set hand [lindex $rest 1]
  if {[hand2idx $dest]=="-1"} {return 0}
  set notes_last_from($dest) "$hand@$from"
  return 0
} 

## this makes sure that the note-keeper is not YOUR bot
if {[string tolower $nick]==[string tolower $note_keeper_bot]} {set note_keeper_bot ""}

if {$note_keeper_bot==""} {putlog ">>> \002Note-Keeper\002 Bot is \002not\002 set. <<<"} else {
  putlog ">>> \002Note-Keeper\002 Bot is: \002$note_keeper_bot\002 <<<"}

putlog "### Loaded note-module ver 3.0 (Butchie) ###"
