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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
// chattyDoc.cpp : implementation of the CChattyDoc class
//
#include "stdafx.h"
#include "chatty.h"
#include "chattyDoc.h"
#include "EnterDlg.h"
#include "SetTopicDlg.h"
#include "ChannelModeDlg.h"
#include "TalkDlg.h"
#include "SetPasswordDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChattyDoc
IMPLEMENT_DYNCREATE(CChattyDoc, CDocument)
BEGIN_MESSAGE_MAP(CChattyDoc, CDocument)
//{{AFX_MSG_MAP(CChattyDoc)
ON_COMMAND(ID_CHANNEL_SET_TOPIC, OnChannelSetTopic)
ON_COMMAND(ID_CHANNEL_MODE, OnChannelMode)
ON_COMMAND(ID_CHANNEL_TALK, OnChannelTalk)
ON_COMMAND(ID_CHANNEL_PASSWORD, OnChannelPassword)
ON_COMMAND(ID_CHANNEL_GETBANLIST, OnChannelGetbanlist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChattyDoc construction/destruction
CChattyDoc::CChattyDoc()
{
m_inChannel = FALSE;
m_hide = FALSE;
}
CChattyDoc::~CChattyDoc()
{
}
/**********************
** CHANNEL CALLBACKS **
**********************/
#if 0
CChattyDoc * pDoc; // Get rid of VA complaints.
#endif
#define DOC CChattyDoc * pDoc;\
pDoc = (CChattyDoc *)param;\
ASSERT(pDoc != NULL);\
if(!pDoc->m_inChannel) { OutputDebugString("Not in channel anymore!\n"); return; }
#define ADD(s) *str += " | ";\
*str += #s;\
*str += "--";\
*str += s;
char __i__[32];
#define ADD_INT(i) *str += " | ";\
*str += #i;\
*str += "--";\
itoa(i, __i__, 10);\
*str += __i__;
void ChannelMessage(CHAT chat, const char * channel, const char * user, const char * message, int type, void * param)
{
OutputDebugString("ChannelMessage called\n");
DOC;
CString * str;
str = new CString("ChannelMessage");
ADD(channel);
ADD(user);
ADD(message);
ADD_INT(type);
pDoc->m_addCallbacks.AddTail(str); // MORE DESCRIPTIVE (SHOW PARAMS)
str = new CString;
if(type == CHAT_MESSAGE)
{
*str = user;
*str += ": ";
*str += message;
}
else if(type == CHAT_ACTION)
{
*str += user;
*str += " ";
*str += message;
}
else
{
*str += user;
*str += "* ";
*str += message;
}
pDoc->m_newStuff.AddTail(str);
GSI_UNUSED(chat);
}
void Kicked(CHAT chat, const char * channel, const char * user, const char * reason, void * param)
{
OutputDebugString("Kicked called\n");
DOC;
// Not in the channel anymore.
//////////////////////////////
pDoc->m_inChannel = FALSE;
POSITION pos = pDoc->GetFirstViewPosition();
pDoc->GetNextView(pos)->GetParentFrame()->DestroyWindow();
GSI_UNUSED(reason);
GSI_UNUSED(user);
GSI_UNUSED(channel);
GSI_UNUSED(chat);
}
void UserJoined(CHAT chat, const char * channel, const char * user, int mode, void * param)
{
OutputDebugString("UserJoined called\n");
DOC;
CString * str;
if(!pDoc->m_hide)
{
str = new CString("UserJoined");
ADD(channel);
ADD(user);
ADD_INT(mode);
pDoc->m_addCallbacks.AddTail(str);
}
CModUsers * mod = new CModUsers;
mod->type = NEW;
mod->nick = user;
mod->mode = mode;
pDoc->m_modUsers.AddTail(mod);
GSI_UNUSED(chat);
}
void UserParted(CHAT chat, const char * channel, const char * user, int why, const char * reason, const char * kicker, void * param)
{
OutputDebugString("UserParted called\n");
DOC;
CString * str;
if(!pDoc->m_hide)
{
str = new CString("UserParted");
ADD(channel);
ADD(user);
ADD_INT(why);
ADD(reason);
ADD(kicker);
pDoc->m_addCallbacks.AddTail(str);
}
CModUsers * mod = new CModUsers;
mod->type = DEL;
mod->nick = user;
pDoc->m_modUsers.AddTail(mod);
GSI_UNUSED(chat);
}
void UserChangedNick(CHAT chat, const char * channel, const char * oldNick, const char * newNick, void * param)
{
OutputDebugString("UserChangedNick called\n");
DOC;
CString * str;
if(!pDoc->m_hide)
{
str = new CString("UserChangedNick");
ADD(channel);
ADD(oldNick);
ADD(newNick);
pDoc->m_addCallbacks.AddTail(str);
}
CModUsers * mod = new CModUsers;
mod->type = RENAME;
mod->nick = oldNick;
mod->newNick = newNick;
pDoc->m_modUsers.AddTail(mod);
GSI_UNUSED(chat);
}
void UserModeChanged(CHAT chat, const char * channel, const char * user, int mode, void * param)
{
OutputDebugString("UserModeChanged called\n");
DOC;
CString *str;
str = new CString("UserModeChanged");
ADD(channel);
ADD(user);
ADD_INT(mode);
pDoc->m_addCallbacks.AddTail(str);
CModUsers * mod = new CModUsers;
mod->type = MODE;
mod->nick = user;
mod->mode = mode;
pDoc->m_modUsers.AddTail(mod);
GSI_UNUSED(chat);
}
void TopicChanged(CHAT chat, const char * channel, const char * topic, void * param)
{
OutputDebugString("TopicChanged called\n");
DOC;
CString * str;
str = new CString("TopicChanged");
ADD(channel);
ADD(topic);
pDoc->m_addCallbacks.AddTail(str);
CString title = channel;
title += " - ";
title += topic;
pDoc->SetTitle(title);
pDoc->m_topic = topic;
GSI_UNUSED(chat);
}
void ChannelModeChanged(CHAT chat, const char * channel, CHATChannelMode * mode, void * param)
{
OutputDebugString("ChannelModeChanged called\n");
DOC;
CString * str;
str = new CString("ChannelModeChanged");
ADD(channel);
ADD_INT(mode->InviteOnly);
ADD_INT(mode->Limit);
ADD_INT(mode->Moderated);
ADD_INT(mode->NoExternalMessages);
ADD_INT(mode->OnlyOpsChangeTopic);
ADD_INT(mode->Private);
ADD_INT(mode->Secret);
pDoc->m_addCallbacks.AddTail(str);
str = new CString("Channel Mode Changed");
pDoc->m_newStuff.AddTail(str);
GSI_UNUSED(chat);
}
void UserListUpdated(CHAT chat, const char * channel, void * param)
{
OutputDebugString("UserListUpdated called\n");
DOC;
CString * str;
if(!pDoc->m_hide)
{
str = new CString("UserListUpdated");
ADD(channel);
pDoc->m_addCallbacks.AddTail(str);
}
GSI_UNUSED(chat);
}
void EnumUsersCallback(CHAT chat, CHATBool success, const char * channel, int numUsers, const char ** users, int * modes, void * param)
{
OutputDebugString("EnumUsersCallback called\n");
CChattyDoc * pDoc = (CChattyDoc *)param;
if(success)
{
pDoc->m_numUsers = numUsers;
int i;
for(i = 0 ; i < numUsers ; i++)
{
CModUsers * mod = new CModUsers;
ASSERT(mod != NULL);
mod->type = NEW;
mod->nick = users[i];
mod->mode = modes[i];
pDoc->m_modUsers.AddTail(mod);
}
}
GSI_UNUSED(chat);
GSI_UNUSED(channel);
}
void EnterChannelCallback(CHAT chat, CHATBool success, CHATEnterResult result, const char * channel, void * param)
{
OutputDebugString("EnterChannelCallback called\n");
CChattyDoc * pDoc = (CChattyDoc *)param;
// Check for success or failure.
////////////////////////////////
if(success)
{
pDoc->m_inChannel = TRUE;
// Get the inital list of users.
////////////////////////////////
DWORD before = GetTickCount();
chatEnumUsers(theApp.m_chat, channel, EnumUsersCallback, pDoc, CHATTrue);
DWORD after = GetTickCount();
char buffer[128];
sprintf(buffer, "%dms to enum %d users in %s\n", after - before, pDoc->m_numUsers, pDoc->m_channelName);
OutputDebugString(buffer);
}
GSI_UNUSED(chat);
GSI_UNUSED(result);
}
BOOL CChattyDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// Check for no connection.
///////////////////////////
if(theApp.m_chat == NULL)
return FALSE;
// Set the dialog defaults.
///////////////////////////
CEnterDlg dlg;
//dlg.m_channel = "#test";
//dlg.m_channel = "#montreal";
dlg.m_channel = "#istanbul";
dlg.m_password = "";
// Go modal.
////////////
int rcode = dlg.DoModal();
// Check for a cancel.
//////////////////////
if(rcode != IDOK)
return FALSE;
// Setup the callbacks.
///////////////////////
chatChannelCallbacks callbacks;
memset(&callbacks, 0, sizeof(chatChannelCallbacks));
callbacks.channelMessage = ChannelMessage;
callbacks.channelModeChanged = ChannelModeChanged;
callbacks.kicked = Kicked;
callbacks.topicChanged = TopicChanged;
callbacks.userParted = UserParted;
callbacks.userJoined = UserJoined;
callbacks.userListUpdated = UserListUpdated;
callbacks.userModeChanged = UserModeChanged;
callbacks.userChangedNick = UserChangedNick;
callbacks.param = this;
// Join the group.
//////////////////
if(dlg.m_quickChannel != "")
m_channelName = dlg.m_quickChannel;
else
m_channelName = dlg.m_channel;
chatEnterChannel(theApp.m_chat, m_channelName, dlg.m_password, &callbacks, EnterChannelCallback, this, CHATTrue);
// Check for failure.
/////////////////////
if(!m_inChannel)
return FALSE;
// Set the name.
////////////////
SetTitle(m_channelName);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CChattyDoc serialization
void CChattyDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CChattyDoc diagnostics
#ifdef _DEBUG
void CChattyDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CChattyDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CChattyDoc commands
void CChattyDoc::OnCloseDocument()
{
// Did we ever get in the channel?
//////////////////////////////////
if(m_inChannel)
{
if(theApp.m_chat != NULL)
{
// Leave the channel.
/////////////////////
chatLeaveChannel(theApp.m_chat, m_channelName, NULL);
OutputDebugString("LEFT channel\n");
}
m_inChannel = FALSE;
}
CDocument::OnCloseDocument();
}
void CChattyDoc::OnChannelSetTopic()
{
if(theApp.m_chat != NULL)
{
CSetTopicDlg dlg;
dlg.m_topic = m_topic;
if(dlg.DoModal() == IDOK)
{
chatSetChannelTopic(theApp.m_chat, m_channelName, dlg.m_topic);
}
}
}
void GetChannelModeCallback(CHAT chat, CHATBool success, const char * channel, CHATChannelMode * mode, void * param)
{
CHATChannelMode * modeParam = (CHATChannelMode *)param;
*modeParam = *mode;
GSI_UNUSED(chat);
GSI_UNUSED(channel);
GSI_UNUSED(success);
}
void CChattyDoc::OnChannelMode()
{
if(theApp.m_chat != NULL)
{
CHATChannelMode mode;
// Get the channel mode.
////////////////////////
chatGetChannelMode(theApp.m_chat, m_channelName, GetChannelModeCallback, &mode, CHATTrue);
// Setup the dlg.
/////////////////
CChannelModeDlg dlg;
dlg.m_inviteOnly = mode.InviteOnly;
dlg.m_private = mode.Private;
dlg.m_secret = mode.Secret;
dlg.m_moderated = mode.Moderated;
dlg.m_noExternalMessages = mode.NoExternalMessages;
dlg.m_onlyOpsChangeTopic = mode.OnlyOpsChangeTopic;
dlg.m_limit = mode.Limit;
// Show the dlg.
////////////////
if(dlg.DoModal() == IDOK)
{
// Copy back the new mode.
//////////////////////////
mode.InviteOnly = (CHATBool)dlg.m_inviteOnly;
mode.Private = (CHATBool)dlg.m_private;
mode.Secret = (CHATBool)dlg.m_secret;
mode.Moderated = (CHATBool)dlg.m_moderated;
mode.NoExternalMessages = (CHATBool)dlg.m_noExternalMessages;
mode.OnlyOpsChangeTopic = (CHATBool)dlg.m_onlyOpsChangeTopic;
mode.Limit = (CHATBool)dlg.m_limit;
// Set the mode.
////////////////
chatSetChannelMode(theApp.m_chat, m_channelName, &mode);
}
}
}
void CChattyDoc::OnChannelTalk()
{
if(theApp.m_chat != NULL)
{
CTalkDlg dlg;
dlg.m_type = 0;
if(dlg.DoModal() == IDOK)
{
int type;
if(dlg.m_type == 0)
type = CHAT_NORMAL;
else if(dlg.m_type == 1)
type = CHAT_ACTION;
else
type = CHAT_NOTICE;
chatSendChannelMessage(theApp.m_chat, m_channelName, dlg.m_message, type);
}
}
}
void GetChannelPasswordCallback(CHAT chat, CHATBool success, const char * channel, CHATBool enabled, const char * password, void * param)
{
CSetPasswordDlg * dlg = (CSetPasswordDlg *)param;
if(success)
{
dlg->m_password = password;
dlg->m_enable = enabled;
}
else
{
dlg->m_password = "";
dlg->m_enable = FALSE;
}
GSI_UNUSED(channel);
GSI_UNUSED(chat);
}
void CChattyDoc::OnChannelPassword()
{
if(theApp.m_chat != NULL)
{
CSetPasswordDlg dlg;
chatGetChannelPassword(theApp.m_chat, m_channelName, GetChannelPasswordCallback, &dlg, CHATTrue);
if(dlg.DoModal() == IDOK)
{
chatSetChannelPassword(theApp.m_chat, m_channelName, (CHATBool)dlg.m_enable, dlg.m_password);
}
}
}
void EnumChannelBansCallback(CHAT chat, CHATBool success, const char * channel, int numBans, const char ** bans, void * param)
{
DOC;
int i;
pDoc->m_newStuff.AddTail(new CString("Bans:"));
for(i = 0 ; i < numBans ; i++)
{
pDoc->m_newStuff.AddTail(new CString(bans[i]));
}
GSI_UNUSED(channel);
GSI_UNUSED(success);
GSI_UNUSED(chat);
}
void CChattyDoc::OnChannelGetbanlist()
{
if(theApp.m_chat != NULL)
{
chatEnumChannelBans(theApp.m_chat, m_channelName, EnumChannelBansCallback, this, CHATFalse);
}
}
|