aboutsummaryrefslogtreecommitdiffstats
path: root/run/jabber.py
blob: c9dd097bd79313cbcf5a8cd424d9f919808a5a68 (plain) (blame)
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
#!/usr/bin/env python
import os
import sys
import xmpp

class Jabber(object):
    def __init__(self, target):
        self.target = target
        self.client = self.__create_client()

    def __create_client(self):
        params = {}
        conf_file = os.environ['HOME'] + '/.xsend'

        if os.access(conf_file, os.R_OK):
            for line in open(conf_file, 'r').readlines():
                key, val = line.strip().split('=', 1)
                params[key.lower()] = val

        for field in ['login', 'password']:
            if field not in params.keys():
                with open(conf_file, 'wc') as f:
                    f.write('#LOGIN=romeo@montague.net\n#PASSWORD=juliet\n')
                raise IOError("Please ensure the ~/.xsend file has valid "+\
                              "credentials for sending messages.")

        jid = xmpp.protocol.JID(params['login'])
        client = xmpp.Client(jid.getDomain(), debug=[])

        client.connect(server=(jid.getDomain(), 5223))
        client.auth(jid.getNode(), params['password'])

        sys.stderr.write(("Jabber client loaded. Add '%s' to your friends " + \
                          "or Jabber messages will not send") % params['login'])

        return client

    def send(self, text):
        message = xmpp.protocol.Message(to=self.target, body=text, typ='chat')
        self.client.send(message)