a3b9b22c986e703620fc0d14f6e927105036ae25
[fms.git] / src / freenet / boardlistinserter.cpp
1 #include "../../include/freenet/boardlistinserter.h"\r
2 #include "../../include/freenet/boardlistxml.h"\r
3 \r
4 #include <Poco/DateTime.h>\r
5 #include <Poco/Timespan.h>\r
6 #include <Poco/DateTimeFormatter.h>\r
7 \r
8 #ifdef XMEM\r
9         #include <xmem.h>\r
10 #endif\r
11 \r
12 BoardListInserter::BoardListInserter()\r
13 {\r
14         Initialize();\r
15 }\r
16 \r
17 BoardListInserter::BoardListInserter(FCPv2::Connection *fcp):IIndexInserter<long>(fcp)\r
18 {\r
19         Initialize();\r
20 }\r
21 \r
22 void BoardListInserter::CheckForNeededInsert()\r
23 {\r
24         // only do 1 insert at a time\r
25         if(m_inserting.size()==0)\r
26         {\r
27                 Poco::DateTime today;\r
28                 Poco::DateTime daysback;\r
29 \r
30                 // 20 days back\r
31                 daysback-=Poco::Timespan(20,0,0,0,0);\r
32 \r
33                 // get identities who posted messages to boards in the past 20 days\r
34                 SQLite3DB::Statement st=m_db->Prepare("SELECT tblLocalIdentity.LocalIdentityID FROM tblLocalIdentity INNER JOIN tblMessageInserts ON tblLocalIdentity.LocalIdentityID=tblMessageInserts.LocalIdentityID WHERE tblLocalIdentity.PublishBoardList='true' AND (tblLocalIdentity.LastInsertedBoardList<? OR tblLocalIdentity.LastInsertedBoardList IS NULL) AND tblMessageInserts.Day>=? GROUP BY tblLocalIdentity.LocalIdentityID;");\r
35                 st.Bind(0,Poco::DateTimeFormatter::format(today,"%Y-%m-%d"));\r
36                 st.Bind(1,Poco::DateTimeFormatter::format(daysback,"%Y-%m-%d"));\r
37                 st.Step();\r
38 \r
39                 if(st.RowReturned())\r
40                 {\r
41                         int localidentityid;\r
42                         st.ResultInt(0,localidentityid);\r
43                         StartInsert(localidentityid);\r
44                 }\r
45         }\r
46 }\r
47 \r
48 const bool BoardListInserter::HandlePutFailed(FCPv2::Message &message)\r
49 {\r
50         std::vector<std::string> idparts;\r
51         long localidentityid;\r
52         long index;\r
53 \r
54         StringFunctions::Split(message["Identifier"],"|",idparts);\r
55         StringFunctions::Convert(idparts[1],localidentityid);\r
56         StringFunctions::Convert(idparts[2],index);\r
57 \r
58         if(message["Fatal"]=="true" || message["Code"]=="9")\r
59         {\r
60                 SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblBoardListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');");\r
61                 st.Bind(0,localidentityid);\r
62                 st.Bind(1,idparts[4]);\r
63                 st.Bind(2,index);\r
64                 st.Step();\r
65         }\r
66 \r
67         RemoveFromInsertList(localidentityid);\r
68 \r
69         return true;\r
70 }\r
71 \r
72 const bool BoardListInserter::HandlePutSuccessful(FCPv2::Message &message)\r
73 {\r
74         Poco::DateTime now;\r
75         std::vector<std::string> idparts;\r
76         long localidentityid;\r
77         long index;\r
78 \r
79         StringFunctions::Split(message["Identifier"],"|",idparts);\r
80         StringFunctions::Convert(idparts[1],localidentityid);\r
81         StringFunctions::Convert(idparts[2],index);\r
82 \r
83         SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblBoardListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');");\r
84         st.Bind(0,localidentityid);\r
85         st.Bind(1,idparts[4]);\r
86         st.Bind(2,index);\r
87         st.Step();\r
88 \r
89         st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedBoardList=? WHERE LocalIdentityID=?;");\r
90         st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S"));\r
91         st.Bind(1,localidentityid);\r
92         st.Step();\r
93 \r
94         RemoveFromInsertList(localidentityid);\r
95 \r
96         m_log->debug("BoardListInserter::HandlePutSuccessful successfully inserted BoardList.");\r
97 \r
98         return true;\r
99 }\r
100 \r
101 void BoardListInserter::Initialize()\r
102 {\r
103         m_fcpuniquename="BoardListInserter";\r
104 }\r
105 \r
106 const bool BoardListInserter::StartInsert(const long &localidentityid)\r
107 {\r
108         Poco::DateTime daysback;\r
109         Poco::DateTime now;\r
110         BoardListXML xml;\r
111         FCPv2::Message message;\r
112         std::string data;\r
113         std::string datasizestr;\r
114         std::string privatekey="";\r
115         int index;\r
116         std::string indexstr="";\r
117         std::string localidentityidstr;\r
118 \r
119         // 20 days back\r
120         daysback-=Poco::Timespan(20,0,0,0,0);\r
121 \r
122         // get boards\r
123         SQLite3DB::Statement st=m_db->Prepare("SELECT BoardName,BoardDescription FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID INNER JOIN tblMessage ON tblMessageBoard.MessageID=tblMessage.MessageID INNER JOIN tblMessageInserts ON tblMessage.MessageUUID=tblMessageInserts.MessageUUID WHERE tblMessageInserts.LocalIdentityID=? AND tblMessageInserts.Day>=? GROUP BY tblBoard.BoardID;");\r
124         st.Bind(0,localidentityid);\r
125         st.Bind(1,Poco::DateTimeFormatter::format(daysback,"%Y-%m-%d"));\r
126         st.Step();\r
127 \r
128         while(st.RowReturned())\r
129         {\r
130                 std::string name="";\r
131                 std::string description="";\r
132 \r
133                 st.ResultText(0,name);\r
134                 st.ResultText(1,description);\r
135 \r
136                 xml.AddBoard(name,description);\r
137 \r
138                 st.Step();\r
139         }\r
140 \r
141         // get public key\r
142         st=m_db->Prepare("SELECT PrivateKey FROM tblLocalIdentity WHERE LocalIdentityID=?;");\r
143         st.Bind(0,localidentityid);\r
144         st.Step();\r
145         if(st.RowReturned())\r
146         {\r
147                 st.ResultText(0,privatekey);\r
148         }\r
149 \r
150         // get last index\r
151         index=0;\r
152         st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblBoardListInserts WHERE LocalIdentityID=? AND Day=?;");\r
153         st.Bind(0,localidentityid);\r
154         st.Bind(1,Poco::DateTimeFormatter::format(now,"%Y-%m-%d"));\r
155         st.Step();\r
156         if(st.RowReturned())\r
157         {\r
158                 if(st.ResultNull(0)==false)\r
159                 {\r
160                         st.ResultInt(0,index);\r
161                         index++;\r
162                 }\r
163         }\r
164         StringFunctions::Convert(index,indexstr);\r
165 \r
166         data=xml.GetXML();\r
167         StringFunctions::Convert(data.size(),datasizestr);\r
168         StringFunctions::Convert(localidentityid,localidentityidstr);\r
169 \r
170         message.SetName("ClientPut");\r
171         message["URI"]=privatekey+m_messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|BoardList|"+indexstr+".xml";\r
172         message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];\r
173         message["UploadFrom"]="direct";\r
174         message["DataLength"]=datasizestr;\r
175         m_fcp->Send(message);\r
176         m_fcp->Send(std::vector<char>(data.begin(),data.end()));\r
177 \r
178         m_inserting.push_back(localidentityid);\r
179 \r
180         return true;\r
181 \r
182 }\r