Code: Select all
bind ctcp - DCC mydcc
proc mydcc {n u h t a} {
if {![isbotnick $t]} {return 0}
switch -- [scan $a %s] {
"SEND" {
# determine if you want to allow it and resume or create a new file
# then ignore/reply(+remember)/connect based on that
}
"ACCEPT" {
# if expected, connect
}
default {
# allow further processing of other keywords (to allow ACTION, CHAT etc.)
return 0
}
}
return 1;# this will halt further processing of SEND and ACCEPT requests
}
Code: Select all
set sock [socket -async $ipFromCtcp $portFromCtcp]
fconfigure $sock -blocking 0 -translation binary
fileevent $sock writable [list sockConnected $sock $fileName etc...]
proc sockConnected {sock filename etc...} {
fileevent $sock writable {}
if {![eof $sock]&&[fconfigure $sock -error]==""} {
# connected... open the file and start fcopy (that's a tcl command - look it up in the manual)
} else {
# complain
}
}
Code: Select all
proc sockConnected {sock file} {
global total done
fileevent $sock writable {}
if {![eof $sock]&&[fconfigure $sock -error]==""} {
set out [open /home/bot/upltmp/[unixtime]-$file w]
set in $sock
set chunk 2048
set total 0
fcopy $in $out -command [list CopyMore $in $out $chunk] -size $chunk
vwait done
} else {
# complain
}
}
proc CopyMore {in out chunk bytes {error {}}} {
global total done
incr total $bytes
if {([string length $error] != 0) || [eof $in]} {
putlog "file saved. total $total"
set done $total
close $in
close $out
} else {
fcopy $in $out -command [list CopyMore $in $out $chunk] \
-size $chunk
}
}
Code: Select all
# notes
# socket, file and size are set elsewhere
proc sockConnected {sock file} {
global total done size chunk
fileevent $sock writable {}
if {![eof $sock]&&[fconfigure $sock -error]==""} {
# connected... open the file and start fcopy
# FCOPY
set out [open /tmp/[clock-seconds]-$file w]
set in $sock
putlog "in: $in | out: $out"
set chunk [expr $size / 2]
set total 0
putlog "chunk is $chunk, size is $size, starting fcopy"
fcopy $in $out -command [list CopyMore $in $out] -size $chunk
vwait done
putlog "done?"
} else {
# complain
}
}
proc CopyMore {in out bytes {error {}}} {
set timestamp [clock format [clock seconds]]
set fl [open CopyMore {WRONLY CREAT APPEND}]
puts $fl "$timestamp: CopyMore started (chunk: $chunk)"
close $fl
global total done size chunk
putlog "CopyMore chunk is $chunk and bytes is $bytes"
incr total $bytes
if {([string length $error] != 0)} {
putlog "no error"
if {[eof $in]} {
putlog "in ($in) EOF"
}
if {$total >= $size} {
putlog "file saved. total is $total and size is $size"
set done $total
close $in
close $out
}
} else {
putlog "total is now $total (of $size) bytes - chunk is $chunk"
fcopy $in $out -command [list CopyMore $in $out $chunk] -size $chunk
}
}
proc bgerror {message} {
set timestamp [clock format [clock seconds]]
set fl [open bgerror.txt {WRONLY CREAT APPEND}]
puts $fl "$timestamp: bgerror in $::argv '$message'"
close $fl
}