-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaffWikiBot.py
More file actions
151 lines (115 loc) · 4.99 KB
/
StaffWikiBot.py
File metadata and controls
151 lines (115 loc) · 4.99 KB
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
# -*- coding: cp1252 -*-
"""
Bot for staff wiki
Requirements:
* Python 2.7
* Twisted 14.0.0 (https://twistedmatrix.com)
* bugzillatools 0.5.3.1 (https://pypi.python.org/pypi/bugzillatools)
Usage:
Add the following to LocalSettings.php
$wgRCFeeds['exampleirc'] = array(
'formatter' => 'IRCColourfulRCFeedFormatter',
'uri' => 'udp://localhost:1338',
'add_interwiki_prefix' => false,
'omit_bots' => true,
);
@version 0.2
@author Richard Cook <cook879@shoutwiki.com>
@copyright Copyright © 2014 Richard Cook
@license http://www.gnu.org/copyleft/gpl.html GNU General Public License 3.0 or later
"""
# User-defined variables
HOST = "irc.freenode.net"
IRC_PORT = 6667
#CHANNEL = "#ShoutWiki-staff"
CHANNEL = "#cook879" # test channel
NICKNAME = "StaffWikiRC"
UDP_PORT = 1338
IRC_OPERATOR = '@unaffiliated/cook879'
IRC_OPERATOR2 = '@wikimedia/Lcawte'
IRC_OPERATOR3 = '@MediaWiki/Jack-Phoenix'
# End user-defined variables
import re
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
recver = None
class StaffWikiBot( irc.IRCClient ):
""" Bot class inherits from irc.IRCClient
see https://twistedmatrix.com/documents/14.0.0/api/twisted.words.protocols.irc.IRCClient.html
"""
# self.nickname is needed for something - I forget what
nickname = NICKNAME
def signedOn( self ):
global recver
self.join( CHANNEL )
recver = self
def kickedFrom( self, channel, kicker, message ):
self.join( channel )
def gotUDP( self, broadcast ):
self.msg( CHANNEL, broadcast )
def privmsg( self, user, channel, message ):
""" Processes messages
Quits if operator private messages the bot 'quit'
Responds appropriately to known triggers
Links to revisions on Code Review
Links to tickets in osTicket
"""
if channel.lower() == self.nickname.lower():
if re.search( IRC_OPERATOR, user ) or re.search( IRC_OPERATOR2, user ) or re.search( IRC_OPERATOR3, user ):
if message.lower() == 'quit':
reactor.stop()
elif channel.lower() == CHANNEL:
# I'm sure there's more than 3 important pages :P
if re.search( "^!private", message ):
self.msg( CHANNEL, "Private wiki code: https://staff.shoutwiki.com/wiki/Private_wiki" )
elif re.search( "^!social", message ):
self.msg( CHANNEL, "Social tools: https://staff.shoutwiki.com/wiki/Social_tools" )
self.msg( CHANNEL, "Social tools installation guide: https://staff.shoutwiki.com/wiki/KB:Installing_social_profile" )
elif re.search( "^!targets", message ):
self.msg( CHANNEL, "Our targets for the year: https://staff.shoutwiki.com/wiki/ShoutWiki_2014" )
elif re.search( "r[0-9]+", message ):
revisions = re.findall( "r[0-9]+", message )
for revision in revisions:
rNo = revision[1:]
self.msg( CHANNEL, "https://staff.shoutwiki.com/wiki/Special:Code/ShoutWiki/" + rNo )
elif re.search( "ticket #[0-9]+", message ):
tickets = re.findall( "ticket #[0-9]+", message )
for ticket in tickets:
tNo = ticket[8:]
self.msg( CHANNEL, "https://support.shoutwiki.com/scp/tickets.php?id=" + tNo )
elif re.search( "bug #[0-9]+", message ):
bugs = re.findall( "bug #[0-9]+", message)
for bug in bugs:
bNo = bug[5:]
self.msg( CHANNEL, "https://bugzilla.shoutwiki.com/show_bug.cgi?id=" + bNo )
class StaffWikiBotFactory( protocol.ClientFactory ):
""" Factory class inherits from ClientFactory
see https://twistedmatrix.com/documents/14.0.0/api/twisted.internet.protocol.ClientFactory.html
"""
def buildProtocol( self, addr ):
b = StaffWikiBot()
b.factory = self
return b
def clientConnectionLost( self, connector, reason ):
""" If we get disconnected, try and reconnect to server. """
connector.connect()
def clientConnectionFailed( self, connector, reason ):
print("Connection failed: ", reason)
connector.connect()
class Echo( protocol.DatagramProtocol ):
""" Handles the recieving of recent changes data
Inherits from DatagramProtocol
see https://twistedmatrix.com/documents/14.0.0/api/twisted.internet.protocol.DatagramProtocol.html
"""
def datagramReceived( self, data, (host, port) ):
global recver
recver.gotUDP( data )
# Create factory protocol and application
f = StaffWikiBotFactory()
# UDP stuff
reactor.listenUDP( UDP_PORT, Echo() )
# Connect factory to host and port
reactor.connectTCP( HOST, IRC_PORT, f )
# Run bot
reactor.run()