agentList.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include <algorithm>
  2. #include <stdio.h>
  3. #include "agentList.h"
  4. #include "global.h"
  5. #include "stl/stringUtils.h"
  6. #include "file/fileUtils.h"
  7. #include "services/stdServiceImpl.h"
  8. #include "macros.h"
  9. #include <assert.h>
  10. using namespace std;
  11. using namespace uniString;
  12. using namespace stringUtil;
  13. #define LOGNAME "[AGENT] "
  14. agentList g_agentList;
  15. class agentList::impl
  16. {
  17. private:
  18. struct agentEntrySave
  19. {
  20. FILE *f;
  21. size_t stream_ID;
  22. };
  23. struct agentEntry: public agent_t
  24. {
  25. void save(agentEntrySave entrySave) throw(exception)
  26. {
  27. if(m_stream_ID == entrySave.stream_ID)
  28. {
  29. utf8 s(m_agent + eol());
  30. if (fwrite(s.c_str(),1,s.size(),entrySave.f) != s.size())
  31. {
  32. throwEx<tagged_error>(LOGNAME "I/O error writing " + (!entrySave.stream_ID ? "global" : "sid=" + tos(entrySave.stream_ID)) + " agent file");
  33. }
  34. }
  35. }
  36. agentEntry(const utf8 &agent, const size_t stream_ID) throw() : agent_t(agent, stream_ID) {}
  37. agentEntry() throw() {}
  38. };
  39. AOL_namespace::mutex m_lock;
  40. list<agentEntry> m_list;
  41. public:
  42. bool load(const uniFile::filenameType &fn, size_t stream_ID) throw(exception)
  43. {
  44. if (fn.empty())
  45. {
  46. throwEx<tagged_error>(LOGNAME "No " + (!stream_ID ? "global" : "sid=" + tos(stream_ID)) + " agent file");
  47. }
  48. else if (gOptions.microServerDebug())
  49. {
  50. DLOG(LOGNAME "Attempting to read agent file: " + fileUtil::getFullFilePath(fn));
  51. }
  52. stackLock sml(m_lock);
  53. FILE *f = uniFile::fopen(fn,"rb");
  54. if (!f) return false;
  55. bool updating = (!m_list.empty());
  56. m_list.clear();
  57. try
  58. {
  59. int l = 0;
  60. int count = 0;
  61. while (true)
  62. {
  63. char buffer[4096] = {0};
  64. if (!fgets(buffer, sizeof(buffer), f)) break; // get a line
  65. ++l; // increment line counter
  66. utf8 s;
  67. // skip utf-8 BOM
  68. if ((strlen(buffer) > 2) &&
  69. (((unsigned char*)buffer)[0] == 0xef) &&
  70. (((unsigned char*)buffer)[1] == 0xbb) &&
  71. (((unsigned char*)buffer)[2] == 0xbf))
  72. s = &(buffer[3]);
  73. else
  74. s = buffer;
  75. if (stripWhitespace(s).empty())
  76. {
  77. WLOG(LOGNAME "Line " + tos(l) + " of " + (!stream_ID ? "global" : "sid=" + tos(stream_ID)) + " user agent list has been ignored");
  78. }
  79. else
  80. {
  81. agentEntry e(stripWhitespace(s),stream_ID);
  82. if(this->find(e.m_agent,e.m_stream_ID,false) == false)
  83. {
  84. m_list.push_back(e);
  85. ++count;
  86. }
  87. }
  88. }
  89. if (!updating)
  90. {
  91. ILOG(LOGNAME "Loaded " + tos(count) + " blocked user agents" + (count != 1 ? "'s" : "") + " from " + (!stream_ID ? "global" : "sid=" + tos(stream_ID)) + " agent file");
  92. }
  93. else
  94. {
  95. ILOG(LOGNAME "Reloaded " + tos(count) + " blocked user agents" + (count != 1 ? "'s" : "") + " from " + (!stream_ID ? "global" : "sid=" + tos(stream_ID)) + " agent file");
  96. }
  97. }
  98. catch(...)
  99. {
  100. if (f) ::fclose(f);
  101. throw;
  102. }
  103. if (f) ::fclose(f);
  104. return true;
  105. }
  106. void save(const uniFile::filenameType &fn,size_t stream_ID) throw(exception)
  107. {
  108. stackLock sml(m_lock);
  109. FILE *f = uniFile::fopen(fn,"wb");
  110. if (!f)
  111. {
  112. throwEx<tagged_error>(LOGNAME "Could not open " + (!stream_ID ? "global" : "sid=" + tos(stream_ID)) +
  113. " agent file `" + fn + "' for writing (" + errMessage().hideAsString() + ")");
  114. }
  115. try
  116. {
  117. agentEntrySave entrySave;
  118. entrySave.f = f;
  119. entrySave.stream_ID = stream_ID;
  120. for_each(m_list.begin(),m_list.end(),bind2nd(mem_fun_ref(&agentEntry::save),entrySave));
  121. }
  122. catch(...)
  123. {
  124. if (f) ::fclose(f);
  125. throw;
  126. }
  127. if (f) ::fclose(f);
  128. if(!uniFile::fileSize(fn))
  129. {
  130. uniFile::unlink(fn);
  131. }
  132. }
  133. bool add(const utf8 &agent, const size_t stream_ID, const bool soft) throw(exception)
  134. {
  135. if (agent.empty())
  136. {
  137. if (!soft) throwEx<runtime_error>(LOGNAME "Empty User Agent specified");
  138. else return false;
  139. }
  140. agentEntry e(agent,stream_ID);
  141. stackLock sml(m_lock);
  142. m_list.push_back(e);
  143. return true;
  144. }
  145. // true if removed
  146. bool remove(const utf8 &agent, const size_t stream_ID, const bool allStream) throw()
  147. {
  148. stackLock sml(m_lock);
  149. for (list<agentEntry>::iterator i = m_list.begin(); i != m_list.end(); ++i)
  150. {
  151. if (allStream || (((!allStream && ((*i).m_agent == agent))) && ((*i).m_stream_ID == stream_ID)))
  152. {
  153. m_list.erase(i);
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. // true if found
  160. bool find(const utf8 &agent, size_t stream_ID, bool use_lock = true) throw()
  161. {
  162. if(use_lock)
  163. {
  164. stackLock sml(m_lock);
  165. }
  166. if(!m_list.empty())
  167. {
  168. for (list<agentEntry>::const_iterator i = m_list.begin(); i != m_list.end(); ++i)
  169. {
  170. if (((*i).m_stream_ID == stream_ID) && (agent == (*i).m_agent))
  171. {
  172. return true;
  173. }
  174. }
  175. }
  176. return false;
  177. }
  178. void get(std::vector<agentList::agent_t> &rl, size_t stream_ID) throw()
  179. {
  180. stackLock sml(m_lock);
  181. for (list<agentEntry>::const_iterator i = m_list.begin(); i != m_list.end(); ++i)
  182. {
  183. if ((*i).m_stream_ID == stream_ID)
  184. {
  185. rl.push_back(*i);
  186. }
  187. }
  188. }
  189. };
  190. //////////////////////////////////////////////////////////////////////////////
  191. //////////////////////////////////////////////////////////////////////////////
  192. agentList::agentList():m_impl(0)
  193. {
  194. m_impl = new agentList::impl;
  195. }
  196. agentList::~agentList() throw()
  197. {
  198. forget(m_impl);
  199. }
  200. bool agentList::load(const uniFile::filenameType &fn,size_t stream_ID) throw()
  201. {
  202. assert(m_impl);
  203. bool result(false);
  204. try
  205. {
  206. result = m_impl->load(fn,stream_ID);
  207. }
  208. catch(const exception &ex)
  209. {
  210. ELOG(ex.what());
  211. }
  212. return result;
  213. }
  214. bool agentList::save(const uniFile::filenameType &fn,size_t stream_ID) throw()
  215. {
  216. assert(m_impl);
  217. bool result(false);
  218. try
  219. {
  220. m_impl->save(fn,stream_ID);
  221. result = true;
  222. }
  223. catch(const exception &ex)
  224. {
  225. ELOG(ex.what());
  226. }
  227. return result;
  228. }
  229. // throws if parameters are invalid
  230. bool agentList::add(const utf8 &agent, const size_t stream_ID, bool soft) throw(exception)
  231. {
  232. assert(m_impl);
  233. return m_impl->add(agent,stream_ID,soft);
  234. }
  235. // true if removed
  236. bool agentList::remove(const utf8 &agent, const size_t stream_ID, bool allStream) throw()
  237. {
  238. assert(m_impl);
  239. return m_impl->remove(agent,stream_ID,allStream);
  240. }
  241. // true if found
  242. bool agentList::find(const utf8 &agent, const size_t stream_ID) throw()
  243. {
  244. assert(m_impl);
  245. return m_impl->find(agent, stream_ID);
  246. }
  247. void agentList::get(vector<agentList::agent_t> &bl,size_t stream_ID) throw()
  248. {
  249. assert(m_impl);
  250. m_impl->get(bl,stream_ID);
  251. }