void* irc_thread_net(void *data) { irc_thread_data *self = (irc_thread_data*)data; irc_callbacks_t callbacks; irc_ctx_t ctx; irc_session_t *irc_s; void *socket = zmq_socket(self->d.context, ZMQ_PUSH); // "server" from irc -> tun int ret = zmq_connect(socket, "inproc://#irc_to_#tun"); if (ret) { irc_debug(self, "(irc_thread_net) error when creating IPC socket - %s", zmq_strerror(errno)); goto exit; } memset (&callbacks, 0, sizeof(callbacks)); callbacks.event_connect = event_connect; callbacks.event_join = event_join; callbacks.event_privmsg = event_message; callbacks.event_channel = event_message; ctx.channel = strdup(self->chan); ctx.nick = malloc(sizeof(char)*512); snprintf(ctx.nick, 511, self->nick, rand()); ctx.self = self; ctx.buffer = malloc(sizeof(char)*MTU*4); ctx.data = socket; // WE ARE PASSING A NON-THREAD-SAFE SOCKET HERE! </redwarning> irc_s = irc_create_session(&callbacks); if (!irc_s) { irc_debug(self, "error when creating irc_session"); goto exit; } self->irc_s = irc_s; irc_debug(self, "created irc_session!"); irc_set_ctx(self->irc_s, &ctx); if (irc_connect (self->irc_s, self->server, 6667, self->pass, ctx.nick, "ipoirc", "IP over IRC - http://github.com/EXio4/IPoIRC")) { irc_debug(self, "error when connecting to irc (%s)", irc_strerror(irc_errno(self->irc_s))); goto exit; } sleep(1); // wait for the network to answer THIS SHOULD BE DONE IN A RIGHT WAY! int rc = irc_run(self->irc_s); (void) rc; exit: if (self->irc_s) { irc_destroy_session(self->irc_s); } if (socket) { zmq_close(socket); } pthread_exit(NULL); }
void ChatNetworkingIRC::run() { IRCSessionContext* p_ctx = static_cast< IRCSessionContext* >( irc_get_ctx( _p_session ) ); // passing through an exception silently kills the process, very subtile source of error! try { // start the IRC protocol loop irc_run( _p_session ); } catch ( ... ) { log_error << "*** internal error occured in ChatNetworkingIRC::run" << std::endl; irc_destroy_session( _p_session ); } delete p_ctx; irc_destroy_session( _p_session ); _p_session = NULL; }
//////////////////////////////////////////////////////////////////////////////// // Dtor //virtual GaMatchmakingState::~GaMatchmakingState() { BcPrintf("~GaMatchmakingState();\n"); if( pSession_ != NULL ) { irc_destroy_session( pSession_ ); pSession_ = NULL; } }
Kommunikater::~Kommunikater() { Doc->closeDB(); delete Doc; delete Inter; irc_destroy_session(session); session = NULL; state.session = NULL; }
/* ==================== IRC_TerminateSession Terminates an IRC session completely, freeing allocated memory. The session handle may be later reused by another session. ==================== */ void IRC_TerminateSession(int handle) { irc_session_t *s; if(!IS_VALID_IRC_SESSION(handle)) { IRC_Printf("Attempted to terminate an invalid session %i\n", handle); return; } if(IRC_SessionIsConnected(handle)) IRC_DisconnectSession(handle, "Session terminated"); s = &irc_sessions[handle]; irc_destroy_session(s->session); s->session = NULL; IRC_Printf("Session %i terminated\n", handle); }
bool CslIrcThread::RemoveContext(CslIrcContext *context) { wxCriticalSectionLocker lock(m_section); wxInt32 i; if ((i=m_contexts.Index(context))==wxNOT_FOUND) return false; m_contexts.RemoveAt(i); irc_destroy_session(context->Session); delete context; return true; }
//////////////////////////////////////////////////////////////////////////////// // update eSysStateReturn GaMatchmakingState::main() { BcScopedLock< BcMutex > Lock( Lock_ ); BcReal Delta = SysKernel::pImpl()->getFrameTime(); switch( HandshakeState_ ) { case HSS_STUN: { // Only do once. if( MappedHandshakeAddr_ == 0 ) { if( doSTUN() ) { SysID_ = static_cast< BcU32 >( BcHash( (BcU8*)&MappedHandshakeAddr_, sizeof( MappedHandshakeAddr_ ) ) ); // Hash the mapped address to we don't broadcast it. if( MappedHandshakeAddr_ != 0 ) { HandshakeState_ = HSS_IDLE; } } else { HandshakeState_ = HSS_STUN; } } else { HandshakeState_ = HSS_IDLE; } } break; case HSS_IDLE: { ConnectTimer_ -= Delta; if( ConnectTimer_ < 0.0f ) { if( pSession_ == NULL ) { pSession_ = irc_create_session( &Callbacks_ ); } if( pSession_ != NULL && !irc_is_connected( pSession_ ) ) { irc_set_ctx( pSession_, this ); std::string Channel = "#testchannel"; BcFile File; if( File.open( "config.json" ) ) { char* pData = new char[ File.size() ]; File.read( pData, File.size() ); Json::Reader Reader; Json::Value Root; if( Reader.parse( pData, pData + File.size(), Root ) ) { Channel = Root["channel"].asCString(); } delete [] pData; } BcSPrintf( ScreenName_, "%s_%x", "PSY", BcRandom::Global.rand() ); BcSPrintf( Channel_, Channel.c_str() ); // Connect to the server. int RetVal = irc_connect( pSession_, "www.neilo.gd", 8000, NULL, ScreenName_, ScreenName_, ScreenName_ ); if( RetVal == 0 ) { // Start the thread to tick the client. BcThread::start( "EvtBridgeIRC" ); ClientID_ = BcErrorCode; RemoteHandshakeAddr_ = 0; RemoteHandshakePort_ = 0; //LocalHandshakeAddr_ = 0; //LocalHandshakePort_ = 0; //MappedHandshakeAddr_ = 0; //MappedHandshakePort_ = 0; HandshakeState_ = HSS_WAIT_INVITE; } else { BcThread::join(); irc_destroy_session( pSession_ ); pSession_ = NULL; } } } } break; case HSS_WAIT_INVITE: { InviteTimer_ -= Delta; if( InviteTimer_ < 0.0f ) { InviteTimer_ = BcAbs( BcRandom::Global.randReal() ) * 5.0f + 5.0f; // Send play with me message to channel. BcChar PlayBuffer[256]; BcSPrintf( PlayBuffer, "REQ:%u", SysID_ ); irc_cmd_msg( pSession_, Channel_, PlayBuffer ); } } break; case HSS_WAIT_ADDR: { HandshakeTimer_ -= Delta; if( HandshakeTimer_ < 0.0f ) { HandshakeState_ = HSS_WAIT_INVITE; } } break; case HSS_COMPLETE: { BcPrintf("GaMatchmakingState: Complete! ClientID of ours is %u\n", ClientID_); return sysSR_FINISHED; } break; } if( HandshakeState_ != HSS_STUN ) { if( HandshakeState_ != HSS_IDLE && ( pSession_ == NULL || !irc_is_connected( pSession_ ) ) ) { BcSleep( 0.1f ); BcThread::join(); BcSleep( 0.1f ); if( pSession_ != NULL ) { irc_destroy_session( pSession_ ); pSession_ = NULL; } HandshakeState_ = HSS_IDLE; ConnectTimer_ = 10.0f; } } return sysSR_CONTINUE; }
dazeus::Server::~Server() { irc_destroy_session(IRC); }
// Main function int main(int argc, char** argv){ irc_callbacks_t irc_callbacks; char* host_str; int host_len, ssl; I = 0; if(argc != 2) error(1, "Error: config file must be specified on comand line\n"); // load config from ini conf_file = argv[1]; C = iniparser_load(conf_file); // parse server config and generate a string to give to libircclient ssl = iniparser_getboolean(C, "server:ssl", 0); host_len = strlen(iniparser_getstring(C, "server:host", "")) + (ssl ? 1 : 0); host_str = malloc(host_len + 1); host_str[0] = '#'; host_str[host_len] = '\0'; strcpy(&host_str[ssl ? 1 : 0], iniparser_getstring(C, "server:host", "")); // init local data structures mem_init(); cbtable_init(); // init mysql S = mysql_init(NULL); if(mysql_real_connect(S, iniparser_getstring(C, "mysql:host", "localhost"), iniparser_getstring(C, "mysql:user", "root"), iniparser_getstring(C, "mysql:pass", ""), iniparser_getstring(C, "mysql:database", ""), iniparser_getint(C, "mysql:port", 0), 0, 0) == NULL) error(1, "Unable to connect to mysql: %s\n", mysql_error(S)); // init lua L = luaL_newstate(); luaL_openlibs(L); register_lua_functions(); if(luaL_dofile(L, iniparser_getstring(C,"bot:file","/dev/null"))){ size_t lua_errlen = 0; const char* lua_error = luaL_checklstring(L, -1, &lua_errlen); error(1, "Error processing Lua script:\n%s\n", lua_error); } // init libircclient memset(&irc_callbacks, 0, sizeof(irc_callbacks)); irc_callbacks.event_connect = event_generic; irc_callbacks.event_nick = event_generic; irc_callbacks.event_quit = event_generic; irc_callbacks.event_join = event_generic; irc_callbacks.event_part = event_generic; irc_callbacks.event_mode = event_generic; irc_callbacks.event_umode = event_generic; irc_callbacks.event_topic = event_generic; irc_callbacks.event_kick = event_generic; irc_callbacks.event_channel = event_command; irc_callbacks.event_privmsg = event_command; irc_callbacks.event_notice = event_generic; irc_callbacks.event_unknown = event_generic; irc_callbacks.event_invite = event_generic; irc_callbacks.event_ctcp_req = event_generic; irc_callbacks.event_ctcp_rep = event_generic; irc_callbacks.event_ctcp_action = event_generic; irc_callbacks.event_channel_notice = event_generic; irc_callbacks.event_numeric = event_numeric; do_quit = 0; while(!do_quit){ if(I) irc_destroy_session(I); I = irc_create_session(&irc_callbacks); if(!I) error(1, "Unable to create IRC session... probably out of memory\n"); irc_option_set(I, LIBIRC_OPTION_STRIPNICKS); irc_option_set(I, LIBIRC_OPTION_SSL_NO_VERIFY); //irc_option_set(I, LIBIRC_OPTION_DEBUG); // initialize irc server connection if(irc_connect(I, host_str, iniparser_getint(C,"server:port",6667), 0, iniparser_getstring(C,"bot:nick","bot"), iniparser_getstring(C,"bot:user","bot"), "libircclient" )) irc_error(I,1); // not sure why we need to sleep here, but if we don't, we can't connect sleep(1); // run the irc client loop if(irc_run(I)) irc_error(I,0); } // clean up mysql_close(S); irc_destroy_session(I); lua_close(L); cbtable_destroy(); iniparser_freedict(C); free(host_str); return EXIT_SUCCESS; }