Ejemplo n.º 1
0
    def auth(self, botID=None, login=None, password=None):
        """
        Fetches current authentication adapter.
        If credentials are given - adapter is instantinated.

        int     botID
        str     login
        str     password
        return  Authentication
        """
        if botID is not None:
            self._auth = Authentication(botID, login, password, self)
        if self._auth is None:
            raise AuthenticationNotSetError('Authentication not set')
        if not self._auth.is_valid():
            raise BotAuthenticationError('Could not authenticate')
        return self._auth
Ejemplo n.º 2
0
class Client(object):

    tokenDiscoveryUrl = 'https://botapi.gadu-gadu.pl/botmaster/getToken/%u'
    #tokenDiscoveryUrl = 'http://localhost:8866/%u'
    pushUrl = 'http://%s:%u/%s/%u'

    def __init__(self, adapter=None, authentication=None):
        """
        Prepares object instance

        CommunicationAdapter    adapter
        Authentication          authentication
        """
        self._adapter = adapter
        if authentication is not None:
            authentication.botmaster = self
        self._auth = authentication

    def adapter(self, adapter=None):
        """
        Fetches current communication adapter.
        If adapter is given - replaces current instance.

        CommunicationAdapter     adapter
        return  CommunicationAdapter
        """
        if adapter is not None:
            self._adapter = adapter
        if self._adapter is None:
            self._adapter = UrllibAdapter()
        if self._adapter is None:
            raise AdapterNotSetError('Communication Adapter not set')
        return self._adapter

    def auth(self, botID=None, login=None, password=None):
        """
        Fetches current authentication adapter.
        If credentials are given - adapter is instantinated.

        int     botID
        str     login
        str     password
        return  Authentication
        """
        if botID is not None:
            self._auth = Authentication(botID, login, password, self)
        if self._auth is None:
            raise AuthenticationNotSetError('Authentication not set')
        if not self._auth.is_valid():
            raise BotAuthenticationError('Could not authenticate')
        return self._auth

    def authorize(self, botID, login, password):
        """
        Issues autorization request

        int     botID
        str     login
        str     password
        return  str
        """
        # prepare basic authorization header
        auth = base64.b64encode('%s:%s' % (login, password))
        return self.call(self.tokenDiscoveryUrl % botID,\
            {'Authorization': 'Basic %s' % auth})

    def get_push_url(self, action):
        credentials = self.auth().credentials
        return self.pushUrl % (credentials.server, credentials.port, \
            action, credentials.botID)

    def send(self, message, recipients, sendToOffline=True):
        """
        Sends message

        str     message
        list    recipients
        bool    sendToOffline
        return  str
        """
        credentials = self.auth().credentials
        url = self.get_push_url('sendMessage')
        data = {\
            'to': ','.join(map(str, recipients)),\
            'token': credentials.token,\
            'msg': message}
        headers = {\
            'Content-Type': 'application/x-www-form-urlencoded'}
        headers['Send-to-offline'] = int(sendToOffline)
        return self.call(url, headers, data)

    def set_status(self, status, desc):
        """
        Sets bot status

        int     status  status number
        str     desc    status description
        return  str
        """
        credentials = self.auth().credentials
        url = self.get_push_url('setStatus')
        if len(desc) > 255:
            raise TooLongStatusError('Status description could not be ' \
                + 'longer than 255 chars')
        data = {\
            'token': credentials.token,\
            'status': int(status),\
            'desc': desc}
        return self.call(url, {}, data)

    def call(self, url, headers, data=None):
        """
        Sends request to botmaster

        str     url
        dict    headers
        dict    data        data to send (using POST request)
        return  str
        """
        return self.adapter().call(url, headers, data)