filters.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. __filename__ = "filters.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "1.1.0"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import os
  9. def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool:
  10. """Adds a filter for particular words within the content of a incoming posts
  11. """
  12. filtersFilename = baseDir + '/accounts/' + \
  13. nickname + '@' + domain + '/filters.txt'
  14. if os.path.isfile(filtersFilename):
  15. if words in open(filtersFilename).read():
  16. return False
  17. filtersFile = open(filtersFilename, "a+")
  18. filtersFile.write(words + '\n')
  19. filtersFile.close()
  20. return True
  21. def removeFilter(baseDir: str, nickname: str, domain: str,
  22. words: str) -> bool:
  23. """Removes a word filter
  24. """
  25. filtersFilename = baseDir + '/accounts/' + \
  26. nickname + '@' + domain + '/filters.txt'
  27. if os.path.isfile(filtersFilename):
  28. if words in open(filtersFilename).read():
  29. with open(filtersFilename, 'r') as fp:
  30. with open(filtersFilename + '.new', 'w+') as fpnew:
  31. for line in fp:
  32. line = line.replace('\n', '')
  33. if line != words:
  34. fpnew.write(line + '\n')
  35. if os.path.isfile(filtersFilename + '.new'):
  36. os.rename(filtersFilename + '.new', filtersFilename)
  37. return True
  38. return False
  39. def isTwitterPost(content: str) -> bool:
  40. """Returns true if the given post content is a retweet or twitter crosspost
  41. """
  42. if '/twitter.' in content or '@twitter.' in content:
  43. return True
  44. elif '>RT <' in content:
  45. return True
  46. return False
  47. def isFiltered(baseDir: str, nickname: str, domain: str, content: str) -> bool:
  48. """Should the given content be filtered out?
  49. This is a simple type of filter which just matches words, not a regex
  50. You can add individual words or use word1+word2 to indicate that two
  51. words must be present although not necessarily adjacent
  52. """
  53. # optionally remove retweets
  54. removeTwitter = baseDir + '/accounts/' + \
  55. nickname + '@' + domain + '/.removeTwitter'
  56. if os.path.isfile(removeTwitter):
  57. if isTwitterPost(content):
  58. return True
  59. filtersFilename = baseDir + '/accounts/' + \
  60. nickname + '@' + domain + '/filters.txt'
  61. if os.path.isfile(filtersFilename):
  62. with open(filtersFilename, 'r') as fp:
  63. for line in fp:
  64. filterStr = line.replace('\n', '').replace('\r', '')
  65. if '+' not in filterStr:
  66. if filterStr in content:
  67. return True
  68. else:
  69. filterWords = filterStr.replace('"', '').split('+')
  70. for word in filterWords:
  71. if word not in content:
  72. return False
  73. return True
  74. return False