~bnrubin/bantrackertwo/devel

« back to all changes in this revision

Viewing changes to BantrackerTwo/plugin.py

  • Committer: Benjamin Rubin
  • Date: 2009-11-27 20:49:23 UTC
  • Revision ID: bnrubin@romulus-20091127204923-8w0qom2bqhdofni6
- Bot now handles removes

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
from ircTrack.bt.models import UserProfile
12
12
from ircTrack.bt.models import Event,Mark,Operator,Log,Kick
13
13
import datetime
 
14
import re
14
15
 
15
16
class BantrackerTwo(callbacks.Plugin):
16
17
    """Add the help for "@plugin help BantrackerTwo" here
17
18
    This should describe *how* to use this plugin."""
18
19
    threaded = True
19
20
 
20
 
    def getUser(self,msg):
21
 
        host = ircutils.hostFromHostmask(msg.prefix)
 
21
    remove_pattern = re.compile('requested by (?P<op>.*?)(?:: "(?P<reason>.*)")?$')
 
22
 
 
23
 
 
24
    def getUser(self,host):
22
25
        try:
23
26
            return UserProfile.objects.get(host=host).user
24
27
        except:
25
28
            return None
26
29
 
27
 
    def getOp(self,msg):
28
 
        user = self.getUser(msg)
 
30
    def getOp(self,host):
 
31
        user = self.getUser(host)
29
32
        if user:
30
 
            op, new = Operator.objects.get_or_create(user=user,host=msg.prefix)
 
33
            op, new = Operator.objects.get_or_create(user=user,host=host)
31
34
        else:
32
 
            op, new = Operator.objects.get_or_create(host=msg.prefix)
 
35
            op, new = Operator.objects.get_or_create(host=host)
33
36
        op.save()
34
37
        return op
35
38
    def getLog(self,channel):
41
44
        log.save()
42
45
        return log
43
46
 
 
47
    def getHost(self,irc,nick):
 
48
        try:
 
49
            return irc.state.nickToHostmask(nick)
 
50
        except:
 
51
            return nick
 
52
        
 
53
    def now(self):
 
54
        return datetime.datetime.now()
 
55
 
44
56
    def bt(self,irc,msg,args):
45
 
        user = self.getUser(msg)
 
57
        host = ircutils.hostFromHostmask(msg.prefix)
 
58
        user = self.getUser(host)
46
59
        irc.reply(user.username)
47
 
    bt = wrap(bt)
48
 
    
 
60
    bt = wrap(bt) 
49
61
    
50
62
    def mark(self,irc,msg,args,channel,target,message):
51
63
        """[<channel>] <nick|hostmask> [<comment>]
53
65
        Creates an entry in the Bantracker as if <nick|hostmask> was kicked from <channel> with the comment <comment>,
54
66
        <channel> is only needed when send in /msg
55
67
        """
56
 
        user = self.getUser(msg)
 
68
        host = ircutils.hostFromHostmask(msg.prefix)
 
69
        user = self.getUser(host)
57
70
        if not user:
58
71
            irc.error('Hostmask not recognized. Are you identified?')
59
72
            return
63
76
            return
64
77
        
65
78
        channel = channel.lower()
66
 
        hostmask = 'foo!n=bar@baz.org'
 
79
        hostmask = self.getHost(irc,target)
 
80
        
 
81
        op, new = Operator.objects.get_or_create(user=user,host=host)
 
82
        op.save()
 
83
        mark = Mark.objects.create(channel=channel,operator=op,create_date=self.now(),hostmask = hostmask,reason = message,log=self.getLog(channel))
67
84
 
68
 
        op, new = Operator.objects.get_or_create(user=user,host=msg.prefix)
69
 
        op.save()
70
 
        mark = Mark.objects.create(channel=channel,operator=op,create_date=datetime.datetime.now(),hostmask = hostmask,reason = message,log=self.getLog(channel))
71
85
        mark.save()
72
86
        irc.replySuccess()
73
87
    mark = wrap(mark, [optional('channel'), 'something', additional('text')])
79
93
            reason = ''        
80
94
        else:
81
95
            channel, target, reason = msg.args
82
 
        op = self.getOp(msg)
83
 
        hostmask = 'foo!n=bar@baz.com)'
84
 
        kick = Kick.objects.create(channel=channel,operator=op,create_date=datetime.datetime.now(),hostmask=hostmask,reason=reason,log=self.getLog(channel))
 
96
        host = ircutils.hostFromHostmask(msg.prefix)
 
97
        op = self.getOp(host)
 
98
        hostmask = self.getHost(irc,target)
 
99
 
 
100
        kick = Kick.objects.create(channel=channel,operator=op,create_date=self.now(),hostmask=hostmask,reason=reason,log=self.getLog(channel))
85
101
        kick.save()
86
 
        irc.replySuccess()
87
102
        
88
 
    
 
103
    def doPart(self, irc, msg):
 
104
        if msg.args[1].startswith('requested by'):
 
105
            channel = msg.args[0]
 
106
            target = msg.prefix
 
107
            match_obj = self.remove_pattern.match(msg.args[1])
 
108
            reason = match_obj.group('reason')
 
109
            op = self.getOp(self.getHost(irc,match_obj.group('op')))
 
110
 
 
111
            remove = Kick.objects.create(channel=channel,operator=op,create_date=self.now(),hostmask=target,reason=reason,log=self.getLog(channel))
 
112
            remove.save()
 
113
            
 
114
            
89
115
    
90
116
Class = BantrackerTwo