2007-09-04 15:05:02 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* Copyright (C) 2007 by Lothar May *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it 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. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <net/ircthread.h>
|
|
|
|
|
|
|
|
|
|
#include <libircclient.h>
|
2007-09-27 20:59:00 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <cctype>
|
2007-09-04 15:05:02 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
struct IrcContext
|
|
|
|
|
{
|
2007-09-25 21:26:22 +00:00
|
|
|
IrcContext(IrcThread &t) : ircThread(t), session(NULL), serverPort(0) {}
|
|
|
|
|
IrcThread &ircThread;
|
|
|
|
|
irc_session_t *session;
|
2007-09-04 15:05:02 +00:00
|
|
|
string serverAddress;
|
|
|
|
|
unsigned serverPort;
|
|
|
|
|
string nick;
|
|
|
|
|
string channel;
|
|
|
|
|
};
|
|
|
|
|
|
2007-09-27 20:59:00 +00:00
|
|
|
void irc_auto_rename_nick(irc_session_t *session)
|
2007-09-04 15:05:02 +00:00
|
|
|
{
|
2007-09-26 11:48:33 +00:00
|
|
|
IrcContext *context = (IrcContext *) irc_get_ctx(session);
|
2007-09-04 15:05:02 +00:00
|
|
|
|
2007-09-27 20:59:00 +00:00
|
|
|
// Automatically rename the nick on collision.
|
|
|
|
|
// First: Try to append the string "Lobby".
|
|
|
|
|
if (context->nick.find("|Lobby") == string::npos)
|
|
|
|
|
context->nick = context->nick + "|Lobby";
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// This didn't work out. Append a number or increment it.
|
|
|
|
|
string::reverse_iterator end = context->nick.rbegin();
|
|
|
|
|
if (!context->nick.empty() && isdigit(*end))
|
|
|
|
|
{
|
|
|
|
|
if (*end != '9')
|
|
|
|
|
*end = (*end) + 1;
|
|
|
|
|
else
|
|
|
|
|
context->nick = context->nick + "0";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
context->nick = context->nick + "1";
|
|
|
|
|
}
|
|
|
|
|
irc_cmd_nick(session, context->nick.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void irc_notify_player_list(irc_session_t *session, const char *players)
|
|
|
|
|
{
|
|
|
|
|
IrcContext *context = (IrcContext *) irc_get_ctx(session);
|
|
|
|
|
|
|
|
|
|
istringstream input(players);
|
|
|
|
|
string name;
|
|
|
|
|
input >> name;
|
|
|
|
|
while (!input.fail() && !input.eof())
|
|
|
|
|
{
|
2007-09-29 14:05:41 +00:00
|
|
|
context->ircThread.GetCallback().SignalIrcPlayerJoined(name);
|
2007-09-27 20:59:00 +00:00
|
|
|
input >> name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
irc_event_connect(irc_session_t *session, const char *irc_event, const char *origin, const char **params, unsigned count)
|
|
|
|
|
{
|
|
|
|
|
IrcContext *context = (IrcContext *) irc_get_ctx(session);
|
|
|
|
|
|
|
|
|
|
context->ircThread.GetCallback().SignalIrcConnect(origin);
|
2007-09-26 11:48:33 +00:00
|
|
|
irc_cmd_join(session, context->channel.c_str(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2007-09-27 20:59:00 +00:00
|
|
|
irc_event_join(irc_session_t *session, const char *irc_event, const char *origin, const char **params, unsigned count)
|
|
|
|
|
{
|
|
|
|
|
// someone joined the channel.
|
|
|
|
|
IrcContext *context = (IrcContext *) irc_get_ctx(session);
|
|
|
|
|
|
|
|
|
|
if (context->nick == origin)
|
|
|
|
|
context->ircThread.GetCallback().SignalIrcSelfJoined(context->nick, context->channel);
|
|
|
|
|
else
|
|
|
|
|
context->ircThread.GetCallback().SignalIrcPlayerJoined(origin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
irc_event_leave(irc_session_t *session, const char *irc_event, const char *origin, const char **params, unsigned count)
|
|
|
|
|
{
|
|
|
|
|
// someone left the channel.
|
|
|
|
|
IrcContext *context = (IrcContext *) irc_get_ctx(session);
|
|
|
|
|
|
|
|
|
|
context->ircThread.GetCallback().SignalIrcPlayerLeft(origin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
irc_event_channel(irc_session_t *session, const char *irc_event, const char *origin, const char **params, unsigned count)
|
2007-09-26 11:48:33 +00:00
|
|
|
{
|
|
|
|
|
IrcContext *context = (IrcContext *) irc_get_ctx(session);
|
|
|
|
|
|
|
|
|
|
if (context->channel == params[0]) // check whether this message is for our channel
|
|
|
|
|
{
|
|
|
|
|
// Signal the message (if any) to GUI.
|
|
|
|
|
if (params[1] && *params[1] != 0)
|
|
|
|
|
context->ircThread.GetCallback().SignalIrcChatMsg(origin, params[1]);
|
|
|
|
|
}
|
2007-09-04 15:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
2007-09-27 20:59:00 +00:00
|
|
|
void
|
|
|
|
|
irc_event_numeric(irc_session_t * session, unsigned irc_event, const char *origin, const char **params, unsigned count)
|
|
|
|
|
{
|
|
|
|
|
switch (irc_event)
|
|
|
|
|
{
|
|
|
|
|
case LIBIRC_RFC_ERR_NICKNAMEINUSE :
|
|
|
|
|
case LIBIRC_RFC_ERR_NICKCOLLISION :
|
|
|
|
|
irc_auto_rename_nick(session);
|
|
|
|
|
break;
|
|
|
|
|
case LIBIRC_RFC_RPL_TOPIC :
|
|
|
|
|
break;
|
|
|
|
|
case LIBIRC_RFC_RPL_NAMREPLY :
|
|
|
|
|
if (count >= 4)
|
|
|
|
|
irc_notify_player_list(session, params[3]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-09-04 15:05:02 +00:00
|
|
|
|
2007-09-25 21:26:22 +00:00
|
|
|
IrcThread::IrcThread(IrcCallback &callback)
|
|
|
|
|
: m_callback(callback)
|
2007-09-04 15:05:02 +00:00
|
|
|
{
|
2007-09-25 21:26:22 +00:00
|
|
|
m_context.reset(new IrcContext(*this));
|
2007-09-04 15:05:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IrcThread::~IrcThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
IrcThread::Init(const std::string &serverAddress, unsigned serverPort, bool ipv6, const std::string &nick, const std::string &channel)
|
|
|
|
|
{
|
2007-09-27 20:59:00 +00:00
|
|
|
if (IsRunning() || serverAddress.empty() || nick.empty() || channel.empty())
|
2007-09-04 15:05:02 +00:00
|
|
|
return; // TODO: throw exception
|
|
|
|
|
|
|
|
|
|
IrcContext &context = GetContext();
|
|
|
|
|
|
|
|
|
|
context.serverAddress = serverAddress;
|
|
|
|
|
context.serverPort = serverPort;
|
|
|
|
|
context.nick = nick;
|
|
|
|
|
context.channel = channel;
|
|
|
|
|
|
2007-09-25 21:26:22 +00:00
|
|
|
// Initialize libirc stuff.
|
2007-09-04 15:05:02 +00:00
|
|
|
irc_callbacks_t callbacks;
|
|
|
|
|
memset (&callbacks, 0, sizeof(callbacks));
|
|
|
|
|
|
2007-09-27 20:59:00 +00:00
|
|
|
callbacks.event_connect = irc_event_connect;
|
|
|
|
|
callbacks.event_join = irc_event_join;
|
2007-09-04 15:05:02 +00:00
|
|
|
//callbacks.event_nick
|
2007-09-27 20:59:00 +00:00
|
|
|
callbacks.event_quit = irc_event_leave;
|
|
|
|
|
callbacks.event_part = irc_event_leave;
|
2007-09-04 15:05:02 +00:00
|
|
|
//callbacks.event_mode
|
|
|
|
|
//callbacks.event_topic
|
|
|
|
|
//callbacks.event_kick
|
2007-09-27 20:59:00 +00:00
|
|
|
callbacks.event_channel = irc_event_channel;
|
2007-09-04 15:05:02 +00:00
|
|
|
//callbacks.event_privmsg
|
|
|
|
|
//callbacks.event_notice
|
|
|
|
|
//callbacks.event_invite
|
|
|
|
|
//callbacks.event_umode
|
|
|
|
|
//callbacks.event_ctcp_rep
|
|
|
|
|
//callbacks.event_ctcp_action
|
|
|
|
|
//callbacks.event_unknown
|
2007-09-27 20:59:00 +00:00
|
|
|
callbacks.event_numeric = irc_event_numeric;
|
2007-09-04 15:05:02 +00:00
|
|
|
|
|
|
|
|
//callbacks.event_dcc_chat_req
|
|
|
|
|
//callbacks.event_dcc_send_req
|
|
|
|
|
|
2007-09-25 21:26:22 +00:00
|
|
|
context.session = irc_create_session(&callbacks);
|
2007-09-04 15:05:02 +00:00
|
|
|
|
2007-09-25 21:26:22 +00:00
|
|
|
if (context.session)
|
|
|
|
|
{
|
|
|
|
|
irc_set_ctx(context.session, &context);
|
|
|
|
|
// We want nicknames only, strip them from nick!host.
|
|
|
|
|
irc_option_set(context.session, LIBIRC_OPTION_STRIPNICKS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 11:48:33 +00:00
|
|
|
void
|
|
|
|
|
IrcThread::SendChatMessage(const std::string &msg)
|
|
|
|
|
{
|
|
|
|
|
IrcContext &context = GetContext();
|
|
|
|
|
irc_cmd_msg(context.session, context.channel.c_str(), msg.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-25 21:26:22 +00:00
|
|
|
void
|
|
|
|
|
IrcThread::SignalTermination()
|
|
|
|
|
{
|
|
|
|
|
Thread::SignalTermination();
|
|
|
|
|
irc_cmd_quit(GetContext().session, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
IrcThread::Main()
|
|
|
|
|
{
|
|
|
|
|
IrcContext &context = GetContext();
|
|
|
|
|
irc_session_t *s = context.session;
|
2007-09-04 15:05:02 +00:00
|
|
|
if (s)
|
|
|
|
|
{
|
2007-09-25 21:26:22 +00:00
|
|
|
if (irc_connect(s, context.serverAddress.c_str(), context.serverPort, 0, context.nick.c_str(), 0, 0) == 0)
|
|
|
|
|
irc_run(s);
|
2007-09-04 15:05:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IrcContext &
|
|
|
|
|
IrcThread::GetContext() const
|
|
|
|
|
{
|
|
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IrcContext &
|
|
|
|
|
IrcThread::GetContext()
|
|
|
|
|
{
|
|
|
|
|
assert(m_context.get());
|
|
|
|
|
return *m_context;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-25 21:26:22 +00:00
|
|
|
IrcCallback &
|
|
|
|
|
IrcThread::GetCallback()
|
|
|
|
|
{
|
|
|
|
|
return m_callback;
|
|
|
|
|
}
|
|
|
|
|
|