X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnntp%2Fnntpconnection.cpp;h=431c1e6153e8347f2fc0d78cbe8805c5a07b1935;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=eecbec605844ac1c1ef1d0ecebf7ad60591a481f;hpb=3f6f19146f015fa8d2c89f1e72cd467dbc4115aa;p=fms.git diff --git a/src/nntp/nntpconnection.cpp b/src/nntp/nntpconnection.cpp index eecbec6..431c1e6 100644 --- a/src/nntp/nntpconnection.cpp +++ b/src/nntp/nntpconnection.cpp @@ -1,27 +1,25 @@ #include "../../include/nntp/nntpconnection.h" #include "../../include/nntp/uwildmat.h" #include "../../include/stringfunctions.h" -#include "../../include/datetime.h" #include "../../include/boardlist.h" #include "../../include/message.h" #include "../../include/messagelist.h" #include "../../include/option.h" #include "../../include/nntp/extensiontrust.h" +#include "../../include/threadwrapper/cancelablethread.h" #include - -//#include -#include "../../include/pthreadwrapper/thread.h" +#include +#include +#include #ifdef XMEM #include #endif -NNTPConnection::NNTPConnection(SOCKET sock) +NNTPConnection::NNTPConnection(SOCKET sock):m_socket(sock),m_status(0) { - std::string tempval; - m_socket=sock; m_tempbuffer.resize(32768); m_status.m_isposting=false; @@ -31,12 +29,6 @@ NNTPConnection::NNTPConnection(SOCKET sock) m_status.m_mode=MODE_NONE; m_status.m_authenticated=false; - Option::Instance()->Get("NNTPAllowPost",tempval); - if(tempval=="true") - { - m_status.m_allowpost=true; - } - } NNTPConnection::~NNTPConnection() @@ -97,7 +89,7 @@ const bool NNTPConnection::HandleAuthInfoCommand(const NNTPCommand &command) } if(arg=="USER") { - LocalIdentity localid; + LocalIdentity localid(m_db); if(localid.Load(name)) { m_status.m_authuser=localid; @@ -247,9 +239,8 @@ const bool NNTPConnection::HandleCommand(const NNTPCommand &command) const bool NNTPConnection::HandleDateCommand(const NNTPCommand &command) { - DateTime now; - now.SetToGMTime(); - SendBufferedLine("111 "+now.Format("%Y%m%d%H%M%S")); + Poco::DateTime now; + SendBufferedLine("111 "+Poco::DateTimeFormatter::format(now,"%Y%m%d%H%M%S")); return true; } @@ -275,7 +266,7 @@ const bool NNTPConnection::HandleGetTrustCommand(const NNTPCommand &command) nntpname+=command.m_arguments[i]; } - TrustExtension tr(m_status.m_authuser.GetID()); + TrustExtension tr(m_db,m_status.m_authuser.GetID()); if(type=="MESSAGE") { @@ -343,7 +334,7 @@ const bool NNTPConnection::HandleGetTrustListCommand(const NNTPCommand &command) { if(m_status.m_authenticated) { - TrustExtension tr(m_status.m_authuser.GetID()); + TrustExtension tr(m_db,m_status.m_authuser.GetID()); std::map trustlist; if(tr.GetTrustList(trustlist)) { @@ -412,7 +403,7 @@ const bool NNTPConnection::HandleGroupCommand(const NNTPCommand &command) { if(command.m_arguments.size()==1) { - Board board; + Board board(m_db); if(board.Load(command.m_arguments[0])==true) { std::ostringstream tempstr; @@ -435,7 +426,7 @@ const bool NNTPConnection::HandleGroupCommand(const NNTPCommand &command) else { SendBufferedLine("501 Syntax error"); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleGroupCommand syntax error"); + m_log->debug("NNTPConnection::HandleGroupCommand syntax error"); } return true; @@ -464,7 +455,7 @@ const bool NNTPConnection::HandleLastCommand(const NNTPCommand &command) { if(m_status.m_messageid!=-1) { - Message mess; + Message mess(m_db); if(mess.LoadPrevious(m_status.m_messageid,m_status.m_boardid)) { @@ -534,7 +525,7 @@ const bool NNTPConnection::HandleListCommand(const NNTPCommand &command) { bool show; std::ostringstream tempstr; - BoardList bl; + BoardList bl(m_db); bl.Load(); SendBufferedLine("215 list of newsgroups follows"); @@ -565,7 +556,7 @@ const bool NNTPConnection::HandleListCommand(const NNTPCommand &command) { bool show; std::ostringstream tempstr; - BoardList bl; + BoardList bl(m_db); bl.Load(); SendBufferedLine("215 list of newsgroups follows"); @@ -608,7 +599,7 @@ const bool NNTPConnection::HandleListCommand(const NNTPCommand &command) { // unknown arg SendBufferedLine("501 Syntax error"); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleListCommand unhandled LIST variant"); + m_log->debug("NNTPConnection::HandleListCommand unhandled LIST variant"); } return true; @@ -618,7 +609,7 @@ const bool NNTPConnection::HandleListGroupCommand(const NNTPCommand &command) { std::ostringstream tempstr; - Board board; + Board board(m_db); bool validgroup=false; int lownum=-1; int highnum=-1; @@ -665,7 +656,7 @@ const bool NNTPConnection::HandleListGroupCommand(const NNTPCommand &command) { // unknown arg SendBufferedLine("501 Syntax error"); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleListGroupCommand unknown arguments"); + m_log->debug("NNTPConnection::HandleListGroupCommand unknown arguments"); } if(validgroup) @@ -687,7 +678,7 @@ const bool NNTPConnection::HandleListGroupCommand(const NNTPCommand &command) tempstr << "211 " << board.GetMessageCount() << " " << board.GetLowMessageID() << " " << board.GetHighMessageID() << " " << board.GetBoardName(); SendBufferedLine(tempstr.str()); - MessageList ml; + MessageList ml(m_db); ml.LoadRange(lownum,highnum,board.GetBoardID()); for(std::vector::iterator i=ml.begin(); i!=ml.end(); i++) @@ -724,18 +715,18 @@ const bool NNTPConnection::HandleModeCommand(const NNTPCommand &command) SendBufferedLine("201 Posting prohibited"); } - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleModeCommand set mode to reader"); + m_log->debug("NNTPConnection::HandleModeCommand set mode to reader"); } else { SendBufferedLine("501 Syntax error"); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleModeCommand unknown MODE argument : "+arg); + m_log->debug("NNTPConnection::HandleModeCommand unknown MODE argument : "+arg); } } else { SendBufferedLine("501 Syntax error"); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleModeCommand no argument supplied for MODE"); + m_log->debug("NNTPConnection::HandleModeCommand no argument supplied for MODE"); } return true; @@ -745,16 +736,23 @@ const bool NNTPConnection::HandleNewGroupsCommand(const NNTPCommand &command) { if(command.m_arguments.size()>=2) { - DateTime date; - int tempint; + Poco::DateTime date; + int tempyear=0; + int tempmonth=0; + int tempday=0; if(command.m_arguments[0].size()==8) { - StringFunctions::Convert(command.m_arguments[0].substr(0,4),tempint); - date.SetYear(tempint); - StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempint); - date.SetMonth(tempint); - StringFunctions::Convert(command.m_arguments[0].substr(6,2),tempint); - date.SetDay(tempint); + StringFunctions::Convert(command.m_arguments[0].substr(0,4),tempyear); + StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempmonth); + StringFunctions::Convert(command.m_arguments[0].substr(6,2),tempday); + try + { + date.assign(tempyear,tempmonth,tempday,date.hour(),date.minute(),date.second()); + } + catch(...) + { + m_log->fatal("NNTPConnection::HandleNewGroupsCommand error assigning date"); + } } else { @@ -766,27 +764,29 @@ const bool NNTPConnection::HandleNewGroupsCommand(const NNTPCommand &command) the current year, and the previous century otherwise. */ int century; - DateTime now; - now.SetToGMTime(); - century=now.GetYear()-(now.GetYear()%100); + Poco::DateTime now; + century=now.year()-(now.year()%100); - StringFunctions::Convert(command.m_arguments[0].substr(0,2),tempint); - tempint<=now.GetYear()-century ? tempint+=century : tempint+=(century-100); + StringFunctions::Convert(command.m_arguments[0].substr(0,2),tempyear); + tempyear<=now.year()-century ? tempyear+=century : tempyear+=(century-100); //tempint > 50 ? tempint+=1900 : tempint+=2000; - date.SetYear(tempint); - StringFunctions::Convert(command.m_arguments[0].substr(2,2),tempint); - date.SetMonth(tempint); - StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempint); - date.SetDay(tempint); + StringFunctions::Convert(command.m_arguments[0].substr(2,2),tempmonth); + StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempday); + try + { + date.assign(tempyear,tempmonth,tempday); + } + catch(...) + { + m_log->fatal("NNTPConnection::HandleNewGroupsCommand error assigning date"); + } } - date.Normalize(); - - BoardList bl; + BoardList bl(m_db); - bl.LoadNew(date.Format("%Y-%m-%d %H:%M:%S")); + bl.LoadNew(Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S")); SendBufferedLine("231 List of new newsgroups follows"); @@ -806,7 +806,7 @@ const bool NNTPConnection::HandleNewGroupsCommand(const NNTPCommand &command) else { SendBufferedLine("501 Syntax error"); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleNewGroupsCommand syntax error"); + m_log->debug("NNTPConnection::HandleNewGroupsCommand syntax error"); } return true; @@ -819,7 +819,7 @@ const bool NNTPConnection::HandleNextCommand(const NNTPCommand &command) { if(m_status.m_messageid!=-1) { - Message mess; + Message mess(m_db); if(mess.LoadNext(m_status.m_messageid,m_status.m_boardid)) { @@ -909,7 +909,7 @@ const bool NNTPConnection::HandleOverCommand(const NNTPCommand &command) if(messageuuid!="") { - Message mess; + Message mess(m_db); if(mess.Load(messageuuid)) { SendBufferedLine("224 Overview information follows"); @@ -923,13 +923,13 @@ const bool NNTPConnection::HandleOverCommand(const NNTPCommand &command) } else { - Board bd; + Board bd(m_db); if(m_status.m_boardid!=-1 && bd.Load(m_status.m_boardid)) { // single message if(highmessageid==-2) { - Message mess; + Message mess(m_db); if(mess.Load(lowmessageid,m_status.m_boardid)) { SendBufferedLine("224 Overview information follows"); @@ -944,7 +944,7 @@ const bool NNTPConnection::HandleOverCommand(const NNTPCommand &command) // range with no upper bound else if(highmessageid==-1) { - MessageList ml; + MessageList ml(m_db); ml.LoadRange(lowmessageid,bd.GetHighMessageID(),m_status.m_boardid); if(ml.size()>0) { @@ -963,7 +963,7 @@ const bool NNTPConnection::HandleOverCommand(const NNTPCommand &command) // range with upper and lower bound else if(highmessageid>=lowmessageid) { - MessageList ml; + MessageList ml(m_db); ml.LoadRange(lowmessageid,highmessageid,m_status.m_boardid); if(ml.size()>0) { @@ -1012,7 +1012,7 @@ const bool NNTPConnection::HandlePostCommand(const NNTPCommand &command) void NNTPConnection::HandlePostedMessage(const std::string &message) { - Message mess; + Message mess(m_db); if(mess.ParseNNTPMessage(message)) { @@ -1077,7 +1077,7 @@ void NNTPConnection::HandleReceivedData() { SendBufferedLine("500 Unknown command"); - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleReceivedData received unhandled NNTP command : "+commandline); + m_log->debug("NNTPConnection::HandleReceivedData received unhandled NNTP command : "+commandline); } } @@ -1186,7 +1186,7 @@ const bool NNTPConnection::HandleSetTrustCommand(const NNTPCommand &command) valid=true; } - TrustExtension tr(m_status.m_authuser.GetID()); + TrustExtension tr(m_db,m_status.m_authuser.GetID()); if(type=="MESSAGE") { @@ -1260,15 +1260,26 @@ const bool NNTPConnection::HandleQuitCommand(const NNTPCommand &command) SendBufferedLine("205 Connection Closing"); SocketSend(); Disconnect(); - m_log->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPConnection::HandleQuitCommand client closed connection"); + m_log->information("NNTPConnection::HandleQuitCommand client closed connection"); return true; } -void NNTPConnection::Run() +void NNTPConnection::run() { struct timeval tv; fd_set writefs,readfs; int rval; + std::string tempval(""); + + LoadDatabase(); + + m_status.m_authuser.SetDB(m_db); + Option option(m_db); + option.Get("NNTPAllowPost",tempval); + if(tempval=="true") + { + m_status.m_allowpost=true; + } // seed random number generater for this thread srand(time(NULL)); @@ -1312,10 +1323,17 @@ void NNTPConnection::Run() } else if(rval==SOCKET_ERROR) { - m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPConnection::run select returned -1 : "+GetSocketErrorMessage()); + m_log->error("NNTPConnection::run select returned -1 : "+GetSocketErrorMessage()); + } + + //process all remaining commands in buffer + std::vector::size_type rbs=0; + while(rbs!=m_receivebuffer.size()) + { + rbs=m_receivebuffer.size(); + HandleReceivedData(); } -// }while(!Disconnected() && !ZThread::Thread::interrupted()); }while(!Disconnected() && !IsCancelled()); Disconnect(); @@ -1332,7 +1350,7 @@ void NNTPConnection::SendArticleOverInfo(Message &message) line=tempval+"\t"; line+=message.GetSubject()+"\t"; line+=message.GetFromName()+"\t"; - line+=message.GetDateTime().Format("%a, %d %b %y %H:%M:%S -0000")+"\t"; + line+=Poco::DateTimeFormatter::format(message.GetDateTime(),"%w, %d %b %y %H:%M:%S -0000")+"\t"; line+=message.GetNNTPArticleID()+"\t"; references=message.GetInReplyTo(); if(references.size()>0) @@ -1386,7 +1404,7 @@ void NNTPConnection::SendArticleParts(const NNTPConnection::NNTPCommand &command successcode="223"; } - Message message; + Message message(m_db); int messageid=m_status.m_messageid; std::string articleid=""; int type=0; // default to current messageid, 1=messageid, 2=articleid @@ -1577,7 +1595,7 @@ void NNTPConnection::SocketReceive() else if(rval==0) { Disconnect(); - m_log->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPConnection::SocketReceive remote host closed connection"); + m_log->information("NNTPConnection::SocketReceive remote host closed connection"); } else if(rval==-1) { @@ -1585,7 +1603,7 @@ void NNTPConnection::SocketReceive() StringFunctions::Convert(GetSocketErrorNumber(),errnostr); // error on receive - close the connection Disconnect(); - m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPConnection::SocketReceive recv returned -1 : "+errnostr+" - "+GetSocketErrorMessage()); + m_log->error("NNTPConnection::SocketReceive recv returned -1 : "+errnostr+" - "+GetSocketErrorMessage()); } } @@ -1602,7 +1620,7 @@ void NNTPConnection::SocketSend() { std::string errnostr; StringFunctions::Convert(GetSocketErrorNumber(),errnostr); - m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPConnection::SocketSend returned -1 : "+errnostr+" - "+GetSocketErrorMessage()); + m_log->error("NNTPConnection::SocketSend returned -1 : "+errnostr+" - "+GetSocketErrorMessage()); } } }