### Change this to 0 if you don't want 0.9 support -- you are probably
### better off w/o it anyway because the new versions should work on the
### 0.9 bots, too...  And the autoget works even if the bot isn't using
### its own name -- but just in case, leave this to one if you aren't sure

@talk.oldok= 1

if (0) {
#
# Use to talk to an eggdrop bot in dcc chat
#
# Originally by Robey
# Re-written by Fred1
#
# 06-27-96 cmwagner  Fixed prompt when changing directory in files area on
#                    1.0 bots, changes prompt now.  Fixed removal of files
#                    area prompt when returning from the files area (so that
#                    it is not used in the party line, too)
#
#  Fred1's mods:
#    nTalk v3:
#     Fixed the bug that kept the nickname from showing up for master-only
#      chats.  Before, me doing ",howdy" came out "howdy" vs. "-Fred1- howdy"
#     Nicks are now encoded so that the script works correctly for all valid
#      nicks instead of choking on ones with special chars
#     Fixed the bug where it would always close your current window instead
#      of the proper talk session
#     Greatly simplified robey's parenthesis-happy stuff where I could *grin*
#    nTalk v4:
#     Modified for Eggdrop 1.0 compatibility.  The support for v0.9-specific
#      nTalk is optional and can be disabled by changing the @talk.oldok= 1
#      statement at the beginning of this file to: @talk.oldok= 0
#
#  Warnings, disclaimers, and other comments:
#    While I have not included backdoors deliberately and I have endeavored
#     to prevent them wherever possible, there are no guarantees.  Use this
#     script at your own risk!
#    This script uses an alias "*" which I recommend that you use whenever
#     you want to echo at level crap -- if for no other reason because it
#     is faster and it attempts to correct any termcap highlighting errors
#     [such as INVERSE being left on and screwing up your screen].
#     That shouldn't clash w/ other scripts, but as with any other alias,
#     I guess it's possible...
#    In general, the use of bot responses to key the events is not really
#     a very clean way to do this because the script will not work if
#     some warez puppy has modified the bot to say "Fred1, yo mamma!
#     Slapping ya summa dat file.txt, beeyotch!" instead of the normal
#     "Sending: file.txt to Fred1" and the same goes for the other keys,
#     such as "Connected", the bot version string, "Now in:", "Current
#     directory:", "Returning you to", etc.  But as long as people leave
#     those things alone it won't be a problem -- changing them is pretty
#     lame anyway.
#    This will destroy any "/me" alias you have.
#
}

### The * alias for echoing crap

alias * xecho -level crap *** $0-

### The actual command

alias ntalk {
if (![$0]) {
	*  Usage: /TALK <bot>
	*    Features:
	*      Establish or close a chat session with an Eggdrop bot.
	*      Uses a dedicated IrcII window
	*      Control-\ will switch between windows
	*      Translates /me into .me
}{
	@ talk.z= encode($toupper($0))
	@ talk.q= talk.session[$talk.z]
	if (talk.q) {
		//^dcc close chat $0
		talk.done $0 $talk.z $talk.q
	}{
		@ talk.q= 1
		while (talk.cross[$talk.q]) {@ talk.q= talk.q+1}
		@ talk.cross[$talk.q]= talk.z
		@ talk.session[$talk.z]= talk.q
		@ talk.echo[$talk.q]= [INIT]
		^assign -talk.prompt[$talk.q]
		^window new scroll on shrink 5 name Talk$talk.q level none
		^set hold_mode off
		^query =$0
		^on ^window "% \*\*\* DCC CHAT % % $0 lost*" talk.lost $$6
		^on ^dcc_chat "$0 *" talk.recv.off $$0-
		^on ^send_dcc_chat "$0 *" talk.send $$0-
		xecho -window Talk$talk.q Use /TALK $0 again to end.
		^dcc chat $0 talk
	}
}}

### Various handlers

# Called whenever you receive something from the bot before negotiation.

alias talk.recv.off {
	@ talk.z= encode($toupper($0))
	@ talk.q= talk.session[$talk.z]
	if ([$1] == [///]) {
		if ([$2]==[OK]) {
			^on ^dcc_chat "$0 *" talk.recv.old $$0-
			xecho -window Talk$talk.q Using OLD Talk protocol.
		}
	}{
		if ([$1] == [Connected]) {
			if ((talk.oldok) && match(*v0.* $4$5$6$7$8$9)) {
				msg =$0 /// talk
			}{
				^on ^dcc_chat "$0 *" talk.recv.new $$0-
				xecho -window Talk$talk.q Using NEW Talk protocol.
			}
		}
		xecho -window Talk$talk.q $1-
	}
}

# Called whenever you receive after negotiating old Talk protocol
# This is never used unless you set talk.oldok to 1 and nTalk with
# one of the old v0.9 bots.

alias talk.recv.old {
	@ talk.z= encode($toupper($0))
	@ talk.q= talk.session[$talk.z]
	if ([$1] == [///]) {
		if ([$2] == [ECHO]) {@ talk.echo[$talk.q]= [$3]}
		if ([$2] == [PROMPT]) {@ talk.prompt[$talk.q]= [$3]}
		if ([$2] == [GET]) {
			^on -ctcp "% % DCC SEND $3 *" {
				//^dcc get $0 $4
				^on ctcp -"% % DCC SEND $4 *"
				* nTalk is autogetting $4 from $0
			}
			timer 120 talk.time ^on -ctcp -"% % DCC SEND $3 *"
		}
	}{
		xecho -window Talk$talk.q $1-
	}
}

# Called whenever you receive after negotiating new Talk protocol

alias talk.recv.new {
	@ talk.z= encode($toupper($0))
	@ talk.q= talk.session[$talk.z]
	if ([$1-2]==[Current directory:] || [$1-2]==[Now in:]) {
		@ talk.echo[$talk.q]= [ON]
		@ talk.prompt[$talk.q]= [$3]
	}
	if ([$1-3]==[New current directory:]) {
		@ talk.echo[$talk.q]= [ON]
		@ talk.prompt[$talk.q]= [$4]
	}
	if ([$1-2]==[Returning you]) {
		@ talk.echo[$talk.q]= [OFF]
		@ talk.prompt[$talk.q]= []
	}
	if ([$1 $3]==[Sending: to]) {
		if ([$4]==N) {
			^on -ctcp "% % DCC SEND $2 *" {
				//^dcc get $0 $4
				^on ctcp -"% % DCC SEND $4 *"
				* nTalk is autogetting $4 from $0
			}
			timer 120 ^on -ctcp -"% % DCC SEND $2 *"
		}{
			* Warning: $0 is sending $2 to the wrong nick \($4\)!
			* Use "cancel $2" to abort and use "get $2 $N" instead.
		}
	}
	if ([$1]!=[///]) {;xecho -window Talk$talk.q $1-}
}


# Called whenever you send something to the bot.

alias talk.send {
	@ talk.z= encode($toupper($0))
	@ talk.q= talk.session[$talk.z]
	if (([$(talk.echo[$talk.q])]==[ON])||(([$[1]1]==[.])&&([$1]!=[.me]))) {
		xecho -window Talk$talk.q $(talk.prompt[$talk.q])\> $1-
	}
}

# Tries to catch it when the bot hangs up on you

alias talk.lost {
	@ talk.z= encode($toupper($0))
	@ talk.q= talk.session[$talk.z]
        talk.done $0 $talk.z $talk.q
}

# Clean up whenever a connection closes

alias talk.done {
	^assign -talk.q
	^assign -talk.z
	^window refnum Talk$2 kill
	^on window -"% \*\*\* DCC CHAT % % $0 lost"
	^on dcc_lost -"$0 *"
	^on dcc_chat -"$0 *"
	^on send_dcc_chat -"$0 *"
	^assign -talk.session[$1]
	^assign -talk.cross[$2]
	^assign -talk.prompt[$2]
	^assign -talk.echo[$2]
	^assign -talk.vers[$2]
	* TALK to $0 ended.
}

### For those of us with old habits :)

# Hitting Control-\ will change windows
bind ^\ next_window

# Type /talk instead of /ntalk
alias talk ntalk

# So /me will work correctly in the nTalk window.
alias me if ([$[1]T]!=[=]) {//me $0-} {//msg $T .me $0-}

### All done!

* nTalk v4.2 loaded...
