Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.46 KB | None | 0 0
  1. #include "user.h"
  2. #include "client.h"
  3. #include "auxiliar.h"
  4. #include "channels.h"
  5.  
  6. #include "ban.h"
  7. #include "database.h"
  8. #include "configreader.h"
  9. #include "defines.h"
  10.  
  11. #include <iostream>
  12. #include <cstdio>
  13.  
  14. extern Channels *g_channels;
  15. extern Irc::Config* g_config;
  16.  
  17. int Irc::User::operCount;
  18. int Irc::User::userCount;
  19.  
  20. Irc::User::User(const std::string& _name, Client* _client)
  21.     : name(_name), client(_client)
  22. {
  23.     type = UT_NONE;
  24.     setHost();
  25.     ident = name;
  26.     IP = client->getIP();
  27.     awaymsg = "";
  28.     server = g_config->getString(0x07);
  29.     userCount++;
  30.     nick = "";
  31. }
  32.  
  33. Irc::User::~User()
  34. {
  35.     m_modes.clear();
  36.     for (BansMap::iterator it = m_bans.begin(); it != m_bans.end(); ++it)
  37.         delete it->second;
  38.  
  39.     m_bans.clear();
  40.     for (ChansMap::iterator it = m_channels.begin(); it != m_channels.end(); ++it)
  41.         delete it->second;
  42.  
  43.     m_channels.clear();
  44.     delete client;
  45.     client = NULL;
  46. }
  47.  
  48. std::string Irc::User::getName() const
  49. {
  50.     return name;
  51. }
  52.  
  53. std::string Irc::User::getNick()
  54. {
  55.     return nick;
  56. }
  57.  
  58. std::string Irc::User::getMode()
  59. {
  60.     std::string modes = "";
  61.     for (Modes::iterator it = m_modes.begin(); it != m_modes.end(); ++it)
  62.         modes += *it;
  63.  
  64.     return modes;
  65. }
  66.  
  67. std::string Irc::User::getHost()
  68. {
  69.     return host;
  70. }
  71.  
  72. std::string Irc::User::getServer() const
  73. {
  74.     return server;
  75. }
  76.  
  77. Irc::Client* Irc::User::getClient() const
  78. {
  79.     return client;
  80. }
  81.  
  82. void Irc::User::tmp_quit(const char *msg)
  83. {
  84.     for (ChansMap::const_iterator it = m_channels.begin(); it != m_channels.end(); ++it) {
  85.         it->second->postQuitMessage(this, std::string(msg));
  86.         it->second->removeUser(this);
  87.         it->second->addUser(this);
  88.     }
  89. }
  90.  
  91. bool Irc::User::canJoinChannels()
  92. {
  93.     if (getType() == UT_IRC_OP)
  94.         return true;
  95.  
  96.     if (m_channels.size() >= 20)
  97.         return false;
  98.  
  99.     return true;
  100. }
  101.  
  102. Irc::Ban* Irc::User::getBan()
  103. {
  104.     Database *db = Database::instance();
  105.     if (!db->connect())
  106.         return NULL;
  107.  
  108.     std::stringstream ss;
  109.     ss << "SELECT `time`, `type` FROM `server_bans` WHERE `host` = '" << getHost() << "';";
  110.     Result *r = db->storeQuery(ss);
  111.     if (r == NULL || !r)
  112.         return NULL;
  113.  
  114.     Ban *ban = NULL;
  115.     do {
  116.         time_t p = r->getDataInt("time");
  117.         Irc::BanType_t type = (Irc::BanType_t)r->getDataInt("type");
  118.         ban = new Ban(p, type, const_cast<Irc::User*>(this));
  119.     } while (r->next());
  120.     r->free();
  121.     return ban;
  122. }
  123.  
  124. void Irc::User::quit(const std::string& __msg)
  125. {
  126.     for (ChansMap::iterator it = m_channels.begin(); it != m_channels.end(); ++it) {
  127.         it->second->postQuitMessage(this, __msg);
  128.         delete it->second;
  129.     }
  130.     client->close();
  131.     m_channels.clear();
  132.     delete client;
  133. }
  134.  
  135. bool Irc::User::isAlive()
  136. {
  137.     if (client)
  138.         return true;
  139.  
  140.     return false;
  141. }
  142.  
  143. void Irc::User::setMode(char mode)
  144. {
  145.     Modes::const_iterator it = m_modes.find(mode);
  146.     if (it != m_modes.end())
  147.         return;
  148.  
  149.     m_modes.insert(mode);
  150. }
  151.  
  152. bool Irc::User::joinChannel(const std::string& channel)
  153. {
  154.     ChansMap::iterator it = m_channels.find(channel);
  155.     if (it != m_channels.end())
  156.         return false;
  157.  
  158.     Channel* p = g_channels->getChannel(channel);
  159.     m_channels[channel] = p;
  160.     return true;
  161. }
  162.  
  163. bool Irc::User::partChannel(const std::string& channel)
  164. {
  165.     ChansMap::iterator it = m_channels.find(channel);
  166.     if (it == m_channels.end())
  167.         return false;
  168.  
  169.     m_channels.erase(it);
  170.     return true;
  171. }
  172.  
  173. bool Irc::User::setNick(const std::string& newNick)
  174. {
  175.     if (nick == newNick)
  176.         return false;
  177.  
  178.     nick = newNick;
  179.     return true;
  180. }
  181.  
  182. bool Irc::User::setHost(const std::string& newHost)
  183. {
  184.     if (host == newHost)
  185.         return false;
  186.  
  187.     host = newHost;
  188.     return true;
  189. }
  190.  
  191. bool Irc::User::isBanned(const std::string& channel)
  192. {
  193.     Channel *tmp = g_channels->getChannel(channel);
  194.     if (!tmp)
  195.         return false;
  196.  
  197.     Ban *p = tmp->getBan(this);
  198.     if (!p)
  199.         return false;
  200.  
  201.     return true;
  202. }
  203.  
  204. BansMap Irc::User::getBans()
  205. {
  206.     return m_bans;
  207. }
  208.  
  209. void Irc::User::addServerBan(const Ban* ban)
  210. {
  211.     //TODO
  212. }
  213.  
  214. void Irc::User::addChannelBan(const std::string& channel, Ban* ban)
  215. {
  216.     Channel* tmp = g_channels->getChannel(channel);
  217.     if (!tmp)
  218.         return;
  219.  
  220.     tmp->addBan(this, ban);
  221. }
  222.  
  223. void Irc::User::sendMotd()
  224. {
  225.     //TODO
  226. }
  227.  
  228. std::string Irc::User::getIP() const
  229. {
  230.     return client->getIP();
  231. }
  232.  
  233. bool Irc::User::isIrcOp()
  234. {
  235.     return type == UT_IRC_OP;
  236. }
  237.  
  238. Irc::UserType_t Irc::User::getType()
  239. {
  240.     return type;
  241. }
  242.  
  243. void Irc::User::setType(Irc::UserType_t newType)
  244. {
  245.     if (type == newType)
  246.         return;
  247.  
  248.     type = newType;
  249.     if (type == UT_IRC_OP) {
  250.         userCount--;
  251.         operCount++;
  252.     }
  253. }
  254.  
  255. void Irc::User::sendMessage(Irc::MessageType_t _type, const std::string& msg)
  256. {
  257.     std::string tmp = ":" + getServer() + " ";
  258.     std::string _name = getName();
  259.     switch (_type) {
  260.         case MESSAGE_UNKNOWN:
  261.         {
  262.             tmp.clear();
  263.             tmp = msg;
  264.             break;
  265.         }
  266.         case MESSAGE_MOTD:
  267.         {
  268.             tmp += "372 " + _name + " " + msg;
  269.             break;
  270.         }
  271.         case MESSAGE_001:
  272.         {
  273.             tmp += "001 " + _name + " " + msg;
  274.             break;
  275.         }
  276.         case MESSAGE_002:
  277.         {
  278.             tmp += "002 " + _name + " " + msg;
  279.             break;
  280.         }
  281.         case MESSAGE_004:
  282.         {
  283.             tmp += "004 " + _name + " " + msg;
  284.             break;
  285.         }
  286.         case MESSAGE_005:
  287.         {
  288.             tmp += "005 " + _name + " " + msg;
  289.             break;
  290.         }
  291.         case MESSAGE_NAMESLIST:
  292.         {
  293.             tmp += "353 " + _name + " " + msg;
  294.             break;
  295.         }
  296.         case MESSAGE_ENDOFNAMESLIST:
  297.         {
  298.             tmp += "366 " + _name + " " + msg;
  299.             break;
  300.         }
  301.         case MESSAGE_JOIN:
  302.         {
  303.             tmp.clear();
  304.             tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " JOIN " + msg;
  305.             break;
  306.         }
  307.         case MESSAGE_PART:
  308.         {
  309.             tmp.clear();
  310.             tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " PART " + msg;
  311.             break;
  312.         }
  313.         case MESSAGE_QUIT:
  314.         {
  315.             tmp.clear();
  316.             tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " QUIT :Quit";
  317.             if (!msg.empty())
  318.                 tmp += ":" + msg;
  319.             break;
  320.         }
  321.         case MESSAGE_MODE:
  322.         {
  323.             tmp.clear();
  324.             tmp = ":" + _name + "!" + getIdent() + "@" + getHost() + " MODE " + getNick() + " " + msg;
  325.             break;
  326.         }
  327.         case MESSAGE_MODE_I:
  328.         {
  329.             tmp += "221 " + _name + " +i";
  330.             break;
  331.         }
  332.         case MESSAGE_ENDOFMOTD:
  333.         {
  334.             tmp += "376 " + _name + " :End of /MOTD command.";
  335.             break;
  336.         }
  337.         default:
  338.         {
  339.             tmp.clear();
  340.             break;
  341.         }
  342.     }
  343.     tmp += "\r\n";
  344.     client->send(tmp);
  345. }
  346.  
  347. void Irc::User::sendJoinMessage(const std::string& __name, const std::string& __host,
  348.     const std::string& __ident, const std::string& __channel)
  349. {
  350.     std::stringstream ss;
  351.     ss << ":" << __name << "!" << __ident << "@" << __host << " JOIN " << __channel << "\r\n";
  352.     client->send(ss.str());
  353.     ss.str("");
  354. }
  355.  
  356. void Irc::User::sendQuitMessage(const std::string& __name, const std::string& __host,
  357.     const std::string& __ident, const std::string& __msg)
  358. {
  359.     std::stringstream ss;
  360.     ss << ":" << __name << "!" << __ident << "@" << __host << " QUIT :Quit";
  361.     if (!__msg.empty())
  362.         ss << __msg;
  363.  
  364.     ss << "\r\n";
  365.     client->send(ss.str());
  366.     ss.str("");
  367. }
  368.  
  369. void Irc::User::sendModeMessage(const std::string& __name, const std::string& __host,
  370.     const std::string& __ident, char mode)
  371. {
  372.     std::stringstream ss;
  373.     ss << ":" << __name << "!" << __ident << "@" << __host << " MODE " << __name << " " << mode << "\r\n";
  374.     client->send(ss.str());
  375.     ss.str("");
  376. }
  377.  
  378. void Irc::User::sendPartMessage(const std::string& __name, const std::string& __host,
  379.     const std::string& __ident, const std::string& __channel,
  380.     const std::string& __msg)
  381. {
  382.     std::stringstream ss;
  383.     ss << ":" << __name << "!" << __ident << "@" << __host << " PART " << __channel;
  384.     if (!__msg.empty())
  385.         ss << " :" << __msg;
  386.  
  387.     ss << "\r\n";
  388.     client->send(ss.str());
  389.     ss.str("");
  390. }
  391.  
  392. void Irc::User::sendTopicMessage(const std::string& __name, const std::string& __host,
  393.     const std::string& __ident, const std::string& __channel,
  394.     const std::string& __topic)
  395. {
  396.     std::stringstream ss;
  397.     ss << ":" << __name << "!" << __ident << "@" << __host << " TOPIC " << __channel << " :" << __topic << "\r\n";
  398.     client->send(ss.str());
  399.     ss.str("");
  400. }
  401.  
  402. std::string Irc::User::getIdent() const
  403. {
  404.     return ident;
  405. }
  406.  
  407. void Irc::User::setHost()
  408. {
  409.     if (!client)
  410.         return;
  411.  
  412.     if (!client->getHostName().empty())
  413.         host = client->getHostName();
  414.     else
  415.         host = client->getIP();
  416.  
  417. }
  418.  
  419. bool Irc::User::isAway()
  420. {
  421.     return !awaymsg.empty();
  422. }
  423.  
  424. std::string Irc::User::getAwayMessage()
  425. {
  426.     return awaymsg;
  427. }
  428.  
  429. void Irc::User::setAwayMessage(const std::string& newMsg)
  430. {
  431.     if (awaymsg == newMsg)
  432.         return;
  433.  
  434.     awaymsg = newMsg;
  435. }
  436.  
  437. void Irc::User::sendWhoIsLine(int code, const std::string& line)
  438. {
  439.     std::stringstream ss;
  440.     ss << ":" << getServer() << " " << code << " " << line << "\r\n";
  441.     client->send(ss.str());
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement