updates to irc use in mastermenu:

- shows local username
- bugfixes for detecting new/dropped users in the irc channel
- lots of cleanup to remove use of so many static variables
This commit is contained in:
Mark Vejvoda
2010-12-24 02:10:31 +00:00
parent b954d399df
commit 97ee61165a
9 changed files with 352 additions and 203 deletions

View File

@@ -40,16 +40,17 @@ ChatManager::ChatManager() {
disableTeamMode = false; disableTeamMode = false;
} }
void ChatManager::init(Console* console, int thisTeamIndex, const bool inMenu) { void ChatManager::init(Console* console, int thisTeamIndex, const bool inMenu, string manualPlayerNameOverride) {
this->console= console; this->console= console;
this->thisTeamIndex= thisTeamIndex; this->thisTeamIndex= thisTeamIndex;
this->disableTeamMode= false; this->disableTeamMode= false;
this->inMenu=inMenu; this->inMenu=inMenu;
this->manualPlayerNameOverride = manualPlayerNameOverride;
} }
void ChatManager::keyUp(char key) { void ChatManager::keyUp(char key) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
try { try {
if(editEnabled) { if(editEnabled) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,key,key); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,key,key);
@@ -119,7 +120,13 @@ void ChatManager::keyDown(char key) {
if(text.empty() == false) { if(text.empty() == false) {
string playerName = gameNetworkInterface->getHumanPlayerName(); string playerName = gameNetworkInterface->getHumanPlayerName();
int playerIndex = gameNetworkInterface->getHumanPlayerIndex(); int playerIndex = gameNetworkInterface->getHumanPlayerIndex();
console->addLine(text,false,playerIndex);
if(this->manualPlayerNameOverride != "") {
console->addLine(text,false,this->manualPlayerNameOverride);
}
else {
console->addLine(text,false,playerIndex);
}
gameNetworkInterface->sendTextMessage(text, teamMode? thisTeamIndex: -1); gameNetworkInterface->sendTextMessage(text, teamMode? thisTeamIndex: -1);
if(inMenu == false) { if(inMenu == false) {
@@ -128,7 +135,7 @@ void ChatManager::keyDown(char key) {
} }
else { else {
editEnabled= false; editEnabled= false;
} }
text.clear(); text.clear();
} }
else { else {
@@ -146,7 +153,7 @@ void ChatManager::keyDown(char key) {
text.erase(text.end() -1); text.erase(text.end() -1);
} }
} }
} }
catch(const exception &ex) { catch(const exception &ex) {
char szBuf[1024]=""; char szBuf[1024]="";

View File

@@ -3,9 +3,9 @@
// //
// Copyright (C) 2001-2008 Marti<74>o Figueroa // Copyright (C) 2001-2008 Marti<74>o Figueroa
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the // by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version // License, or (at your option) any later version
// ============================================================== // ==============================================================
@@ -37,11 +37,11 @@ private:
string text; string text;
int thisTeamIndex; int thisTeamIndex;
bool inMenu; bool inMenu;
string manualPlayerNameOverride;
public: public:
ChatManager(); ChatManager();
void init(Console* console, int thisTeamIndex, const bool inMenu=false ); void init(Console* console, int thisTeamIndex, const bool inMenu=false, string manualPlayerNameOverride="");
void keyDown(char key); void keyDown(char key);
void keyUp(char key); void keyUp(char key);

View File

@@ -3,9 +3,9 @@
// //
// Copyright (C) 2001-2008 Martio Figueroa // Copyright (C) 2001-2008 Martio Figueroa
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the // by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version // License, or (at your option) any later version
// ============================================================== // ==============================================================
@@ -79,24 +79,56 @@ void Console::addLine(string line, bool playSound, int playerIndex) {
} }
} }
void Console::addLine(string line, bool playSound, string playerName) {
try {
if(playSound == true) {
SoundRenderer::getInstance().playFx(CoreData::getInstance().getClickSoundA());
}
ConsoleLineInfo info;
info.text = line;
info.timeStamp = timeElapsed;
info.PlayerIndex = -1;
info.originalPlayerName = "";
if(playerName != "") {
info.originalPlayerName = playerName;
}
//printf("info.PlayerIndex = %d, line [%s]\n",info.PlayerIndex,info.originalPlayerName.c_str());
lines.insert(lines.begin(), info);
if(lines.size() > maxLines) {
lines.pop_back();
}
storedLines.insert(storedLines.begin(), info);
if(storedLines.size() > maxStoredLines) {
storedLines.pop_back();
}
}
catch(const exception &ex) {
char szBuf[1024]="";
sprintf(szBuf,"In [%s::%s %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
SystemFlags::OutputDebug(SystemFlags::debugError,szBuf);
throw runtime_error(szBuf);
}
}
void Console::clearStoredLines() { void Console::clearStoredLines() {
while(storedLines.empty() == false) { while(storedLines.empty() == false) {
storedLines.pop_back(); storedLines.pop_back();
} }
} }
void Console::update() { void Console::update() {
timeElapsed += 1.f / GameConstants::updateFps; timeElapsed += 1.f / GameConstants::updateFps;
if(lines.empty() == false) { if(lines.empty() == false) {
if(lines.back().timeStamp < (timeElapsed - timeout)) { if(lines.back().timeStamp < (timeElapsed - timeout)) {
lines.pop_back(); lines.pop_back();
} }
} }
} }
bool Console::isEmpty() { bool Console::isEmpty() {
return lines.empty(); return lines.empty();
} }
string Console::getLine(int i) const { string Console::getLine(int i) const {

View File

@@ -3,9 +3,9 @@
// //
// Copyright (C) 2001-2008 Martio Figueroa // Copyright (C) 2001-2008 Martio Figueroa
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the // by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version // License, or (at your option) any later version
// ============================================================== // ==============================================================
@@ -49,7 +49,7 @@ public:
typedef Lines::const_iterator LineIterator; typedef Lines::const_iterator LineIterator;
private: private:
float timeElapsed; float timeElapsed;
Lines lines; Lines lines;
Lines storedLines; Lines storedLines;
@@ -60,7 +60,7 @@ private:
public: public:
Console(); Console();
int getStoredLineCount() const {return storedLines.size();} int getStoredLineCount() const {return storedLines.size();}
int getLineCount() const {return lines.size();} int getLineCount() const {return lines.size();}
string getLine(int i) const; string getLine(int i) const;
@@ -71,6 +71,7 @@ public:
void clearStoredLines(); void clearStoredLines();
void addStdMessage(const string &s); void addStdMessage(const string &s);
void addLine(string line, bool playSound= false,int playerIndex=-1); void addLine(string line, bool playSound= false,int playerIndex=-1);
void addLine(string line, bool playSound,string playerName);
void update(); void update();
bool isEmpty(); bool isEmpty();
}; };

View File

@@ -189,7 +189,7 @@ Renderer::Renderer() {
particleManager[i]= graphicsFactory->newParticleManager(); particleManager[i]= graphicsFactory->newParticleManager();
fontManager[i]= graphicsFactory->newFontManager(); fontManager[i]= graphicsFactory->newFontManager();
} }
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
saveScreenShotThread = new SimpleTaskThread(this,0,25); saveScreenShotThread = new SimpleTaskThread(this,0,25);
@@ -885,6 +885,28 @@ void Renderer::RenderConsoleLine(int lineIndex, int xPosition, const ConsoleLine
xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine)));
} }
} }
else if(lineInfo->originalPlayerName != "") {
string playerName = lineInfo->originalPlayerName;
string headerLine = playerName + ": ";
const Metrics &metrics= Metrics::getInstance();
const FontMetrics *fontMetrics= CoreData::getInstance().getConsoleFont()->getMetrics();
if(fontMetrics == NULL) {
throw runtime_error("fontMetrics == NULL");
}
renderTextShadow(
headerLine,
CoreData::getInstance().getConsoleFont(),
fontColor,
xPosition, lineIndex * 20 + 20);
fontColor = defaultFontColor;
//xPosition += (8 * (playerName.length() + 2));
// Proper font spacing after username portion of chat text rendering
xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine)));
}
else { else {
fontColor = defaultFontColor; fontColor = defaultFontColor;
} }
@@ -2320,27 +2342,27 @@ void Renderer::renderMinimap(){
glVertex2f(attackX, attackY); glVertex2f(attackX, attackY);
glEnd(); glEnd();
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor4f(1.f, 1.f, 0.f, alpha); glColor4f(1.f, 1.f, 0.f, alpha);
glVertex2f(attackX-scale, attackY+scale); glVertex2f(attackX-scale, attackY+scale);
glVertex2f(attackX+scale, attackY+scale); glVertex2f(attackX+scale, attackY+scale);
glColor4f(1.f, 1.f, 0.f, 0.8f); glColor4f(1.f, 1.f, 0.f, 0.8f);
glVertex2f(attackX, attackY); glVertex2f(attackX, attackY);
glEnd(); glEnd();
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor4f(1.f, 1.f, 0.f, alpha); glColor4f(1.f, 1.f, 0.f, alpha);
glVertex2f(attackX+scale, attackY+scale); glVertex2f(attackX+scale, attackY+scale);
glVertex2f(attackX+scale, attackY-scale); glVertex2f(attackX+scale, attackY-scale);
glColor4f(1.f, 1.f, 0.f, 0.8f); glColor4f(1.f, 1.f, 0.f, 0.8f);
glVertex2f(attackX, attackY); glVertex2f(attackX, attackY);
glEnd(); glEnd();
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor4f(1.f, 1.f, 0.f, alpha); glColor4f(1.f, 1.f, 0.f, alpha);
glVertex2f(attackX+scale, attackY-scale); glVertex2f(attackX+scale, attackY-scale);
glVertex2f(attackX-scale, attackY-scale); glVertex2f(attackX-scale, attackY-scale);
glColor4f(1.f, 1.f, 0.f, 0.8f); glColor4f(1.f, 1.f, 0.f, 0.8f);
glVertex2f(attackX, attackY); glVertex2f(attackX, attackY);
glEnd(); glEnd();
} }
} }
glDisable(GL_BLEND); glDisable(GL_BLEND);
@@ -2436,7 +2458,7 @@ void Renderer::renderMinimap(){
glVertex2i(x2,y2); glVertex2i(x2,y2);
glEnd(); glEnd();
glPopAttrib(); glPopAttrib();
assertGl(); assertGl();

View File

@@ -196,7 +196,6 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen
containerName = "MasterServer"; containerName = "MasterServer";
updateFromMasterserverThread = NULL; updateFromMasterserverThread = NULL;
ircClient = NULL; ircClient = NULL;
lastNickListUpdate = 0;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
Lang &lang= Lang::getInstance(); Lang &lang= Lang::getInstance();
@@ -352,8 +351,13 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen
GraphicComponent::applyAllCustomProperties(containerName); GraphicComponent::applyAllCustomProperties(containerName);
char szIRCNick[80]="";
srand(time(NULL));
int randomNickId = rand() % 999;
sprintf(szIRCNick,"MG_%s_%d",Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()).c_str(),randomNickId);
consoleIRC.addLine(lang.get("To switch off music press")+" - \""+configKeys.getCharKey("ToggleMusic")+"\""); consoleIRC.addLine(lang.get("To switch off music press")+" - \""+configKeys.getCharKey("ToggleMusic")+"\"");
chatManager.init(&consoleIRC, -1,true); chatManager.init(&consoleIRC, -1, true, szIRCNick);
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor); MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
masterServerThreadInDeletion = false; masterServerThreadInDeletion = false;
@@ -362,11 +366,6 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen
updateFromMasterserverThread->setUniqueID(__FILE__); updateFromMasterserverThread->setUniqueID(__FILE__);
updateFromMasterserverThread->start(); updateFromMasterserverThread->start();
char szIRCNick[80]="";
srand(time(NULL));
int randomNickId = rand() % 999;
sprintf(szIRCNick,"MG_%s_%d",Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()).c_str(),randomNickId);
ircArgs.push_back(IRC_SERVER); ircArgs.push_back(IRC_SERVER);
ircArgs.push_back(szIRCNick); ircArgs.push_back(szIRCNick);
ircArgs.push_back(IRC_CHANNEL); ircArgs.push_back(IRC_CHANNEL);
@@ -626,11 +625,18 @@ void MenuStateMasterserver::render(){
renderer.renderLabel(&externalConnectPort,&titleLabelColor); renderer.renderLabel(&externalConnectPort,&titleLabelColor);
renderer.renderLabel(&selectButton,&titleLabelColor); renderer.renderLabel(&selectButton,&titleLabelColor);
renderer.renderLabel(&ircOnlinePeopleLabel,&titleLabelColor); if(ircClient != NULL &&
renderer.renderLabel(&ircOnlinePeopleListLabel,&titleLabelColor); ircClient->isConnected() == true &&
ircClient->getHasJoinedChannel() == true) {
// render console const Vec4f titleLabelColor = GREEN;
//renderer.renderConsole(&console,false,false); renderer.renderLabel(&ircOnlinePeopleLabel,&titleLabelColor);
}
else {
const Vec4f titleLabelColor = RED;
renderer.renderLabel(&ircOnlinePeopleLabel,&titleLabelColor);
}
const Vec4f titleLabelColorList = YELLOW;
renderer.renderLabel(&ircOnlinePeopleListLabel,&titleLabelColorList);
for(int i=0; i<serverLines.size(); ++i){ for(int i=0; i<serverLines.size(); ++i){
serverLines[i]->render(); serverLines[i]->render();
@@ -669,18 +675,15 @@ void MenuStateMasterserver::update() {
consoleIRC.update(); consoleIRC.update();
if(ircClient != NULL) { if(ircClient != NULL) {
if(difftime(time(NULL),lastNickListUpdate) >= 5) { std::vector<string> nickList = ircClient->getNickList();
lastNickListUpdate = time(NULL); string nicks = "";
std::vector<string> nickList = ircClient->getNickList(); for(int i = 0; i < nickList.size(); ++i) {
string nicks = ""; if(nicks != "") {
for(int i = 0; i < nickList.size(); ++i) { nicks += ", ";
if(nicks != "") {
nicks += " ";
}
nicks += nickList[i];
} }
ircOnlinePeopleListLabel.setText(nicks); nicks += nickList[i];
} }
ircOnlinePeopleListLabel.setText(nicks);
} }
else { else {
ircOnlinePeopleListLabel.setText(""); ircOnlinePeopleListLabel.setText("");

View File

@@ -132,7 +132,6 @@ private:
std::vector<string> ircArgs; std::vector<string> ircArgs;
IRCThread *ircClient; IRCThread *ircClient;
time_t lastNickListUpdate;
Console consoleIRC; Console consoleIRC;
ChatManager chatManager; ChatManager chatManager;

View File

@@ -42,20 +42,49 @@ protected:
std::vector<string> argv; std::vector<string> argv;
irc_session_t *ircSession; irc_session_t *ircSession;
string channel;
string nick;
bool hasJoinedChannel;
bool eventDataDone;
Mutex mutexNickList;
time_t lastNickListUpdate;
std::vector<string> eventData;
IRCCallbackInterface *callbackObj;
public: public:
IRCThread(const std::vector<string> &argv,IRCCallbackInterface *callbackObj); IRCThread(const std::vector<string> &argv,IRCCallbackInterface *callbackObj);
virtual void execute(); virtual void execute();
virtual void signalQuit(); virtual void signalQuit();
virtual bool shutdownAndWait(); virtual bool shutdownAndWait();
void SendIRCCmdMessage(string target, string msg); void SendIRCCmdMessage(string target, string msg);
std::vector<string> GetIRCConnectedNickList(string target); std::vector<string> getNickList();
std::vector<string> getNickList() { return eventData; } bool isConnected();
static IRCCallbackInterface *callbackObj; std::vector<string> GetIRCConnectedNickList(string target, bool waitForCompletion);
static std::vector<string> eventData;
static bool eventDataDone; bool getEventDataDone() const { return eventDataDone; }
static bool isConnected; void setEventDataDone(bool value) { eventDataDone=value; }
bool getHasJoinedChannel() const { return hasJoinedChannel; }
void setHasJoinedChannel(bool value) { hasJoinedChannel=value; }
time_t getLastNickListUpdate() const { return lastNickListUpdate; }
void setLastNickListUpdate(time_t value) { lastNickListUpdate = value;}
string getChannel() const { return channel;}
string getNick() const { return nick;}
std::vector<string> getArgs() const { return argv;}
Mutex * getMutexNickList() { return &mutexNickList; }
std::vector<string> & getCachedNickList() { return eventData; }
void setCachedNickList(std::vector<string> &list) { eventData = list; }
IRCCallbackInterface * getCallbackObj() { return callbackObj;}
}; };
}}//end namespace }}//end namespace

View File

@@ -16,9 +16,9 @@
#ifndef WIN32 #ifndef WIN32
#ifndef _LIBIRCCLIENTLOCAL #ifndef _LIBIRCCLIENTLOCAL
#include <libircclient/libircclient.h> #include <libircclient/libircclient.h>
#else #else
#include "libircclient.h" #include "libircclient.h"
#endif #endif
#else #else
#include "libircclient.h" #include "libircclient.h"
@@ -34,18 +34,6 @@ using namespace Shared::PlatformCommon;
namespace Shared { namespace PlatformCommon { namespace Shared { namespace PlatformCommon {
IRCCallbackInterface *IRCThread::callbackObj=NULL;
std::vector<string> IRCThread::eventData;
bool IRCThread::eventDataDone = false;
bool IRCThread::isConnected = false;
//
// We store data in IRC session context.
//
typedef struct {
string channel;
string nick;
} irc_ctx_t;
void addlog (const char * fmt, ...) { void addlog (const char * fmt, ...) {
FILE * fp; FILE * fp;
char buf[1024]; char buf[1024];
@@ -82,30 +70,61 @@ void dump_event (irc_session_t * session, const char * event, const char * origi
} }
addlog ("Event \"%s\", origin: \"%s\", params: %d [%s]", event, origin ? origin : "NULL", cnt, buf); addlog ("Event \"%s\", origin: \"%s\", params: %d [%s]", event, origin ? origin : "NULL", cnt, buf);
IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
if(ctx != NULL) {
if(difftime(time(NULL),ctx->getLastNickListUpdate()) >= 7) {
ctx->setLastNickListUpdate(time(NULL));
ctx->GetIRCConnectedNickList(ctx->getArgs()[2],false);
}
}
} }
void event_join(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { void event_join(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) {
dump_event (session, event, origin, params, count); dump_event (session, event, origin, params, count);
if(IRCThread::isConnected == false) { IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
irc_cmd_user_mode (session, "+i"); if(ctx != NULL) {
irc_cmd_msg (session, params[0], "MG Bot says hello!"); if(ctx->getHasJoinedChannel() == false) {
} irc_cmd_user_mode (session, "+i");
else { irc_cmd_msg (session, params[0], "MG Bot says hello!");
char realNick[128]=""; ctx->setHasJoinedChannel(true);
irc_target_get_nick(origin,&realNick[0],127);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: user joined channel realNick [%s] origin [%s]\n", realNick,origin);
IRCThread::eventData.push_back(realNick);
}
IRCThread::isConnected = true; ctx->GetIRCConnectedNickList(ctx->getArgs()[2],true);
}
else {
char realNick[128]="";
irc_target_get_nick(origin,&realNick[0],127);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: user joined channel realNick [%s] origin [%s]\n", realNick,origin);
bool foundNick = false;
MutexSafeWrapper safeMutex(ctx->getMutexNickList());
std::vector<string> nickList = ctx->getCachedNickList();
for(unsigned int i = 0;
i < nickList.size(); ++i) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: lookingfor match [%s] realNick [%s]\n", nickList[i].c_str(),realNick);
if(nickList[i] == realNick) {
foundNick = true;
break;
}
}
if(foundNick == false) {
nickList.push_back(realNick);
}
}
}
} }
void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) {
irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session); IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
dump_event (session, event, origin, params, count); dump_event (session, event, origin, params, count);
irc_cmd_join (session, ctx->channel.c_str(), 0); if(ctx != NULL) {
irc_cmd_join (session, ctx->getChannel().c_str(), 0);
}
} }
void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) {
@@ -179,9 +198,12 @@ void event_channel(irc_session_t * session, const char * event, const char * ori
irc_target_get_nick(origin, nickbuf, sizeof(nickbuf)); irc_target_get_nick(origin, nickbuf, sizeof(nickbuf));
if(IRCThread::callbackObj) { IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
IRCThread::callbackObj->IRC_CallbackEvent(nickbuf, params, count); if(ctx != NULL) {
} if(ctx->getCallbackObj() != NULL) {
ctx->getCallbackObj()->IRC_CallbackEvent(nickbuf, params, count);
}
}
if ( !strcmp (params[1], "quit") ) if ( !strcmp (params[1], "quit") )
irc_cmd_quit (session, "of course, Master!"); irc_cmd_quit (session, "of course, Master!");
@@ -247,7 +269,6 @@ void event_leave(irc_session_t *session, const char *event, const char *origin,
sprintf (buf, "%s", event); sprintf (buf, "%s", event);
// someone left the channel. // someone left the channel.
if(origin) { if(origin) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: user left channel [%s]\n", origin); if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: user left channel [%s]\n", origin);
@@ -256,14 +277,20 @@ void event_leave(irc_session_t *session, const char *event, const char *origin,
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: user left channel realNick [%s]\n", realNick); if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: user left channel realNick [%s]\n", realNick);
for(unsigned int i = 0; i < IRCThread::eventData.size(); ++i) { IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: lookingfor match [%s] realNick [%s]\n", IRCThread::eventData[i].c_str(),realNick); if(ctx != NULL) {
MutexSafeWrapper safeMutex(ctx->getMutexNickList());
std::vector<string> &nickList = ctx->getCachedNickList();
for(unsigned int i = 0;
i < nickList.size(); ++i) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: lookingfor match [%s] realNick [%s]\n", nickList[i].c_str(),realNick);
if(IRCThread::eventData[i] == realNick) { if(nickList[i] == realNick) {
IRCThread::eventData.erase(IRCThread::eventData.begin() + i); nickList.erase(nickList.begin() + i);
break; break;
} }
} }
}
} }
dump_event (session, buf, origin, params, count); dump_event (session, buf, origin, params, count);
@@ -282,23 +309,33 @@ void event_numeric(irc_session_t * session, unsigned int event, const char * ori
case LIBIRC_RFC_RPL_NAMREPLY : case LIBIRC_RFC_RPL_NAMREPLY :
{ {
if(event == LIBIRC_RFC_RPL_NAMREPLY) { if(event == LIBIRC_RFC_RPL_NAMREPLY) {
IRCThread::eventData.clear(); std::vector<string> nickList;
if(count >= 4) { if(count >= 4) {
for(unsigned int i = 3; i < count && params[i]; ++i) { for(unsigned int i = 3; i < count && params[i]; ++i) {
vector<string> tokens; vector<string> tokens;
Tokenize(params[i],tokens," "); Tokenize(params[i],tokens," ");
for(unsigned int j = 0; j < tokens.size(); ++j) { for(unsigned int j = 0; j < tokens.size(); ++j) {
IRCThread::eventData.push_back(tokens[j]); nickList.push_back(tokens[j]);
} }
} }
} }
IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
if(ctx != NULL) {
MutexSafeWrapper safeMutex(ctx->getMutexNickList());
ctx->setCachedNickList(nickList);
}
} }
break; break;
} }
case LIBIRC_RFC_RPL_ENDOFNAMES: case LIBIRC_RFC_RPL_ENDOFNAMES:
IRCThread::eventDataDone = true; {
IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
if(ctx != NULL) {
ctx->setEventDataDone(true);
}
}
break; break;
} }
@@ -309,9 +346,10 @@ IRCThread::IRCThread(const std::vector<string> &argv, IRCCallbackInterface *call
this->argv = argv; this->argv = argv;
this->callbackObj = callbackObj; this->callbackObj = callbackObj;
ircSession = NULL; ircSession = NULL;
IRCThread::eventData.clear(); eventData.clear();
IRCThread::eventDataDone = false; eventDataDone = false;
isConnected = false; hasJoinedChannel = false;
lastNickListUpdate = time(NULL);
} }
void IRCThread::signalQuit() { void IRCThread::signalQuit() {
@@ -322,7 +360,7 @@ void IRCThread::signalQuit() {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: Quitting Channel\n"); if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: Quitting Channel\n");
irc_cmd_quit(ircSession, "MG Bot is closing!"); irc_cmd_quit(ircSession, "MG Bot is closing!");
BaseThread::signalQuit(); BaseThread::signalQuit();
isConnected = false; hasJoinedChannel = false;
} }
} }
@@ -334,130 +372,148 @@ bool IRCThread::shutdownAndWait() {
} }
void IRCThread::SendIRCCmdMessage(string target, string msg) { void IRCThread::SendIRCCmdMessage(string target, string msg) {
if(ircSession != NULL && isConnected == true) { if(ircSession != NULL && hasJoinedChannel == true) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC command to [%s] cmd [%s]\n",__FILE__,__FUNCTION__,__LINE__,target.c_str(),msg.c_str()); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC command to [%s] cmd [%s]\n",__FILE__,__FUNCTION__,__LINE__,target.c_str(),msg.c_str());
int ret = irc_cmd_msg (ircSession, target.c_str(), msg.c_str()); int ret = irc_cmd_msg (ircSession, target.c_str(), msg.c_str());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC command to [%s] cmd [%s] ret = %d\n",__FILE__,__FUNCTION__,__LINE__,target.c_str(),msg.c_str(),ret); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC command to [%s] cmd [%s] ret = %d\n",__FILE__,__FUNCTION__,__LINE__,target.c_str(),msg.c_str(),ret);
} }
} }
std::vector<string> IRCThread::GetIRCConnectedNickList(string target) { std::vector<string> IRCThread::GetIRCConnectedNickList(string target, bool waitForCompletion) {
IRCThread::eventDataDone = false; eventDataDone = false;
//IRCThread::eventData.clear(); if(ircSession != NULL && hasJoinedChannel == true) {
if(ircSession != NULL && isConnected == true) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC nick list command to [%s]\n",__FILE__,__FUNCTION__,__LINE__,target.c_str()); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC nick list command to [%s]\n",__FILE__,__FUNCTION__,__LINE__,target.c_str());
int ret = irc_cmd_names (ircSession, target.c_str()); int ret = irc_cmd_names (ircSession, target.c_str());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC nick list command to [%s] ret = %d\n",__FILE__,__FUNCTION__,__LINE__,target.c_str(),ret); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending IRC nick list command to [%s] ret = %d\n",__FILE__,__FUNCTION__,__LINE__,target.c_str(),ret);
for(time_t tElapsed = time(NULL); if(waitForCompletion == true) {
IRCThread::eventDataDone == false && for(time_t tElapsed = time(NULL);
this->getQuitStatus() == false && eventDataDone == false &&
difftime(time(NULL),tElapsed) <= 5;) { this->getQuitStatus() == false &&
sleep(50); difftime(time(NULL),tElapsed) <= 5;) {
sleep(50);
}
} }
} }
return IRCThread::eventData; MutexSafeWrapper safeMutex(&mutexNickList);
std::vector<string> nickList = eventData;
safeMutex.ReleaseLock();
return nickList;
}
bool IRCThread::isConnected() {
bool ret = false;
if(ircSession != NULL) {
ret = irc_is_connected(ircSession);
}
return ret;
}
std::vector<string> IRCThread::getNickList() {
MutexSafeWrapper safeMutex(&mutexNickList);
std::vector<string> nickList = eventData;
safeMutex.ReleaseLock();
return nickList;
} }
void IRCThread::execute() { void IRCThread::execute() {
{ {
RunningStatusSafeWrapper runningStatus(this); RunningStatusSafeWrapper runningStatus(this);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] argv.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,argv.size()); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] argv.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,argv.size());
if(getQuitStatus() == true) { if(getQuitStatus() == true) {
return;
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"IRC thread is running\n");
try {
irc_callbacks_t callbacks;
irc_ctx_t ctx;
ircSession=NULL;
if(argv.size() != 3) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Usage: <server> <nick> <channel> : got params [%ld]\n",argv.size());
return; return;
} }
memset (&callbacks, 0, sizeof(callbacks)); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"IRC thread is running\n");
callbacks.event_connect = event_connect; try {
callbacks.event_join = event_join; irc_callbacks_t callbacks;
callbacks.event_nick = dump_event; ircSession=NULL;
callbacks.event_quit = dump_event;
callbacks.event_part = event_leave;
callbacks.event_mode = dump_event;
callbacks.event_topic = dump_event;
callbacks.event_kick = dump_event;
callbacks.event_channel = event_channel;
callbacks.event_privmsg = event_privmsg;
callbacks.event_notice = dump_event;
callbacks.event_invite = dump_event;
callbacks.event_umode = dump_event;
callbacks.event_ctcp_rep = dump_event;
callbacks.event_ctcp_action = dump_event;
callbacks.event_unknown = dump_event;
callbacks.event_numeric = event_numeric;
callbacks.event_dcc_chat_req = irc_event_dcc_chat; if(argv.size() != 3) {
callbacks.event_dcc_send_req = irc_event_dcc_send; if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Usage: <server> <nick> <channel> : got params [%ld]\n",argv.size());
return;
if(this->getQuitStatus() == true) {
return;
}
ircSession = irc_create_session (&callbacks);
if(!ircSession) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Could not create session\n");
return;
}
ctx.channel = argv[2];
ctx.nick = argv[1];
if(this->getQuitStatus() == true) {
return;
}
irc_set_ctx(ircSession, &ctx);
if(irc_connect(ircSession, argv[0].c_str(), 6667, 0, argv[1].c_str(), 0, 0)) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Could not connect: %s\n", irc_strerror (irc_errno(ircSession)));
return;
}
if(this->getQuitStatus() == true) {
return;
}
GetIRCConnectedNickList(argv[2]);
if(this->getQuitStatus() == true) {
return;
}
for(int iAttempts=1;
this->getQuitStatus() == false && iAttempts <= 5;
++iAttempts) {
if(irc_run(ircSession)) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Could not run the session: %s\n", irc_strerror (irc_errno(ircSession)));
} }
memset (&callbacks, 0, sizeof(callbacks));
callbacks.event_connect = event_connect;
callbacks.event_join = event_join;
callbacks.event_nick = dump_event;
callbacks.event_quit = dump_event;
callbacks.event_part = event_leave;
callbacks.event_mode = dump_event;
callbacks.event_topic = dump_event;
callbacks.event_kick = dump_event;
callbacks.event_channel = event_channel;
callbacks.event_privmsg = event_privmsg;
callbacks.event_notice = dump_event;
callbacks.event_invite = dump_event;
callbacks.event_umode = dump_event;
callbacks.event_ctcp_rep = dump_event;
callbacks.event_ctcp_action = dump_event;
callbacks.event_unknown = dump_event;
callbacks.event_numeric = event_numeric;
callbacks.event_dcc_chat_req = irc_event_dcc_chat;
callbacks.event_dcc_send_req = irc_event_dcc_send;
if(this->getQuitStatus() == true) {
return;
}
ircSession = irc_create_session (&callbacks);
if(!ircSession) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Could not create session\n");
return;
}
this->channel = argv[2];
this->nick = argv[1];
irc_set_ctx(ircSession, this);
if(this->getQuitStatus() == true) {
return;
}
if(irc_connect(ircSession, argv[0].c_str(), 6667, 0, argv[1].c_str(), 0, 0)) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Could not connect: %s\n", irc_strerror (irc_errno(ircSession)));
return;
}
if(this->getQuitStatus() == true) {
return;
}
if(this->getQuitStatus() == true) {
return;
}
for(int iAttempts=1;
this->getQuitStatus() == false && iAttempts <= 5;
++iAttempts) {
if(irc_run(ircSession)) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Could not run the session: %s\n", irc_strerror (irc_errno(ircSession)));
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC exiting IRC CLient!\n");
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
}
catch(...) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",__FILE__,__FUNCTION__,__LINE__);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] unknown error\n",__FILE__,__FUNCTION__,__LINE__);
} }
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC exiting IRC CLient!\n"); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] IRC thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
}
catch(...) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",__FILE__,__FUNCTION__,__LINE__);
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] unknown error\n",__FILE__,__FUNCTION__,__LINE__);
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] IRC thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
} }
// Delete ourself when the thread is done (no other actions can happen after this // Delete ourself when the thread is done (no other actions can happen after this