e0d8957c3deafac5354e6e9a652c19fd9c4f0ea4
[fms.git] / src / freenet / messagelistinserter.cpp
1 #include "../../include/freenet/messagelistinserter.h"\r
2 #include "../../include/freenet/messagexml.h"\r
3 #include "../../include/freenet/messagelistxml.h"\r
4 \r
5 #ifdef XMEM\r
6         #include <xmem.h>\r
7 #endif\r
8 \r
9 MessageListInserter::MessageListInserter()\r
10 {\r
11         Initialize();\r
12 }\r
13 \r
14 MessageListInserter::MessageListInserter(FCPv2 *fcp):IIndexInserter<long>(fcp)\r
15 {\r
16         Initialize();\r
17 }\r
18 \r
19 void MessageListInserter::CheckForNeededInsert()\r
20 {\r
21         // only do 1 insert at a time\r
22         if(m_inserting.size()==0)\r
23         {\r
24                 std::string sql;\r
25                 DateTime now;\r
26                 DateTime previous;\r
27                 bool startedinsert=false;\r
28 \r
29                 now.SetToGMTime();\r
30                 previous.SetToGMTime();\r
31 \r
32                 previous.Add(0,0,0,-m_daysbackward);\r
33 \r
34                 // query for identities that have messages in the past X days and (we haven't inserted lists for in the past 30 minutes OR identity has a record in tmpMessageListInsert)\r
35                 sql="SELECT tblLocalIdentity.LocalIdentityID ";\r
36                 sql+="FROM tblLocalIdentity INNER JOIN tblMessageInserts ON tblLocalIdentity.LocalIdentityID=tblMessageInserts.LocalIdentityID ";\r
37                 sql+="WHERE tblMessageInserts.Day>=? AND ((tblLocalIdentity.LastInsertedMessageList<=? OR tblLocalIdentity.LastInsertedMessageList IS NULL OR tblLocalIdentity.LastInsertedMessageList='') OR tblLocalIdentity.LocalIdentityID IN (SELECT LocalIdentityID FROM tmpMessageListInsert)) ";\r
38                 sql+=";";\r
39 \r
40                 SQLite3DB::Statement st=m_db->Prepare(sql);\r
41                 st.Bind(0,previous.Format("%Y-%m-%d"));\r
42                 st.Bind(1,(now-(1.0/48.0)).Format("%Y-%m-%d %H:%M:%S"));\r
43                 st.Step();\r
44 \r
45                 while(st.RowReturned() && startedinsert==false)\r
46                 {\r
47                         int localidentityid;\r
48                         st.ResultInt(0,localidentityid);\r
49                         startedinsert=StartInsert(localidentityid);\r
50                         st.Step();\r
51                 }\r
52         }\r
53 \r
54 }\r
55 \r
56 const bool MessageListInserter::HandlePutFailed(FCPMessage &message)\r
57 {\r
58         std::vector<std::string> idparts;\r
59         long localidentityid;\r
60         long index;\r
61 \r
62         StringFunctions::Split(message["Identifier"],"|",idparts);\r
63         StringFunctions::Convert(idparts[1],localidentityid);\r
64         StringFunctions::Convert(idparts[2],index);\r
65 \r
66         if(message["Fatal"]=="true" || message["Code"]=="9")\r
67         {\r
68                 SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'false');");\r
69                 st.Bind(0,localidentityid);\r
70                 st.Bind(1,idparts[4]);\r
71                 st.Bind(2,index);\r
72                 st.Step();\r
73         }\r
74 \r
75         RemoveFromInsertList(localidentityid);\r
76 \r
77         return true;\r
78 \r
79 }\r
80 \r
81 const bool MessageListInserter::HandlePutSuccessful(FCPMessage &message)\r
82 {\r
83         DateTime now;\r
84         std::vector<std::string> idparts;\r
85         long localidentityid;\r
86         long index;\r
87 \r
88         StringFunctions::Split(message["Identifier"],"|",idparts);\r
89         StringFunctions::Convert(idparts[1],localidentityid);\r
90         StringFunctions::Convert(idparts[2],index);\r
91 \r
92         SQLite3DB::Statement st=m_db->Prepare("INSERT INTO tblMessageListInserts(LocalIdentityID,Day,InsertIndex,Inserted) VALUES(?,?,?,'true');");\r
93         st.Bind(0,localidentityid);\r
94         st.Bind(1,idparts[4]);\r
95         st.Bind(2,index);\r
96         st.Step();\r
97 \r
98         now.SetToGMTime();\r
99         st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedMessageList=? WHERE LocalIdentityID=?;");\r
100         st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S"));\r
101         st.Bind(1,localidentityid);\r
102         st.Step();\r
103 \r
104         // delete any record from tmpMessageListInsert\r
105         st=m_db->Prepare("DELETE FROM tmpMessageListInsert WHERE LocalIdentityID=?;");\r
106         st.Bind(0,localidentityid);\r
107         st.Step();\r
108 \r
109         RemoveFromInsertList(localidentityid);\r
110 \r
111         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"MessageListInserter::HandlePutSuccessful successfully inserted MessageList.");\r
112 \r
113         return true;\r
114 }\r
115 \r
116 void MessageListInserter::Initialize()\r
117 {\r
118         std::string tempval;\r
119 \r
120         m_fcpuniquename="MessageListInserter";\r
121         m_daysbackward=0;\r
122         Option::Instance()->Get("MessageListDaysBackward",tempval);\r
123         StringFunctions::Convert(tempval,m_daysbackward);\r
124 }\r
125 \r
126 const bool MessageListInserter::StartInsert(const long &localidentityid)\r
127 {\r
128         FCPMessage message;\r
129         DateTime date;\r
130         DateTime now;\r
131         std::string privatekey;\r
132         std::string localidentityidstr;\r
133         MessageListXML mlxml;\r
134         MessageXML messxml;\r
135         std::string xmlstr;\r
136         std::string xmlsizestr;\r
137         int index;\r
138         std::string indexstr;\r
139 \r
140         now.SetToGMTime();\r
141         date.SetToGMTime();\r
142         date.Add(0,0,0,-m_daysbackward);\r
143         StringFunctions::Convert(localidentityid,localidentityidstr);\r
144 \r
145         SQLite3DB::Statement st=m_db->Prepare("SELECT Day, InsertIndex, MessageXML, PrivateKey FROM tblMessageInserts INNER JOIN tblLocalIdentity ON tblMessageInserts.LocalIdentityID=tblLocalIdentity.LocalIdentityID WHERE tblLocalIdentity.LocalIdentityID=? AND Day>=?;");\r
146         st.Bind(0,localidentityid);\r
147         st.Bind(1,date.Format("%Y-%m-%d"));\r
148         st.Step();\r
149 \r
150         while(st.RowReturned())\r
151         {\r
152                 std::string day;\r
153                 int index;\r
154                 std::string xmlstr;\r
155                 std::vector<std::string> boards;\r
156 \r
157                 st.ResultText(0,day);\r
158                 st.ResultInt(1,index);\r
159                 st.ResultText(2,xmlstr);\r
160                 st.ResultText(3,privatekey);\r
161 \r
162                 messxml.ParseXML(xmlstr);\r
163 \r
164                 mlxml.AddMessage(day,index,messxml.GetBoards());\r
165 \r
166                 st.Step();\r
167         }\r
168         st.Finalize();\r
169 \r
170 \r
171         st=m_db->Prepare("SELECT MessageDate, MessageIndex, PublicKey, MessageID FROM tblMessage INNER JOIN tblIdentity ON tblMessage.IdentityID=tblIdentity.IdentityID WHERE MessageIndex IS NOT NULL ORDER BY MessageDate DESC, MessageTime DESC LIMIT 100;");\r
172         SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardName FROM tblBoard INNER JOIN tblMessageBoard ON tblBoard.BoardID=tblMessageBoard.BoardID WHERE tblMessageBoard.MessageID=?;");\r
173         st.Step();\r
174         while(st.RowReturned())\r
175         {\r
176                 std::string day;\r
177                 int index;\r
178                 std::string publickey;\r
179                 std::vector<std::string> boardlist;\r
180                 int messageid;\r
181                 \r
182                 st.ResultText(0,day);\r
183                 st.ResultInt(1,index);\r
184                 st.ResultText(2,publickey);\r
185                 st.ResultInt(3,messageid);\r
186 \r
187                 st2.Bind(0,messageid);\r
188                 st2.Step();\r
189                 while(st2.RowReturned())\r
190                 {\r
191                         std::string boardname="";\r
192                         st2.ResultText(0,boardname);\r
193                         StringFunctions::LowerCase(boardname,boardname);\r
194                         boardlist.push_back(boardname);\r
195                         st2.Step();\r
196                 }\r
197                 st2.Reset();\r
198 \r
199                 mlxml.AddExternalMessage(publickey,day,index,boardlist);\r
200 \r
201                 st.Step();\r
202         }\r
203 \r
204         // get last inserted messagelist index for this day\r
205         index=0;\r
206         st=m_db->Prepare("SELECT MAX(InsertIndex) FROM tblMessageListInserts WHERE LocalIdentityID=? AND Day=?;");\r
207         st.Bind(0,localidentityid);\r
208         st.Bind(1,now.Format("%Y-%m-%d"));\r
209         st.Step();\r
210         if(st.ResultNull(0)==false)\r
211         {\r
212                 st.ResultInt(0,index);\r
213                 index++;\r
214         }\r
215         StringFunctions::Convert(index,indexstr);\r
216 \r
217         // actually insert message\r
218         xmlstr=mlxml.GetXML();\r
219 \r
220         // only insert if the last message this identity inserted is different than this message\r
221         if(m_lastinsertedxml[localidentityid]!=xmlstr)\r
222         {\r
223                 StringFunctions::Convert(xmlstr.size(),xmlsizestr);\r
224 \r
225                 message.SetName("ClientPut");\r
226                 message["URI"]=privatekey+m_messagebase+"|"+now.Format("%Y-%m-%d")+"|MessageList|"+indexstr+".xml";\r
227                 message["Identifier"]=m_fcpuniquename+"|"+localidentityidstr+"|"+indexstr+"|"+message["URI"];\r
228                 message["UploadFrom"]="direct";\r
229                 message["DataLength"]=xmlsizestr;\r
230                 m_fcp->SendMessage(message);\r
231                 m_fcp->SendRaw(xmlstr.c_str(),xmlstr.size());\r
232 \r
233                 m_inserting.push_back(localidentityid);\r
234 \r
235                 m_lastinsertedxml[localidentityid]=xmlstr;\r
236                 return true;\r
237         }\r
238         else\r
239         {\r
240                 return false;\r
241         }\r
242 \r
243 }\r