event.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.mode = ""
  31. self.is_pm = False
  32. self.message_id = -1
  33. def subscribe(self, e):
  34. """
  35. Append passed-in event to our list of subscribing modules.
  36. Args:
  37. e: event.
  38. """
  39. self.subscribers.append(e)
  40. def define(self, definition=None, msg_definition=None, user_definition=None, message_id=None, mode=None):
  41. """
  42. 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)
  43. 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.
  44. Args:
  45. definition: string. regex allowed.
  46. msg_definition: string. regex allowed. this is what someone would say in a channel. like "hello, pybot".
  47. user_definition: string. the user that said the thing. like 'hlmtre' or 'BoneKin'.
  48. message_id: the numerical ID of low-level IRC protocol stuff. 376, for example, tells clients 'hey, this is the MOTD.'
  49. """
  50. if definition is not None:
  51. self.definition = definition
  52. if msg_definition is not None:
  53. self.msg_definition = msg_definition
  54. if user_definition is not None:
  55. self.user_definition = user_definition
  56. if message_id is not None:
  57. self.message_id = message_id
  58. if mode is not None:
  59. self.mode = mode
  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. if len(self.mode):
  79. try:
  80. split_line = line.split()
  81. temp_verb = split_line[1] # first nick, then verb
  82. if self.mode == temp_verb.strip():
  83. return True
  84. except IndexError:
  85. pass
  86. try:
  87. message_id = int(temp)
  88. except (ValueError, UnboundLocalError):
  89. message_id = 0
  90. try:
  91. msg = line.split(":",2)[2]
  92. except IndexError:
  93. return
  94. if len(self.msg_definition):
  95. if re.search(self.msg_definition, msg):
  96. return True
  97. if len(self.definition):
  98. if re.search(self.definition, line):
  99. return True
  100. if len(self.user_definition):
  101. if len(line) and "PRIVMSG" in line > 0:
  102. line_array = line.split()
  103. user_and_mask = line_array[0][1:]
  104. user = user_and_mask.split("!")[0]
  105. if self.user_definition == user:
  106. return True
  107. if type(self.message_id) is int:
  108. if self.message_id == message_id:
  109. return True
  110. return False
  111. def notifySubscribers(self, line):
  112. """
  113. 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.
  114. Args:
  115. line: string
  116. """
  117. self.line = line
  118. self.user = line.split(":")[1].rsplit("!")[0] # nick is first thing on line
  119. if "JOIN" in line or "QUIT" in line:
  120. self.user = line.split("!")[0].replace(":","")
  121. try:
  122. temp = line.split(":")[1].split(" ")[1]
  123. except IndexError:
  124. pass
  125. try:
  126. self.msg = line.split(":",2)[2]
  127. except IndexError:
  128. self.msg = ""
  129. l = line.split()
  130. self.channel = ""
  131. self.verb = ""
  132. ind = 0
  133. privmsg_index = 0
  134. for e in l:
  135. ind+=1
  136. if e == "PRIVMSG":
  137. privmsg_index = ind
  138. if e.startswith("#"):
  139. self.channel = e
  140. break
  141. for v in l:
  142. if v.strip() in ["JOIN","PART","QUIT","NICK","KICK","PRIVMSG","TOPIC", "NOTICE", "PING", "PONG", "MODE"]:
  143. self.verb = v
  144. break
  145. # channel is unset if it does not begin with #
  146. if self.verb == "PRIVMSG" and not len(self.channel):
  147. self.is_pm = True
  148. for s in self.subscribers:
  149. try:
  150. s.handle(self)
  151. except AttributeError:
  152. pass