messagehandler.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #! /usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. #
  4. # gitlab irc sender
  5. # Copyright (C) 2016-2017 Andrei Karas (4144)
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. import threading
  20. import time
  21. from code.configuration import Configuration
  22. from code.core import Core
  23. class MessageHandler(threading.Thread):
  24. def __init__(self):
  25. threading.Thread.__init__(self)
  26. self.die = False
  27. self.messages = []
  28. def run(self):
  29. while (self.die == False):
  30. if len(self.messages) > 0:
  31. data = self.messages.pop()
  32. (channel, msg) = data
  33. print("irc: {0}: {1}".format(channel, msg))
  34. with open("{0}/{1}/in".format(Core.config.global_options.ii_server_directory, channel), "w") as w:
  35. w.write(msg + "\n")
  36. time.sleep(Configuration.irc_message_pause)
  37. def split(self, channel, msg):
  38. arr = []
  39. beforeLen = len("/msg ") + len(channel) + 1
  40. sz = beforeLen + len(msg) + 1
  41. if sz <= 512:
  42. arr.append((channel, msg.strip()))
  43. return arr
  44. pos = 512 - beforeLen
  45. posSplit = msg[ : pos].rfind(" ")
  46. if posSplit > 0:
  47. pos = posSplit
  48. arr.append((channel, msg[ : pos].strip()))
  49. arr.append((channel, msg[pos : ].strip()))
  50. return arr
  51. def add(self, channel, msg):
  52. msg = msg.strip()
  53. if len(msg) == 0 or msg == "\r" or msg == "\n":
  54. return
  55. parts = self.split(channel, msg)
  56. for part in parts:
  57. # revers order
  58. self.messages.insert(0, part)