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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/**
* Whiteboard session manager
* Chatroom message handler
*
* Authors:
* David Yip <yipdw@rose-hulman.edu>
*
* Copyright (c) 2005 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <glibmm/i18n.h>
//#include <boost/lexical_cast.hpp>
#include "message-stack.h"
#include "desktop-handles.h"
#include "document.h"
#include "util/ucompose.hpp"
#include "xml/node.h"
#include "jabber_whiteboard/typedefs.h"
#include "jabber_whiteboard/message-utilities.h"
#include "jabber_whiteboard/message-queue.h"
#include "jabber_whiteboard/jabber-handlers.h"
#include "jabber_whiteboard/defines.h"
#include "jabber_whiteboard/session-manager.h"
#include "jabber_whiteboard/node-tracker.h"
#include "jabber_whiteboard/chat-handler.h"
#include "jabber_whiteboard/error-codes.h"
namespace Inkscape {
namespace Whiteboard {
ChatMessageHandler::ChatMessageHandler(SessionManager* sm) : _sm(sm)
{
}
ChatMessageHandler::~ChatMessageHandler()
{
}
LmHandlerResult
ChatMessageHandler::parse(LmMessage* message)
{
// Retrieve the message type
LmMessageType mtype = lm_message_get_type(message);
// Retrieve root node of message
LmMessageNode* root = lm_message_get_node(message);
if (root == NULL) {
g_warning("Received a chat message with NULL root node; discarding.");
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
LmMessageSubType msubtype;
msubtype = lm_message_get_sub_type(message);
switch (mtype) {
case LM_MESSAGE_TYPE_MESSAGE:
switch(msubtype) {
case LM_MESSAGE_SUB_TYPE_ERROR:
{
LmMessageNode* error = lm_message_node_get_child(root, "error");
if (error != NULL) {
this->_handleError(lm_message_node_get_attribute(error, "code"));
}
break;
}
case LM_MESSAGE_SUB_TYPE_GROUPCHAT:
{
// FIXME: We should be checking to see if we're in a room in the presence stanzas as indicated in
// <http://www.jabber.org/jeps/jep-0045.html#enter-pres> but current versions of mu-conference
// don't broadcast presence in the correct order.
//
// Therefore we need to use some sort of hacked-up method to make this work. We listen for
// the sentinel value in a groupchat message -- currently it is '[username] INKBOARD-JOINED' --
// and begin processing that way.
LmMessageNode* body = lm_message_node_get_child(root, "body");
if (body != NULL) {
gchar const* val = lm_message_node_get_value(body);
if (strcmp(val, String::ucompose("%1 INKBOARD-JOINED", this->_sm->session_data->chat_handle).c_str()) == 0) {
return this->_finishConnection(); break;
} else {
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
break;
}
} else {
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
break;
}
}
default:
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
break;
}
break;
case LM_MESSAGE_TYPE_PRESENCE:
// Retrieve the subtype.
switch (msubtype) {
case LM_MESSAGE_SUB_TYPE_ERROR:
{
g_warning("Could not connect to chatroom");
// Extract error type
LmMessageNode* error = lm_message_node_get_child(root, "error");
if (error != NULL) {
this->_handleError(lm_message_node_get_attribute(error, "code"));
}
// Reset status bits
this->_sm->session_data->status.set(CONNECTING_TO_CHAT, 0);
this->_sm->session_data->recipient = "";
this->_sm->session_data->chat_handle = "";
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
break;
}
case LM_MESSAGE_SUB_TYPE_AVAILABLE:
{
// Extract the handle
// (see JEP-0045, section 6.3.3 - <http://www.jabber.org/jeps/jep-0045.html#enter-pres>)
Glib::ustring sender = lm_message_node_get_attribute(root, MESSAGE_FROM);
Glib::ustring chatter = sender.substr(sender.find_last_of('/') + 1, sender.length());
if (chatter != this->_sm->session_data->chat_handle) {
this->_sm->session_data->chatters.insert(g_strdup(chatter.data()));
// Make a receive queue for this chatter
this->_sm->session_data->receive_queues[sender.raw()] = new ReceiveMessageQueue(this->_sm);
} else {
// If the presence message is from ourselves, then we know that we
// have successfully entered the chatroom _and_ have received the entire room roster,
// and can therefore decide whether we need to synchronize with the rest of the room.
// (see JEP-0045, section 6.3.3 - <http://www.jabber.org/jeps/jep-0045.html#enter-pres>)
}
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}
case LM_MESSAGE_SUB_TYPE_UNAVAILABLE:
{
Glib::ustring sender = lm_message_node_get_attribute(root, MESSAGE_FROM);
Glib::ustring chatter = sender.substr(sender.find_last_of('/') + 1, sender.length());
this->_sm->session_data->chatters.erase(chatter.data());
// Delete the message queue used by this sender
this->_sm->session_data->receive_queues.erase(sender.raw());
SP_DT_MSGSTACK(this->_sm->desktop())->flashF(Inkscape::INFORMATION_MESSAGE, _("<b>%s</b> has left the chatroom."), sender.c_str());
}
default:
// no idea what this message is; discard it
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
break;
}
break;
default:
break;
}
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
LmHandlerResult
ChatMessageHandler::_finishConnection()
{
if (this->_sm->session_data->status[CONNECTING_TO_CHAT]) {
this->_sm->session_data->status.set(CONNECTING_TO_CHAT, 0);
if (this->_sm->session_data->chatters.empty()) {
// We are the only one in the chatroom, so there is no
// need for synchronization
this->_sm->session_data->status.set(IN_CHATROOM, 1);
// Populate node tracker
KeyToNodeMap newids;
NodeToKeyMap newnodes;
NewChildObjectMessageList newchildren;
XML::Node* root = this->_sm->document()->rroot;
this->_sm->setupInkscapeInterface();
this->_sm->setupCommitListener();
for ( Inkscape::XML::Node *child = root->firstChild() ; child != NULL ; child = child->next() ) {
MessageUtilities::newObjectMessage(NULL, newids, newnodes, newchildren, this->_sm->node_tracker(), child, true);
}
this->_sm->node_tracker()->put(newids, newnodes);
// this->_sm->node_tracker()->dump();
} else {
this->_sm->session_data->status.set(WAITING_TO_SYNC_TO_CHAT, 1);
// Send synchronization request to chatroom
this->_sm->sendMessage(CHATROOM_SYNCHRONIZE_REQUEST, 0, "", this->_sm->session_data->recipient, true);
}
}
return LM_HANDLER_RESULT_REMOVE_MESSAGE;
}
void
ChatMessageHandler::_handleError(char const* errcode)
{
// try {
unsigned int code = atoi(errcode);
// unsigned int code = boost::lexical_cast< unsigned int >(errcode);
Glib::ustring buf;
switch (code) {
case ErrorCodes::CHAT_HANDLE_IN_USE:
buf = String::ucompose(_("Nickname %1 is already in use. Please choose a different nickname."), this->_sm->session_data->chat_handle);
this->_sm->connectionError(buf);
break;
case ErrorCodes::SERVER_CONNECT_FAILED:
buf = _("An error was encountered while attempting to connect to the server.");
this->_sm->connectionError(buf);
break;
default:
break;
}
// } catch (boost::bad_lexical_cast&) {
// }
}
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
|