~stefanor/ibid/dc-too-long-503017

« back to all changes in this revision

Viewing changes to ibid/plugins/core.py

  • Committer: Stefano Rivera
  • Date: 2010-01-04 18:28:41 UTC
  • Revision ID: stefano@rivera.za.net-20100104182841-1rz4n18so861q6o2
Trim responses in Format

Show diffs side-by-side

added added

removed removed

Lines of Context:
172
172
class Format(Processor):
173
173
    priority = 2000
174
174
 
 
175
    def _truncate(self, line):
 
176
        return line.encode('utf-8')[:489].decode('utf-8', 'ignore') \
 
177
                               + u'\N{horizontal ellipsis}'
 
178
 
175
179
    def process(self, event):
176
180
        filtered = []
177
181
        for response in event.responses:
182
186
                response['reply'] = u'*%s*' % response['reply']
183
187
 
184
188
            conflate = response.get('conflate', True)
 
189
            # Expand response into multiple single-line responses:
185
190
            if (not conflate and 'multiline' not in supports):
186
191
                for line in response['reply'].split('\n'):
 
192
                    if 'trim' in supports and len(line.encode('utf-8')) > 490:
 
193
                        line = self._truncate(line)
187
194
                    r = {'reply': line}
188
195
                    for k in response.iterkeys():
189
196
                        if k not in ('reply'):
190
197
                            r[k] = response[k]
191
198
                    filtered.append(r)
 
199
 
 
200
            # Expand response into multiple multi-line responses:
 
201
            elif (not conflate and 'multiline' in supports
 
202
                               and 'trim' in supports):
 
203
                message = response['reply']
 
204
                while len(message.encode('utf-8')) > 490:
 
205
                    splitpoint = len(message.encode('utf-8')[:490] \
 
206
                                            .decode('utf-8', 'ignore'))
 
207
                    parts = [message[:splitpoint].rstrip(),
 
208
                             message[splitpoint:].lstrip()]
 
209
                    for sep in u'\n.;:, ':
 
210
                        if sep in parts[0]:
 
211
                            splitpoint = parts[0].rindex(sep)
 
212
                            parts = [message[:splitpoint], message[splitpoint:]]
 
213
                            if sep in u'\n ':
 
214
                                parts[1] = parts[1][1:]
 
215
                            break
 
216
                    r = {'reply': parts[0]}
 
217
                    for k in response.iterkeys():
 
218
                        if k not in ('reply'):
 
219
                            r[k] = response[k]
 
220
                    filtered.append(r)
 
221
                    message = parts[1]
 
222
 
 
223
                response['reply'] = message
 
224
                filtered.append(response)
 
225
 
192
226
            else:
 
227
                line = response['reply']
 
228
                # Remove any characters that make no sense on IRC-like sources:
193
229
                if 'multiline' not in supports:
194
 
                    response['reply'] = response['reply'].expandtabs(1) \
195
 
                            .replace('\n', conflate == True
196
 
                                           and u' ' or conflate or u'')
 
230
                    line = line.expandtabs(1) \
 
231
                               .replace('\n', conflate == True
 
232
                                              and u' ' or conflate or u'')
 
233
 
 
234
                if 'trim' in supports and len(line.encode('utf-8')) > 490:
 
235
                    line = self._truncate(line)
 
236
                response['reply'] = line
 
237
 
197
238
                filtered.append(response)
198
239
 
199
240
        event.responses = filtered