Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "user.h"
- #include "client.h"
- #include "auxiliar.h"
- #include "channels.h"
- #include "ban.h"
- #include "database.h"
- #include "configreader.h"
- #include "defines.h"
- #include <iostream>
- #include <cstdio>
- extern Channels *g_channels;
- extern Irc::Config* g_config;
- int Irc::User::operCount;
- int Irc::User::userCount;
- Irc::User::User(const std::string& _name, Client* _client)
- : name(_name), client(_client)
- {
- type = UT_NONE;
- setHost();
- ident = name;
- IP = client->getIP();
- awaymsg = "";
- server = g_config->getString(0x07);
- userCount++;
- nick = "";
- }
- Irc::User::~User()
- {
- m_modes.clear();
- for (BansMap::iterator it = m_bans.begin(); it != m_bans.end(); ++it)
- delete it->second;
- m_bans.clear();
- for (ChansMap::iterator it = m_channels.begin(); it != m_channels.end(); ++it)
- delete it->second;
- m_channels.clear();
- delete client;
- client = NULL;
- }
- std::string Irc::User::getName() const
- {
- return name;
- }
- std::string Irc::User::getNick()
- {
- return nick;
- }
- std::string Irc::User::getMode()
- {
- std::string modes = "";
- for (Modes::iterator it = m_modes.begin(); it != m_modes.end(); ++it)
- modes += *it;
- return modes;
- }
- std::string Irc::User::getHost()
- {
- return host;
- }
- std::string Irc::User::getServer() const
- {
- return server;
- }
- Irc::Client* Irc::User::getClient() const
- {
- return client;
- }
- void Irc::User::tmp_quit(const char *msg)
- {
- for (ChansMap::const_iterator it = m_channels.begin(); it != m_channels.end(); ++it) {
- it->second->postQuitMessage(this, std::string(msg));
- it->second->removeUser(this);
- it->second->addUser(this);
- }
- }
- bool Irc::User::canJoinChannels()
- {
- if (getType() == UT_IRC_OP)
- return true;
- if (m_channels.size() >= 20)
- return false;
- return true;
- }
- Irc::Ban* Irc::User::getBan()
- {
- Database *db = Database::instance();
- if (!db->connect())
- return NULL;
- std::stringstream ss;
- ss << "SELECT `time`, `type` FROM `server_bans` WHERE `host` = '" << getHost() << "';";
- Result *r = db->storeQuery(ss);
- if (r == NULL || !r)
- return NULL;
- Ban *ban = NULL;
- do {
- time_t p = r->getDataInt("time");
- Irc::BanType_t type = (Irc::BanType_t)r->getDataInt("type");
- ban = new Ban(p, type, const_cast<Irc::User*>(this));
- } while (r->next());
- r->free();
- return ban;
- }
- void Irc::User::quit(const std::string& __msg)
- {
- for (ChansMap::iterator it = m_channels.begin(); it != m_channels.end(); ++it) {
- it->second->postQuitMessage(this, __msg);
- delete it->second;
- }
- client->close();
- m_channels.clear();
- delete client;
- }
- bool Irc::User::isAlive()
- {
- if (client)
- return true;
- return false;
- }
- void Irc::User::setMode(char mode)
- {
- Modes::const_iterator it = m_modes.find(mode);
- if (it != m_modes.end())
- return;
- m_modes.insert(mode);
- }
- bool Irc::User::joinChannel(const std::string& channel)
- {
- ChansMap::iterator it = m_channels.find(channel);
- if (it != m_channels.end())
- return false;
- Channel* p = g_channels->getChannel(channel);
- m_channels[channel] = p;
- return true;
- }
- bool Irc::User::partChannel(const std::string& channel)
- {
- ChansMap::iterator it = m_channels.find(channel);
- if (it == m_channels.end())
- return false;
- m_channels.erase(it);
- return true;
- }
- bool Irc::User::setNick(const std::string& newNick)
- {
- if (nick == newNick)
- return false;
- nick = newNick;
- return true;
- }
- bool Irc::User::setHost(const std::string& newHost)
- {
- if (host == newHost)
- return false;
- host = newHost;
- return true;
- }
- bool Irc::User::isBanned(const std::string& channel)
- {
- Channel *tmp = g_channels->getChannel(channel);
- if (!tmp)
- return false;
- Ban *p = tmp->getBan(this);
- if (!p)
- return false;
- return true;
- }
- BansMap Irc::User::getBans()
- {
- return m_bans;
- }
- void Irc::User::addServerBan(const Ban* ban)
- {
- //TODO
- }
- void Irc::User::addChannelBan(const std::string& channel, Ban* ban)
- {
- Channel* tmp = g_channels->getChannel(channel);
- if (!tmp)
- return;
- tmp->addBan(this, ban);
- }
- void Irc::User::sendMotd()
- {
- //TODO
- }
- std::string Irc::User::getIP() const
- {
- return client->getIP();
- }
- bool Irc::User::isIrcOp()
- {
- return type == UT_IRC_OP;
- }
- Irc::UserType_t Irc::User::getType()
- {
- return type;
- }
- void Irc::User::setType(Irc::UserType_t newType)
- {
- if (type == newType)
- return;
- type = newType;
- if (type == UT_IRC_OP) {
- userCount--;
- operCount++;
- }
- }
- void Irc::User::sendMessage(Irc::MessageType_t _type, const std::string& msg)
- {
- std::string tmp = ":" + getServer() + " ";
- std::string _name = getName();
- switch (_type) {
- case MESSAGE_UNKNOWN:
- {
- tmp.clear();
- tmp = msg;
- break;
- }
- case MESSAGE_MOTD:
- {
- tmp += "372 " + _name + " " + msg;
- break;
- }
- case MESSAGE_001:
- {
- tmp += "001 " + _name + " " + msg;
- break;
- }
- case MESSAGE_002:
- {
- tmp += "002 " + _name + " " + msg;
- break;
- }
- case MESSAGE_004:
- {
- tmp += "004 " + _name + " " + msg;
- break;
- }
- case MESSAGE_005:
- {
- tmp += "005 " + _name + " " + msg;
- break;
- }
- case MESSAGE_NAMESLIST:
- {
- tmp += "353 " + _name + " " + msg;
- break;
- }
- case MESSAGE_ENDOFNAMESLIST:
- {
- tmp += "366 " + _name + " " + msg;
- break;
- }
- case MESSAGE_JOIN:
- {
- tmp.clear();
- tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " JOIN " + msg;
- break;
- }
- case MESSAGE_PART:
- {
- tmp.clear();
- tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " PART " + msg;
- break;
- }
- case MESSAGE_QUIT:
- {
- tmp.clear();
- tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " QUIT :Quit";
- if (!msg.empty())
- tmp += ":" + msg;
- break;
- }
- case MESSAGE_MODE:
- {
- tmp.clear();
- tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " MODE " + getNick() + " " + msg;
- break;
- }
- case MESSAGE_MODE_I:
- {
- tmp += "221 " + _name + " +i";
- break;
- }
- case MESSAGE_ENDOFMOTD:
- {
- tmp += "376 " + _name + " :End of /MOTD command.";
- break;
- }
- default:
- {
- tmp.clear();
- break;
- }
- }
- tmp += "\r\n";
- client->send(tmp);
- }
- void Irc::User::sendJoinMessage(const std::string& __name, const std::string& __host,
- const std::string& __ident, const std::string& __channel)
- {
- std::stringstream ss;
- ss << ":" << __name << "!" << __ident << "@" << __host << " JOIN " << __channel << "\r\n";
- client->send(ss.str());
- ss.str("");
- }
- void Irc::User::sendQuitMessage(const std::string& __name, const std::string& __host,
- const std::string& __ident, const std::string& __msg)
- {
- std::stringstream ss;
- ss << ":" << __name << "!" << __ident << "@" << __host << " QUIT :Quit";
- if (!__msg.empty())
- ss << __msg;
- ss << "\r\n";
- client->send(ss.str());
- ss.str("");
- }
- void Irc::User::sendModeMessage(const std::string& __name, const std::string& __host,
- const std::string& __ident, char mode)
- {
- std::stringstream ss;
- ss << ":" << __name << "!" << __ident << "@" << __host << " MODE " << __name << " " << mode << "\r\n";
- client->send(ss.str());
- ss.str("");
- }
- void Irc::User::sendPartMessage(const std::string& __name, const std::string& __host,
- const std::string& __ident, const std::string& __channel,
- const std::string& __msg)
- {
- std::stringstream ss;
- ss << ":" << __name << "!" << __ident << "@" << __host << " PART " << __channel;
- if (!__msg.empty())
- ss << " :" << __msg;
- ss << "\r\n";
- client->send(ss.str());
- ss.str("");
- }
- void Irc::User::sendTopicMessage(const std::string& __name, const std::string& __host,
- const std::string& __ident, const std::string& __channel,
- const std::string& __topic)
- {
- std::stringstream ss;
- ss << ":" << __name << "!" << __ident << "@" << __host << " TOPIC " << __channel << " :" << __topic << "\r\n";
- client->send(ss.str());
- ss.str("");
- }
- std::string Irc::User::getIdent() const
- {
- return ident;
- }
- void Irc::User::setHost()
- {
- if (!client)
- return;
- if (!client->getHostName().empty())
- host = client->getHostName();
- else
- host = client->getIP();
- }
- bool Irc::User::isAway()
- {
- return !awaymsg.empty();
- }
- std::string Irc::User::getAwayMessage()
- {
- return awaymsg;
- }
- void Irc::User::setAwayMessage(const std::string& newMsg)
- {
- if (awaymsg == newMsg)
- return;
- awaymsg = newMsg;
- }
- void Irc::User::sendWhoIsLine(int code, const std::string& line)
- {
- std::stringstream ss;
- ss << ":" << getServer() << " " << code << " " << line << "\r\n";
- client->send(ss.str());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement