Exemplo n.º 1
0
def create_connection(addr, port):
    sock = socket(AF_INET, SOCK_STREAM)
    spin = Spin(sock)
    Client(spin)
    xmap(spin, CONNECT, is_open, port)
    spin.connect_ex((addr, port))
    return spin
Exemplo n.º 2
0
    def handle_connect(server):
        Stdin(server)
        Stdout(server)
        Shrug(server)

        Irc(server)
        CTCP(server)

        Misc(server)
        adm.install(server)

        xmap(server, 'PING', lambda server, prefix, servaddr: 
                send_cmd(server, 'PONG :%s' % servaddr))
        
        xmap(server, CLOSE, lambda server, err: lose(server))
        logcon(server)

        xmap(server, '376', auto_join)
        xmap(server, '376', get_myaddr)
        xmap(server, 'NICK', update_nick)
        plugmap(server)

        server.servaddr    = servaddr
        server.port        = port
        server.nick        = nick
        server.user        = user
        server.chan_list   = chan_list
        server.nick_passwd = nick_passwd
        server.adm_passwd  = adm_passwd
        server.plugmap     = plugmap

        send_cmd(server, 'NICK %s' % nick)
        send_cmd(server, 'USER %s' % user) 
Exemplo n.º 3
0
def set_up_con(con, data):
    Stdin(con)
    Stdout(con)
    
    HttpClient(con)
    xmap(con, CLOSE, lambda con, err: lose(con))
    con.dump(data)
Exemplo n.º 4
0
 def __init__(self, server):
     fd   = open(join(dirname(__file__), 'quote_database'), 'r')
     data = fd.read()
     fd.close()
     self.data = split('\n+', data)
     
     xmap(server, 'CMSG', self.send_quote)
Exemplo n.º 5
0
 def dump(self, data):
     # If the queue is empty we map it to WRITE
     # otherwise it is already mapped.
     if not self.queue:
        xmap(self.device, WRITE, self.update)
     # Addes it to be sent.
     self.queue.append(data)
Exemplo n.º 6
0
def create_connection(addr, port, msg):
    sock = socket(AF_INET, SOCK_STREAM)
    con  = Spin(sock)

    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, setup, msg)
Exemplo n.º 7
0
    def __init__(self, area, addr, port):
        con = Spin()
        con.connect_ex((addr, int(port)))
        Client(con)

        xmap(con, CONNECT, lambda con: self.set_up_con(con, area))
        xmap(con, CONNECT_ERR, self.on_connect_err)
Exemplo n.º 8
0
    def dump(self, data):
        """
        It sends data asynchronously through the device.
        """

        self.queue.append(data)
        xmap(self.device, WRITE, self.update)
Exemplo n.º 9
0
def scan(addr, min, max):
    task = Task()
    for ind in range(min, max):
        task.add(create_connection(addr, ind), CONNECT, CONNECT_ERR)

    task.start()    
    xmap(task, DONE, lambda task: die())
Exemplo n.º 10
0
    def handle_accept(self, server, client):
        irc = Spin()
        Client(irc)
        xmap(irc, CONNECT, self.handle_connect, client)
        xmap(irc, CONNECT_ERR, self.down_connection)

        irc.connect_ex((self.irc_address, self.irc_port))
Exemplo n.º 11
0
    def __init__(self, spin):
        self.request = None
        xmap(spin, TransferHandle.DONE, self.process)

        # It will not be spawned if it is a websocket connection.
        xmap(spin, TmpFile.DONE,  
                   lambda spin, fd, data: spawn(spin, 
                                 RequestHandle.DONE, self.request))
Exemplo n.º 12
0
    def __init__(self, spin):
        """
        It installs self.update on WRITE. A call a select with this socket
        in the writting list of sockets would succed if the socket is connected.

        """

        xmap(spin, WRITE, self.update)
Exemplo n.º 13
0
    def dump(self, data):
        # If the queue is empty we maps it WRITE
        # otherwise it is already mapped.
        if not self.queue:
           xmap(self.spin, WRITE, self.update)

        # Addes it to be sent.
        self.queue.append(buffer(data))
Exemplo n.º 14
0
    def __init__(self, spin):
        """
        It maps READ to self.update so whenever
        there is data available we can read from the socket.
        """

        # Maps READ to self.update.
        xmap(spin, READ, self.update)
Exemplo n.º 15
0
    def set_up_con(self, con, area):
        Stdin(con)
        Stdout(con)
        Shrug(con)
        Irc(con)

        xmap(con, CLOSE, lambda con, err: lose(con))
        self.set_common_irc_handles(area, con)
        self.set_common_irc_commands(area, con)
Exemplo n.º 16
0
def set_up_con(con, data):
    Stdin(con)
    Stdout(con)
    
    HttpClient(con)
    xmap(con, HTTP_RESPONSE, handle_http_response)
    xmap(con, CLOSE, lambda con, err: lose(con))

    con.dump(data)
Exemplo n.º 17
0
def set_up_con(con):
# We install Stdin to be able of sending data.
    Stdin(con)
# When everything is sent it just disconnects.
    xmap(con, DUMPED, lose)

# Stdin installs dump method in con. 
# So, we can use it to send data.
    con.dump('Hello world\r\n')
Exemplo n.º 18
0
    def __init__(self, spin, fd):
        self.fd    = fd
        self.data  = ''
        
        self.BLOCK = 1024 * 124

        # It maps WRITE to self.update.
        # Whenever WRITE occurs self.update runs.

        # Install dump method into spin.
        xmap(spin, WRITE, self.update)
Exemplo n.º 19
0
    def __init__(self, server, db=['Ameliabot rocks'], excpt=['#freenode']):
        """

        """

        self.pmed = list()
        xmap(server, 'CMSG', self.send_spam)

        # The list of chans whose users shouldnt be spammed.
        self.db    = db
        self.excpt = excpt
Exemplo n.º 20
0
    def __init__(self, server_port, backlog, irc_address, irc_port):
        self.irc_address = irc_address
        self.irc_port    = irc_port

        server = Spin()
        server.bind(('', server_port))
        server.listen(int(backlog))
    
        Server(server)
    
        xmap(server, ACCEPT, self.handle_accept)
Exemplo n.º 21
0
    def record(self, con, header, fd, data, version):
        REQUEST  = '/appdata/%s/%s' % (data['appkey'][0], data['collection'][0])
        kcon     = kpost(REQUEST, data['username'][0], data['password'][0], data['json'][0])

        def kresponse(kcon, version, code, reason, header, data):
            response = Response()
            response.set_response('HTTP/1.1 200 OK')
            response.add_data(data)
            send_response(con, response)
    
        xmap(kcon, HTTP_RESPONSE, kresponse)
        con.ACTIVE = True
Exemplo n.º 22
0
    def __init__(self, spin):

        """

        """

        xmap(spin, LOAD, self.get_header)
        self.spin     = spin
        self.header   = ''
        self.data     = ''
        self.response = ''
        self.size     = 0
Exemplo n.º 23
0
    def __init__(self, server, questions, suggestions, pmed_file='pmed', 
                                            blacklist_file='blacklist', timeout=60 * 3):

        self.questions      = self.load(questions)
        self.suggestions    = self.load(suggestions)
        self.timeout        = timeout
        self.pmed           = self.load(pmed_file)
        self.pmed_file      = pmed_file
        self.blacklist      = self.load(blacklist_file)
        self.blacklist_file = blacklist_file
        xmap(server, 'JOIN', self.send_question)
        xmap(server, 'PMSG', self.check_answer)
Exemplo n.º 24
0
    def __init__(self, server, folder):
        """
        The folder is where to save the logs.
        Example:
        LogMsg(server, '/home/tau')

        Notice it shouldn't be '/home/tau/'.
        """

        self.folder = folder
        xmap(server, 'CMSG', self.store_chan)
        xmap(server, 'PMSG', self.store_user)
Exemplo n.º 25
0
    def __init__(self, server, n_lines, n_secs, cmd):
        """
        The n_lines means the number of lines per n_secs.

        Example for command:
            cmd = 'kick {chan} {nick} :Amelia rocks!'
            cmd = 'chanserv akick {chan} add *!*@{host} : get out!
        """
        xmap(server, 'CMSG', self.watcher)
        self.data    = {}
        self.n_lines = n_lines
        self.n_secs  = n_secs
        self.cmd     = cmd
Exemplo n.º 26
0
    def check_data_existence(self):
        """

        """

        try:
            self.size = int(self.header['Content-Length'])
        except KeyError:
            pass

        # Assume there is a body size larger than 0.
        # Since we will call check_data_size anyway.
        xmap(self.spin, LOAD, self.get_data)
        self.check_data_size()
Exemplo n.º 27
0
    def handle_accept(self, local, spin):
        Stdin(spin)
        Stdout(spin)
        AccUntil(spin)
        TransferHandle(spin)
        RequestHandle(spin)
        MethodHandle(spin)

        # must be improved.
        Locate(spin)

        # InvalidRequest(client)

        xmap(spin, CLOSE, lambda con, err: lose(con))
Exemplo n.º 28
0
    def __init__(self, spin):

        """

        """

        xmap(spin, LOAD, self.get_header)
        xmap(spin, CLOSE,  self.spawn_response)

        self.spin     = spin
        self.header   = ''
        self.data     = ''
        self.response = ''
        self.size     = None
Exemplo n.º 29
0
    def get_header(self, spin, data):
        """

        """

        DELIM       = '\r\n\r\n'
        self.header = self.header + data

        if not DELIM in data:
            return

        self.header, self.data     = self.header.split(DELIM, 1)
        self.response, self.header = self.split_header(self.header)
        zmap(spin, LOAD, self.get_header)
        xmap(spin, LOAD, self.get_data)
Exemplo n.º 30
0
def kpost(rsc, username, password, payload):
    ADDR   = 'baas.kinvey.com'
    PORT   = 80
    HEADER = {
    'Host':'%s' % ADDR,
    'Authorization': auth(username, password)
    }
        
    con  = Spin()
    data = post_data(rsc, data=json.loads(payload), header=HEADER)
    
    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    return con
Exemplo n.º 31
0
    def __init__(self, irc, area, chan):
        self.irc   = irc
        self.area  = area
        self.chan  = chan
        self.peers = set()

        events = (('PRIVMSG->%s' % chan , self.e_privmsg), 
        ('332->%s' % chan, self.e_332), 
        ('PART->%s' % chan, self.e_part), 
        ('JOIN->%s' % chan, self.e_join), 
        # ('*NICK', self.e_nick),
        ('NICK', self.e_nick),
        ('QUIT', self.e_quit),
        ('353->%s' % chan, self.e_353), 
        ('KICK->%s' % chan, self.e_kick), 
        ('MODE->%s' % chan, self.e_mode),
        (CLOSE, self.e_close))

        def unset(con, *args):
            for key, value in events:
                zmap(irc.con, key, value)

        for key, value in events:
            xmap(irc.con, key, value)

        once(irc.con, '*PART->%s' % chan, unset)
        xmap(irc.con, '*KICK->%s' % chan, unset)

        area.bind('<Destroy>', lambda event: 
        unset(irc.con), add=True)

        # When area is destroyed, it sends a PART.
        area.bind('<Destroy>', lambda event: 
        send_cmd(irc.con, 'PART %s' % chan), add=True)
    
        # Hook to send msgs.
        area.hook('vyirc', 'IRC', '<Key-i>', lambda event: Get(
        events={'<Escape>': lambda wid: True, 
        '<Return>': lambda wid: 
        self.irc.drop_msg(area, wid, chan)}))

        # It unbinds the above callback.
        # In case the part command as sent by text
        # by the user. After part it should destroy the
        # area.
        once(irc.con, '*PART->%s' % chan, lambda con, *args: 
        area.unbind('<Destroy>'))
Exemplo n.º 32
0
def create_connection(addr, port):
    con = Spin()
    Client(con)
    xmap(con, CONNECT, on_connect, addr, port)
    xmap(con, CONNECT_ERR, on_connect_err, addr, port)
    xmap(con, CONNECT, lambda con: die())
    xmap(con, CONNECT_ERR, lambda con, err: die())
    
    con.connect_ex((addr, port))
Exemplo n.º 33
0
def connect(addr, port, rsc):
    HEADER = {
        'Host': '%s' % addr,
        'User-Agent': "uxreq/1.0",
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
        'Connection': 'close',
    }

    con = Spin()
    data = get(rsc, header=HEADER)

    Client(con)
    con.connect_ex((addr, port))
    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
Exemplo n.º 34
0
def connect(rsc, username, password, payload):
    ADDR   = 'baas.kinvey.com'
    PORT   = 80
    HEADER = {
    'Host':'%s' % ADDR,
    'Authorization': auth(username, password)
    }
        
    con  = Spin()
    data = post_data(rsc, data=json.loads(payload), header=HEADER)
    
    Client(con)
    con.connect_ex((ADDR, PORT))

    xmap(con, CONNECT, set_up_con, data)
    xmap(con, CONNECT_ERR, lambda con, err: lose(con))

    return con
Exemplo n.º 35
0
    def locate(self, spin, request):
        path = join(spin.app.app_dir, spin.app.static_dir,
                    basename(request.path))
        if not isfile(path):
            return

        # Where we are going to serve files.
        # I might spawn an event like FILE_NOT_FOUND.
        # So, users could use it to send appropriate answers.
        type_file, encoding = guess_type(path)
        default_type = 'application/octet-stream'

        spin.add_header(
            ('Content-Type', type_file if type_file else default_type),
            ('Content-Length', getsize(path)))

        spin.send_headers()
        xmap(spin, OPEN_FILE_ERR, lambda con, err: lose(con))
        drop(spin, path)
Exemplo n.º 36
0
    def __init__(self):
        # I have to improve this code.
        # i have to handle the exception
        # if the port is in use etc. 
     
        Spin.__init__(self)
        server = socket(AF_INET, SOCK_STREAM)
        server.bind(('127.0.0.1', 0))
        server.listen(1)
        self.connect_ex(server.getsockname())

        sock, addr = server.accept()
        self.sock  = sock
        server.close()

        xmap(self, READ, self.dispatch)
        
        self.lock  = Lock()
        self.ident = None
        self.data  = None
Exemplo n.º 37
0
    def install_events(self):
        """

        """

        # When one of the AreaVi instances are destroyed then
        # the process is killed.
        self.output.hook('shell', -1, '<Destroy>',
                         lambda event: self.terminate_process())
        self.area.hook('shell', -1, '<Destroy>',
                       lambda event: self.terminate_process())

        # self.output.hook('shell', 'NORMAL', '<Control-F2>', lambda event:
        # self.map_process_input())

        self.map_process_input()
        # When call.terminnate is called it may happen of having still data to be
        # processed. It would attempt to write on an AreaVi instance that no more exist.
        # So, it executes quietly the AreaVi.append method.
        xmap(self.expect, LOAD,
             lambda expect, data: exec_quiet(self.output.append, data))

        xmap(self.expect, CLOSE, self.handle_close)
Exemplo n.º 38
0
    def __init__(self,
                 addr,
                 port,
                 user,
                 nick,
                 irccmd,
                 channels=[],
                 encoding='utf8'):
        con = Spin()
        self.con = con
        con.connect_ex((addr, int(port)))
        Client(con)

        xmap(con, CONNECT, self.on_connect)
        xmap(con, CONNECT_ERR, self.e_connect_err)
        self.misc = None
        self.addr = addr
        self.port = port
        self.user = user
        self.nick = nick
        self.irccmd = irccmd
        self.channels = channels
        self.encoding = encoding
Exemplo n.º 39
0
class Get(object):
    def __init__(self, server, folder):
        self.folder = folder
        xmap(server, 'DCC SEND', self.dcc_get)

    def dcc_get(self, server, (nick, user, host, 
                        target, msg), filename, address, port, size):
    
        path = join(self.folder, filename)

        if isfile(path):      
            send_msg(server, nick, 'File already exists.')
        else:
            fd = open(path, 'wb')
            dccclient = DccClient(long_to_ip(int(address)), 
                                        int(port), fd, int(size)) 
    
            def is_done(dcclient, msg):
                send_msg(server, nick, msg)
                fd.close()
    
            xmap(dccclient, DONE, is_done, 'Done.')
            xmap(dccclient, CLOSE, lambda dccclient, spin, err: is_done(spin, 'Failed.'))
            xmap(dccclient, CONNECT_ERR, lambda dccclient, spin, err: is_done("It couldn't connect."))
Exemplo n.º 40
0
def main(addr, port, host):
    sock = socket(AF_INET, SOCK_STREAM)
    context = ssl.create_default_context()
    con = SSL(
        context.wrap_socket(sock,
                            do_handshake_on_connect=False,
                            server_hostname=host))
    con.connect_ex((addr, port))

    ClientSSL(con)
    xmap(con, SSL_CONNECT, on_connect, host)
    xmap(con, SSL_CONNECT_ERR, lambda con, err: die(err))
    xmap(con, SSL_CERTIFICATE_ERR, lambda con, err: die(err))
Exemplo n.º 41
0
    def __init__(self, spin):
        self.timer = Timer(PersistentConnection.TIMEOUT, lambda: lose(spin))
        self.count = 0
        xmap(spin, TmpFile.DONE, self.process)
        xmap(spin, DUMPED, self.install_timeout)

        xmap(spin, TransferHandle.DONE,
             lambda spin, request, data: self.timer.cancel())

        spin.add_header(('connection', 'keep-alive'))
        spin.add_header(
            ('keep-alive', 'timeout=%s, max=%s' %
             (PersistentConnection.TIMEOUT, PersistentConnection.MAX)))
Exemplo n.º 42
0
    def start(self):
        self.child = Popen(self.cmd,
                           shell=0,
                           stdout=PIPE,
                           stdin=PIPE,
                           preexec_fn=setsid,
                           stderr=STDOUT,
                           env=environ)

        self.stdout = Device(self.child.stdout)
        self.stdin = Device(self.child.stdin)

        Stdout(self.stdout)
        Stdin(self.stdin)

        xmap(self.stdout, LOAD, lambda con, data: sys.stdout.write(data))
        xmap(self.stdin, CLOSE, lambda dev, err: lose(dev))
        xmap(self.stdout, CLOSE, lambda dev, err: lose(dev))
Exemplo n.º 43
0
    def start(self):
        print('Ibash - Bash process started...')
        self.child = Popen(self.cmd,
                           shell=0,
                           stdout=PIPE,
                           stdin=PIPE,
                           preexec_fn=setsid,
                           stderr=STDOUT,
                           env=environ)

        self.stdout = Device(self.child.stdout)
        self.stdin = Device(self.child.stdin)

        Stdout(self.stdout)
        Stdin(self.stdin)

        xmap(self.stdout, LOAD,
             lambda con, data: sys.stdout.write(data.decode('utf8')))
        xmap(self.stdin, CLOSE, lambda dev, err: lose(dev))
        xmap(self.stdout, CLOSE, lambda dev, err: lose(dev))
Exemplo n.º 44
0
    def __init__(self, server):
        self.database = dict()

        xmap(server, 'CMSG', self.record)
        xmap(server, 'CMSG', self.check)
Exemplo n.º 45
0
def install(server):
    xmap(server, 'JOIN', send_welcome)
Exemplo n.º 46
0
 def __init__(self, spin):
     xmap(spin, HTTP_RESPONSE, self.spawn_method)
Exemplo n.º 47
0
# Name: foo.py
# Description: This example connects to a local socket it sends
# a chunk of text and disconnects.

from untwisted.network import xmap, Spin, core
from untwisted.utils.stdio import Client, Stdin, CONNECT, DUMPED, lose
from socket import *


def set_up_con(con):
    # We install Stdin to be able of sending data.
    Stdin(con)
    # When everything is sent it just disconnects.
    xmap(con, DUMPED, lose)

    # Stdin installs dump method in con.
    # So, we can use it to send data.
    con.dump('Hello world\r\n')


sock = socket(AF_INET, SOCK_STREAM)
con = Spin(sock)
Client(con)
con.connect_ex(('localhost', 1234))
xmap(con, CONNECT, set_up_con)

core.gear.mainloop()
Exemplo n.º 48
0
 def __init__(self, spin):
     xmap(spin, FOUND, self.split)
Exemplo n.º 49
0
 def __init__(self, server, folder):
     self.folder = folder
     xmap(server, 'DCC SEND', self.dcc_get)
Exemplo n.º 50
0
 def __init__(self, server):
     self.title = Title()
     xmap(server, 'CMSG', self.check)
Exemplo n.º 51
0
    def __init__(self, server, folder):
        """
        """

        self.folder = folder
        xmap(server, 'CMSG', self.list_folder)
Exemplo n.º 52
0
	def __init__(self,server):
		xmap(server,'CMSG',self.getinfo)
Exemplo n.º 53
0
 def __init__(self, spin, regstr, event, encoding='utf8'):
     self.encoding = encoding
     self.regstr = regstr
     self.event = event
     xmap(spin, Terminator.FOUND, self.handle_found)
Exemplo n.º 54
0
def install(server):
    xmap(server, 'PMSG', login)
Exemplo n.º 55
0
 def __init__(self, spin):
     xmap(spin, 'which is your nick?', self.get_nick)
Exemplo n.º 56
0
 def __init__(self, spin):
     xmap(spin, LOAD, self.update)
     spin.accumulator = self
     self.data = ''
Exemplo n.º 57
0
    def on_connect(self, con):
        area = self.create_area(self.addr)
        area.bind('<Destroy>',
                  lambda event: send_cmd(con, 'QUIT :vy rules!'),
                  add=True)

        Stdin(con)
        Stdout(con)
        Terminator(con)
        Irc(con)
        self.misc = Misc(con)

        xmap(con, CLOSE, lambda con, err: lose(con))
        xmap(con, '*JOIN', self.create_channel)

        xmap(
            con, Terminator.FOUND,
            lambda con, data: area.append('%s\n' % data.decode(self.encoding)))

        xmap(con, 'PMSG', self.e_pmsg)
        xmap(con, '376', lambda con, *args: send_cmd(con, self.irccmd))

        xmap(con, '376', self.auto_join)

        xmap(
            con, 'PING',
            lambda con, prefix, servaddr: send_cmd(con, 'PONG :%s' % servaddr))

        send_cmd(con, 'NICK %s' % self.nick)
        send_cmd(con, 'USER %s' % self.user)
Exemplo n.º 58
0
def install(server):
    xmap(server, CLOSE, reconnect)
Exemplo n.º 59
0
def install(server):
    xmap(server, 'CMSG', send_spam)
Exemplo n.º 60
0
from untwisted.expect import Expect, LOAD, CLOSE
from untwisted.network import core, xmap, die

def handle(expect, data):
    print data

expect = Expect('python', '-i', '-u')
xmap(expect, LOAD, handle)
xmap(expect, CLOSE, lambda expect: die())
expect.send('print "hello world"\nquit()\n')
core.gear.mainloop()