#
# $Source: /home/nlfm/Working/Zircon/Released/plugins/Dcc/RCS/chat.tcl,v $
# $Date: 1997/01/23 22:21:51 $
# $Revision: 1.18.1.3 $
#
package provide Dcc 1.18
#
proc closeChat {cht who conn} {
    catch {fileevent $conn readable {} ; close $conn}
    catch {uplevel #0 unset $conn}
    switch {} [info proc $cht] {} default {
	$cht addText $who "*** $who has closed the connection"
    }
}
#
proc dccChat {mode conn} {
    upvar #0 $conn chdata
    set who [$chdata(who) name]
    set cht $chdata(obj)
    switch $mode {
    r   {
	    if {[catch {gets $conn} buffer] || 
	      ([string match {} $buffer] && [eof $conn])} {
		closeChat $cht $who $conn
	    } {
		regsub -all "\r" $buffer {} buffer
		if [regexp "(\[^\001\]*)\001\[ \t\]*(\[^ \t\001\]+)\[ \t\]*(\[^\001\]*)\001(\[^\001\]*)" $buffer sub a op cp b] {
		    set value [handleCTCP [$cht net] $op $cht Chat [$cht caller] {} {} $cp]
		    switch {} $value return
		    set buffer $a$value$b
		}
		$cht addText $who "=$who= $buffer"
	    }
	}
    e   {  [$chData(who) net] errmsg "Error on DCC Chat connection with $who" }
    }
}
#
proc user_unChServ {this} {
    upvar #0 AChat[$this net] AChat
    if [info exist AChat($this)] {
	catch {uplevel #0 unset $AChat($this)}
	catch {close $AChat($this)}
	unset AChat($this)
	$this deref
    }
}
#
proc user_unChat {this} {
    $this unChServ
    switch nil [set id [Chat :: find [$this name] [$this net]]] {} default {$id delete}
}
