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:
parent
b954d399df
commit
97ee61165a
|
@ -40,16 +40,17 @@ ChatManager::ChatManager() {
|
|||
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->thisTeamIndex= thisTeamIndex;
|
||||
this->disableTeamMode= false;
|
||||
this->inMenu=inMenu;
|
||||
this->manualPlayerNameOverride = manualPlayerNameOverride;
|
||||
}
|
||||
|
||||
void ChatManager::keyUp(char key) {
|
||||
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
|
||||
|
||||
try {
|
||||
if(editEnabled) {
|
||||
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) {
|
||||
string playerName = gameNetworkInterface->getHumanPlayerName();
|
||||
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);
|
||||
if(inMenu == false) {
|
||||
|
@ -128,7 +135,7 @@ void ChatManager::keyDown(char key) {
|
|||
}
|
||||
else {
|
||||
editEnabled= false;
|
||||
}
|
||||
}
|
||||
text.clear();
|
||||
}
|
||||
else {
|
||||
|
@ -146,7 +153,7 @@ void ChatManager::keyDown(char key) {
|
|||
text.erase(text.end() -1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch(const exception &ex) {
|
||||
char szBuf[1024]="";
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
//
|
||||
// Copyright (C) 2001-2008 Marti<74>o Figueroa
|
||||
//
|
||||
// You can redistribute this code 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
|
||||
// You can redistribute this code 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
|
||||
// ==============================================================
|
||||
|
||||
|
@ -37,11 +37,11 @@ private:
|
|||
string text;
|
||||
int thisTeamIndex;
|
||||
bool inMenu;
|
||||
|
||||
string manualPlayerNameOverride;
|
||||
|
||||
public:
|
||||
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 keyUp(char key);
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
//
|
||||
// Copyright (C) 2001-2008 Martio Figueroa
|
||||
//
|
||||
// You can redistribute this code 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
|
||||
// You can redistribute this code 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
|
||||
// ==============================================================
|
||||
|
||||
|
@ -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() {
|
||||
while(storedLines.empty() == false) {
|
||||
storedLines.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Console::update() {
|
||||
timeElapsed += 1.f / GameConstants::updateFps;
|
||||
|
||||
|
||||
if(lines.empty() == false) {
|
||||
if(lines.back().timeStamp < (timeElapsed - timeout)) {
|
||||
lines.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Console::isEmpty() {
|
||||
return lines.empty();
|
||||
return lines.empty();
|
||||
}
|
||||
|
||||
string Console::getLine(int i) const {
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
//
|
||||
// Copyright (C) 2001-2008 Martio Figueroa
|
||||
//
|
||||
// You can redistribute this code 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
|
||||
// You can redistribute this code 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
|
||||
// ==============================================================
|
||||
|
||||
|
@ -49,7 +49,7 @@ public:
|
|||
typedef Lines::const_iterator LineIterator;
|
||||
|
||||
private:
|
||||
float timeElapsed;
|
||||
float timeElapsed;
|
||||
Lines lines;
|
||||
Lines storedLines;
|
||||
|
||||
|
@ -60,7 +60,7 @@ private:
|
|||
|
||||
public:
|
||||
Console();
|
||||
|
||||
|
||||
int getStoredLineCount() const {return storedLines.size();}
|
||||
int getLineCount() const {return lines.size();}
|
||||
string getLine(int i) const;
|
||||
|
@ -71,6 +71,7 @@ public:
|
|||
void clearStoredLines();
|
||||
void addStdMessage(const string &s);
|
||||
void addLine(string line, bool playSound= false,int playerIndex=-1);
|
||||
void addLine(string line, bool playSound,string playerName);
|
||||
void update();
|
||||
bool isEmpty();
|
||||
};
|
||||
|
|
|
@ -189,7 +189,7 @@ Renderer::Renderer() {
|
|||
particleManager[i]= graphicsFactory->newParticleManager();
|
||||
fontManager[i]= graphicsFactory->newFontManager();
|
||||
}
|
||||
|
||||
|
||||
//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
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 {
|
||||
fontColor = defaultFontColor;
|
||||
}
|
||||
|
@ -2320,27 +2342,27 @@ void Renderer::renderMinimap(){
|
|||
glVertex2f(attackX, attackY);
|
||||
glEnd();
|
||||
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);
|
||||
glColor4f(1.f, 1.f, 0.f, 0.8f);
|
||||
glVertex2f(attackX, attackY);
|
||||
glEnd();
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor4f(1.f, 1.f, 0.f, alpha);
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor4f(1.f, 1.f, 0.f, alpha);
|
||||
glVertex2f(attackX+scale, attackY+scale);
|
||||
glVertex2f(attackX+scale, attackY-scale);
|
||||
glColor4f(1.f, 1.f, 0.f, 0.8f);
|
||||
glVertex2f(attackX, attackY);
|
||||
glEnd();
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor4f(1.f, 1.f, 0.f, alpha);
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor4f(1.f, 1.f, 0.f, alpha);
|
||||
glVertex2f(attackX+scale, attackY-scale);
|
||||
glVertex2f(attackX-scale, attackY-scale);
|
||||
glColor4f(1.f, 1.f, 0.f, 0.8f);
|
||||
glVertex2f(attackX, attackY);
|
||||
glEnd();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
glDisable(GL_BLEND);
|
||||
|
@ -2436,7 +2458,7 @@ void Renderer::renderMinimap(){
|
|||
glVertex2i(x2,y2);
|
||||
|
||||
glEnd();
|
||||
|
||||
|
||||
glPopAttrib();
|
||||
|
||||
assertGl();
|
||||
|
|
|
@ -196,7 +196,6 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen
|
|||
containerName = "MasterServer";
|
||||
updateFromMasterserverThread = NULL;
|
||||
ircClient = NULL;
|
||||
lastNickListUpdate = 0;
|
||||
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
|
||||
|
||||
Lang &lang= Lang::getInstance();
|
||||
|
@ -352,8 +351,13 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen
|
|||
|
||||
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")+"\"");
|
||||
chatManager.init(&consoleIRC, -1,true);
|
||||
chatManager.init(&consoleIRC, -1, true, szIRCNick);
|
||||
|
||||
MutexSafeWrapper safeMutexPtr(&masterServerThreadPtrChangeAccessor);
|
||||
masterServerThreadInDeletion = false;
|
||||
|
@ -362,11 +366,6 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen
|
|||
updateFromMasterserverThread->setUniqueID(__FILE__);
|
||||
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(szIRCNick);
|
||||
ircArgs.push_back(IRC_CHANNEL);
|
||||
|
@ -626,11 +625,18 @@ void MenuStateMasterserver::render(){
|
|||
renderer.renderLabel(&externalConnectPort,&titleLabelColor);
|
||||
renderer.renderLabel(&selectButton,&titleLabelColor);
|
||||
|
||||
renderer.renderLabel(&ircOnlinePeopleLabel,&titleLabelColor);
|
||||
renderer.renderLabel(&ircOnlinePeopleListLabel,&titleLabelColor);
|
||||
|
||||
// render console
|
||||
//renderer.renderConsole(&console,false,false);
|
||||
if(ircClient != NULL &&
|
||||
ircClient->isConnected() == true &&
|
||||
ircClient->getHasJoinedChannel() == true) {
|
||||
const Vec4f titleLabelColor = GREEN;
|
||||
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){
|
||||
serverLines[i]->render();
|
||||
|
@ -669,18 +675,15 @@ void MenuStateMasterserver::update() {
|
|||
consoleIRC.update();
|
||||
|
||||
if(ircClient != NULL) {
|
||||
if(difftime(time(NULL),lastNickListUpdate) >= 5) {
|
||||
lastNickListUpdate = time(NULL);
|
||||
std::vector<string> nickList = ircClient->getNickList();
|
||||
string nicks = "";
|
||||
for(int i = 0; i < nickList.size(); ++i) {
|
||||
if(nicks != "") {
|
||||
nicks += " ";
|
||||
}
|
||||
nicks += nickList[i];
|
||||
std::vector<string> nickList = ircClient->getNickList();
|
||||
string nicks = "";
|
||||
for(int i = 0; i < nickList.size(); ++i) {
|
||||
if(nicks != "") {
|
||||
nicks += ", ";
|
||||
}
|
||||
ircOnlinePeopleListLabel.setText(nicks);
|
||||
nicks += nickList[i];
|
||||
}
|
||||
ircOnlinePeopleListLabel.setText(nicks);
|
||||
}
|
||||
else {
|
||||
ircOnlinePeopleListLabel.setText("");
|
||||
|
|
|
@ -132,7 +132,6 @@ private:
|
|||
|
||||
std::vector<string> ircArgs;
|
||||
IRCThread *ircClient;
|
||||
time_t lastNickListUpdate;
|
||||
|
||||
Console consoleIRC;
|
||||
ChatManager chatManager;
|
||||
|
|
|
@ -42,20 +42,49 @@ protected:
|
|||
std::vector<string> argv;
|
||||
irc_session_t *ircSession;
|
||||
|
||||
string channel;
|
||||
string nick;
|
||||
|
||||
bool hasJoinedChannel;
|
||||
bool eventDataDone;
|
||||
Mutex mutexNickList;
|
||||
time_t lastNickListUpdate;
|
||||
std::vector<string> eventData;
|
||||
|
||||
IRCCallbackInterface *callbackObj;
|
||||
|
||||
public:
|
||||
|
||||
IRCThread(const std::vector<string> &argv,IRCCallbackInterface *callbackObj);
|
||||
virtual void execute();
|
||||
virtual void signalQuit();
|
||||
virtual bool shutdownAndWait();
|
||||
|
||||
void SendIRCCmdMessage(string target, string msg);
|
||||
std::vector<string> GetIRCConnectedNickList(string target);
|
||||
std::vector<string> getNickList() { return eventData; }
|
||||
std::vector<string> getNickList();
|
||||
bool isConnected();
|
||||
|
||||
static IRCCallbackInterface *callbackObj;
|
||||
static std::vector<string> eventData;
|
||||
static bool eventDataDone;
|
||||
static bool isConnected;
|
||||
std::vector<string> GetIRCConnectedNickList(string target, bool waitForCompletion);
|
||||
|
||||
bool getEventDataDone() const { return eventDataDone; }
|
||||
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
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
|
||||
#ifndef WIN32
|
||||
#ifndef _LIBIRCCLIENTLOCAL
|
||||
#include <libircclient/libircclient.h>
|
||||
#include <libircclient/libircclient.h>
|
||||
#else
|
||||
#include "libircclient.h"
|
||||
#include "libircclient.h"
|
||||
#endif
|
||||
#else
|
||||
#include "libircclient.h"
|
||||
|
@ -34,18 +34,6 @@ using namespace Shared::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, ...) {
|
||||
FILE * fp;
|
||||
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);
|
||||
|
||||
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) {
|
||||
dump_event (session, event, origin, params, count);
|
||||
|
||||
if(IRCThread::isConnected == false) {
|
||||
irc_cmd_user_mode (session, "+i");
|
||||
irc_cmd_msg (session, params[0], "MG Bot says hello!");
|
||||
}
|
||||
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);
|
||||
IRCThread::eventData.push_back(realNick);
|
||||
}
|
||||
IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
|
||||
if(ctx != NULL) {
|
||||
if(ctx->getHasJoinedChannel() == false) {
|
||||
irc_cmd_user_mode (session, "+i");
|
||||
irc_cmd_msg (session, params[0], "MG Bot says hello!");
|
||||
ctx->setHasJoinedChannel(true);
|
||||
|
||||
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) {
|
||||
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);
|
||||
|
||||
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) {
|
||||
|
@ -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));
|
||||
|
||||
if(IRCThread::callbackObj) {
|
||||
IRCThread::callbackObj->IRC_CallbackEvent(nickbuf, params, count);
|
||||
}
|
||||
IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
|
||||
if(ctx != NULL) {
|
||||
if(ctx->getCallbackObj() != NULL) {
|
||||
ctx->getCallbackObj()->IRC_CallbackEvent(nickbuf, params, count);
|
||||
}
|
||||
}
|
||||
|
||||
if ( !strcmp (params[1], "quit") )
|
||||
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);
|
||||
|
||||
// someone left the channel.
|
||||
|
||||
if(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);
|
||||
|
||||
for(unsigned int i = 0; i < IRCThread::eventData.size(); ++i) {
|
||||
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: lookingfor match [%s] realNick [%s]\n", IRCThread::eventData[i].c_str(),realNick);
|
||||
IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
|
||||
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) {
|
||||
IRCThread::eventData.erase(IRCThread::eventData.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(nickList[i] == realNick) {
|
||||
nickList.erase(nickList.begin() + i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 :
|
||||
{
|
||||
if(event == LIBIRC_RFC_RPL_NAMREPLY) {
|
||||
IRCThread::eventData.clear();
|
||||
std::vector<string> nickList;
|
||||
if(count >= 4) {
|
||||
for(unsigned int i = 3; i < count && params[i]; ++i) {
|
||||
|
||||
vector<string> tokens;
|
||||
Tokenize(params[i],tokens," ");
|
||||
|
||||
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;
|
||||
}
|
||||
case LIBIRC_RFC_RPL_ENDOFNAMES:
|
||||
IRCThread::eventDataDone = true;
|
||||
{
|
||||
IRCThread *ctx = (IRCThread *)irc_get_ctx(session);
|
||||
if(ctx != NULL) {
|
||||
ctx->setEventDataDone(true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -309,9 +346,10 @@ IRCThread::IRCThread(const std::vector<string> &argv, IRCCallbackInterface *call
|
|||
this->argv = argv;
|
||||
this->callbackObj = callbackObj;
|
||||
ircSession = NULL;
|
||||
IRCThread::eventData.clear();
|
||||
IRCThread::eventDataDone = false;
|
||||
isConnected = false;
|
||||
eventData.clear();
|
||||
eventDataDone = false;
|
||||
hasJoinedChannel = false;
|
||||
lastNickListUpdate = time(NULL);
|
||||
}
|
||||
|
||||
void IRCThread::signalQuit() {
|
||||
|
@ -322,7 +360,7 @@ void IRCThread::signalQuit() {
|
|||
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC: Quitting Channel\n");
|
||||
irc_cmd_quit(ircSession, "MG Bot is closing!");
|
||||
BaseThread::signalQuit();
|
||||
isConnected = false;
|
||||
hasJoinedChannel = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -334,130 +372,148 @@ bool IRCThread::shutdownAndWait() {
|
|||
}
|
||||
|
||||
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());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<string> IRCThread::GetIRCConnectedNickList(string target) {
|
||||
IRCThread::eventDataDone = false;
|
||||
//IRCThread::eventData.clear();
|
||||
if(ircSession != NULL && isConnected == true) {
|
||||
std::vector<string> IRCThread::GetIRCConnectedNickList(string target, bool waitForCompletion) {
|
||||
eventDataDone = false;
|
||||
if(ircSession != NULL && hasJoinedChannel == true) {
|
||||
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());
|
||||
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);
|
||||
IRCThread::eventDataDone == false &&
|
||||
this->getQuitStatus() == false &&
|
||||
difftime(time(NULL),tElapsed) <= 5;) {
|
||||
sleep(50);
|
||||
if(waitForCompletion == true) {
|
||||
for(time_t tElapsed = time(NULL);
|
||||
eventDataDone == false &&
|
||||
this->getQuitStatus() == false &&
|
||||
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() {
|
||||
{
|
||||
RunningStatusSafeWrapper runningStatus(this);
|
||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] argv.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,argv.size());
|
||||
RunningStatusSafeWrapper runningStatus(this);
|
||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] argv.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,argv.size());
|
||||
|
||||
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());
|
||||
if(getQuitStatus() == true) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset (&callbacks, 0, sizeof(callbacks));
|
||||
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"IRC thread is running\n");
|
||||
|
||||
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;
|
||||
try {
|
||||
irc_callbacks_t callbacks;
|
||||
ircSession=NULL;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)));
|
||||
if(argv.size() != 3) {
|
||||
if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> IRC Usage: <server> <nick> <channel> : got params [%ld]\n",argv.size());
|
||||
return;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
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__);
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue