📄 irc_connection.cpp
字号:
/* $Id: irc_connection.cpp,v 1.9 2003/12/31 15:47:27 mbn Exp $
**
** ClanLib Game SDK
** Copyright (C) 2003 The ClanLib Team
** For a total list of contributers see the file CREDITS.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library 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
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "API/Network/IRC/irc_connection.h"
#include "irc_connection_generic.h"
/////////////////////////////////////////////////////////////////////////////
// CL_IRCConnection construction:
CL_IRCConnection::CL_IRCConnection() : impl(0)
{
}
CL_IRCConnection::CL_IRCConnection(const std::string &server, const std::string &port) :
impl(new CL_IRCConnection_Generic(server, port))
{
impl->add_ref();
}
CL_IRCConnection::CL_IRCConnection(const CL_IRCConnection ©) : impl(copy.impl)
{
if (impl) impl->add_ref();
}
CL_IRCConnection::~CL_IRCConnection()
{
if (impl) impl->release_ref();
}
/////////////////////////////////////////////////////////////////////////////
// CL_IRCConnection attributes:
const std::string &CL_IRCConnection::get_nick() const
{
return impl->nick;
}
const std::string &CL_IRCConnection::get_username() const
{
return impl->username;
}
const std::string &CL_IRCConnection::get_hostname() const
{
return impl->hostname;
}
const std::string &CL_IRCConnection::get_servername() const
{
return impl->servername;
}
const std::string &CL_IRCConnection::get_realname() const
{
return impl->realname;
}
/////////////////////////////////////////////////////////////////////////////
// CL_IRCConnection operations:
CL_IRCConnection &CL_IRCConnection::operator =(const CL_IRCConnection ©)
{
if (impl) impl->release_ref();
impl = copy.impl;
if (impl) impl->add_ref();
return *this;
}
void CL_IRCConnection::send_command(const std::string &prefix, const std::string &command, const std::vector<std::string> ¶ms)
{
std::string line;
if (!prefix.empty())
{
line.append(":");
line.append(prefix);
line.append(" ");
}
line.append(command);
int size = params.size();
for (int i=0; i<size;i++)
{
line.append(" ");
if (i == size-1) line.append(":");
line.append(params[i]);
}
// Add low level escapes:
std::string::size_type pos;
pos = 0;
while (pos != std::string::npos)
{
pos = line.find('\020', pos);
if (pos == std::string::npos) break;
line.replace( pos, 1, "\020\020" );
pos++;
}
pos = 0;
while (pos != std::string::npos)
{
pos = line.find('\0', pos);
if (pos == std::string::npos) break;
line.replace( pos, 1, "\020""0" );
pos++;
}
pos = 0;
while (pos != std::string::npos)
{
pos = line.find('\r', pos);
if (pos == std::string::npos) break;
line.replace( pos, 1, "\020r" );
pos++;
}
pos = 0;
while (pos != std::string::npos)
{
pos = line.find('\n', pos);
if (pos == std::string::npos) break;
line.replace( pos, 1, "\020n" );
pos++;
}
line.append("\r\n");
impl->sock.send(line);
}
void CL_IRCConnection::send_pass(const std::string &password)
{
std::vector<std::string> params;
params.push_back(password);
send_command("", "PASS", params);
}
void CL_IRCConnection::send_nick(const std::string &nick, int hopcount)
{
std::vector<std::string> params;
params.push_back(nick);
// if (hopcount > 0) params.push_back(CL_String::from_int(hopcount));
send_command("", "NICK", params);
impl->nick = nick;
}
void CL_IRCConnection::send_user(const std::string &username, const std::string &hostname, const std::string &servername, const std::string &realname)
{
std::vector<std::string> params;
params.push_back(username);
params.push_back(hostname);
params.push_back(servername);
params.push_back(realname);
send_command("", "USER", params);
impl->username = username;
impl->hostname = hostname;
impl->servername = servername;
impl->realname = realname;
}
void CL_IRCConnection::send_oper(const std::string &user, const std::string &password)
{
std::vector<std::string> params;
params.push_back(user);
params.push_back(password);
send_command("", "OPER", params);
}
void CL_IRCConnection::send_quit(const std::string &quitmessage)
{
std::vector<std::string> params;
if (!quitmessage.empty()) params.push_back(quitmessage);
send_command("", "QUIT", params);
}
void CL_IRCConnection::send_join(const std::string &channel, const std::string &key)
{
std::list<std::string> channels;
std::list<std::string> keys;
channels.push_back(channel);
keys.push_back(key);
send_join(channels, keys);
}
void CL_IRCConnection::send_join(const std::list<std::string> &channels, const std::list<std::string> &keys)
{
std::list<std::string>::const_iterator it, it_keys;
std::string channel_list, key_list;
bool first = true;
// Add channels with keys first. Limitation in IRC protocol.
for (it = channels.begin(), it_keys = keys.begin(); it != channels.end() && it_keys != keys.end(); ++it, ++it_keys)
{
if (it_keys->empty()) continue;
if (!first)
{
channel_list.append(",");
key_list.append(",");
}
first = false;
channel_list.append(*it);
key_list.append(*it_keys);
}
// Add channels without keys.
for (it = channels.begin(), it_keys = keys.begin(); it != channels.end() && it_keys != keys.end(); ++it, ++it_keys)
{
if (!it_keys->empty()) continue;
if (!first)
{
channel_list.append(",");
}
first = false;
channel_list.append(*it);
}
std::vector<std::string> params;
params.push_back(channel_list);
if (!key_list.empty()) params.push_back(key_list);
send_command("", "JOIN", params);
}
void CL_IRCConnection::send_part(const std::string &channel, const std::string &reason)
{
std::list<std::string> channels;
channels.push_back(channel);
send_part(channels, reason);
}
void CL_IRCConnection::send_part(const std::list<std::string> &channels, const std::string &reason)
{
std::list<std::string>::const_iterator it;
std::string channel_list;
bool first = true;
for (it = channels.begin(); it != channels.end(); ++it)
{
if (!first) channel_list.append(",");
first = false;
channel_list.append(*it);
}
std::vector<std::string> params;
params.push_back(channel_list);
if (!reason.empty()) params.push_back(reason);
send_command("", "PART", params);
}
void CL_IRCConnection::send_mode(const std::string &channel_or_nick, const std::string &mode, const std::vector<std::string> &args)
{
std::vector<std::string> params;
params.push_back(channel_or_nick);
params.push_back(mode);
int size = args.size();
for (int i=0; i<size; i++) params.push_back(args[i]);
send_command("", "MODE", params);
}
void CL_IRCConnection::send_topic(const std::string &channel, const std::string &topic)
{
std::vector<std::string> params;
params.push_back(channel);
params.push_back(topic);
send_command("", "TOPIC", params);
}
void CL_IRCConnection::send_topic(const std::string &channel)
{
std::vector<std::string> params;
params.push_back(channel);
send_command("", "TOPIC", params);
}
void CL_IRCConnection::send_names(const std::string &channel)
{
std::list<std::string> channels;
channels.push_back(channel);
send_names(channels);
}
void CL_IRCConnection::send_names(const std::list<std::string> &channels)
{
std::list<std::string>::const_iterator it;
std::string channel_list;
bool first = true;
for (it = channels.begin(); it != channels.end(); ++it)
{
if (!first) channel_list.append(",");
first = false;
channel_list.append(*it);
}
std::vector<std::string> params;
params.push_back(channel_list);
send_command("", "NAMES", params);
}
void CL_IRCConnection::send_list(const std::string &channel, const std::string &server)
{
std::list<std::string> channels;
channels.push_back(channel);
send_list(channels, server);
}
void CL_IRCConnection::send_list(const std::list<std::string> &channels, const std::string &server)
{
std::list<std::string>::const_iterator it;
std::string channel_list;
bool first = true;
for (it = channels.begin(); it != channels.end(); ++it)
{
if (!first) channel_list.append(",");
first = false;
channel_list.append(*it);
}
std::vector<std::string> params;
params.push_back(channel_list);
if (!server.empty()) params.push_back(server);
send_command("", "LIST", params);
}
void CL_IRCConnection::send_invite(const std::string &nickname, const std::string &channel)
{
std::vector<std::string> params;
params.push_back(nickname);
params.push_back(channel);
send_command("", "INVITE", params);
}
void CL_IRCConnection::send_kick(const std::string &channel, const std::string &user, const std::string &comment)
{
std::list<std::string> channels, users;
channels.push_back(channel);
users.push_back(user);
send_kick(channels, users, comment);
}
void CL_IRCConnection::send_kick(const std::list<std::string> &channels, const std::list<std::string> &users, const std::string &comment)
{
std::list<std::string>::const_iterator it;
std::string channel_list, user_list;
bool first = true;
for (it = channels.begin(); it != channels.end(); ++it)
{
if (!first) channel_list.append(",");
first = false;
channel_list.append(*it);
}
first = true;
for (it = users.begin(); it != users.end(); ++it)
{
if (!first) user_list.append(",");
first = false;
user_list.append(*it);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -