DWORD IRCMsgThread::Run() { onSysMsg(L"IRCMsgThread Start"); iStatus=IRC_NORMAL; IRCMsgQueue.clear(); unsigned int bufferlen; std::string recvstring,msg,umsg; //TwitchIRC use MultiByte while(!bKillThread ) { //Receive First bufferlen=0; memset(buffer, 0, DEFAULT_BUFLEN); if(TheSocketPtr->recv_data((char*)buffer, bufferlen)) { //Disconnected iStatus=IRC_DISCONNECTED; break; } recvstring=std::string(buffer,bufferlen); //onDebugMsg(L"%ls",from_utf8(recvstring).c_str()); if(recvstring.compare(":tmi.twitch.tv NOTICE * :Login unsuccessful\r\n")==0 || //Twitch recvstring.compare(":tmi.twitch.tv NOTICE * :Error encountered while attempting login\r\n")==0 ){ //Justin Sleep(100); //Wait for logging onSysMsg(L"Login unsuccessful"); iStatus=IRC_WRONGLOGIN; break; }//else if(recvstring.compare(":[email protected] PRIVMSG #append :break\r\n")==0){TheSocketPtr->CloseSocket();} //Basic parsing: make it into a string, check for PING, if not-send for further parsing. std::stringstream recvstr(recvstring); while( recvstr.tellg() < bufferlen ) { std::getline(recvstr,msg,'\n'); //Old Style //:[email protected] PRIVMSG #ptken :what song is this ? //PING LAG1967655232 //int colon_pos = (int)msg.find(":"); //if (msg[0] != ':' && colon_pos > 0) msg=msg.substr(colon_pos); //if an irc message begins with " ", then trim the leading spaces // //New Style // @color=#FF4500;display-name=Gm470520;emotes=;mod=0;room-id=47281189;subscriber=1;turbo=0;user-id=24255667;user-type= :[email protected] PRIVMSG #tetristhegrandmaster3 :這片到底在沖三小w int at_pos = (int)msg.find("@"); if (msg[0] != '@' && at_pos > 0) msg=msg.substr(at_pos); //if an irc message begins with " ", then trim the leading spaces if (msg[msg.length()-1]=='\r') msg=msg.substr(0,msg.length()-1); //trim the CR: getline only trim the LF log(from_utf8(msg), LogType(RECEIVE)); //Although log file is in UTF8....using vswprintf //Ping Check umsg=msg.substr(0,5); ToUpperString(umsg); if (!umsg.compare("PING ")) sendRaw( L"PONG " + from_utf8(msg.substr(5)) ); // respond to PINGs //Reply Immediately //Then parse the message else parseMessage(from_utf8(msg)); } Sleep(100); } //Determine the reason to break the loop. if(!bKillThread) { if(iStatus==IRC_DISCONNECTED) onSysMsg(L"IRCMsgThread: Disconnect Unexpectedly"); if(iStatus==IRC_WRONGLOGIN) onSysMsg(L"IRCMsgThread: Wrong Login Information"); } else iStatus=IRC_CLOSED; //onDebugMsg(L"[Last Buffer]%ls",from_utf8(recvstring).c_str()); onSysMsg(L"IRCMsgThread Close"); thread=0; return 0; }
bool load_log_settings(const char *filename) { //this is a terrible algorithm, but im lazy today FILE *f = fopen(filename, "r"); if(f == NULL) return false; char linebuf[512], type_name[256], value[256]; while(!feof(f)) { if(fgets(linebuf, 512, f) == NULL) continue; #ifdef WIN32 if (sscanf(linebuf, "%[^=]=%[^\n]\n", type_name, value) != 2) continue; #else if (sscanf(linebuf, "%[^=]=%[^\r\n]\n", type_name, value) != 2) continue; #endif if(type_name[0] == '\0' || type_name[0] == '#') continue; //first make sure we understand the value bool enabled; if(!strcasecmp(value, "on") || !strcasecmp(value, "yes") || !strcasecmp(value, "enabled") || !strcmp(value, "1")) enabled = true; else if(!strcasecmp(value, "off") || !strcasecmp(value, "no") || !strcasecmp(value, "disabled") || !strcmp(value, "0")) enabled = false; else { printf("Unable to parse value '%s' from %s. Skipping line.", value, filename); continue; } int r; //first see if it is a category name for(r = 0; r < NUMBER_OF_LOG_CATEGORIES; r++) { if(!strcasecmp(log_category_names[r], type_name)) break; } if(r != NUMBER_OF_LOG_CATEGORIES) { //matched a category. int k; for(k = 0; k < NUMBER_OF_LOG_TYPES; k++) { if(log_type_info[k].category != r) continue; //does not match this category. if(enabled) log_enable(LogType(k)); else log_disable(LogType(k)); } continue; } for(r = 0; r < NUMBER_OF_LOG_TYPES; r++) { if(!strcasecmp(log_type_info[r].name, type_name)) break; } if(r == NUMBER_OF_LOG_TYPES) { printf("Unable to locate log type %s from file %s. Skipping line.", type_name, filename); continue; } //got it all figured out, do something now... if(enabled) log_enable(LogType(r)); else log_disable(LogType(r)); } fclose(f); return true; }
void IRCMsgThread::sendRaw(std::wstring message){ std::string send_msg=to_utf8(message+L"\r\n"); TheSocketPtr->send_data((char*)send_msg.c_str(),(unsigned int)send_msg.length()); log(message, LogType(SEND)); }