File: factoid.py

package info (click to toggle)
ibid 0.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,828 kB
  • ctags: 3,208
  • sloc: python: 15,312; makefile: 124
file content (813 lines) | stat: -rw-r--r-- 34,469 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# Copyright (c) 2009-2011, Michael Gorven, Stefano Rivera
# Released under terms of the MIT/X/Expat Licence. See COPYING for details.

from datetime import datetime
import logging
from random import choice
import re

from dateutil.tz import tzlocal, tzutc

from ibid.plugins import Processor, match, handler, authorise, auth_responses, \
                         RPC
from ibid.config import Option, IntOption, ListOption
from ibid.db import IbidUnicode, IbidUnicodeText, Boolean, Integer, DateTime, \
                    Table, Column, ForeignKey, PassiveDefault, \
                    relation, synonym, func, or_, \
                    Base, VersionedSchema, \
                    get_regexp_op
from ibid.plugins.identity import get_identities
from ibid.utils import format_date

features = {'factoid': {
    'description': u'Factoids are arbitrary pieces of information stored by a '
                   u'key. Factoids beginning with a command such as "<action>" '
                   u'or "<reply>" will supress the "name verb value" output. '
                   u"Search and replace functions won't use real regexs unless "
                   u"appended with the 'r' flag.",
    'categories': ('lookup', 'remember',),
}}

log = logging.getLogger('plugins.factoid')

default_verbs = ('is', 'are', 'has', 'have', 'was', 'were', 'do', 'does', 'can', 'should', 'would')
default_interrogatives = ('what', 'wtf', 'where', 'when', 'who', "what's", "who's")

def strip_name(unstripped):
    "Apply to factoid names, as we use unstripped matches"
    return re.match(r'^\s*(.*?)\s*[?!.]*\s*$', unstripped, re.DOTALL).group(1)

def escape_name(name):
    "Turn a $arg factoid name to _%"
    return name.replace('%', '\\%').replace('_', '\\_').replace('$arg', '_%')

def unescape_name(name):
    "Turn a _% factoid name to $arg"
    return name.replace('_%', '$arg').replace('\\%', '%').replace('\\_', '_')

class FactoidName(Base):
    __table__ = Table('factoid_names', Base.metadata,
    Column('id', Integer, primary_key=True),
    Column('name', IbidUnicodeText(32, case_insensitive=True),
           key='_name', nullable=False, unique=True, index=True),
    Column('factoid_id', Integer, ForeignKey('factoids.id'), nullable=False,
           index=True),
    Column('identity_id', Integer, ForeignKey('identities.id'), index=True),
    Column('time', DateTime, nullable=False),
    Column('factpack', Integer, ForeignKey('factpacks.id'), index=True),
    Column('wild', Boolean, nullable=False, default=False, index=True),
    useexisting=True)

    class FactoidNameSchema(VersionedSchema):
        def upgrade_1_to_2(self):
            self.add_column(Column('factpack', Integer,
                                   ForeignKey('factpacks.id')))
        def upgrade_2_to_3(self):
            self.add_index(self.table.c.name)
        def upgrade_3_to_4(self):
            self.add_index(self.table.c.name)
            self.add_index(self.table.c.factoid_id)
            self.add_index(self.table.c.identity_id)
            self.add_index(self.table.c.factpack)
        def upgrade_4_to_5(self):
            self.alter_column(Column('name', IbidUnicode(64), key='_name',
                                     nullable=False, unique=True, index=True))
        def upgrade_5_to_6(self):
            self.alter_column(Column('name', IbidUnicodeText(32), key='_name',
                                     nullable=False, unique=True, index=True))
        def upgrade_6_to_7(self):
            self.add_column(Column('wild', Boolean, PassiveDefault('0'),
                                   nullable=False, index=True, default=False))
            for row in self.upgrade_session.query(FactoidName) \
                    .filter(FactoidName.name.like('%#_#%%', escape='#')) \
                    .all():
                row.wild = True
                self.upgrade_session.add(row)
        def upgrade_7_to_8(self):
            self.drop_index(self.table.c._name)
            self.alter_column(Column('name',
                                     IbidUnicodeText(32, case_insensitive=True),
                                     key='_name', nullable=False, unique=True,
                                     index=True), force_rebuild=True)
            self.add_index(self.table.c._name)

    __table__.versioned_schema = FactoidNameSchema(__table__, 8)

    def __init__(self, name, identity_id, factoid_id=None, factpack=None):
        self.name = name
        self.factoid_id = factoid_id
        self.identity_id = identity_id
        self.time = datetime.utcnow()
        self.factpack = factpack

    def __repr__(self):
        return u'<FactoidName %s %s>' % (self.name, self.factoid_id)

    def _get_name(self):
        return unescape_name(self._name)

    def _set_name(self, name):
        self.wild = u'$arg' in name
        self._name = escape_name(name)

    name = synonym('_name', descriptor=property(_get_name, _set_name))

class FactoidValue(Base):
    __table__ = Table('factoid_values', Base.metadata,
    Column('id', Integer, primary_key=True),
    Column('value', IbidUnicodeText, nullable=False),
    Column('factoid_id', Integer, ForeignKey('factoids.id'), nullable=False,
           index=True),
    Column('identity_id', Integer, ForeignKey('identities.id'), index=True),
    Column('time', DateTime, nullable=False),
    Column('factpack', Integer, ForeignKey('factpacks.id'), index=True),
    useexisting=True)

    class FactoidValueSchema(VersionedSchema):
        def upgrade_1_to_2(self):
            self.add_column(Column('factpack', Integer, ForeignKey('factpacks.id')))
        def upgrade_2_to_3(self):
            self.add_index(self.table.c.factoid_id)
            self.add_index(self.table.c.identity_id)
            self.add_index(self.table.c.factpack)
        def upgrade_3_to_4(self):
            self.alter_column(Column('value', IbidUnicodeText, nullable=False),
                              force_rebuild=True)

    __table__.versioned_schema = FactoidValueSchema(__table__, 4)

    def __init__(self, value, identity_id, factoid_id=None, factpack=None):
        self.value = value
        self.factoid_id = factoid_id
        self.identity_id = identity_id
        self.time = datetime.utcnow()
        self.factpack = factpack

    def __repr__(self):
        return u'<FactoidValue %s %s>' % (self.factoid_id, self.value)

class Factoid(Base):
    __table__ = Table('factoids', Base.metadata,
    Column('id', Integer, primary_key=True),
    Column('time', DateTime, nullable=False),
    Column('factpack', Integer, ForeignKey('factpacks.id'), index=True),
    useexisting=True)

    names = relation(FactoidName, cascade='all,delete', backref='factoid')
    values = relation(FactoidValue, cascade='all,delete', backref='factoid')

    class FactoidSchema(VersionedSchema):
        def upgrade_1_to_2(self):
            self.add_column(Column('factpack', Integer,
                                   ForeignKey('factpacks.id')))
        def upgrade_2_to_3(self):
            self.add_index(self.table.c.factpack)

    __table__.versioned_schema = FactoidSchema(__table__, 3)

    def __init__(self, factpack=None):
        self.time = datetime.utcnow()
        self.factpack = factpack

    def __repr__(self):
        return u"<Factoid %s = %s>" % (', '.join([name.name for name in self.names]), ', '.join([value.value for value in self.values]))

class Factpack(Base):
    __table__ = Table('factpacks', Base.metadata,
    Column('id', Integer, primary_key=True),
    Column('name', IbidUnicode(64, case_insensitive=True),
           nullable=False, unique=True, index=True),
    useexisting=True)

    class FactpackSchema(VersionedSchema):
        def upgrade_1_to_2(self):
            self.add_index(self.table.c.name)
        def upgrade_2_to_3(self):
            self.drop_index(self.table.c.name)
            self.alter_column(Column('name',
                                     IbidUnicode(64, case_insensitive=True),
                                     nullable=False, unique=True, index=True),
                              force_rebuild=True)
            self.add_index(self.table.c.name)

    __table__.versioned_schema = FactpackSchema(__table__, 3)

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return u'<Factpack %s>' % (self.name,)

action_re = re.compile(r'^\s*<action>\s*')
reply_re = re.compile(r'^\s*<reply>\s*')
escape_like_re = re.compile(r'([%_#])')

def get_factoid(session, name, number, pattern, is_regex, all=False,
                literal=False):
    """session: SQLAlchemy session
    name: Factoid name (can contain arguments unless literal query)
    number: If not None, restrict to factoid[number] (or factoid[number:] for literal queries)
    pattern: If not None, restrict to factoids matching this pattern (cannot be used in conjuction with number)
    is_regex: Pattern is a real regex
    all: Return a random factoid from the set if False
    literal: Match factoid name literally (implies all)
    if all or literal, a list is returned; otherwise a single factoid is returned
    if nothing is found, either an empty list or None is returned
    """
    assert not (number and pattern), u'number and pattern cannot be used together'
    if literal:
        all = True # as mentioned in the docstring
    # First pass for exact matches, if necessary again for wildcard matches
    if literal:
        passes = (False,)
    else:
        passes = (False, True)
    for wild in passes:
        factoid = None
        query = session.query(Factoid)\
                .add_entity(FactoidName).join(Factoid.names)\
                .add_entity(FactoidValue).join(Factoid.values)
        if wild:
            # Reversed LIKE because factoid name contains SQL wildcards if
            # factoid supports arguments
            query = query.filter(
                    'lower(:fact) LIKE lower(name) ESCAPE :escape'
                ).params(fact=name, escape='\\')
        else:
            query = query.filter(FactoidName.name == escape_name(name))
        # For normal matches, restrict to the subset applicable
        if not literal:
            query = query.filter(FactoidName.wild == wild)

        if pattern:
            if is_regex:
                op = get_regexp_op(session)
                query = query.filter(op(FactoidValue.value, pattern))
            else:
                pattern = '%%%s%%' % escape_like_re.sub(r'#\1', pattern)
                query = query.filter(func.lower(FactoidValue.value)
                                     .like(pattern, escape='#'))

        if number is not None:
            number = max(0, int(number) - 1)
            try:
                # The .all() is to ensure the result is a list.
                # It gets len()ed later
                if literal:
                    return query.order_by(FactoidValue.id).all()[number:]
                else:
                    factoid = query.order_by(FactoidValue.id).all()[number]
            except IndexError:
                continue
        if all:
            if factoid is not None:
                return [factoid]
            else:
                factoid = query.all()
        else:
            factoid = factoid or query.order_by(func.random()).first()
        if factoid:
            return factoid
    if all:
        return []
    return None

class Utils(Processor):
    usage = u'literal <name> [( #<from number> | /<pattern>/[r] )]'
    feature = ('factoid',)

    @match(r'^literal\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$')
    def literal(self, event, name, number, pattern, is_regex):
        factoids = get_factoid(event.session, name, number, pattern, is_regex,
                literal=True)
        number = number and int(number) or 1
        if factoids:
            event.addresponse(u', '.join(u'%i: %s'
                % (index + number, value.value)
                  for index, (factoid, name, value) in enumerate(factoids)))
        else:
            factoids = get_factoid(event.session, name, None, pattern, is_regex,
                literal=True)
            if factoids:
                event.addresponse(u"I only know %(number)d things about %(name)s", {
                    u'number': len(factoids),
                    u'name': name,
                })


class Forget(Processor):
    usage = u"""forget <name> [( #<number> | /<pattern>/[r] )]
    <name> is the same as <other name>"""
    feature = ('factoid',)

    priority = 10
    permission = u'factoid'
    permissions = (u'factoidadmin',)

    @match(r'^forget\s+(.+?)(?:\s+#(\d+)|\s+(?:/(.+?)/(r?)))?$')
    @authorise(fallthrough=False)
    def forget(self, event, name, number, pattern, is_regex):
        factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True, literal=True)
        if factoids:
            factoidadmin = auth_responses(event, u'factoidadmin')
            identities = get_identities(event)
            factoid = event.session.query(Factoid).get(factoids[0][0].id)

            if len(factoids) > 1 and pattern is not None:
                event.addresponse(u'Pattern matches multiple factoids, please be more specific')
                return

            if number or pattern:
                if factoids[0][2].identity_id not in identities and not factoidadmin:
                    return

                if event.session.query(FactoidValue).filter_by(factoid_id=factoid.id).count() == 1:
                    if len(filter(lambda x: x.identity_id not in identities, factoid.names)) > 0 and not factoidadmin:
                        return
                    id = factoid.id
                    event.session.delete(factoid)
                    event.session.commit()
                    log.info(u"Deleted factoid %s (%s) by %s/%s (%s)",
                            id, name, event.account, event.identity, event.sender['connection'])
                else:
                    id = factoids[0][2].id
                    event.session.delete(factoids[0][2])
                    event.session.commit()
                    log.info(u"Deleted value %s (%s) of factoid %s (%s) by %s/%s (%s)",
                            id, factoids[0][2].value, factoid.id, name,
                            event.account, event.identity, event.sender['connection'])

            else:
                if factoids[0][1].identity_id not in identities and not factoidadmin:
                    return

                if event.session.query(FactoidName).filter_by(factoid_id=factoid.id).count() == 1:
                    if len(filter(lambda x: x.identity_id not in identities, factoid.values)) > 0 and not factoidadmin:
                        return
                    id = factoid.id
                    event.session.delete(factoid)
                    event.session.commit()
                    log.info(u"Deleted factoid %s (%s) by %s/%s (%s)",
                            id, name, event.account, event.identity,
                            event.sender['connection'])
                else:
                    id = factoids[0][1].id
                    event.session.delete(factoids[0][1])
                    event.session.commit()
                    log.info(u"Deleted name %s (%s) of factoid %s (%s) by %s/%s (%s)",
                            id, factoids[0][1].name, factoid.id, factoids[0][0].names[0].name,
                            event.account, event.identity, event.sender['connection'])

            event.addresponse(True)
        else:
            factoids = get_factoid(event.session, name, None, pattern, is_regex, all=True, literal=True)
            if factoids:
                event.addresponse(u"I only know %(number)d things about %(name)s", {
                    u'number': len(factoids),
                    u'name': name,
                })
            else:
                event.addresponse(u"I didn't know about %s anyway", name)

    @match(r'^(.+)\s+is\s+the\s+same\s+as\s+(.+)$')
    @authorise(fallthrough=False)
    def alias(self, event, target, source):

        target = strip_name(target)

        if target == u'':
            event.addresponse(u"Sorry, I'm not interested in empty factoids")
            return

        if target.lower() == source.lower():
            event.addresponse(u"That makes no sense, they *are* the same")
            return

        factoid = event.session.query(Factoid).join(Factoid.names) \
                .filter(FactoidName.name==escape_name(source)).first()
        if factoid:
            target_factoid = event.session.query(FactoidName) \
                    .filter(FactoidName.name==escape_name(target)).first()
            if target_factoid:
                event.addresponse(u"I already know stuff about %s", target)
                return

            name = FactoidName(unicode(target), event.identity)
            factoid.names.append(name)
            event.session.add(factoid)
            event.session.commit()
            event.addresponse(True)
            log.info(u"Added name '%s' to factoid %s (%s) by %s/%s (%s)",
                    name.name, factoid.id, factoid.names[0].name,
                    event.account, event.identity, event.sender['connection'])
        else:
            event.addresponse(u"I don't know about %s", source)

class Search(Processor):
    usage = u'search [for] [<limit>] [(facts|values) [containing]] (<pattern>|/<pattern>/[r]) [from <start>]'
    feature = ('factoid',)

    limit = IntOption('search_limit', u'Maximum number of results to return', 30)
    default = IntOption('search_default', u'Default number of results to return', 10)

    regex_re = re.compile(r'^/(.*)/(r?)$')

    @match(r'^search\s+(?:for\s+)?(?:(\d+)\s+)?(?:(facts?|values?)\s+)?(?:containing\s+)?(.+?)(?:\s+from)?(?:\s+(\d+))?\s*$',
            version='deaddressed')
    def search(self, event, limit, search_type, pattern, start):
        limit = limit and min(int(limit), self.limit) or self.default
        start = start and max(int(start) - 1, 0) or 0

        search_type = search_type and search_type.lower() or u""

        origpattern = pattern
        m = self.regex_re.match(pattern)
        is_regex = False
        if m:
            pattern = m.group(1)
            is_regex = bool(m.group(2))

        # Hack: We replace $arg with _%, but this won't match a partial
        # "$arg" string
        if is_regex:
            filter_op = get_regexp_op(event.session)
            name_pattern = pattern.replace(r'\$arg', '_%')
        else:
            filter_op = lambda x, y: x.like(y, escape='#')
            pattern = '%%%s%%' % escape_like_re.sub(r'#\1', pattern)
            name_pattern = pattern.replace('$arg', '#_#%')

        query = event.session.query(Factoid)\
                             .join(Factoid.names).add_entity(FactoidName)\
                             .join(Factoid.values)

        if search_type.startswith('fact'):
            query = query.filter(filter_op(FactoidName.name, name_pattern))
        elif search_type.startswith('value'):
            query = query.filter(filter_op(FactoidValue.value, pattern))
        else:
            query = query.filter(or_(filter_op(FactoidName.name, name_pattern),
                                     filter_op(FactoidValue.value, pattern)))

        # Pre-evalute the iterable or the if statement will be True in SQLAlchemy 0.4. LP: #383286
        matches = [match for match in query.all()]

        bounded_matches = matches[start:start+limit]
        if bounded_matches:
            event.addresponse(u'; '.join(
                u'%s [%s]' % (fname.name, len(factoid.values))
                for factoid, fname in bounded_matches))
        elif len(matches):
            event.addresponse(u"I could only find %(number)d things that matched '%(pattern)s'", {
                u'number': len(matches),
                u'pattern': origpattern,
            })
        else:
            event.addresponse(u"I couldn't find anything that matched '%s'" % origpattern)

def _interpolate(message, event):
    "Expand factoid variables"
    utcnow = datetime.utcnow()
    now = utcnow.replace(tzinfo=tzutc()).astimezone(tzlocal())

    message = message.replace(u'$who', event.sender['nick'])
    message = message.replace(u'$channel', event.channel)
    message = message.replace(u'$year', unicode(now.year))
    message = message.replace(u'$month', unicode(now.month))
    message = message.replace(u'$day', unicode(now.day))
    message = message.replace(u'$hour', unicode(now.hour))
    message = message.replace(u'$minute', unicode(now.minute))
    message = message.replace(u'$second', unicode(now.second))
    message = message.replace(u'$date', format_date(utcnow, 'date'))
    message = message.replace(u'$time', format_date(utcnow, 'time'))
    message = message.replace(u'$dow', unicode(now.strftime('%A')))
    message = message.replace(u'$unixtime', unicode(utcnow.strftime('%s')))
    return message

class Get(Processor, RPC):
    usage = u'<factoid> [( #<number> | /<pattern>/[r] )]'
    feature = ('factoid',)

    priority = 200

    interrogatives = ListOption('interrogatives', 'Question words to strip', default_interrogatives)
    verbs = ListOption('verbs', 'Verbs that split name from value', default_verbs)

    def __init__(self, name):
        super(Get, self).__init__(name)
        RPC.__init__(self)

    def setup(self):
        self.get.im_func.pattern = re.compile(
                r'^(?:(?:%s)\s+(?:(?:%s)\s+)?)?(.+?)(?:\s+#(\d+))?(?:\s+/(.+?)/(r?))?$'
                  % ('|'.join(self.interrogatives),
                '|'.join(self.verbs)), re.I)

    @handler
    def get(self, event, name, number, pattern, is_regex):
        response = self.remote_get(name, number, pattern, is_regex, event)
        if response:
            event.addresponse(response)

    def remote_get(self, name, number=None, pattern=None, is_regex=None, event={}):
        factoid = get_factoid(event.session, name, number, pattern, is_regex)

        if factoid:
            (factoid, fname, fvalue) = factoid
            reply = fvalue.value
            oname = fname.name
            pattern = re.escape(fname.name).replace(r'\$arg', '(.*)')
            args = re.match(pattern, name, re.I | re.U).groups()

            for i, capture in enumerate(args):
                reply = reply.replace('$%s' % (i + 1), capture)
                oname = oname.replace('$arg', capture, 1)

            reply = _interpolate(reply, event)

            (reply, count) = action_re.subn('', reply)
            if count:
                return {'action': True, 'reply': reply}

            (reply, count) = reply_re.subn('', reply)
            if count:
                return {'address': False, 'reply': reply}

            reply = u'%s %s' % (oname, reply)
            return reply

class Set(Processor):
    usage = u"""<name> (<verb>|=<verb>=) [also] <value>
    last set factoid"""
    feature = ('factoid',)

    interrogatives = ListOption('interrogatives', 'Question words to strip', default_interrogatives)
    verbs = ListOption('verbs', 'Verbs that split name from value', default_verbs)

    priority = 800
    permission = u'factoid'
    last_set_factoid = None

    def setup(self):
        self.set_factoid.im_func.pattern = re.compile(
            r'^(no[,.: ]\s*)?(.+?)\s+(also\s+)?(?:=(\S+)=)?(?(4)|(%s))(\s+also)?\s+((?(3).+|(?!.*=\S+=).+))$'
            % '|'.join(self.verbs), re.I)
        self.set_factoid.im_func.message_version = 'deaddressed'

    @handler
    @authorise(fallthrough=False)
    def set_factoid(self, event, correction, name, addition1, verb1, verb2, addition2, value):
        verb = verb1 or verb2
        addition = addition1 or addition2

        name = strip_name(name)

        if name == u'':
            event.addresponse(u"Sorry, I'm not interested in empty factoids")
            return

        if name.lower() in self.interrogatives:
            event.addresponse(choice((
                u"I'm afraid I have no idea",
                u"Not a clue, sorry",
                u"Erk, dunno",
            )))
            return

        factoid = event.session.query(Factoid).join(Factoid.names)\
                .filter(FactoidName.name==escape_name(name)).first()
        if factoid:
            if correction:
                identities = get_identities(event)
                if not auth_responses(event, u'factoidadmin') and len(filter(lambda x: x.identity_id not in identities, factoid.values)) > 0:
                    return
                for fvalue in factoid.values:
                    event.session.delete(fvalue)
            elif not addition:
                event.addresponse(u'I already know stuff about %s', name)
                return
        else:
            factoid = Factoid()
            fname = FactoidName(unicode(name), event.identity)
            factoid.names.append(fname)
            event.session.add(factoid)
            event.session.flush()
            log.info(u"Creating factoid %s with name '%s' by %s", factoid.id, fname.name, event.identity)

        if not reply_re.match(value) and not action_re.match(value):
            value = '%s %s' % (verb, value)
        fvalue = FactoidValue(unicode(value), event.identity)
        factoid.values.append(fvalue)
        event.session.add(factoid)
        event.session.commit()
        self.last_set_factoid=factoid.names[0].name
        log.info(u"Added value '%s' to factoid %s (%s) by %s/%s (%s)",
                fvalue.value, factoid.id, factoid.names[0].name,
                event.account, event.identity, event.sender['connection'])
        event.addresponse(choice((
                u'If you say so',
                u'One learns a new thing every day',
                u"I'll remember that",
                u'Got it',
            )))

    @match(r'^(?:last\s+set\s+factoid|what\s+did\s+\S+\s+just\s+set)$')
    def last_set(self, event):
        if self.last_set_factoid is None:
            event.addresponse(u'Sorry, nobody has taught me anything recently')
        else:
            event.addresponse(u'It was: %s', self.last_set_factoid)

class Modify(Processor):
    usage = u"""<name> [( #<number> | /<pattern>/[r] )] += <suffix>
    <name> [( #<number> | /<pattern>/[r] )] ~= ( s/<regex>/<replacement>/[g][i][r] | y/<source>/<dest>/ )"""
    feature = ('factoid',)

    permission = u'factoid'
    permissions = (u'factoidadmin',)
    priority = 190

    @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*\+=(.+)$', version='deaddressed')
    @authorise(fallthrough=False)
    def append(self, event, name, number, pattern, is_regex, suffix):
        name = strip_name(name)
        factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True, literal=True)
        if len(factoids) == 0:
            if pattern:
                event.addresponse(u"I don't know about any %(name)s matching %(pattern)s", {
                    'name': name,
                    'pattern': pattern,
                })
            else:
                factoids = get_factoid(event.session, name, None, pattern, is_regex, all=True)
                if factoids:
                    event.addresponse(u"I only know %(number)d things about %(name)s", {
                        u'number': len(factoids),
                        u'name': name,
                    })
                else:
                    event.addresponse(u"I don't know about %s", name)
        elif len(factoids) > 1 and number is None:
            event.addresponse(u"Pattern matches multiple factoids, please be more specific")
        else:
            factoidadmin = auth_responses(event, u'factoidadmin')
            identities = get_identities(event)
            factoid = factoids[0]

            if factoid[2].identity_id not in identities and not factoidadmin:
                return

            oldvalue = factoid[2].value
            factoid[2].value += suffix
            event.session.add(factoid[2])
            event.session.commit()

            log.info(u"Appended '%s' to value %s of factoid %s (%s) by %s/%s (%s)",
                    suffix, factoid[2].id, factoid[0].id, oldvalue, event.account,
                    event.identity, event.sender['connection'])
            event.addresponse(True)

    @match(r'^(.+?)(?:\s+#(\d+)|\s+/(.+?)/(r?))?\s*(?:~=|=~)\s*([sy](?P<sep>.).+(?P=sep).*(?P=sep)[gir]*)$')
    @authorise(fallthrough=False)
    def modify(self, event, name, number, pattern, is_regex, operation, separator):
        factoids = get_factoid(event.session, name, number, pattern, is_regex, all=True)
        if len(factoids) == 0:
            if pattern:
                event.addresponse(u"I don't know about any %(name)s matching %(pattern)s", {
                    'name': name,
                    'pattern': pattern,
                })
            else:
                event.addresponse(u"I don't know about %s", name)
        elif len(factoids) > 1:
            event.addresponse(u"Pattern matches multiple factoids, please be more specific")
        else:
            factoidadmin = auth_responses(event, u'factoidadmin')
            identities = get_identities(event)
            factoid = factoids[0]

            if factoid[2].identity_id not in identities and not factoidadmin:
                return

            # Not very pythonistic, but escaping is a nightmare.
            parts = [[]]
            pos = 0
            while pos < len(operation):
                char = operation[pos]
                if char == separator:
                    parts.append([])
                elif char == u"\\":
                    if pos < len(operation) - 1:
                        if operation[pos+1] == u"\\":
                            parts[-1].append(u"\\")
                            pos += 1
                        elif operation[pos+1] == separator:
                            parts[-1].append(separator)
                            pos += 1
                        else:
                            parts[-1].append(u"\\")
                    else:
                        parts[-1].append(u"\\")
                else:
                    parts[-1].append(char)
                pos += 1

            parts = [u"".join(x) for x in parts]

            if len(parts) != 4:
                event.addresponse(u"That operation makes no sense. Try something like s/foo/bar/")
                return

            oldvalue = factoid[2].value
            op, search, replace, flags = parts
            flags = flags.lower()
            if op == "s":
                if "r" in flags:
                    if "i" in flags:
                        search += "(?i)"
                    try:
                        factoid[2].value = re.sub(search, replace, oldvalue, int("g" not in flags))
                    except:
                        event.addresponse(u"That operation makes no sense. Try something like s/foo/bar/")
                        return
                else:
                    newvalue = oldvalue.replace(search, replace, "g" in flags and -1 or 1)
                    if newvalue == oldvalue:
                        event.addresponse(u"I couldn't find '%(terms)s' in '%(oldvalue)s'. If that was a proper regular expression, append the 'r' flag", {
                            'terms': search,
                            'oldvalue': oldvalue,
                        })
                        return
                    factoid[2].value = newvalue

            elif op == "y":
                if len(search) != len(replace):
                    event.addresponse(u"That operation makes no sense. The source and destination must be the same length")
                    return
                try:
                    table = dict((ord(x), ord(y)) for x, y in zip(search, replace))
                    factoid[2].value = oldvalue.translate(table)

                except:
                    event.addresponse(u"That operation makes no sense. Try something like y/abcdef/ABCDEF/")
                    return

            event.session.add(factoid[2])
            event.session.commit()

            log.info(u"Applying '%s' to value %s of factoid %s (%s) by %s/%s (%s)",
                    operation, factoid[2].id, factoid[0].id, oldvalue, event.account, event.identity, event.sender['connection'])

            event.addresponse(True)

greetings = (
        u'lo', u'ello', u'hello', u'hi', u'hi there', u'howdy', u'hey',
        u'heya', u'hiya', u'hola', u'salut', u'bonjour', u'sup', u'wussup',
        u'hoezit', u'wotcha', u'wotcher', u'yo', u'word', u'good day',
        u'wasup', u'wassup', u'howzit', u'howsit', u'buon giorno',
        u'hoe lyk it', u'hoe gaan dit', u'good morning', u'morning',
        u'afternoon', u'evening',
)
static_default = {
    'greet': {
        'matches': [r'\b(' + '|'.join(list(greetings) +
            [g.replace(' ', '') for g in greetings if ' ' in g]) + r')\b'],
        'responses': greetings,
    },
    'reward': {
        'matches': [r'\bbot(\s+|\-)?snack\b'],
        'responses': [u'thanks, $who', u'$who: thankyou!', u':)'],
    },
    'praise': {
        'matches': [r'\bgood(\s+fuckin[\'g]?)?\s+(lad|bo(t|y)|g([ui]|r+)rl)\b', r'\byou\s+(rock|rocks|rewl|rule|are\s+so+\s+co+l)\b'],
        'responses': [u'thanks, $who', u'$who: thankyou!', u':)'],
    },
    'thanks': {
        'matches': [r'\bthank(s|\s*you)\b', r'^\s*ta\s*$', r'^\s*shot\s*$'],
        'responses': [u'no problem, $who', u'$who: my pleasure', u'sure thing, $who', u'no worries, $who', u'$who: np', u'no probs, $who', u'$who: no problemo', u'$who: not at all'],
    },
    'criticism': {
        'matches': [r'\b((kak|bad|st(u|oo)pid|dumb)(\s+fuckin[\'g]?)?\s+(bo(t|y)|g([ui]|r+)rl))|(bot(\s|\-)?s(mack|lap))\b'],
        'responses': [u'*whimper*', u'sorry, $who :(', u':(', u'*cringe*'],
    },
}

class StaticFactoid(Processor):
    priority = 900

    extras = Option('static', 'List of static factoids using regexes', {})

    def setup(self):
        self.factoids = static_default.copy()
        self.factoids.update(self.extras)

    @handler
    def static(self, event):
        for factoid in self.factoids.values():
            for match in factoid['matches']:
                if re.search(match, event.message['stripped'], re.I|re.DOTALL):
                    event.addresponse(_interpolate(choice(factoid['responses']), event),
                            address=False)
                    return

# vi: set et sta sw=4 ts=4: