Source

irc / dccsend

#! /usr/bin/env python
#
# Example program using irclib.py.
#
# This program is free without restrictions; do anything you like with
# it.
#
# Joel Rosdahl <joel@rosdahl.net>

import irclib
import os
import string
import struct
import sys

class DCCSend(irclib.SimpleIRCClient):
    def __init__(self, receiver, filename):
        irclib.SimpleIRCClient.__init__(self)
        self.receiver = receiver
        self.filename = filename
        self.filesize = os.path.getsize(self.filename)
        self.file = open(filename)
        self.sent_bytes = 0

    def on_welcome(self, connection, event):
        self.dcc = self.dcc_listen("raw")
        self.connection.ctcp("DCC", self.receiver, "SEND %s %s %d %d" % (
            os.path.basename(self.filename),
            irclib.ip_quad_to_numstr(self.dcc.localaddress),
            self.dcc.localport,
            self.filesize))

    def on_dcc_connect(self, connection, event):
        if self.filesize == 0:
            self.dcc.disconnect()
            return
        self.send_chunk()

    def on_dcc_disconnect(self, connection, event):
        print "Sent file %s (%d bytes)." % (self.filename, self.filesize)
        self.connection.quit()

    def on_dccmsg(self, connection, event):
        acked = struct.unpack("!I", event.arguments()[0])[0]
        if acked == self.filesize:
            self.dcc.disconnect()
            self.connection.quit()
        elif acked == self.sent_bytes:
            self.send_chunk()

    def on_disconnect(self, connection, event):
        sys.exit(0)

    def on_nosuchnick(self, connection, event):
        print "No such nickname:", event.arguments()[0]
        self.connection.quit()

    def send_chunk(self):
        data = self.file.read(1024)
        self.dcc.privmsg(data)
        self.sent_bytes = self.sent_bytes + len(data)

def main():
    if len(sys.argv) != 5:
        print "Usage: dccsend <server[:port]> <nickname> <receiver nickname> <filename>"
        print "\nSends <filename> to <receiver nickname> via DCC and then exits."
        sys.exit(1)

    s = string.split(sys.argv[1], ":", 1)
    server = s[0]
    if len(s) == 2:
        try:
            port = int(s[1])
        except ValueError:
            print "Error: Erroneous port."
            sys.exit(1)
    else:
        port = 6667
    nickname = sys.argv[2]
    receiver = sys.argv[3]
    filename = sys.argv[4]

    c = DCCSend(receiver, filename)
    try:
        c.connect(server, port, nickname)
    except irclib.ServerConnectionError, x:
        print x
        sys.exit(1)
    c.start()

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.