chat.py 2.28 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3
# Copyright(C) 2010-2011 Christophe Benz
4
#
5
# This file is part of weboob.
6
#
7
# weboob is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9 10 11 12
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
19 20


21 22
import datetime

23
from .base import Capability, BaseObject, StringField, UserError
24
from .date import DateField
25 26


27
__all__ = ['ChatException', 'ChatMessage', 'CapChat']
28 29


30
class ChatException(UserError):
31 32 33
    """
    Exception raised when there is a problem with the chat.
    """
34 35


36
class ChatMessage(BaseObject):
37 38 39 40 41 42 43 44
    """
    Message on the chat.
    """
    id_from =       StringField('ID of sender')
    id_to =         StringField('ID of recipient')
    message =       StringField('Content of message')
    date =          DateField('Date when the message has been sent')

45
    def __init__(self, id_from, id_to, message, date=None, url=None):
46
        super(ChatMessage, self).__init__('%s.%s' % (id_from, id_to), url)
47 48 49 50
        self.id_from = id_from
        self.id_to = id_to
        self.message = message
        self.date = date
51 52 53

        if self.date is None:
            self.date = datetime.datetime.utcnow()
54

55

56
class CapChat(Capability):
57 58 59
    """
    Websites with a chat system.
    """
60

61
    def iter_chat_messages(self, _id=None):
62 63 64 65 66 67 68 69
        """
        Iter messages.

        :param _id: optional parameter to only get messages
                    from a given contact.
        :type _id: str
        :rtype: iter[:class:`ChatMessage`]
        """
70 71 72
        raise NotImplementedError()

    def send_chat_message(self, _id, message):
73 74 75 76 77 78 79 80 81
        """
        Send a message to a contact.

        :param _id: ID of recipient
        :type _id: str
        :param message: message to send
        :type message: str
        :raises: :class:`ChatException`
        """
82
        raise NotImplementedError()