~ubuntu-branches/ubuntu/hardy/pymsn/hardy-proposed

« back to all changes in this revision

Viewing changes to pymsn/msnp/message.py

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville, Sjoerd Simons, Laurent Bigonville, Jonny Lamb
  • Date: 2008-01-17 18:23:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080117182314-lwymmpnk2ut3rvr1
Tags: 0.3.1-0ubuntu1
[ Sjoerd Simons ]
* debian/rules: remove dh_python, it's no longer needed

[ Laurent Bigonville ]
* New upstream release (0.3.1)
* debian/control:
  - Add myself as an Uploaders
  - Add python:Provides for binary package
  - Add python-ctypes and python-crypto to build-deps/deps
* debian/rules: remove binary-install rule
* Add watch file
* remove pycompat file, not needed anymore
* Modify Maintainer value to match the DebianMaintainerField
  specification.

[ Jonny Lamb ]
* Added python-adns to build-deps/deps.
* Added python-pyopenssl to build-deps/deps.
* Updated copyright.
* Upped Standards-Version to 3.7.3.
* Added "XS-Dm-Upload-Allowed: yes" under the request of Sjoerd Simons.
* Added myself to Uploaders.
* Added Homepage to control.
* Added Vcs-Bzr to control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# pymsn - a python client library for Msn
 
4
#
 
5
# Copyright (C) 2005-2006 Ali Sabil <ali.sabil@gmail.com>
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation; either version 2 of the License, or
 
10
# (at your option) any later version.
 
11
#
 
12
# This program 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 General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 
 
21
"""MSN protocol special command : MSG"""
 
22
 
 
23
from pymsn.gnet.message.HTTP import HTTPMessage
 
24
import pymsn.util.debug as debug
 
25
 
 
26
from urllib import quote, unquote
 
27
import struct
 
28
 
 
29
__all__ = ['MessageAcknowledgement', 'Message']
 
30
 
 
31
 
 
32
class MessageAcknowledgement(object):
 
33
    """Message Acknowledgement"""
 
34
    FULL = 'A'
 
35
    """Acknowledgement required for both delivery success and failure"""
 
36
    MSNC = 'D'
 
37
    """Direct connection, no acknowledgment required from the server"""
 
38
    HALF = 'N'
 
39
    """Acknowledgment on delivery failures"""
 
40
    NONE = 'U'
 
41
    """No Acknowledgment"""
 
42
 
 
43
class Message(HTTPMessage):
 
44
    """Base Messages class.
 
45
    
 
46
        @ivar sender: sender
 
47
        @type sender: profile.Contact
 
48
        
 
49
        @ivar body: message body
 
50
        @type body: string
 
51
        
 
52
        @ivar headers: message headers
 
53
        @type headers: {header_name: string => header_value:string}
 
54
        
 
55
        @ivar content_type: the message content type
 
56
        @type content_type: tuple(mime_type, encoding)"""
 
57
 
 
58
    def __init__(self, sender=None, message=""):
 
59
        """Initializer
 
60
            
 
61
            @param body: The body of the message, it is put after the headers
 
62
            @type body: string"""
 
63
        HTTPMessage.__init__(self)
 
64
        self.sender = sender
 
65
        if message:
 
66
            self.parse(message)
 
67
 
 
68
    def __repr__(self):
 
69
        """Represents the payload of the message"""
 
70
        message = ''
 
71
        for header_name, header_value in self.headers.iteritems():
 
72
            message += '\t%s: %s\\r\\n\n' % (header_name, header_value)
 
73
        message += '\t\\r\\n\n'
 
74
        if self.headers['Content-Type'] != "application/x-msnmsgrp2p":
 
75
            message += '\t' + debug.escape_string(self.body).\
 
76
                    replace("\r\n", "\\r\\n\n\t")
 
77
        else:
 
78
            tlp_header = self.body[:48]
 
79
            tlp_footer = self.body[-4:]
 
80
            tlp_flags = struct.unpack("<L", self.body[28:32])[0]
 
81
            body = self.body[48:-4]
 
82
 
 
83
            message += "\t" + debug.hexify_string(tlp_header).replace("\r\n", "\n\t")
 
84
 
 
85
            if tlp_flags == 0:
 
86
                message += "\n\t" + debug.escape_string(body).\
 
87
                        replace("\r\n", "\\r\\n\n\t")
 
88
            elif len(body) > 0:
 
89
                message += "\n\t" + "[%d bytes of data]" % len(body)
 
90
            message += "\n\t" + debug.hexify_string(tlp_footer)
 
91
 
 
92
        return message.rstrip("\n\t")
 
93
 
 
94
    def __get_content_type(self):
 
95
        if 'Content-Type' in self.headers:
 
96
            content_type = self.headers['Content-Type'].split(';', 1)
 
97
            if len(content_type) == 1:
 
98
                return (content_type[0].strip(), 'UTF-8')
 
99
            mime_type = content_type[0].strip()
 
100
            encoding = content_type[1].split('=', 1)[1].strip()
 
101
            return (mime_type, encoding)
 
102
        return ('text/plain', 'UTF-8')
 
103
    
 
104
    def __set_content_type(self, content_type):
 
105
        if not isinstance(content_type, str):
 
106
            content_type = '; charset='.join(content_type)
 
107
        self.headers['Content-Type'] = content_type
 
108
 
 
109
    content_type = property(__get_content_type, __set_content_type,
 
110
            doc="a tuple specifying the content type")
 
111