version 0.3.19
[fms.git] / src / nntp / nntpconnection.cpp
index 99914cd..84d07ff 100644 (file)
@@ -1,17 +1,17 @@
 #include "../../include/nntp/nntpconnection.h"\r
 #include "../../include/nntp/uwildmat.h"\r
 #include "../../include/stringfunctions.h"\r
-#include "../../include/datetime.h"\r
 #include "../../include/boardlist.h"\r
 #include "../../include/message.h"\r
 #include "../../include/messagelist.h"\r
 #include "../../include/option.h"\r
 #include "../../include/nntp/extensiontrust.h"\r
+#include "../../include/threadwrapper/cancelablethread.h"\r
 \r
 #include <algorithm>\r
-\r
-//#include <zthread/Thread.h>\r
-#include "../../include/pthreadwrapper/thread.h"\r
+#include <Poco/DateTime.h>\r
+#include <Poco/DateTimeFormatter.h>\r
+#include <Poco/Timestamp.h>\r
 \r
 #ifdef XMEM\r
        #include <xmem.h>\r
@@ -247,9 +247,8 @@ const bool NNTPConnection::HandleCommand(const NNTPCommand &command)
 \r
 const bool NNTPConnection::HandleDateCommand(const NNTPCommand &command)\r
 {\r
-       DateTime now;\r
-       now.SetToGMTime();\r
-       SendBufferedLine("111 "+now.Format("%Y%m%d%H%M%S"));\r
+       Poco::DateTime now;\r
+       SendBufferedLine("111 "+Poco::DateTimeFormatter::format(now,"%Y%m%d%H%M%S"));\r
        return true;\r
 }\r
 \r
@@ -268,6 +267,10 @@ const bool NNTPConnection::HandleGetTrustCommand(const NNTPCommand &command)
                                std::string nntpname="";\r
                                for(int i=1; i<command.m_arguments.size(); i++)\r
                                {\r
+                                       if(i!=1)\r
+                                       {\r
+                                               nntpname+=" ";\r
+                                       }\r
                                        nntpname+=command.m_arguments[i];\r
                                }\r
 \r
@@ -340,31 +343,54 @@ const bool NNTPConnection::HandleGetTrustListCommand(const NNTPCommand &command)
        if(m_status.m_authenticated)\r
        {\r
                TrustExtension tr(m_status.m_authuser.GetID());\r
-               std::map<std::string,std::pair<int,int> > trustlist;\r
+               std::map<std::string,TrustExtension::trust> trustlist;\r
                if(tr.GetTrustList(trustlist))\r
                {\r
                        SendBufferedLine("280 Trust list follows");\r
-                       for(std::map<std::string,std::pair<int,int> >::iterator i=trustlist.begin(); i!=trustlist.end(); i++)\r
+                       for(std::map<std::string,TrustExtension::trust>::iterator i=trustlist.begin(); i!=trustlist.end(); i++)\r
                        {\r
                                std::ostringstream tempstr;\r
                                tempstr << (*i).first << "\t";\r
-                               if((*i).second.first>-1)\r
+                               if((*i).second.m_localmessagetrust>-1)\r
                                {\r
-                                       tempstr << (*i).second.first;\r
+                                       tempstr << (*i).second.m_localmessagetrust;\r
                                } \r
                                else\r
                                {\r
                                        tempstr << "null";\r
                                }\r
                                tempstr << "\t";\r
-                               if((*i).second.second>-1)\r
+                               if((*i).second.m_localtrustlisttrust>-1)\r
+                               {\r
+                                       tempstr << (*i).second.m_localtrustlisttrust;\r
+                               }\r
+                               else\r
+                               {\r
+                                       tempstr << "null";\r
+                               }\r
+                               tempstr << "\t";\r
+                               if((*i).second.m_peermessagetrust>-1)\r
+                               {\r
+                                       tempstr << (*i).second.m_peermessagetrust;\r
+                               }\r
+                               else\r
+                               {\r
+                                       tempstr << "null";\r
+                               }\r
+                               tempstr << "\t";\r
+                               if((*i).second.m_peertrustlisttrust>-1)\r
                                {\r
-                                       tempstr << (*i).second.second;\r
+                                       tempstr << (*i).second.m_peertrustlisttrust;\r
                                }\r
                                else\r
                                {\r
                                        tempstr << "null";\r
                                }\r
+                               tempstr << "\t";\r
+                               tempstr << (*i).second.m_messagetrustcomment;\r
+                               tempstr << "\t";\r
+                               tempstr << (*i).second.m_trustlisttrustcomment;\r
+\r
                                SendBufferedLine(tempstr.str());\r
                        }\r
                        SendBufferedLine(".");\r
@@ -408,7 +434,7 @@ const bool NNTPConnection::HandleGroupCommand(const NNTPCommand &command)
        else\r
        {\r
                SendBufferedLine("501 Syntax error");\r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleGroupCommand syntax error");\r
+               m_log->debug("NNTPConnection::HandleGroupCommand syntax error");\r
        }\r
 \r
        return true;\r
@@ -581,7 +607,7 @@ const bool NNTPConnection::HandleListCommand(const NNTPCommand &command)
        {\r
                // unknown arg\r
                SendBufferedLine("501 Syntax error");\r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleListCommand unhandled LIST variant");\r
+               m_log->debug("NNTPConnection::HandleListCommand unhandled LIST variant");\r
        }\r
 \r
        return true;\r
@@ -638,7 +664,7 @@ const bool NNTPConnection::HandleListGroupCommand(const NNTPCommand &command)
        {\r
                // unknown arg\r
                SendBufferedLine("501 Syntax error");\r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleListGroupCommand unknown arguments");\r
+               m_log->debug("NNTPConnection::HandleListGroupCommand unknown arguments");\r
        }\r
 \r
        if(validgroup)\r
@@ -697,18 +723,18 @@ const bool NNTPConnection::HandleModeCommand(const NNTPCommand &command)
                                SendBufferedLine("201 Posting prohibited");\r
                        }\r
                        \r
-                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleModeCommand set mode to reader");\r
+                       m_log->debug("NNTPConnection::HandleModeCommand set mode to reader");\r
                }\r
                else\r
                {\r
                        SendBufferedLine("501 Syntax error");\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleModeCommand unknown MODE argument : "+arg);\r
+                       m_log->debug("NNTPConnection::HandleModeCommand unknown MODE argument : "+arg);\r
                }\r
        }\r
        else\r
        {\r
                SendBufferedLine("501 Syntax error");\r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleModeCommand no argument supplied for MODE");     \r
+               m_log->debug("NNTPConnection::HandleModeCommand no argument supplied for MODE");        \r
        }\r
 \r
        return true;\r
@@ -718,16 +744,23 @@ const bool NNTPConnection::HandleNewGroupsCommand(const NNTPCommand &command)
 {\r
        if(command.m_arguments.size()>=2)\r
        {\r
-               DateTime date;\r
-               int tempint;\r
+               Poco::DateTime date;\r
+               int tempyear=0;\r
+               int tempmonth=0;\r
+               int tempday=0;\r
                if(command.m_arguments[0].size()==8)\r
                {\r
-                       StringFunctions::Convert(command.m_arguments[0].substr(0,4),tempint);\r
-                       date.SetYear(tempint);\r
-                       StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempint);\r
-                       date.SetMonth(tempint);\r
-                       StringFunctions::Convert(command.m_arguments[0].substr(6,2),tempint);\r
-                       date.SetDay(tempint);\r
+                       StringFunctions::Convert(command.m_arguments[0].substr(0,4),tempyear);\r
+                       StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempmonth);\r
+                       StringFunctions::Convert(command.m_arguments[0].substr(6,2),tempday);\r
+                       try\r
+                       {\r
+                               date.assign(tempyear,tempmonth,tempday,date.hour(),date.minute(),date.second());\r
+                       }\r
+                       catch(...)\r
+                       {\r
+                               m_log->fatal("NNTPConnection::HandleNewGroupsCommand error assigning date");\r
+                       }\r
                }\r
                else\r
                {\r
@@ -739,27 +772,29 @@ const bool NNTPConnection::HandleNewGroupsCommand(const NNTPCommand &command)
                        the current year, and the previous century otherwise.\r
                        */\r
                        int century;\r
-                       DateTime now;\r
-                       now.SetToGMTime();\r
-                       century=now.GetYear()-(now.GetYear()%100);\r
+                       Poco::DateTime now;\r
+                       century=now.year()-(now.year()%100);\r
 \r
-                       StringFunctions::Convert(command.m_arguments[0].substr(0,2),tempint);\r
-                       tempint<=now.GetYear()-century ? tempint+=century : tempint+=(century-100);\r
+                       StringFunctions::Convert(command.m_arguments[0].substr(0,2),tempyear);\r
+                       tempyear<=now.year()-century ? tempyear+=century : tempyear+=(century-100);\r
                        \r
                        //tempint > 50 ? tempint+=1900 : tempint+=2000;\r
                        \r
-                       date.SetYear(tempint);\r
-                       StringFunctions::Convert(command.m_arguments[0].substr(2,2),tempint);\r
-                       date.SetMonth(tempint);\r
-                       StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempint);\r
-                       date.SetDay(tempint);\r
+                       StringFunctions::Convert(command.m_arguments[0].substr(2,2),tempmonth);\r
+                       StringFunctions::Convert(command.m_arguments[0].substr(4,2),tempday);\r
+                       try\r
+                       {\r
+                               date.assign(tempyear,tempmonth,tempday);\r
+                       }\r
+                       catch(...)\r
+                       {\r
+                               m_log->fatal("NNTPConnection::HandleNewGroupsCommand error assigning date");\r
+                       }\r
                }\r
 \r
-               date.Normalize();\r
-\r
                BoardList bl;\r
 \r
-               bl.LoadNew(date.Format("%Y-%m-%d %H:%M:%S"));\r
+               bl.LoadNew(Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S"));\r
 \r
                SendBufferedLine("231 List of new newsgroups follows");\r
 \r
@@ -779,7 +814,7 @@ const bool NNTPConnection::HandleNewGroupsCommand(const NNTPCommand &command)
        else\r
        {\r
                SendBufferedLine("501 Syntax error");\r
-               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleNewGroupsCommand syntax error");\r
+               m_log->debug("NNTPConnection::HandleNewGroupsCommand syntax error");\r
        }\r
 \r
        return true;\r
@@ -1050,7 +1085,7 @@ void NNTPConnection::HandleReceivedData()
                        {\r
                                SendBufferedLine("500 Unknown command");\r
 \r
-                               m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"NNTPConnection::HandleReceivedData received unhandled NNTP command : "+commandline);\r
+                               m_log->debug("NNTPConnection::HandleReceivedData received unhandled NNTP command : "+commandline);\r
                        }\r
 \r
                }\r
@@ -1086,26 +1121,76 @@ const bool NNTPConnection::HandleSetTrustCommand(const NNTPCommand &command)
        {\r
                std::string type=command.m_arguments[0];\r
                StringFunctions::UpperCase(type,type);\r
-               if(type=="MESSAGE" || type=="TRUSTLIST")\r
+               if(type=="MESSAGE" || type=="TRUSTLIST" || type=="MESSAGECOMMENT" || type=="TRUSTLISTCOMMENT")\r
                {\r
                        if(m_status.m_authenticated)\r
                        {\r
                                bool found=false;\r
                                bool valid=false;\r
                                int trust=-1;\r
+                               std::string comment="";\r
                                std::string nntpname="";\r
-                               for(int i=1; i<command.m_arguments.size()-1; i++)\r
-                               {\r
-                                       nntpname+=command.m_arguments[i];\r
-                               }\r
 \r
-                               if(command.m_arguments[command.m_arguments.size()-1]!="null")\r
+                               if(type=="MESSAGE" || type=="TRUSTLIST")\r
                                {\r
-                                       StringFunctions::Convert(command.m_arguments[command.m_arguments.size()-1],trust);\r
-                               }\r
+                                       for(int i=1; i<command.m_arguments.size()-1; i++)\r
+                                       {\r
+                                               if(i!=1)\r
+                                               {\r
+                                                       nntpname+=" ";\r
+                                               }\r
+                                               nntpname+=command.m_arguments[i];\r
+                                       }\r
+\r
+                                       if(command.m_arguments[command.m_arguments.size()-1]!="null")\r
+                                       {\r
+                                               StringFunctions::Convert(command.m_arguments[command.m_arguments.size()-1],trust);\r
+                                       }\r
 \r
-                               if(trust>=-1 && trust<=100)\r
+                                       if(trust>=-1 && trust<=100)\r
+                                       {\r
+                                               valid=true;\r
+                                       }\r
+                               }\r
+                               else\r
                                {\r
+                                       int startpos=-1;\r
+                                       // get nntpname\r
+                                       for(int i=1; i<command.m_arguments.size() && startpos==-1; i++)\r
+                                       {\r
+                                               if(command.m_arguments[i].size()>0 && command.m_arguments[i][0]!='\"')\r
+                                               {\r
+                                                       if(i!=1)\r
+                                                       {\r
+                                                               nntpname+=" ";\r
+                                                       }\r
+                                                       nntpname+=command.m_arguments[i];\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       startpos=i;\r
+                                               }\r
+                                       }\r
+\r
+                                       // get comment\r
+                                       for(int i=startpos; i<command.m_arguments.size(); i++)\r
+                                       {\r
+                                               if(i!=startpos)\r
+                                               {\r
+                                                       comment+=" ";\r
+                                               }\r
+                                               comment+=command.m_arguments[i];\r
+                                       }\r
+                                       // strip " from comment beginning and end\r
+                                       if(comment.size()>0 && comment[0]=='\"')\r
+                                       {\r
+                                               comment.erase(0,1);\r
+                                       }\r
+                                       if(comment.size()>0 && comment[comment.size()-1]=='\"')\r
+                                       {\r
+                                               comment.erase(comment.size()-1);\r
+                                       }\r
+\r
                                        valid=true;\r
                                }\r
 \r
@@ -1125,6 +1210,20 @@ const bool NNTPConnection::HandleSetTrustCommand(const NNTPCommand &command)
                                                found=true;\r
                                        }\r
                                }\r
+                               if(type=="MESSAGECOMMENT")\r
+                               {\r
+                                       if(tr.SetMessageTrustComment(nntpname,comment))\r
+                                       {\r
+                                               found=true;\r
+                                       }\r
+                               }\r
+                               if(type=="TRUSTLISTCOMMENT")\r
+                               {\r
+                                       if(tr.SetTrustListTrustComment(nntpname,comment))\r
+                                       {\r
+                                               found=true;\r
+                                       }\r
+                               }\r
 \r
                                if(found && valid)\r
                                {\r
@@ -1169,11 +1268,11 @@ const bool NNTPConnection::HandleQuitCommand(const NNTPCommand &command)
        SendBufferedLine("205 Connection Closing");\r
        SocketSend();\r
        Disconnect();\r
-       m_log->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPConnection::HandleQuitCommand client closed connection");\r
+       m_log->information("NNTPConnection::HandleQuitCommand client closed connection");\r
        return true;\r
 }\r
 \r
-void NNTPConnection::Run()\r
+void NNTPConnection::run()\r
 {\r
        struct timeval tv;\r
        fd_set writefs,readfs;\r
@@ -1221,10 +1320,17 @@ void NNTPConnection::Run()
                }\r
                else if(rval==SOCKET_ERROR)\r
                {\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPConnection::run select returned -1 : "+GetSocketErrorMessage());   \r
+                       m_log->error("NNTPConnection::run select returned -1 : "+GetSocketErrorMessage());      \r
+               }\r
+\r
+               //process all remaining commands in buffer\r
+               std::vector<char>::size_type rbs=0;\r
+               while(rbs!=m_receivebuffer.size())\r
+               {\r
+                       rbs=m_receivebuffer.size();\r
+                       HandleReceivedData();\r
                }\r
 \r
-//     }while(!Disconnected() && !ZThread::Thread::interrupted());\r
        }while(!Disconnected() && !IsCancelled());\r
 \r
        Disconnect();\r
@@ -1241,7 +1347,7 @@ void NNTPConnection::SendArticleOverInfo(Message &message)
        line=tempval+"\t";\r
        line+=message.GetSubject()+"\t";\r
        line+=message.GetFromName()+"\t";\r
-       line+=message.GetDateTime().Format("%a, %d %b %y %H:%M:%S -0000")+"\t";\r
+       line+=Poco::DateTimeFormatter::format(message.GetDateTime(),"%w, %d %b %y %H:%M:%S -0000")+"\t";\r
        line+=message.GetNNTPArticleID()+"\t";\r
        references=message.GetInReplyTo();\r
        if(references.size()>0)\r
@@ -1486,7 +1592,7 @@ void NNTPConnection::SocketReceive()
        else if(rval==0)\r
        {\r
                Disconnect();\r
-               m_log->WriteLog(LogFile::LOGLEVEL_INFO,"NNTPConnection::SocketReceive remote host closed connection");\r
+               m_log->information("NNTPConnection::SocketReceive remote host closed connection");\r
        }\r
        else if(rval==-1)\r
        {\r
@@ -1494,7 +1600,7 @@ void NNTPConnection::SocketReceive()
                StringFunctions::Convert(GetSocketErrorNumber(),errnostr);\r
                // error on receive - close the connection\r
                Disconnect();\r
-               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPConnection::SocketReceive recv returned -1 : "+errnostr+" - "+GetSocketErrorMessage());\r
+               m_log->error("NNTPConnection::SocketReceive recv returned -1 : "+errnostr+" - "+GetSocketErrorMessage());\r
        }\r
 }\r
 \r
@@ -1511,7 +1617,7 @@ void NNTPConnection::SocketSend()
                {\r
                        std::string errnostr;\r
                        StringFunctions::Convert(GetSocketErrorNumber(),errnostr);\r
-                       m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"NNTPConnection::SocketSend returned -1 : "+errnostr+" - "+GetSocketErrorMessage());\r
+                       m_log->error("NNTPConnection::SocketSend returned -1 : "+errnostr+" - "+GetSocketErrorMessage());\r
                }\r
        }\r
 }\r