Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. /* dont remove the following line!1! */
  2. /* RequiredLibraries: mysqlpp,mysqlclient,xml2 */
  3.  
  4. #include "gseen.h"
  5.  
  6. mysqlpp::Connection con;
  7. mysqlpp::Query query = con.query();
  8. langs lng;
  9.  
  10.  
  11. class BS_GSEEN : public Module
  12. {
  13. public:
  14. BS_GSEEN(const std::string &modname, const std::string &creator) : Module(modname, creator)
  15. {
  16. this->SetAuthor("Anope");
  17. this->SetVersion("$Id$");
  18. this->SetType(THIRD);
  19. this->OnReload(true);
  20. this->AddCommand(CHANSERV, new CommandSEEN());
  21. ModuleManager::Attach(I_OnReload, this);
  22. ModuleManager::Attach(I_OnUserConnect, this);
  23. ModuleManager::Attach(I_OnUserNickChange, this);
  24. ModuleManager::Attach(I_OnUserQuit, this);
  25. ModuleManager::Attach(I_OnJoinChannel, this);
  26. ModuleManager::Attach(I_OnPartChannel, this);
  27. ModuleManager::Attach(I_OnUserKicked, this);
  28. }
  29.  
  30.  
  31. void OnReload(bool starting)
  32. {
  33. ConfigReader config;
  34. std::string mysqlHost, mysqlName, mysqlUser, mysqlPass, mysqlPort;
  35.  
  36. mysqlHost = config.ReadValue("database", "host", 0);
  37. mysqlPort = config.ReadValue("database", "port", 0);
  38. mysqlName = config.ReadValue("database", "name", 0);
  39. mysqlUser = config.ReadValue("database", "user", 0);
  40. mysqlPass = config.ReadValue("database", "pass", 0);
  41.  
  42. if (mysqlName.size() == 0) throw ModuleException("missing configuration parameter: database name");
  43. if (mysqlHost.size() == 0) throw ModuleException("missing configuration parameter: database host");
  44. if (mysqlUser.size() == 0) throw ModuleException("missing configuration parameter: database user");
  45. if (mysqlPass.size() == 0) throw ModuleException("missing configuration parameter: database pass");
  46. if (mysqlPort.size() > 0)
  47. {
  48. mysqlHost += ":";
  49. mysqlHost += mysqlPort;
  50. }
  51. if (!con.connect(mysqlName.c_str(), mysqlHost.c_str(), mysqlUser.c_str(), mysqlPass.c_str()))
  52. throw ModuleException("Error connecting to the MySQL Database");
  53. lng.load("gseen.xml");
  54. SQL_Initialize_DB();
  55. } // OnReload
  56.  
  57. void SQL_Initialize_DB()
  58. {
  59. mysqlpp::StoreQueryResult res;
  60.  
  61. // 1. check for database and create it
  62. query.reset();
  63. query << "SHOW TABLES LIKE " << DBNAME << ";";
  64. query.parse();
  65. if (debug)
  66. Alog(LOG_DEBUG) << query.str();
  67. if ((res = query.store()) && (res.num_rows() == 0))
  68. { // table does not exist, so create it
  69. query.reset();
  70. query << "CREATE TABLE " << DBNAME << " "
  71. << "nick VARCHAR(128) NOT NULL, PRIMARY KEY (nick), "
  72. << "type INT, "
  73. << "host TEXT, "
  74. << "vhost TEXT, "
  75. << "chan VARCHAR(512), " // in some ircds the length is free configurable
  76. << "msg TEXT, "
  77. << "last BIGINT(14), "
  78. << "spent INT, " // not used atm
  79. << "newnick VARCHAR(128)) " // in some ircds the length is free configurable
  80. << "TYPE=myISAM ";
  81. query.parse();
  82. if (debug)
  83. Alog(LOG_DEBUG) << query.str();
  84. query.store();
  85. }
  86. /**
  87. * 2. insert the cleanup-event into mysql (drop the old cron before)
  88. * damn! the EVENT system in mysql requires version 5.1
  89. * most stable linux distributions use still 5.0
  90. * so we have to cleanup the database with an external, cron-triggered script
  91. */
  92. }
  93. void OnUserConnect(User *u)
  94. {
  95. std::string host, vhost;
  96. size_t now = time(NULL);
  97.  
  98. host = u->GetIdent() + "@" + u->host;
  99. vhost = u->GetVIdent() + "@" + u->GetCloakedHost();
  100. query.reset();
  101. query << "INSERT INTO " << DBNAME << " "
  102. << "(`nick`, `type`, `host`, `vhost`, `last`) "
  103. << "VALUES (%0q, 1, %1q, %2q, %3q) "
  104. << "ON DUPLICATE KEY UPDATE "
  105. << "type=VALUES(type), host=VALUES(host), vhost=VALUES(vhost), "
  106. << "last=VALUES(last);";
  107. query.parse();
  108. if (debug)
  109. Alog(LOG_DEBUG) << query.str(u->nick, host, vhost, now);
  110. query.store(u->nick, host, vhost, now);
  111. }
  112.  
  113. void OnUserNickChange(User *u, const std::string &oldnick)
  114. {
  115. std::string host, vhost;
  116. size_t now = time(NULL);
  117.  
  118. host = u->GetIdent() + "@" + u->host;
  119. vhost = u->GetVIdent() + "@" + u->GetCloakedHost();
  120. // build query
  121. query.reset();
  122. query << "INSERT INTO " << DBNAME << " "
  123. << "(`nick`, `type`, `host`, `vhost`, `last`, `newnick`) "
  124. << "VALUES (%0q, %1q, %2q, %3q, %4q, %5q) "
  125. << "ON DUPLICATE KEY UPDATE "
  126. << "type=VALUES(type), host=VALUES(host), vhost=VALUES(vhost), "
  127. << "last=VALUES(last), newnick=VALUES(newnick);";
  128. query.parse();
  129. // update old nick
  130. if (debug)
  131. Alog(LOG_DEBUG) << query.str(oldnick, 2, host, vhost, now, u->nick);
  132. query.store(oldnick, 2, host, vhost, now, u->nick);
  133. // update new nick
  134. if (debug)
  135. Alog(LOG_DEBUG) << query.str(u->nick, 3, host, vhost, now, oldnick);
  136. query.store(u->nick, 3, host, vhost, now, oldnick);
  137. }
  138.  
  139. void OnUserQuit(User *u, const std::string &msg)
  140. {
  141. std::string host, vhost;
  142. size_t now = time(NULL);
  143.  
  144. host = u->GetIdent() + "@" + u->host;
  145. vhost = u->GetVIdent() + "@" + u->GetCloakedHost();
  146.  
  147. query.reset();
  148. query << "INSERT INTO " << DBNAME << " "
  149. << "(nick, type, host, vhost, last, msg) "
  150. << "VALUES (%0q, 6, %1q, %2q, %3q, %4q) "
  151. << "ON DUPLICATE KEY UPDATE "
  152. << "type=VALUES(type), host=VALUES(host), vhost=VALUES(vhost), "
  153. << "last=VALUES(last), msg=VALUES(msg);";
  154. query.parse();
  155. if (debug)
  156. Alog(LOG_DEBUG) << query.str(u->nick, host, vhost, now, msg);
  157. query.store(u->nick, host, vhost, now, msg);
  158. }
  159.  
  160. void OnJoinChannel(User *u, Channel *c)
  161. {
  162. std::string host, vhost;
  163. size_t now = time(NULL);
  164.  
  165. host = u->GetIdent() + "@" + u->host;
  166. vhost = u->GetVIdent() + "@" + u->GetCloakedHost();
  167.  
  168. query.reset();
  169. query << "INSERT INTO " << DBNAME << " "
  170. << "(nick, type, host, vhost, last, chan) "
  171. << "VALUES (%0q, 4, %1q, %2q, %3q, %4q) "
  172. << "ON DUPLICATE KEY UPDATE "
  173. << "type=VALUES(type), host=VALUES(host), vhost=VALUES(vhost), "
  174. << "last=VALUES(last), chan=VALUES(chan);";
  175. query.parse();
  176. if (debug)
  177. Alog(LOG_DEBUG) << query.str(u->nick, host, vhost, now, c->name);
  178. query.store(u->nick, host, vhost, now, c->name);
  179. }
  180.  
  181. void OnPartChannel(User *u, Channel *c, const std::string &msg)
  182. {
  183. std::string host, vhost;
  184. size_t now = time(NULL);
  185.  
  186. host = u->GetIdent() + "@" + u->host;
  187. vhost = u->GetVIdent() + "@" + u->GetCloakedHost();
  188.  
  189. query.reset();
  190. query << "INSERT INTO " << DBNAME << " "
  191. << "(nick, type, host, vhost, last, chan, msg) "
  192. << "VALUES (%0q, 5, %1q, %2q, %3q, %4q, %5q) "
  193. << "ON DUPLICATE KEY UPDATE "
  194. << "type=VALUES(type), host=VALUES(host), vhost=VALUES(vhost), "
  195. << "last=VALUES(last), chan=VALUES(chan), msg=VALUES(msg);";
  196. query.parse();
  197. if (debug)
  198. Alog(LOG_DEBUG) << query.str(u->nick, host, vhost, now, c->name, msg);
  199. query.store(u->nick, host, vhost, now, c->name, msg);
  200. }
  201.  
  202. void OnUserKicked(Channel *c, User *u, const std::string &source, const std::string &kickmsg)
  203. {
  204. std::string host, vhost;
  205. size_t now = time(NULL);
  206.  
  207. host = u->GetIdent() + "@" + u->host;
  208. vhost = u->GetVIdent() + "@" + u->GetCloakedHost();
  209.  
  210. query.reset();
  211. query << "INSERT INTO " << DBNAME << " "
  212. << "(nick, type, host, vhost, last, chan, msg, newnick) "
  213. << "VALUES (%0q, 7, %1q, %2q, %3q, %4q, %5q, %6q) "
  214. << "ON DUPLICATE KEY UPDATE "
  215. << "type=VALUES(type), host=VALUES(host), vhost=VALUES(vhost), "
  216. << "last=VALUES(last), chan=VALUES(chan), msg=VALUES(msg), newnick=VALUES(newnick);";
  217. query.parse();
  218. if (debug)
  219. Alog(LOG_DEBUG) << query.str(u->nick, host, vhost, now, c->name, kickmsg, source);
  220. query.store(u->nick, host, vhost, now, c->name, kickmsg, source);
  221.  
  222.  
  223.  
  224.  
  225. }
  226. };
  227.  
  228. MODULE_INIT(BS_GSEEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement