14 #include <libircclient.h>
20 #define IRC (irc_session_t*)irc_
24 std::stringstream res;
43 , whois_identified_(false)
50 irc_destroy_session(
IRC);
60 std::string reasonString;
64 reasonString =
"Shutting down";
67 reasonString =
"Reloading configuration";
70 reasonString =
"Switching servers";
73 reasonString =
"Timeout";
76 reasonString =
"Unknown error";
79 reasonString =
"An admin asked me to disconnect";
83 reasonString =
"See you around!";
92 fprintf(stderr,
"MOTD cannot be retrieved.\n");
97 irc_cmd_quit(
IRC, reason.c_str());
101 irc_cmd_whois(
IRC, destination.c_str());
109 void dazeus::Server::ircEventMe(
const std::string &eventname,
const std::string &destination,
const std::string &message) {
110 std::vector<std::string> parameters;
111 parameters.push_back(destination);
112 parameters.push_back(message);
113 slotIrcEvent(eventname, network_->nick(), parameters);
117 ircEventMe(
"ACTION_ME", destination, message);
118 irc_cmd_me(
IRC, destination.c_str(), message.c_str());
122 irc_cmd_names(
IRC, channel.c_str());
126 ircEventMe(
"CTCP_ME", destination, message);
127 irc_cmd_ctcp_request(
IRC, destination.c_str(), message.c_str());
131 irc_cmd_join(
IRC, channel.c_str(), key.c_str());
136 irc_cmd_part(
IRC, channel.c_str());
140 std::stringstream ss(message);
142 while(std::getline(ss, line)) {
143 ircEventMe(
"PRIVMSG_ME", destination, message);
144 irc_cmd_msg(
IRC, destination.c_str(), line.c_str());
149 irc_send_raw(
IRC,
"PING");
153 irc_add_select_descriptors(
IRC, in_set, out_set, maxfd);
157 irc_process_select_descriptors(
IRC, in_set, out_set);
161 const std::vector<std::string> &args )
163 assert( network_ != 0 );
164 assert( network_->activeServer() == this );
167 in_whois_for_ = args[1];
168 assert( !whois_identified_ );
171 else if(code == 307 || code == 330)
173 whois_identified_ =
true;
177 network_->slotWhoisReceived( origin, in_whois_for_, whois_identified_ );
178 std::vector<std::string> parameters;
179 parameters.push_back(in_whois_for_);
180 parameters.push_back(whois_identified_ ?
"true" :
"false");
181 slotIrcEvent(
"WHOIS", origin, parameters );
182 whois_identified_ =
false;
183 in_whois_for_.clear();
188 std::vector<std::string> names;
189 std::stringstream ss(args.back());
191 while(std::getline(ss, name,
' ')) {
192 in_names_.push_back(name);
197 network_->slotNamesReceived( origin, args.at(1), in_names_, args.at(0) );
198 std::vector<std::string> parameters;
199 parameters.push_back(args.at(1));
200 std::vector<std::string>::const_iterator it;
201 for(it = in_names_.begin(); it != in_names_.end(); ++it) {
202 parameters.push_back(*it);
204 slotIrcEvent(
"NAMES", origin, parameters );
209 std::vector<std::string> parameters;
210 parameters.push_back(args.at(1));
211 parameters.push_back(args.at(2));
212 slotIrcEvent(
"TOPIC", origin, parameters );
214 std::stringstream codestream;
216 std::vector<std::string> params;
217 params.push_back(codestream.str());
218 std::vector<std::string>::const_iterator it;
219 for(it = args.begin(); it != args.end(); ++it) {
220 params.push_back(*it);
222 slotIrcEvent(
"NUMERIC", origin, params );
227 network_->onFailedConnection();
232 assert(network_ != 0);
233 assert(network_->activeServer() ==
this);
234 network_->slotIrcEvent(event, origin, args);
237 void irc_eventcode_callback(irc_session_t *s,
unsigned int event,
const char *origin,
const char **p,
unsigned int count) {
239 std::vector<std::string> params;
240 for(
unsigned int i = 0; i < count; ++i) {
241 params.push_back(std::string(p[i]));
246 void irc_callback(irc_session_t *s,
const char *e,
const char *o,
const char **params,
unsigned int count) {
249 std::string event(e);
251 if(event ==
"CHANNEL_NOTICE") {
253 }
else if(event ==
"CHANNEL") {
260 origin = std::string(o);
261 size_t exclamMark = origin.find(
'!');
262 if(exclamMark != std::string::npos) {
263 origin = origin.substr(0, exclamMark);
266 std::vector<std::string> arguments;
267 for(
unsigned int i = 0; i < count; ++i) {
268 arguments.push_back(std::string(params[i]));
276 if(event ==
"ERROR") {
277 fprintf(stderr,
"Error received from libircclient; origin=%s.\n", origin.c_str());
279 }
else if(event ==
"CONNECT") {
288 printf(
"Connecting to server: %s\n", toString(
this).c_str());
289 assert( !config_->network->nickName.length() == 0 );
291 irc_callbacks_t callbacks;
292 memset(&callbacks, 0,
sizeof(irc_callbacks_t));
303 #if LIBIRC_VERSION_HIGH > 1 || LIBIRC_VERSION_LOW >= 6
315 irc_ = (
void*)irc_create_session(&callbacks);
317 std::cerr <<
"Couldn't create IRC session in Server.";
320 irc_set_ctx(
IRC,
this);
322 assert( config_->network->nickName.length() != 0 );
323 std::string host = config_->host;
325 #if defined(LIBIRC_OPTION_SSL_NO_VERIFY)
327 if(!config_->ssl_verify) {
328 std::cerr <<
"Warning: connecting without SSL certificate verification." << std::endl;
329 irc_option_set(
IRC, LIBIRC_OPTION_SSL_NO_VERIFY);
332 std::cerr <<
"Error: Your version of libircclient does not support SSL. Failing connection." << std::endl;
337 irc_connect(
IRC, host.c_str(),
339 config_->network->password.c_str(),
340 config_->network->nickName.c_str(),
341 config_->network->userName.c_str(),
342 config_->network->fullName.c_str());