-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.py
218 lines (186 loc) · 7.6 KB
/
irc.py
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
#!/usr/bin/python
'''
Requires run with $ python irc.py
It saves the log in log_<date>.html
Made by Jaspreet Singh (jaspreet on IRC)
Code is inspired from these links
-- https://github.com/ignaciouy/Google-Code-In-Bot
-- https://github.com/aviraldg/gcibot
-- https://github.com/sant0sh/PyBot
'''
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
import time
import sys
import os
from subprocess import call, Popen
import signal
today_date = time.strftime("%d-%m-%Y")
html_header = '''<!DOCTYPE html>
<html lang='en'>
<head>
<title>
pgmpy Log of '''+today_date+'''
</title>
<link rel='stylesheet' type='text/css' href='css/bootstrap.min.css' media='screen' />
</head>
<body>
<div class='container'>
<h2>Log of the <code>#pgmpy</code> IRC Channel</h2><br/>
<h3>All the times shown here presently are in Indian Standard Time(IST) +0530Hrs<h3/>
<h3>Date : '''+today_date+''' </h3><br/><br/>
'''
html_footer = '''</div>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js'>
</script>
</body>
</html>'''
class MessageLogger:
"""
An independent logger class (because separation of application
and protocol logic is a good thing).
"""
def __init__(self, file):
self.file = file
def log(self, message,flag):
"""Write a message to the file."""
timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
if flag==1:
self.file.write('<kbd>%s</kbd> ' % (timestamp))
self.file.write('%s<br/>\n' % (message))
self.file.flush()
def close(self):
self.file.close()
class LogBot(irc.IRCClient):
"""A logging IRC bot."""
nickname = "pgmpybot"
def connectionMade(self):
irc.IRCClient.connectionMade(self)
if os.path.isfile(self.factory.filename):
fo = open(self.factory.filename , "r+")
contents = fo.read()
seek_position = contents.find(html_footer)
fo.seek(seek_position-1)
self.logger = MessageLogger(fo)
else:
self.append_flag=0
self.logger = MessageLogger(open(self.factory.filename, "w"))
self.logger.log(html_header,0)
self.logger.log("<strong>[connected at %s]</strong>" % time.asctime(time.localtime(time.time())),1)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
self.logger.log("<strong>[disconnected at %s]</strong>" % time.asctime(time.localtime(time.time())),1)
self.logger.log(html_footer,0)
self.logger.close()
# callbacks for events
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
if self.factory.filename.find(file_name_gen())!=-1:
self.logger.log("<strong>[I have joined %s]</strong>" % channel,1)
else:
global repeat_run
repeat_run = 1
reactor.stop()
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
if self.factory.filename.find(file_name_gen())!=-1:
self.logger.log("<code><%s></code> %s" % (user, msg),1)
else:
global repeat_run
repeat_run = 1
reactor.stop()
# Check to see if they're sending me a private message
if channel == self.nickname:
msg = "Private Chat doesnt interest me!"
self.msg(user, msg)
return
# Otherwise check to see if it is a message directed at me
if msg.startswith(self.nickname + ":"):
msg = "%s: Hello, I am an automated bot made to keep the logs of the pgmpy IRC Channel" % user
self.msg(channel, msg)
if self.factory.filename.find(file_name_gen())!=-1:
self.logger.log("<code><%s></code> %s" % (self.nickname, msg),1)
else:
#global repeat_run
repeat_run = 1
reactor.stop()
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
user = user.split('!', 1)[0]
if self.factory.filename.find(file_name_gen())!=-1:
self.logger.log("<em>* %s %s</em>" % (user, msg),1)
else:
global repeat_run
repeat_run = 1
reactor.stop()
# irc callbacks
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
old_nick = prefix.split('!')[0]
new_nick = params[0]
if self.factory.filename.find(file_name_gen())!=-1:
self.logger.log("<em>%s is now known as %s</em>" % (old_nick, new_nick),1)
else:
global repeat_run
repeat_run = 1
reactor.stop()
# For fun, override the method that determines how a nickname is changed on
# collisions. The default method appends an underscore.
def alterCollidedNick(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.
"""
return nickname + '^'
class LogBotFactory(protocol.ClientFactory):
"""A factory for LogBots.
A new protocol instance will be created each time we connect to the server.
"""
def __init__(self, channel, filename):
self.channel = channel
#generating the absolute path
dirPath = os.path.dirname(os.path.abspath(__file__))
self.filename = os.path.join(dirPath,filename)
def buildProtocol(self, addr):
p = LogBot()
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Connection failed:", reason
reactor.stop()
def file_name_gen():
return "log"+time.strftime("%Y_%m_%d")+".html"
def main():
global repeat_run
repeat_run=0
# Generating the name of the file
filename = file_name_gen()
# initialize logging
log.startLogging(sys.stdout)
if len(sys.argv)==1:
# create factory protocol and application
f = LogBotFactory("pgmpy", filename)
# connect factory to this host and port
reactor.connectTCP("irc.freenode.net", 6667, f)
# run bot
reactor.run()
''' this part is executed when there is some
unexpected problem in the bot leading to the
closing of the connection
'''
call(['python', 'irc.py'])
else:
print "Please run the program in a correct way. $->python irc.py"
if repeat_run==1:
# this part is executed when there is a change in date
call(['python', 'irc.py'])
main()