Created
January 3, 2017 13:25
-
-
Save tomnomnom/55dcfa13e2f8580f0d565ae3da2846f6 to your computer and use it in GitHub Desktop.
IRC User List Script - Updated for Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# IRC User List | |
# Written by Tom Hudson for the Raspberry Pi User Guide | |
# Updated 2017-01-03 for Python 3.* | |
# http://tomhudson.co.uk/ | |
import sys, socket, time | |
RPL_NAMREPLY = '353' | |
RPL_ENDOFNAMES = '366' | |
irc = { | |
'host': 'chat.freenode.net', | |
'port': 6667, | |
'channel': '#raspiuserguide', | |
'namesinterval': 5 | |
} | |
user = { | |
'nick': 'botnick', | |
'username': 'botuser', | |
'hostname': 'localhost', | |
'servername': 'localhost', | |
'realname': 'Raspberry Pi Names Bot' | |
} | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
print('Connecting to {host}:{port}...'.format(**irc)) | |
try: | |
s.connect((irc['host'], irc['port'])) | |
except socket.error: | |
print('Error connecting to IRC server {host}:{port}'.format(**irc)) | |
sys.exit(1) | |
s.send('NICK {nick}\r\n'.format(**user).encode()) | |
s.send('USER {username} {hostname} {servername} :{realname}\r\n'.format(**user).encode()) | |
s.send('JOIN {channel}\r\n'.format(**irc).encode()) | |
s.send('NAMES {channel}\r\n'.format(**irc).encode()) | |
read_buffer = '' | |
names = [] | |
while True: | |
read_buffer += s.recv(1024).decode() | |
lines = read_buffer.split('\r\n') | |
read_buffer = lines.pop(); | |
for line in lines: | |
response = line.rstrip().split(' ', 3) | |
response_code = response[1] | |
if response_code == RPL_NAMREPLY: | |
names_list = response[3].split(':')[1] | |
names += names_list.split(' ') | |
if response_code == RPL_ENDOFNAMES: | |
print('\r\nUsers in {channel}:'.format(**irc)) | |
for name in names: | |
print(name) | |
names = [] | |
time.sleep(irc['namesinterval']) | |
s.send('NAMES {channel}\r\n'.format(**irc).encode()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1c1 | |
< #!/usr/bin/env python | |
--- | |
> #!/usr/bin/env python3 | |
3a4 | |
> # Updated 2017-01-03 for Python 3.* | |
28c29 | |
< print 'Connecting to %(host)s:%(port)s...' % irc | |
--- | |
> print('Connecting to {host}:{port}...'.format(**irc)) | |
32c33 | |
< print 'Error connecting to IRC server %(host)s:%(port)s' % irc | |
--- | |
> print('Error connecting to IRC server {host}:{port}'.format(**irc)) | |
35,38c36,39 | |
< s.send('NICK %(nick)s\r\n' % user) | |
< s.send('USER %(username)s %(hostname)s %(servername)s :%(realname)s\r\n' % user) | |
< s.send('JOIN %(channel)s\r\n' % irc) | |
< s.send('NAMES %(channel)s\r\n' % irc) | |
--- | |
> s.send('NICK {nick}\r\n'.format(**user).encode()) | |
> s.send('USER {username} {hostname} {servername} :{realname}\r\n'.format(**user).encode()) | |
> s.send('JOIN {channel}\r\n'.format(**irc).encode()) | |
> s.send('NAMES {channel}\r\n'.format(**irc).encode()) | |
44c45 | |
< read_buffer += s.recv(1024) | |
--- | |
> read_buffer += s.recv(1024).decode() | |
54c55 | |
< print '\r\nUsers in %(channel)s:' % irc | |
--- | |
> print('\r\nUsers in {channel}:'.format(**irc)) | |
56c57 | |
< print name | |
--- | |
> print(name) | |
59c60 | |
< s.send('NAMES %(channel)s\r\n' % irc) | |
--- | |
> s.send('NAMES {channel}\r\n'.format(**irc).encode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment