InstantMessaging.h 5.86 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
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
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

32
33
34
35
36
#ifndef _INSTANT_MESSAGING_H
#define _INSTANT_MESSAGING_H

#include <string>
#include <iostream>
37
#include <fstream>
38
39
40
41
42
43
#include <pjsip.h>
#include <pjlib.h>
#include <pjsip_ua.h>
#include <pjlib-util.h>

#include "call.h"
44
#include "sip/sipcall.h"
45

46
#include <map>
47
#include <list>
48
#include <stdexcept>
49

50
51
#include <iax-client.h>

52
#define EMPTY_MESSAGE   pj_str((char*)"")
53
#define MAXIMUM_MESSAGE_LENGTH		1560			/* PJSIP's sip message limit */
54

55
56
57
#define MODE_APPEND			std::ios::out || std::ios::app
#define MODE_TEST			std::ios::out

58
59
60
namespace sfl
{

61
const std::string IM_XML_URI ("uri");
62
const std::string BOUNDARY ("--boundary");
63

64
class InstantMessageException : public std::runtime_error
65
66
{
    public:
67
68
        InstantMessageException (const std::string& str="") :
            std::runtime_error("InstantMessageException occured: " + str) {}
69
70
};

71
72
class InstantMessaging
{
73

74
    public:
75

76
        typedef std::map <std::string, std::string> UriEntry;
77
        typedef std::list <UriEntry> UriList;
78

79
80
81
82
83
84
85
86
87
88
        /*
         * Class constructor
         */
        InstantMessaging();

        /*
         * Class destructor
         */
        ~InstantMessaging();

89
90
91
92
        /**
         * Return the maximum number if character for a single SIP MESSAGE.
         * Longer messages should be splitted in several smaller messages using split_message
         */
Rafaël Carré's avatar
Rafaël Carré committed
93
94
        size_t getMessageMaximumSize (void) {
            return MAXIMUM_MESSAGE_LENGTH;
95
96
        }

97
98
99
100
101
102
103
104
        /*
         * Write the text message to the right file
         * The call ID is associated to a file descriptor, so it is easy then to retrieve the right file
         *
         * @param message	The text message
         * @param id	The current call
         * @return True if the message could have been successfully saved, False otherwise
         */
105
        bool saveMessage (const std::string& message, const std::string& author, const std::string& id, int mode = MODE_APPEND);
106
107
108
109
110
111
112

        /*
         * Send a SIP string message inside a call
         *
         * @param id	The call ID we will retrieve the invite session from
         * @param message	The string message, as sent by the client
         */
Rafaël Carré's avatar
Rafaël Carré committed
113
        void sip_send (pjsip_inv_session*, const std::string& id, const std::string&);
114

Rafaël Carré's avatar
Rafaël Carré committed
115
116
        void send_sip_message (pjsip_inv_session*, const std::string& id, const std::string&);
        void send_iax_message (iax_session *session, const std::string& id, const std::string&);
117

Rafaël Carré's avatar
Rafaël Carré committed
118
        std::vector<std::string> split_message(std::string);
119

120
121
122
123

        /**
         * Generate Xml participant list for multi recipient based on RFC Draft 5365
         *
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        * @param A UriList of UriEntry
        *
        * @return A string containing the full XML formated information to be included in the
        *         sip instant message.
        */
        std::string generateXmlUriList (UriList& list);

        /**
         * Parse the Urilist from a SIP Instant Message provided by a UriList service.
         *
         * @param A XML formated string as obtained from a SIP instant message.
         *
         * @return An UriList of UriEntry containing parsed XML information as a map.
         */
        UriList parseXmlUriList (std::string& urilist);
139

140
141
142
143
144
145
146
147
148
149
        /**
         * Format text message according to RFC 5365, append recipient-list to the message
         *
         * @param text to be displayed
         * @param list containing the recipients
         *
         * @return formated text stored into a string to be included in sip MESSAGE
         */
        std::string appendUriList (std::string text, UriList& list);

150
151
152
153
154
155
156
        /**
             * Retreive the xml formated uri list in formated text data according to RFC 5365
             *
         * @param text The formated text message as retreived in the SIP message
         *
         * @return A string containing the XML content
         */
157
158
        std::string findTextUriList (std::string& text);

159
160
161
162
        /**
             * Retrive the plain text message in formated text data according to RFC 5365
             *
         * @param text The formated text message as retreived in the SIP message
163
         *
164
165
         * @return A string containing the actual message
         */
166
        std::string findTextMessage (std::string& text);
167

168
169
170
171
172
    private:

        InstantMessaging (const InstantMessaging&); //No Copy Constructor
        InstantMessaging& operator= (const InstantMessaging&); //No Assignment Operator
};
173
174
}
#endif // _INSTANT_MESSAGING_H