-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathChatSelector.cpp
365 lines (291 loc) · 9.75 KB
/
ChatSelector.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( [email protected] / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( [email protected] / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// 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 2 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include <wx/tokenzr.h>
#include <wx/imaglist.h>
#include <wx/datetime.h>
#include "pixmaps/chat.ico.xpm"
#include "ChatSelector.h" // Interface declarations
#include "Preferences.h" // Needed for CPreferences
#include "amule.h" // Needed for theApp
#include "ClientRef.h" // Needed for CClientRef
#include "OtherFunctions.h"
#include "muuli_wdr.h" // Needed for amuleSpecial
#include "UserEvents.h"
#include "Constants.h" // Needed for MS_NONE
//#warning Needed while not ported
#include "ClientList.h"
#include <common/Format.h> // Needed for CFormat
// Default colors,
#define COLOR_BLACK wxTextAttr( wxColor( 0, 0, 0 ) )
#define COLOR_BLUE wxTextAttr( wxColor( 0, 0, 255 ) )
#define COLOR_GREEN wxTextAttr( wxColor( 0, 102, 0 ) )
#define COLOR_RED wxTextAttr( wxColor( 255, 0, 0 ) )
CChatSession::CChatSession(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name)
: CMuleTextCtrl( parent, id, value, pos, size, style | wxTE_READONLY | wxTE_RICH | wxTE_MULTILINE, validator, name )
{
m_client_id = 0;
m_active = false;
SetBackgroundColour(*wxWHITE);
}
CChatSession::~CChatSession()
{
//#warning EC NEEDED
#ifndef CLIENT_GUI
theApp->clientlist->SetChatState(m_client_id,MS_NONE);
#endif
}
void CChatSession::AddText(const wxString& text, const wxTextAttr& style, bool newline)
{
// Split multi-line messages into individual lines
wxStringTokenizer tokens( text, wxT("\n") );
while ( tokens.HasMoreTokens() ) {
// Check if we should add a time-stamp
if ( GetNumberOfLines() > 1 ) {
// Check if the last line ended with a newline
wxString line = GetLineText( GetNumberOfLines() - 1 );
if ( line.IsEmpty() ) {
SetDefaultStyle( COLOR_BLACK );
AppendText( wxT(" [") + wxDateTime::Now().FormatISOTime() + wxT("] ") );
}
}
SetDefaultStyle(style);
AppendText( tokens.GetNextToken() );
// Only add newlines after the last line if it is desired
if ( tokens.HasMoreTokens() || newline ) {
AppendText( wxT("\n") );
}
}
}
CChatSelector::CChatSelector(wxWindow* parent, wxWindowID id, const wxPoint& pos, wxSize siz, long style)
: CMuleNotebook(parent, id, pos, siz, style)
{
wxImageList* imagelist = new wxImageList(16,16);
// Chat icon -- default state
imagelist->Add(wxBitmap(chat_ico_xpm));
// Close icon -- on mouseover
imagelist->Add(amuleSpecial(4));
AssignImageList(imagelist);
}
CChatSession* CChatSelector::StartSession(uint64 client_id, const wxString& client_name, bool show)
{
// Check to see if we've already opened a session for this user
if ( GetPageByClientID( client_id ) ) {
if ( show ) {
SetSelection( GetTabByClientID( client_id ) );
}
return NULL;
}
CChatSession* chatsession = new CChatSession(this);
chatsession->m_client_id = client_id;
wxString text;
text = wxT(" *** ") + (CFormat(_("Chat-Session Started: %s (%s:%u) - %s %s"))
% client_name
% Uint32toStringIP(IP_FROM_GUI_ID(client_id))
% PORT_FROM_GUI_ID(client_id)
% wxDateTime::Now().FormatISODate()
% wxDateTime::Now().FormatISOTime());
chatsession->AddText( text, COLOR_RED );
AddPage(chatsession, client_name, show, 0);
CUserEvents::ProcessEvent(CUserEvents::NewChatSession, &client_name);
return chatsession;
}
CChatSession* CChatSelector::GetPageByClientID(uint64 client_id)
{
for ( unsigned int i = 0; i < (unsigned int ) GetPageCount(); i++ ) {
CChatSession* page = static_cast<CChatSession*>(GetPage(i));
if( page->m_client_id == client_id ) {
return page;
}
}
return NULL;
}
int CChatSelector::GetTabByClientID(uint64 client_id)
{
for ( unsigned int i = 0; i < (unsigned int) GetPageCount(); i++ ) {
CChatSession* page = static_cast<CChatSession*>(GetPage(i));
if( page->m_client_id == client_id ) {
return i;
}
}
return -1;
}
bool CChatSelector::ProcessMessage(uint64 sender_id, const wxString& message)
{
CChatSession* session = GetPageByClientID(sender_id);
// Try to get the name (core sent it?)
int separator = message.Find(wxT("|"));
wxString client_name;
wxString client_message;
if (separator != -1) {
client_name = message.Left(separator);
client_message = message.Mid(separator+1);
} else {
// No need to define client_name. If needed, will be build on tab creation.
client_message = message;
}
bool newtab = !session;
if ( !session ) {
// This must be a message from a client that is not already chatting
if (client_name.IsEmpty()) {
// Core did not send us the name.
// This must NOT happen.
// Build a client name based on the ID
uint32 ip = IP_FROM_GUI_ID(sender_id);
client_name = CFormat(wxT("IP: %s Port: %u")) % Uint32toStringIP(ip) % PORT_FROM_GUI_ID(sender_id);
}
session = StartSession( sender_id, client_name, true );
}
// Other client connected after disconnection or a new session
if ( !session->m_active ) {
session->m_active = true;
session->AddText( _("*** Connected to Client ***"), COLOR_RED );
}
// Page text is client name
session->AddText( GetPageText(GetTabByClientID(sender_id)), COLOR_BLUE, false );
session->AddText( wxT(": ") + client_message, COLOR_BLACK );
return newtab;
}
bool CChatSelector::SendMessage( const wxString& message, const wxString& client_name, uint64 to_id )
{
// Dont let the user send empty messages
// This is also a user-fix for people who mash the enter-key ...
if ( message.IsEmpty() ) {
return false;
}
if (to_id) {
// Checks if there's a page with this client, and selects it or creates it
StartSession(to_id, client_name, true);
}
int usedtab = GetSelection();
// Workaround for a problem with wxNotebook, where an invalid selection is returned
if (usedtab >= (int)GetPageCount()) {
usedtab = GetPageCount() - 1;
}
if (usedtab == -1) {
return false;
}
CChatSession* ci = static_cast<CChatSession*>(GetPage(usedtab));
ci->m_active = true;
//#warning EC needed here.
#ifndef CLIENT_GUI
if (theApp->clientlist->SendChatMessage(ci->m_client_id, message)) {
ci->AddText( thePrefs::GetUserNick(), COLOR_GREEN, false );
ci->AddText( wxT(": ") + message, COLOR_BLACK );
} else {
ci->AddText( _("*** Connecting to Client ***"), COLOR_RED );
}
#endif
return true;
}
//#warning Creteil? I know you are here Creteil... follow the white rabbit.
/* Madcat - knock knock ...
,-.,-.
\ \\ \
\ \\_\
/ \
__| a a|
/` `'. = y)=
/ `"`}
_| \ }
{ \ ), //
'-', /__\ ( (
jgs (______)\_)_)
*/
void CChatSelector::ConnectionResult(bool success, const wxString& message, uint64 id)
{
CChatSession* ci = GetPageByClientID(id);
if ( !ci ) {
return;
}
if ( !success ) {
ci->AddText( _("*** Failed to Connect to client / Connection lost ***"), COLOR_RED );
ci->m_active = false;
} else {
// Kry - Woops, fix for the everlasting void message sending.
if ( !message.IsEmpty() ) {
ci->AddText( _("*** Connected to Client ***"), COLOR_RED );
ci->AddText( thePrefs::GetUserNick(), COLOR_GREEN, false );
ci->AddText( wxT(": ") + message, COLOR_BLACK );
}
}
}
void CChatSelector::EndSession(uint64 client_id)
{
int usedtab;
if (client_id) {
usedtab = GetTabByClientID(client_id);
} else {
usedtab = GetSelection();
}
if (usedtab == -1) {
return;
}
DeletePage(usedtab);
}
// Refresh the tab associated with a client
void CChatSelector::RefreshFriend(uint64 toupdate_id, const wxString& new_name)
{
wxASSERT( toupdate_id );
int tab = GetTabByClientID(toupdate_id);
if (tab != -1) {
// This client has a tab.
SetPageText(tab,new_name);
} else {
// This client has no tab (friend disconnecting, etc)
// Nothing to be done here.
}
}
void CChatSelector::ShowCaptchaResult(uint64 id, bool ok)
{
CChatSession* ci = GetPageByClientID(id);
if (ci) {
ci->AddText(ok
? _("*** You have passed the captcha check and the user has received your message. ***")
: _("*** Your response to the captcha was wrong and your message has been ignored. You can request a new captcha by sending a new message. ***"),
COLOR_RED );
}
}
#ifdef CLIENT_GUI
bool CChatSelector::GetCurrentClient(CClientRef&) const
{
return false;
}
#else
bool CChatSelector::GetCurrentClient(CClientRef& clientref) const
{
// Get the chat session associated with the active tab
CChatSession* ci = static_cast<CChatSession*>(GetPage(GetSelection()));
// Get the client that the session is open to
if (ci) {
CUpDownClient * client = theApp->clientlist->FindClientByIP(IP_FROM_GUI_ID(ci->m_client_id), PORT_FROM_GUI_ID(ci->m_client_id));
if (client) {
clientref.Link(client CLIENT_DEBUGSTRING("CChatSelector::GetCurrentClient"));
return true;
}
}
return false;
}
#endif
// File_checked_for_headers