summaryrefslogtreecommitdiff
path: root/modules/cons.py
blob: 61569f6e86c7d543031b701d45edcfd301ffaf05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# vi: encoding=utf-8 ts=8 sts=4 sw=4 et

import socket
import os
import select
from common import fileexists
import ftpio
import cmds

class Connection:
    def __init__(self, sock, authorized):
        sock.setblocking(False)
        self.sock=sock
        self.authorized=authorized
        self.fileno=sock.fileno
        self.name=""
        self.data=""

    def destroy(self):
        self.sock.close()
        rm(self)

    def handleinput(self):
        newdata = None
        try:
            newdata = self.sock.recv(8192)
        except:
            self.destroy()

        if not newdata:
            self.destroy()
        else:
            self.data = self.data + newdata

        try:
            cmds.parse(self)
        except cmds.BailOut:
            self.destroy()

def add(con):
    cons.append(con)
   
def rm(con):
    cons.remove(con)

def readables():
    lst=cons[:]
    lst.append(privlistener)
    lst.append(publistener)
    inlst,outlst,errlst = select.select(lst, [], [], 0.1)
    return inlst

def createlistener(path):
    if fileexists(path):
        os.remove(path)

    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.setblocking(False)
    s.bind(path)
    if path==ftpio.pubsock:
        os.chmod(path, 0606)
    else:
        os.chmod(path, 0600)
    s.listen(3)
    return s

cons=[]

privlistener=createlistener(ftpio.privsock)
publistener=createlistener(ftpio.pubsock)