Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc.py
More file actions
Latest commit
152 lines (122 loc) · 4.44 KB
/
Copy pathirc.py
File metadata and controls
152 lines (122 loc) · 4.44 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
152
# twisted imports
fromtwisted.words.protocolsimportirc
fromtwisted.internetimportreactor, protocol
fromtwisted.pythonimportlog
# system imports
importtime, sys
# Local imports
fromactionsimportconfig, bot, security
classclient(irc.IRCClient):
"""
An IRC bot...
That does stuff...
"""
def__init__(self, nick, **k):
'''Call the other functions we\'ll need to do stuff'''
self.nick=nick
self.nickname=nick
self.bot=bot.worker(self.nick)
# self.logger = bot.logger
self.channels=bot.channelPool()
defconnectionMade(self):
irc.IRCClient.connectionMade(self)
defconnectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
defsignedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
defjoined(self, channel):
"""This will get called when the bot joins the channel."""
print ('/joined '+channel)
self.bot.channelPool.joined(channel)
defleft(self, channel):
"""This will get called when the bot joins the channel."""
print ('/left '+channel)
self.bot.channelPool.parted(channel)
defprivmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
print("user: '%s' channel: '%s' message: '%s' "% (user, channel, msg))
defrestart():
print('Trying to restart')
self.msg(suser, 'attempting reload bot...')
self.bot.wisper(suser, 'storeing session...')
self.bot.save()
reload(bot)
self.msg(suser, 'reload complete!')
self.bot=bot.worker(self.nick)
self.bot.wisper(suser, 'handeler reset...')
self.bot.wisper(suser, 'loading session...')
self.bot.load()
self.bot.wisper(suser, 'session loaded...')
ifchannel==self.nick:
suser=user.split('!',1)[0]
pvmsg=True
else:
suser=channel
pvmsg=False
ifmsg=="!backdoor"andpvmsg:
restart()
elifmsg=='!restart':
ifself.bot.auth.owner(user):
restart()
else:
self.msg(suser, 'you\'re not my mommy...')
else:
self.bot.msgin(user, channel, msg)
jobs=self.bot.queue()
forjobinjobs:
do, target, params=job
action=getattr(self, do, None)
try:
ifactionisnotNone:
action(target, params)
except:
print(action)
print(target)
print(params)
print('!!!!!Not implemented: '+do+' !!!!!')
defaction(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
pass
defirc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
pass
defalterCollidedNick(self, nickname):
"""
Generate an altered version of a nickname that caused a collision in an
effort to create an unused related name for subsequent registration.
"""
returnnickname+'^'
classclientFactory(protocol.ClientFactory):
"""
A factory for irc Bots.
A new protocol instance will be created each time we connect to the server.
"""
def__init__(self, channel, nick):
self.channel=channel
self.nick=nick
defbuildProtocol(self, addr):
p=client(self.nick)
p.factory=self
returnp
defclientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
defclientConnectionFailed(self, connector, reason):
print("connection failed:", reason)
reactor.stop()
defconnect(server, port, channel, nick):
# initialize logging to stdout
log.startLogging(sys.stdout)
# create factory protocol and application
factory=clientFactory(channel, nick)
# connect factory to this host and port
## todo error checking for port
reactor.connectTCP(server, port, factory)
# run bot
## start running in ractorbase in base.py (good luck)
reactor.run()
defmain():
connect(config.server, config.port, config.channel, config.nick)
if__name__=='__main__':
main()