Source

oberchatten / ServerCallbacks.cpp

Full commit
  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
/*
Copyright (C) 2001-2006 Daybo Logic.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "overchat_config.h"
#include <assert.h>
#include <limits.h>
#include <time.h>
#include <vcl.h>
#include <stdlib.h>
#include <stdio.h>
#include <deque>
#include <string>
#ifdef HDRSTOP
# pragma hdrstop
#endif

#include "master.h"
#include "NetBuffer.h"
#include "ServerCallbacks.h"
#include "ShadowUnit.h"
#include "History.h"
#include "MsgUnit.h"
#include "ChatFormUnit.h" // TChatForm
#include "StatusBarManagerUnit.h" // TStatusBarManager
#include "StatsManagerUnit.h" // TStatsManager
#include "MyUserUnit.h" // TMyUser
#include "MainFormUnit.h"
#include "SignonUnit.h"
#include "InfoBoxUnit.h"
#include "Globals.h"
#include "Options.h"

#pragma package(smart_init)
//---------------------------------------------------------------------------
#ifdef OVERCHAT_RCS_STAMPS
static const char *const rcsid =
  "$Id: ServerCallbacks.cpp,v 0.8 2006/03/05 14:08:00 ddrp Exp $";
#endif /*OVERCHAT_RCS_STAMPS*/

using namespace Oberchatten;
//---------------------------------------------------------------------------
static void RawBlakeOutgoingCallback(
  int Socket,
  const void *RawData,
  size_t RawDataSize
);
static void BrokenPipeCallback(
  int SockDesc
);
static void LoggingCallback(
  const char *Message
);
static void DataReader(
  int S
);
void ClientSidePacketCompiler(
  unsigned int Bytes
);
static blake_bool_t GetChallenge(
  int s,
  const void *pkt,
  const void *Nothing
);
static blake_bool_t GetLoginReply(
  int s,
  const void *pkt,
  const void *Nothing
);
static blake_bool_t GetMsg(
  int s,
  const void *pkt,
  const void *Message
);
static blake_bool_t GetMsg2(
  int s,
  const void *pkt,
  const void *Command
);
static blake_bool_t GetWhoisInfo(
  int s,
  const void *pkt,
  const void *Nothing
);
static blake_bool_t GetVersioned(
  int s,
  const void *pkt,
  const void *Nothing
);
static blake_bool_t GetIsOnlineReply(
  int s,
  const void *pkt,
  const void *Nothing
);
static blake_bool_t GetSuper(
  int s,
  const void *pkt,
  const void *Nothing
);
static blake_bool_t GetError(
  int s,
  const void *pkt,
  const void *Nothing
);
//---------------------------------------------------------------------------
void InstallServerCallbacks()
{
  #ifdef OVERCHAT_RCS_STAMPS
    blake_Unused(rcsid);
  #endif /*OVERCHAT_RCS_STAMPS*/

  network_InstallOutgoingCallback(RawBlakeOutgoingCallback);
  network_InstallBrokenPipeCallback(BrokenPipeCallback);
  blake_InstallLogger(LoggingCallback);
  network_InstallIncomingCallback(-1, DataReader);
  blake_InstallCallback(
    true, /* It's a server code */
    (WORD)bscchallenge, /* Listen for challenge packet */
    GetChallenge /* Callback for the challenge */
  );
  blake_InstallCallback(true, (WORD)bscloginreply, GetLoginReply);
  /*
    This is the odd one out, IsServer==false because bccmessage is shared
    between both directions
  */
  blake_InstallCallback(false, (WORD)bccmessage, GetMsg);
  /*
    This is the second odd one, see above reason
  */
  blake_InstallCallback(false, (WORD)bccmessage2, GetMsg2);
  blake_InstallCallback(true, (WORD)bscwhois_info, GetWhoisInfo);
  blake_InstallCallback(true, (WORD)bscversioned, GetVersioned);
  blake_InstallCallback(true, (WORD)bscisonlinereply, GetIsOnlineReply);
  blake_InstallCallback(true, (WORD)bscsuper, GetSuper);
  blake_InstallCallback(true, (WORD)bscgoterror, GetError);
}
//---------------------------------------------------------------------------
static void RawBlakeOutgoingCallback(int Socket, const void* RawData, size_t RawDataSize)
{
  enum blakeNetBufferError err;

  assert(RawData); /* Blake must have sent this */
  assert(RawDataSize); /* and this */

  err = GlobalNetBuffer.Push(bnbdOutgoing, RawData, RawDataSize);
  if ( err == bnbeNoError )
    StatsManager.IncrementBytes(bnbdOutgoing, RawDataSize); // Update stats
  else
    MainForm->BlakeNetBufferError(__FILE__, __LINE__, err);

  return;
}
//---------------------------------------------------------------------------
static void BrokenPipeCallback(int SockDesc)
{
  MainForm->MyUser.AbruptClosure(); // Record that this was a dirty closure
  MainForm->MnuOberchattenSignoffClick(NULL);
  InfoBox(
    MainForm,
    "You have been disconnected from the service",
    "Disconnected",
    MB_OK | MB_ICONSTOP,
    INFOBOX_NOHELP
  );
}
//---------------------------------------------------------------------------
static void LoggingCallback(const char* Message)
{
  if ( Message ) {
    char* msgcopy = new char[strlen(Message)+1];
    if ( msgcopy ) {
      strcpy(msgcopy, Message);
      InfoBox(MainForm, msgcopy, "Message passed to Blake log", MB_OK | MB_ICONINFORMATION, INFOBOX_NOHELP);
      delete[] msgcopy;
    }
  }
}
//---------------------------------------------------------------------------
void DataWriter()
{
  int s = network_ReturnWritableSocket();
  if ( s != -1 ) {
    unsigned char oneByte;
    unsigned int numberDone;
    enum blakeNetBufferError err;

    do {
      numberDone = GlobalNetBuffer.Pop(bnbdOutgoing, &oneByte, sizeof(oneByte), &err);
      if ( !numberDone ) break;
      assert(err == bnbeNoError);
    } while ( network_Write(s, &oneByte, sizeof(oneByte)) );
  }
}
//---------------------------------------------------------------------------
static void DataReader(int S)
{
  unsigned int rAmount;
  char data[OVERCHAT_NET_PKTMAXSZ * OVERCHAT_NET_TXSZ];

  if ( S != GlobalNetBuffer.GetSocket() ) return; /* A bug in Blake (0.0.14) means sometimes -1 (any) type ata reader callbacks get sockets which don't even exist */

  rAmount = network_Read(GlobalNetBuffer.GetSocket(), data, sizeof(data));
  if ( rAmount == 0 ) { /* We were told there was data!  Connection must have been closed by the client */
    BrokenPipeCallback(GlobalNetBuffer.GetSocket());
  }
  else if ( rAmount != -1 ) { /* If -1, broken pipe will have already been notified */
    enum blakeNetBufferError err = GlobalNetBuffer.Push(bnbdIncoming, data, rAmount);
    StatsManager.IncrementBytes(bnbdIncoming, rAmount); // Update stats
    assert(err == bnbeNoError);
    if ( err == bnbeNoError ) {
      OVERCHAT_EVENT ev;
      InitOverChatEvent(&ev);
      ev.Code = eventDataArrived;
      ev.eventData.DataArrived.Socket = S;
      ev.eventData.DataArrived.Amount = rAmount;
      PostOverChatEvent(&ev);
    }
  }

  return;
}
//---------------------------------------------------------------------------
void DataArrivedCollector(OVERCHAT_EVENT* PEv)
{
  assert(PEv->Code == eventDataArrived); // We only want to know about data arrived events
  assert(PEv->eventData.DataArrived.Socket == GlobalNetBuffer.GetSocket()); /* Ensure Blake is sane, the client only supports one connection! */

  GlobalNetBuffer.PacketCompiler(PEv->eventData.DataArrived.Amount); /* Pass to NetBuffer's packet compiler */
}
//---------------------------------------------------------------------------
void MessageCollector(OVERCHAT_EVENT* PEv)
{
  assert(PEv->Code == eventMsg); // Paranoid check it's the right event

  MainForm->NewMsg_Event(PEv);
  delete[] PEv->eventData.Msg.DynamicMessage;
}
//---------------------------------------------------------------------------
void Message2Collector(OVERCHAT_EVENT* PEv)
{
  assert(PEv->Code == eventMsg2);

  MainForm->ProcessMessage2(PEv->eventData.Msg2.DynamicCommand);
  delete[] PEv->eventData.Msg2.DynamicCommand;
}
//---------------------------------------------------------------------------
void WhoisInfoCollector(OVERCHAT_EVENT* PEv)
{
  assert(PEv->Code == eventWhoisInfo);

  MainForm->WhoisInfo(&PEv->eventData.WhoisInfo.wii);
}
//---------------------------------------------------------------------------
void VersionedCollector(OVERCHAT_EVENT* PEv)
{
  assert(PEv->Code == eventVersioned);

  MainForm->Versioned(&PEv->eventData.Versioned.vd);
}
//---------------------------------------------------------------------------
void IsOnlineReplyCollector(OVERCHAT_EVENT* PEv)
{
  assert(PEv->Code == eventIsOnlineReply);

  MainForm->IsOnlineReply(&PEv->eventData.IsOnlineReply.ior);
}
//---------------------------------------------------------------------------
void SuperCollector(OVERCHAT_EVENT* PEv)
{
  assert(PEv->Code == eventSuper);

  MainForm->Super(&PEv->eventData.Super.spr);
}
//---------------------------------------------------------------------------
void ErrorCollector(OVERCHAT_EVENT *PEv)
{
  assert(PEv->Code == eventGotError);

  MainForm->GotError(&PEv->eventData.GotError.ge);
}
//---------------------------------------------------------------------------
static blake_bool_t GetChallenge(int s, const void* pkt, const void* Nothing)
{
  struct blakeChallenge* cha;
  assert(pkt);

  /* We can totally ignore the challenge, it only tells us how long we have to
  login.  Let's just send the login now */
  SignonForm->NextStage(); // Received challenge
  cha = (struct blakeChallenge*)pkt;
  MainForm->MyUser.SetChallengeDigest(cha->rnum); // Save the unique challenge random digest for later use
  SignonForm->NextStage(); // Sending login information
  SignonForm->SendLoginInformation(s);
  return true;
}
//---------------------------------------------------------------------------
static blake_bool_t GetLoginReply(
  int s,
  const void *pkt,
  const void *Nothing
)
{
  struct blakeLoginReply *reply;

  assert(pkt);
  if ( !pkt ) return false;
  SignonForm->NextStage(); // You are logged in
  daybothreads_Sleep(1); // Wait for a second so user sees message

  reply = (struct blakeLoginReply*)pkt;
  if ( reply->failcode != blrSuccess )
    SignonForm->FailCode((enum blakeLoginReplyCode)reply->failcode);
  else
    SignonForm->SignedIn();

  return true;
}
//---------------------------------------------------------------------------
static blake_bool_t GetMsg(int s, const void* pkt, const void* Message)
{
  struct blakeMessage* msg;
  OVERCHAT_EVENT ev;

  assert(pkt);
  if ( !pkt ) return false;

  msg = (struct blakeMessage*)pkt;

  InitOverChatEvent(&ev);
  ev.Code = eventMsg;
  strncat(ev.eventData.Msg.From, (const char*)msg->username, sizeof(ev.eventData.Msg.From)-1);
  time(&ev.eventData.Msg.When); // Record time when the message arrived
  ev.eventData.Msg.CipherID = (enum blakeCipherID)msg->cipher;
  ev.eventData.Msg.Flags1 = msg->flags1;
  ev.eventData.Msg.DynamicMessage = new char[strlen((const char*)Message)+1];
  if ( ev.eventData.Msg.DynamicMessage ) {
    strcpy(ev.eventData.Msg.DynamicMessage, (const char*)Message);
    PostOverChatEvent(&ev);
    return true;
  }

  return false;
}
//---------------------------------------------------------------------------
static blake_bool_t GetMsg2(int s, const void* pkt, const void* Command)
{
  struct blakeMessage2* msg2;
  OVERCHAT_EVENT ev;

  assert(pkt);
  if ( !pkt ) return false;

  msg2 = (struct blakeMessage2*)pkt;

  InitOverChatEvent(&ev);
  ev.Code = eventMsg2;

  ev.eventData.Msg2.Reserved0 = msg2->reserved0;
  ev.eventData.Msg2.Reserved1 = msg2->reserved1;
  ev.eventData.Msg2.Reserved2 = msg2->reserved2;
  ev.eventData.Msg2.DynamicCommand = new char[strlen((const char*)Command)+1];
  if ( ev.eventData.Msg2.DynamicCommand ) {
    strcpy(ev.eventData.Msg2.DynamicCommand, (const char*)Command);
    PostOverChatEvent(&ev);
    return true;
  }

  return false;
}
//---------------------------------------------------------------------------
static blake_bool_t GetWhoisInfo(int s, const void* pkt, const void* Nothing)
{
  struct blakeWhoisInfo* wii;
  OVERCHAT_EVENT ev;

  assert(pkt);
  if ( !pkt ) return false;

  wii = (struct blakeWhoisInfo*)pkt;

  InitOverChatEvent(&ev);
  ev.Code = eventWhoisInfo;

  // Transfer the rest of the data from the packet to the event
  ev.eventData.WhoisInfo.wii = *wii;
  PostOverChatEvent(&ev);
  return true;
}
//---------------------------------------------------------------------------
static blake_bool_t GetVersioned(int s, const void *pkt, const void *Nothing)
{
  struct blakeVersioned *vd;
  OVERCHAT_EVENT ev;

  assert(pkt);
  if ( !pkt ) return false;

  vd = (struct blakeVersioned*)pkt;

  InitOverChatEvent(&ev);
  ev.Code = eventVersioned;

  // Transfer the rest of the data from the packet to the event
  ev.eventData.Versioned.vd = *vd;
  PostOverChatEvent(&ev);
  return true;
}
//---------------------------------------------------------------------------
static blake_bool_t GetIsOnlineReply(int s, const void* pkt, const void* Nothing)
{
  struct blakeIsOnlineReply* ior;
  OVERCHAT_EVENT ev;

  assert(pkt);
  if ( !pkt ) return false;

  ior = (struct blakeIsOnlineReply*)pkt;

  InitOverChatEvent(&ev);
  ev.Code = eventIsOnlineReply;

  ev.eventData.IsOnlineReply.ior = *ior;
  PostOverChatEvent(&ev);
  return true;
}
//---------------------------------------------------------------------------
static blake_bool_t GetSuper(int s, const void* pkt, const void* Nothing)
{
  struct blakeSuper* spr;
  OVERCHAT_EVENT ev;

  assert(pkt);
  if ( !pkt ) return false;

  spr = (struct blakeSuper*)pkt;

  InitOverChatEvent(&ev);
  ev.Code = eventSuper;

  ev.eventData.Super.spr = *spr;
  PostOverChatEvent(&ev);
  return true;
}
//---------------------------------------------------------------------------
static blake_bool_t GetError(
  int s,
  const void *pkt,
  const void *Nothing
)
{
  const struct blakeGotError *ge;
  OVERCHAT_EVENT ev;

  assert(pkt);
  if ( !pkt ) return false;

  ge = (const struct blakeGotError*)pkt;

  InitOverChatEvent(&ev);
  ev.Code = eventGotError;

  ev.eventData.GotError.ge = *ge;
  PostOverChatEvent(&ev);
  return true;
}
//---------------------------------------------------------------------------