Skip to content

Instantly share code, notes, and snippets.

@dominikh

dominikh/dcc.rb Secret

Created January 22, 2013 19:03
Show Gist options
  • Save dominikh/78eb46b79a2892c6bc62 to your computer and use it in GitHub Desktop.
Save dominikh/78eb46b79a2892c6bc62 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'cinch'
require 'tempfile'
require 'filesize'
module Cinch
module Plugins
class XDCC
include Cinch::Plugin
class Download
attr_reader :start_time
attr_reader :written
def initialize(file)
@file = file
@written = 0
@start_time = Time.now
end
def close
@file.close
end
def <<(s)
@written += s.bytesize
@file << s
end
end
listen_to :dcc_send, method: :incoming_dcc
def incoming_dcc(m, dcc)
# FIXME check if the file already exists, prompt user for
# input, possibly make use of RESUME
File.open("/tmp/#{dcc.filename}", "w") do |f|
dl = Download.new(f)
timer = Timer(5) do
filename = dcc.filename
written = Filesize.new(dl.written).pretty
total = Filesize.new(dcc.size).pretty
rate = Filesize.new((dl.written) / (Time.now - dl.start_time)).pretty
info "DCC: %s - %s of %s (%s/s)" % [
filename,
written,
total,
rate
]
end
dcc.accept(dl)
dl.close
timer.stop
info "DCC: %s - finished" % dcc.filename
@done[m.user.nick] << true
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment