| @@ 84-133 (lines=50) @@ | ||
| 81 | """Uses regex to replace any special formatting in IRC (bold, colors) with nothing""" |
|
| 82 | return re.sub('([\x02\x1D\x1F\x16\x0F]|\x03([0-9]{2})?)', '', msg) |
|
| 83 | ||
| 84 | def add_buffer(self, event=None, debug=False): |
|
| 85 | """Takes a channel name and line passed to it and stores them in the bot's mem_store dict |
|
| 86 | for future access. The dict will have channel as key. The value to that key will be a list |
|
| 87 | of formatted lines of activity. |
|
| 88 | If the buffer size is not yet exceeded, lines are just added. If the buffer |
|
| 89 | is maxed out, the oldest line is removed and newest one inserted at the beginning. |
|
| 90 | """ |
|
| 91 | if debug: |
|
| 92 | print "Line: " + event.line |
|
| 93 | print "Verb: " + event.verb |
|
| 94 | print "Channel: " + event.channel |
|
| 95 | print "" |
|
| 96 | if not event: |
|
| 97 | return |
|
| 98 | #there are certain things we want to record in history, like nick changes and quits |
|
| 99 | #these often add to the humor of a quote. however, these are not specific to a channel |
|
| 100 | #in IRC and our bot does not maintain a userlist per channel. Therefore, when nick |
|
| 101 | #changes and quits occur, we will add them to every buffer. This is not technically |
|
| 102 | #correct behavior and could very well lead to quits/nick changes that are not visible |
|
| 103 | #showing up in a quote, but it's the best we can do at the moment |
|
| 104 | if not event.channel: |
|
| 105 | #discard events with unwanted verbs |
|
| 106 | if event.verb not in ["QUIT", "NICK"]: |
|
| 107 | return |
|
| 108 | try: |
|
| 109 | for chan in self.bot.mem_store['qdb'].keys(): |
|
| 110 | if chan != '_recent': |
|
| 111 | if len(self.bot.mem_store['qdb'][chan]) >= self.MAX_BUFFER_SIZE: |
|
| 112 | self.bot.mem_store['qdb'][chan].pop() |
|
| 113 | line = self.format_line(event) |
|
| 114 | if line: |
|
| 115 | self.bot.mem_store['qdb'][chan].insert(0, line) |
|
| 116 | except (KeyError, IndexError): |
|
| 117 | print "QDB add_buffer() error when no event channel" |
|
| 118 | #now we continue with normal, per channel line addition |
|
| 119 | #create a dictionary associating the channel with an empty list if it doesn't exist yet |
|
| 120 | else: |
|
| 121 | if event.channel not in self.bot.mem_store['qdb']: |
|
| 122 | self.bot.mem_store['qdb'][event.channel] = [] |
|
| 123 | try: |
|
| 124 | #check for the length of the buffer. if it's too long, pop the last item |
|
| 125 | if len(self.bot.mem_store['qdb'][event.channel]) >= self.MAX_BUFFER_SIZE: |
|
| 126 | self.bot.mem_store['qdb'][event.channel].pop() |
|
| 127 | #get a line by passing event to format_line |
|
| 128 | #insert the line into the first position in the list |
|
| 129 | line = self.format_line(event) |
|
| 130 | if line: |
|
| 131 | self.bot.mem_store['qdb'][event.channel].insert(0, line) |
|
| 132 | except IndexError: |
|
| 133 | print "QDB add_buffer() error. Couldn't access the list index." |
|
| 134 | ||
| 135 | def format_line(self, event): |
|
| 136 | """Takes an event and formats a string appropriate for quotation from it""" |
|
| @@ 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""" |
|