|
| 1 | + |
| 2 | +#include <iostream> |
| 3 | +#include <ctime> |
| 4 | + |
| 5 | +#include <boost/bind.hpp> |
| 6 | +#include <boost/spirit/include/classic_core.hpp> |
| 7 | +#include <boost/lexical_cast.hpp> |
| 8 | +#include <boost/algorithm/string/replace.hpp> |
| 9 | +#include <boost/random.hpp> |
| 10 | + |
| 11 | +#include "quotes.h" |
| 12 | +#include "spirit.h" |
| 13 | + |
| 14 | +using namespace std; |
| 15 | +using namespace BOOST_SPIRIT_CLASSIC_NS; |
| 16 | + |
| 17 | +namespace weberknecht { |
| 18 | + quotes::quotes( irc::client& c, Database& db ) |
| 19 | + : c_( c ), |
| 20 | + quote_( db ), |
| 21 | + numResults_( -1 ), |
| 22 | + currResult_( -1 ) |
| 23 | + { |
| 24 | + ADD_MSG_HANDLER( "PRIVMSG", quotes, quotes_handler, 0 ) |
| 25 | + } |
| 26 | + |
| 27 | + quotes::~quotes() |
| 28 | + { |
| 29 | + quote_.free_result(); |
| 30 | + } |
| 31 | + |
| 32 | + bool quotes::quotes_handler( const irc::message& m ) |
| 33 | + { |
| 34 | + std::string action; |
| 35 | + std::string selector; |
| 36 | + rule< > quote_parser, char_p; |
| 37 | + |
| 38 | + quote_parser = ( str_p( "!quote" ) | str_p( "!addquote" ) )[assign_a(action)] |
| 39 | + >> !(+ch_p( ' ' ) >> !( (+(char_p) )[assign_a(selector)] ) ) >> *ch_p( ' ' ); |
| 40 | + char_p = ~ch_p( 0x0A ) & ~ch_p( 0x0D ) & ~ch_p( 0x3A ); |
| 41 | + |
| 42 | + if( !quote_.Connected() ) |
| 43 | + { |
| 44 | + cerr << "No database connection!" << endl; |
| 45 | + } |
| 46 | + |
| 47 | + if( m.param( 0 ) == c_.nick ) return false; |
| 48 | + |
| 49 | + if( parse( m.param( 1 ).c_str(), quote_parser ).full ) |
| 50 | + { |
| 51 | + if( action == "!quote" ) |
| 52 | + { |
| 53 | + std::string sql_select_count = "SELECT COUNT(text) FROM quotes WHERE channel='" + m.param( 0 ) + "'"; |
| 54 | + std::string sql_select = "SELECT text FROM quotes WHERE channel='" + m.param( 0 ) + "' AND "; |
| 55 | + |
| 56 | + if( selector.length() != 0 ) |
| 57 | + { |
| 58 | + std::list<std::string> select_list; |
| 59 | + std::string tmp; |
| 60 | + std::string quoteID = "0"; |
| 61 | + rule<> selector_rule, quoted, word; |
| 62 | + selector_rule = (+digit_p)[assign_a( quoteID )] | |
| 63 | + (*( *ch_p( ' ' ) >> ( quoted[push_back_a( select_list, tmp )] | word[push_back_a( select_list )] ) >> *ch_p( ' ' ) ) ); |
| 64 | + quoted = ( ch_p( '"' ) >> *( ( *ch_p(' ') >> word >> *ch_p( ' ' ) )[append_a( tmp )] ) >> ch_p( '"' ) ) | |
| 65 | + ( ch_p( '\'' ) >> *( ( *ch_p(' ') >> word >> *ch_p( ' ' ) )[append_a( tmp )] ) >> ch_p( '\'' ) ); |
| 66 | + |
| 67 | + word = +( char_p & ~ch_p( '"' ) & ~ch_p( '\'' ) ); |
| 68 | + |
| 69 | + sql_select_count += " AND "; |
| 70 | + |
| 71 | + if( parse( selector.c_str(), selector_rule ).full ) |
| 72 | + { |
| 73 | + if( quoteID == "0" ) |
| 74 | + { |
| 75 | + std::list<std::string>::iterator it; |
| 76 | + for( it = select_list.begin(); it != --select_list.end(); ++it ) |
| 77 | + { |
| 78 | + sql_select += "text LIKE '%" + *it + "%' OR "; |
| 79 | + sql_select_count += "text LIKE '%" + *it + "%' OR "; |
| 80 | + } |
| 81 | + |
| 82 | + sql_select += "text LIKE '%" + *it + "%';"; |
| 83 | + sql_select_count += "text LIKE '%" + *it + "%';"; |
| 84 | + } |
| 85 | + else |
| 86 | + sql_select += "id=" + quoteID + ";"; |
| 87 | + |
| 88 | + cout << sql_select << endl; |
| 89 | + if( numResults_ != -1 ) |
| 90 | + quote_.free_result(); |
| 91 | + |
| 92 | + numResults_ = quote_.get_count( sql_select_count ); |
| 93 | + quote_.free_result(); |
| 94 | + |
| 95 | + cout << "Number of results: " << numResults_ << endl; |
| 96 | + currResult_ = -1; |
| 97 | + |
| 98 | + quote_.get_result( sql_select ); |
| 99 | + if( numResults_ || quoteID != "0" ) |
| 100 | + { |
| 101 | + quote_.fetch_row(); |
| 102 | + currResult_ = 1; |
| 103 | + std::string text = quote_.getstr(); |
| 104 | + if( numResults_ < 2 ) |
| 105 | + { |
| 106 | + quote_.free_result(); |
| 107 | + numResults_ = -1; |
| 108 | + } |
| 109 | + else |
| 110 | + text += " ( " + boost::lexical_cast<std::string>(numResults_ - currResult_++) + " left )"; |
| 111 | + |
| 112 | + c_ << irc::PRIVMSG( m.param( 0 ), text ); |
| 113 | + } |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + if( numResults_ == -1 ) |
| 118 | + { |
| 119 | + if( numResults_ != -1 ) |
| 120 | + quote_.free_result(); |
| 121 | + |
| 122 | + int max = quote_.get_count( sql_select_count ); |
| 123 | + boost::mt19937 rng( time( NULL ) ) ; |
| 124 | + boost::uniform_int<> uni(1, max); |
| 125 | + boost::variate_generator<boost::mt19937&, boost::uniform_int<> > |
| 126 | + die(rng, uni); |
| 127 | + |
| 128 | + sql_select += " id=" + boost::lexical_cast<std::string>( die() ) + ";"; |
| 129 | + |
| 130 | + cout << sql_select << endl; |
| 131 | + quote_.free_result(); |
| 132 | + |
| 133 | + c_ << irc::PRIVMSG( m.param( 0 ), quote_.get_string( sql_select ) ); |
| 134 | + quote_.free_result(); |
| 135 | + |
| 136 | + return true; |
| 137 | + } |
| 138 | + if( (currResult_ <= numResults_) && (numResults_ > 0 ) ) |
| 139 | + { |
| 140 | + quote_.fetch_row(); |
| 141 | + |
| 142 | + std::string text = quote_.getstr(); |
| 143 | + text += " ( " + boost::lexical_cast<std::string>(numResults_ - currResult_++) + " left )"; |
| 144 | + |
| 145 | + if( numResults_ < currResult_ ) |
| 146 | + { |
| 147 | + quote_.free_result(); |
| 148 | + numResults_ = -1; |
| 149 | + } |
| 150 | + |
| 151 | + c_ << irc::PRIVMSG( m.param( 0 ), text ); |
| 152 | + |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + if( selector.length() != 0 ) |
| 159 | + { |
| 160 | + if( numResults_ != -1 ) |
| 161 | + quote_.free_result(); |
| 162 | + |
| 163 | + std::string text = m.param( 1 ).substr( 10, m.param( 1 ).length() ); |
| 164 | + boost::replace_all( text, "'", "\"" ); |
| 165 | + cout << text << endl; |
| 166 | + |
| 167 | + if( quote_.execute( "INSERT INTO quotes (text, channel) VALUES ('" +text+ "', '" +m.param(0)+ "' );" ) ) |
| 168 | + { |
| 169 | + quote_.free_result(); |
| 170 | + long id = quote_.get_count( "SELECT MAX(id) FROM quotes WHERE channel='" + m.param(0) + "';"); |
| 171 | + c_ << irc::PRIVMSG( m.param(0), |
| 172 | + "Added Quote with id: " + boost::lexical_cast<std::string>( id) + "." ); |
| 173 | + quote_.free_result(); |
| 174 | + } |
| 175 | + } |
| 176 | + return true; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return false; |
| 181 | + } |
| 182 | +} // end weberknecht |
0 commit comments