event.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import re
  2. class Event:
  3. """
  4. Allows event type definition. The definition accepts a regex.
  5. Every event can be triggered by specific lines, messages, message_id or users.
  6. Eventually (see time_event branch for proof-of-concept implementation) time-sensitive events will be triggerable as well.
  7. Each line received by the bot is passed to each module in the modules_list. If the module determines the line matches what the event cares about,
  8. the event calls each of its subscribers itself, which contains all the information the module needs to respond appropriately.
  9. To use:
  10. e = Event("__my_type__")
  11. e.define("some_regex")
  12. bot.register_event(e, calling_module)
  13. """
  14. def __init__(self, _type):
  15. """
  16. Define your own type here. Make sure if you're making a broad event (all messages, for example) you use a sane type, as other modules that care about this kind of event can subscribe to it.
  17. Args:
  18. _type: string. like "__youtube__" or "__weather__". Underscores are a convention.
  19. """
  20. self._type = _type
  21. self.subscribers = list() # this is a list of subscribers to notify
  22. self.user = ""
  23. self.definition = ""
  24. self.msg_definition = ""
  25. self.user_definition = ""
  26. self.channel = ""
  27. self.line = ""
  28. self.msg = ""
  29. self.verb = ""
  30. self.is_pm = False
  31. self.message_id = -1
  32. def subscribe(self, e):
  33. """
  34. Append passed-in event to our list of subscribing modules.
  35. Args:
  36. e: event.
  37. """
  38. self.subscribers.append(e)
  39. def define(self, definition=None, msg_definition=None, user_definition=None, message_id=None):
  40. """
  41. Define ourself by general line (definition), msg_definition (what someone says in a channel or PM), user_definition (the user who said the thing), or message_id (like 376 for MOTD or 422 for no MOTD)
  42. Currently, an event is defined by only one type of definition. If one were to remove the returns after each self. set, an event could be defined and triggered by any of several definitions.
  43. Args:
  44. definition: string. regex allowed.
  45. msg_definition: string. regex allowed. this is what someone would say in a channel. like "hello, pybot".
  46. user_definition: string. the user that said the thing. like 'hlmtre' or 'BoneKin'.
  47. message_id: the numerical ID of low-level IRC protocol stuff. 376, for example, tells clients 'hey, this is the MOTD.'
  48. """
  49. if definition is not None:
  50. self.definition = definition
  51. return
  52. if msg_definition is not None:
  53. self.msg_definition = msg_definition
  54. return
  55. if user_definition is not None:
  56. self.user_definition = user_definition
  57. return
  58. if message_id is not None:
  59. self.message_id = message_id
  60. return
  61. def matches(self, line):
  62. """
  63. Fills out the event object per line, and returns True or False if the line matches one of our definitions.
  64. Args:
  65. line: string. The entire incoming line.
  66. Return:
  67. boolean; True or False.
  68. """
  69. # perhaps TODO
  70. # first try very simply
  71. if len(self.definition) and self.definition in line:
  72. return True
  73. # grab message id. not always present
  74. try:
  75. temp = line.split(":")[1].split(" ")[1]
  76. except IndexError:
  77. pass
  78. try:
  79. message_id = int(temp)
  80. except (ValueError, UnboundLocalError):
  81. message_id = 0
  82. try:
  83. msg = line.split(":",2)[2]
  84. except IndexError:
  85. return
  86. if len(self.msg_definition):
  87. if re.search(self.msg_definition, msg):
  88. return True
  89. if len(self.definition):
  90. if re.search(self.definition, line):
  91. return True
  92. if len(self.user_definition):
  93. if len(line) and "PRIVMSG" in line > 0:
  94. line_array = line.split()
  95. user_and_mask = line_array[0][1:]
  96. user = user_and_mask.split("!")[0]
  97. if self.user_definition == user:
  98. return True
  99. if type(self.message_id) is int:
  100. if self.message_id == message_id:
  101. return True
  102. return False
  103. def notifySubscribers(self, line):
  104. """
  105. Fills out the object with all necessary information, then notifies subscribers with itself (an event with all the line information parsed out) as an argument.
  106. Args:
  107. line: string
  108. """
  109. self.line = line
  110. self.user = line.split(":")[1].rsplit("!")[0] # nick is first thing on line
  111. if "JOIN" in line or "QUIT" in line:
  112. self.user = line.split("!")[0].replace(":","")
  113. try:
  114. temp = line.split(":")[1].split(" ")[1]
  115. except IndexError:
  116. pass
  117. try:
  118. self.msg = line.split(":",2)[2]
  119. except IndexError:
  120. self.msg = ""
  121. l = line.split()
  122. self.channel = ""
  123. self.verb = ""
  124. ind = 0
  125. privmsg_index = 0
  126. for e in l:
  127. ind+=1
  128. if e == "PRIVMSG":
  129. privmsg_index = ind
  130. if e.startswith("#"):
  131. self.channel = e
  132. break
  133. for v in l:
  134. if v in ["JOIN","PART","QUIT","NICK","KICK","PRIVMSG","TOPIC", "NOTICE", "PING", "PONG", "MODE"]:
  135. self.verb = v
  136. break
  137. # our channel is the next one from PRIVMSG
  138. if self.verb == "PRIVMSG" and not l[privmsg_index].startswith("#"):
  139. self.is_pm = True
  140. for s in self.subscribers:
  141. try:
  142. s.handle(self)
  143. except AttributeError:
  144. pass