| @@ 26-75 (lines=50) @@ | ||
| 23 | self.MAX_BUFFER_SIZE = 300 |
|
| 24 | self.MAX_HISTORY_SIZE = 10 |
|
| 25 | ||
| 26 | def add_buffer(self, event=None, debug=False): |
|
| 27 | """Takes a channel name and line passed to it and stores them in the bot's mem_store dict |
|
| 28 | for future access. The dict will have channel as key. The value to that key will be a list |
|
| 29 | of formatted lines of activity. |
|
| 30 | If the buffer size is not yet exceeded, lines are just added. If the buffer |
|
| 31 | is maxed out, the oldest line is removed and newest one inserted at the beginning. |
|
| 32 | """ |
|
| 33 | if debug: |
|
| 34 | print "Line: " + event.line |
|
| 35 | print "Verb: " + event.verb |
|
| 36 | print "Channel: " + event.channel |
|
| 37 | print "" |
|
| 38 | if not event: |
|
| 39 | return |
|
| 40 | #there are certain things we want to record in history, like nick changes and quits |
|
| 41 | #these often add to the humor of a quote. however, these are not specific to a channel |
|
| 42 | #in IRC and our bot does not maintain a userlist per channel. Therefore, when nick |
|
| 43 | #changes and quits occur, we will add them to every buffer. This is not technically |
|
| 44 | #correct behavior and could very well lead to quits/nick changes that are not visible |
|
| 45 | #showing up in a quote, but it's the best we can do at the moment |
|
| 46 | if not event.channel: |
|
| 47 | #discard events with unwanted verbs |
|
| 48 | if event.verb not in ["QUIT", "NICK"]: |
|
| 49 | return |
|
| 50 | try: |
|
| 51 | for chan in self.bot.mem_store['replace'].keys(): |
|
| 52 | if len(self.bot.mem_store['replace'][chan]) >= self.MAX_BUFFER_SIZE: |
|
| 53 | self.bot.mem_store['replace'][chan].pop() |
|
| 54 | line = self.format_line(event) |
|
| 55 | if line: |
|
| 56 | self.bot.mem_store['replace'][chan].insert(0, line) |
|
| 57 | except KeyError, IndexError: |
|
| 58 | print "Replace add_buffer() error when no event channel" |
|
| 59 | #now we continue with normal, per channel line addition |
|
| 60 | #create a dictionary associating the channel with an empty list if it doesn't exist yet |
|
| 61 | # END if not event.channel: |
|
| 62 | else: |
|
| 63 | if event.channel not in self.bot.mem_store['replace']: |
|
| 64 | self.bot.mem_store['replace'][event.channel] = [] |
|
| 65 | try: |
|
| 66 | #check for the length of the buffer. if it's too long, pop the last item |
|
| 67 | if len(self.bot.mem_store['replace'][event.channel]) >= self.MAX_BUFFER_SIZE: |
|
| 68 | self.bot.mem_store['replace'][event.channel].pop() |
|
| 69 | #get a line by passing event to format_line |
|
| 70 | #insert the line into the first position in the list |
|
| 71 | line = self.format_line(event) |
|
| 72 | if line: |
|
| 73 | self.bot.mem_store['replace'][event.channel].insert(0, line) |
|
| 74 | except IndexError: |
|
| 75 | print "Replace add_buffer() error. Couldn't access the list index." |
|
| 76 | ||
| 77 | def format_line(self, event): |
|
| 78 | """Takes an event and formats a string appropriate for quotation from it""" |
|
| @@ 116-165 (lines=50) @@ | ||
| 113 | """Uses regex to replace any special formatting in IRC (bold, colors) with nothing""" |
|
| 114 | return re.sub('([\x02\x1D\x1F\x16\x0F]|\x03([0-9]{2})?)', '', msg) |
|
| 115 | ||
| 116 | def add_buffer(self, event=None, debug=False): |
|
| 117 | """Takes a channel name and line passed to it and stores them in the bot's mem_store dict |
|
| 118 | for future access. The dict will have channel as key. The value to that key will be a list |
|
| 119 | of formatted lines of activity. |
|
| 120 | If the buffer size is not yet exceeded, lines are just added. If the buffer |
|
| 121 | is maxed out, the oldest line is removed and newest one inserted at the beginning. |
|
| 122 | """ |
|
| 123 | if debug: |
|
| 124 | print "Line: " + event.line |
|
| 125 | print "Verb: " + event.verb |
|
| 126 | print "Channel: " + event.channel |
|
| 127 | print "" |
|
| 128 | if not event: |
|
| 129 | return |
|
| 130 | #there are certain things we want to record in history, like nick changes and quits |
|
| 131 | #these often add to the humor of a quote. however, these are not specific to a channel |
|
| 132 | #in IRC and our bot does not maintain a userlist per channel. Therefore, when nick |
|
| 133 | #changes and quits occur, we will add them to every buffer. This is not technically |
|
| 134 | #correct behavior and could very well lead to quits/nick changes that are not visible |
|
| 135 | #showing up in a quote, but it's the best we can do at the moment |
|
| 136 | if not event.channel: |
|
| 137 | #discard events with unwanted verbs |
|
| 138 | if event.verb not in ["QUIT", "NICK"]: |
|
| 139 | return |
|
| 140 | try: |
|
| 141 | for chan in self.bot.mem_store['qdb'].keys(): |
|
| 142 | if chan != '_recent': |
|
| 143 | if len(self.bot.mem_store['qdb'][chan]) >= self.MAX_BUFFER_SIZE: |
|
| 144 | self.bot.mem_store['qdb'][chan].pop() |
|
| 145 | line = self.format_line(event) |
|
| 146 | if line: |
|
| 147 | self.bot.mem_store['qdb'][chan].insert(0, line) |
|
| 148 | except (KeyError, IndexError): |
|
| 149 | print "QDB add_buffer() error when no event channel" |
|
| 150 | #now we continue with normal, per channel line addition |
|
| 151 | #create a dictionary associating the channel with an empty list if it doesn't exist yet |
|
| 152 | else: |
|
| 153 | if event.channel not in self.bot.mem_store['qdb']: |
|
| 154 | self.bot.mem_store['qdb'][event.channel] = [] |
|
| 155 | try: |
|
| 156 | #check for the length of the buffer. if it's too long, pop the last item |
|
| 157 | if len(self.bot.mem_store['qdb'][event.channel]) >= self.MAX_BUFFER_SIZE: |
|
| 158 | self.bot.mem_store['qdb'][event.channel].pop() |
|
| 159 | #get a line by passing event to format_line |
|
| 160 | #insert the line into the first position in the list |
|
| 161 | line = self.format_line(event) |
|
| 162 | if line: |
|
| 163 | self.bot.mem_store['qdb'][event.channel].insert(0, line) |
|
| 164 | except IndexError: |
|
| 165 | print "QDB add_buffer() error. Couldn't access the list index." |
|
| 166 | ||
| 167 | def format_line(self, event): |
|
| 168 | """Takes an event and formats a string appropriate for quotation from it""" |
|