aboutsummaryrefslogtreecommitdiffstats
path: root/server.py
blob: c561193dac5cd1a65193cb5c17c6ec98be34fdf3 (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
from sleekxmpp.componentxmpp import ComponentXMPP
from argparse import ArgumentParser
from user import UserHandler
from sleekxmpp.xmlstream.xmlstream import XMLStream

class ObjectComponent(ComponentXMPP):

    def __init__(self, jid, secret, server, port, root):
        ComponentXMPP.__init__(self, jid, secret, server, port)
        self.add_event_handler("session_start", self.start)
        self.add_event_handler("presence_probe", self.presenceProbe)
        self.add_event_handler("message", self.message)
        self.add_event_handler("changed_subscription", self.presenceSubscription)
        self.userHandler = UserHandler(root)

    def start(self, event):
        for user in self.userHandler.getUserList() :
            self.sendPresence(pto = user)
            
    def disconnect(self, reconnect=False):
        for user in self.userHandler.getUserList() :
            self.sendPresence(pto = user, ptype = "unavailable")
        XMLStream.disconnect(self, reconnect)

    def message(self, msg):
        msg.reply("Thanks for sending\n%(body)s" % msg).send()
        
    def presenceSubscription(self, subscription) :
        if subscription["type"] == "subscribe" :
            userJID = subscription["from"].full
            self.userHandler.register(userJID)
            self.sendPresenceSubscription(pto=userJID, ptype="subscribed")
            self.sendPresence(pto = userJID)
            self.sendPresenceSubscription(pto=userJID, ptype="subscribe")
        if subscription["type"] == "unsubscribe" :
            userJID = subscription["from"].full
            self.userHandler.unregister(userJID)
        
    def presenceProbe(self, event):
        self.sendPresence(pto = event["from"].full)

        
if __name__ == '__main__' :
    commandline = ArgumentParser(description='Connect the alias component to a given server')
    commandline.add_argument('-p', '--port', 
                             help='Port to connect to', 
                             type=int)
    commandline.add_argument('-s', '--secret', 
                         help='password')
    commandline.add_argument('-n', '--name', 
                     help='Name the component will have')
    commandline.add_argument('-r', '--root', 
                     help='Root directory of the user files')
    commandline.add_argument('host', 
                         help='Host to connect to')
    args = commandline.parse_args()
    
    component = ObjectComponent(args.name, args.secret, args.host, args.port, args.root)

    if component.connect() :
        print 'Component', args.name, 'connected to', args.host + ':' + str(args.port)
        component.process(False)
    else :
        print "Couldn't connect"