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
|
/* bzflag
* Copyright (c) 1993-2025 Tim Riker
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
// logDetail.cpp : Plugin module for logging server events to stdout
//
#include "bzfsAPI.h"
#include <iostream>
#include <sstream>
enum action { join, auth, part };
class LogDetail : public bz_Plugin
{
public:
virtual const char* Name ()
{
return "Log Detail";
}
virtual void Init ( const char* config );
virtual void Cleanup ();
virtual void Event ( bz_EventData *eventData );
private:
std::string displayPlayerPrivs( int playerID );
std::string displayCallsign( bz_ApiString callsign );
std::string displayCallsign( int playerID );
std::string displayMotto( int playerID );
std::string displayBZid( int playerID );
std::string displayTeam( bz_eTeamType team );
virtual void listPlayers( action act, bz_PlayerJoinPartEventData_V1 *data );
};
BZ_PLUGIN(LogDetail)
void LogDetail::Init ( const char* /*commandLine*/ )
{
Register(bz_eSlashCommandEvent);
Register(bz_eRawChatMessageEvent);
Register(bz_eServerMsgEvent);
Register(bz_ePlayerJoinEvent);
Register(bz_ePlayerPartEvent);
Register(bz_ePlayerAuthEvent);
Register(bz_eMessageFilteredEvent);
bz_debugMessage(0, "SERVER-STATUS Running");
bz_debugMessagef(0, "SERVER-MAPNAME %s", bz_getPublicDescription().c_str());
listPlayers( join, NULL );
}
void LogDetail::Cleanup()
{
listPlayers( part, NULL );
bz_debugMessage(0, "SERVER-STATUS Stopped");
Flush();
}
void LogDetail::Event( bz_EventData *eventData )
{
bz_ChatEventData_V1 *chatData = (bz_ChatEventData_V1 *) eventData;
bz_ServerMsgEventData_V1 *serverMsgData = (bz_ServerMsgEventData_V1 *) eventData;
bz_SlashCommandEventData_V1 *cmdData = (bz_SlashCommandEventData_V1 *) eventData;
bz_PlayerJoinPartEventData_V1 *joinPartData = (bz_PlayerJoinPartEventData_V1 *) eventData;
bz_PlayerAuthEventData_V1 *authData = (bz_PlayerAuthEventData_V1 *) eventData;
bz_MessageFilteredEventData_V1 *filteredData = (bz_MessageFilteredEventData_V1 *) eventData;
if (eventData)
{
switch (eventData->eventType)
{
case bz_eSlashCommandEvent:
{
// Slash commands are case insensitive
// Tokenize the stream and check the first word
// /report -> MSG-REPORT
// anything -> MSG-COMMAND
char temp[9] = {0};
strncpy(temp, cmdData->message.c_str(), 8);
if (strcasecmp( temp, "/REPORT ") == 0)
{
bz_debugMessagef(0, "MSG-REPORT %s %s",
displayCallsign( cmdData->from ).c_str(),
cmdData->message.c_str()+8);
}
else
{
bz_debugMessagef(0, "MSG-COMMAND %s %s",
displayCallsign( cmdData->from ).c_str(),
cmdData->message.c_str()+1);
}
break;
}
case bz_eRawChatMessageEvent:
if ((chatData->to == BZ_ALLUSERS) && (chatData->team == eNoTeam))
{
bz_debugMessagef(0, "MSG-BROADCAST %s %s",
displayCallsign( chatData->from ).c_str(),
chatData->message.c_str());
}
else if (chatData->to == BZ_NULLUSER)
{
if (chatData->team == eAdministrators)
{
bz_debugMessagef(0, "MSG-ADMIN %s %s",
displayCallsign( chatData->from ).c_str(),
chatData->message.c_str());
}
else
{
bz_debugMessagef(0, "MSG-TEAM %s %s %s",
displayCallsign( chatData->from ).c_str(),
displayTeam( chatData->team ).c_str(),
chatData->message.c_str());
}
}
else
{
bz_debugMessagef(0, "MSG-DIRECT %s %s %s",
displayCallsign( chatData->from ).c_str(),
displayCallsign( chatData->to ).c_str(),
chatData->message.c_str());
}
break;
case bz_eMessageFilteredEvent:
bz_debugMessagef(0, "MSG-FILTERED %s %s",
displayCallsign( filteredData->playerID ).c_str(),
filteredData->filteredMessage.c_str());
break;
case bz_eServerMsgEvent:
if ((serverMsgData->to == BZ_ALLUSERS) && (serverMsgData->team == eNoTeam))
{
bz_debugMessagef(0, "MSG-BROADCAST 6:SERVER %s",
serverMsgData->message.c_str());
}
else if (serverMsgData->to == BZ_NULLUSER)
{
if (serverMsgData->team == eAdministrators)
{
bz_debugMessagef(0, "MSG-ADMIN 6:SERVER %s",
serverMsgData->message.c_str());
}
else
{
bz_debugMessagef(0, "MSG-TEAM 6:SERVER %s %s",
displayTeam( serverMsgData->team ).c_str(),
serverMsgData->message.c_str());
}
}
else
{
bz_debugMessagef(0, "MSG-DIRECT 6:SERVER %s %s",
displayCallsign( serverMsgData->to ).c_str(),
serverMsgData->message.c_str());
}
break;
case bz_ePlayerJoinEvent:
{
if (joinPartData->record)
{
bz_debugMessagef(0, "PLAYER-JOIN %s #%d%s %s %s",
displayCallsign( joinPartData->playerID).c_str(),
joinPartData->playerID,
displayBZid( joinPartData->playerID ).c_str(),
displayTeam( joinPartData->record->team ).c_str(),
displayPlayerPrivs( joinPartData->playerID ).c_str());
bz_debugMessagef(0, "PLAYER-MOTTO %s #%d%s %s",
displayCallsign( joinPartData->playerID).c_str(),
joinPartData->playerID,
displayBZid( joinPartData->playerID ).c_str(),
displayMotto( joinPartData->playerID).c_str());
listPlayers( join, joinPartData);
}
}
break;
case bz_ePlayerPartEvent:
bz_debugMessagef(0, "PLAYER-PART %s #%d%s %s",
displayCallsign( joinPartData->playerID ).c_str(),
joinPartData->playerID,
displayBZid( joinPartData->playerID ).c_str(),
joinPartData->reason.c_str());
listPlayers( part, joinPartData);
break;
case bz_ePlayerAuthEvent:
bz_debugMessagef(0, "PLAYER-AUTH %s %s",
displayCallsign( authData->playerID ).c_str(),
displayPlayerPrivs( authData->playerID ).c_str()),
listPlayers( join, joinPartData);
break;
default :
break;
}
}
}
std::string LogDetail::displayBZid( int playerID )
{
std::ostringstream bzid;
bz_BasePlayerRecord *player = bz_getPlayerByIndex( playerID );
if (player)
{
if (player->globalUser)
bzid << " BZid:" << player->bzID.c_str();
bz_freePlayerRecord( player );
}
return bzid.str();
}
std::string LogDetail::displayPlayerPrivs( int playerID )
{
std::ostringstream playerPrivs;
bz_BasePlayerRecord *player = bz_getPlayerByIndex( playerID );
if (player)
{
playerPrivs << "IP:" << player->ipAddress.c_str();
if (player->verified ) playerPrivs << " VERIFIED";
if (player->globalUser ) playerPrivs << " GLOBALUSER";
if (player->admin ) playerPrivs << " ADMIN";
if (player->op ) playerPrivs << " OPERATOR";
bz_freePlayerRecord( player );
}
else
playerPrivs << "IP:0.0.0.0";
return playerPrivs.str();
}
std::string LogDetail::displayCallsign( bz_ApiString callsign )
{
std::ostringstream result;
result << strlen( callsign.c_str() ) << ":" << callsign.c_str();
return result.str();
}
std::string LogDetail::displayCallsign( int playerID )
{
std::ostringstream callsign;
bz_BasePlayerRecord *player = bz_getPlayerByIndex( playerID );
if (player)
{
callsign << strlen( player->callsign.c_str() ) << ":";
callsign << player->callsign.c_str();
bz_freePlayerRecord( player );
}
else
callsign << "7:UNKNOWN";
return callsign.str();
}
std::string LogDetail::displayMotto( int playerID )
{
std::ostringstream motto;
bz_PlayerRecordV2 *player = (bz_PlayerRecordV2*)bz_getPlayerByIndex( playerID );
if (player)
{
motto << strlen( player->motto.c_str() ) << ":";
motto << player->motto.c_str();
bz_freePlayerRecord( player );
}
else
motto << "7:UNKNOWN";
return motto.str();
}
std::string LogDetail::displayTeam( bz_eTeamType team )
{
// Display the player team
switch ( team )
{
case eRogueTeam:
return std::string("ROGUE");
case eRedTeam:
return std::string("RED");
case eGreenTeam:
return std::string("GREEN");
case eBlueTeam:
return std::string("BLUE");
case ePurpleTeam:
return std::string("PURPLE");
case eRabbitTeam:
return std::string("RABBIT");
case eHunterTeam:
return std::string("HUNTER");
case eObservers:
return std::string("OBSERVER");
default :
return std::string("NOTEAM");
}
}
void LogDetail::listPlayers( action act, bz_PlayerJoinPartEventData_V1 *data )
{
bz_APIIntList *playerList = bz_newIntList();
bz_BasePlayerRecord *player = NULL;
std::ostringstream msg;
char playerStatus;
int numPlayers;
bz_getPlayerIndexList( playerList );
bz_debugMessage( 4, "Players:" );
//
// Count number of players
//
numPlayers = 0;
for ( unsigned int i = 0; i < playerList->size(); i++ )
{
player = bz_getPlayerByIndex( playerList->get(i));
if (player)
{
if ((player->callsign != "") && (act == join || act == auth || (data && (player->playerID != data->playerID))))
numPlayers++;
bz_freePlayerRecord( player );
}
}
//
// Display number of players, callsign, and motto string in the following format:
//
// PLAYERS (nn) [G]cc:callsign(ee:mottostring)
// nn - number of players
// G - global auth identifier (+|-| |@)
// cc - count of characters in player callsign
// callsign - player callsign
// ee - count of characters in motto string
// mottostring - player motto string
//
// eg.
// PLAYERS (2) [@]7:Thumper(16:me@somewhere.net) [ ]3:xxx()
//
msg.str("");
msg << "PLAYERS (" << numPlayers << ")";
for ( unsigned int i = 0; i < playerList->size(); i++ )
{
player = bz_getPlayerByIndex( playerList->get(i));
if (player)
{
if ((player->callsign != "") && (act == join || act == auth || (data && (player->playerID != data->playerID))))
{
playerStatus = ' ';
if (player->globalUser) playerStatus = '+';
if (player->verified) playerStatus = '+';
if (player->admin && !bz_hasPerm(player->playerID, bz_perm_hideAdmin)) playerStatus = '@';
msg << " [" << playerStatus << "]";
msg << player->callsign.size() << ':';
msg << player->callsign.c_str() << "(" << bz_getPlayerMotto(player->playerID) << ")";
}
}
}
bz_debugMessage(0, msg.str().c_str());
bz_deleteIntList(playerList);
}
// Local Variables: ***
// mode: C++ ***
// tab-width: 4 ***
// c-basic-offset: 4 ***
// indent-tabs-mode: nil ***
// End: ***
// ex: shiftwidth=4 tabstop=4
|