Skip to content

Instantly share code, notes, and snippets.

@AmauryCarrade
Last active December 9, 2016 01:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmauryCarrade/43b1c31e69eb1b67ad16852141d1c835 to your computer and use it in GitHub Desktop.
Save AmauryCarrade/43b1c31e69eb1b67ad16852141d1c835 to your computer and use it in GitHub Desktop.
Script to retrieve the users list in an IRC channel without running a bot. The “bot“ does not connects to the channels. Intended to be executed periodically. Does not works with secret (+s) channels. Python 2.7+.
{"#chan": ["CheshireScrat", "Ichi", "Voren", "Anna", "GLaDOS", "Guy", "Swaps", "@Amaury", "Fanch"]}
from __future__ import print_function
import socket
import random
import json
settings = {
'network': 'irc.example.com',
'port': 6667,
'nick': 'Lst' + str(random.Random().randint(10000, 99999)),
'channels': ['#chan'] # must be in mode -s
}
names = {}
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((settings['network'], settings['port']))
irc.send("USER "+ settings['nick'] +" "+ settings['nick'] +" "+ settings['nick'] +" :List bot\n") #user authentication
irc.send("NICK "+ settings['nick'] +"\n")
for chan in settings['channels']:
irc.send("NAMES " + chan + "\n")
wait_for_names = True
while wait_for_names:
text = irc.recv(2048)
if text:
lines = text.split("\r\n")
for text in lines:
if 'PING' in text:
irc.send('PONG ' + text.split() [1] + '\r\n')
elif ' 353 ' in text: # code for names answer
parts = text.strip().split(':')
channel_names = parts[2].split(' ')
channel = parts[1].strip().split(' ')[-1]
if channel not in names:
names[channel] = channel_names
else:
names[channel] += channel_names
elif ' 366 ' in text: # code for end of names answer
parts = text.strip().split(':')
channel = parts[1].strip().split(' ')[-1]
# if received without names before then the channel is empty or
# not accessible (+s).
if channel not in names:
names[channel] = []
if len(names) == len(settings['channels']):
wait_for_names = False
with open('./irc.json', 'w') as f:
print(json.dumps(names), file=f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment