Skip to content

Instantly share code, notes, and snippets.

@dpflug
Forked from zeekay/say-hi.py
Last active August 29, 2015 14:01
Show Gist options
  • Save dpflug/26d91049771d54dac1da to your computer and use it in GitHub Desktop.
Save dpflug/26d91049771d54dac1da to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import os, pwd, select, signal, socket, sys, time
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
username = pwd.getpwuid(os.getuid())
hostname = socket.gethostname()
if not sys.stdin.isatty(): sys.stdin = open('/dev/tty')
def hello():
irc.send(b'PRIVMSG zeekay :hello, i am from the internet!\r\n')
def goodbye(*args):
irc.send(b'DISCONNECT')
print '\nbye!'
sys.exit(0)
signal.signal(signal.SIGINT, goodbye)
print 'Hi {0}! Please wait while I connect your call... (press CTRL-C to exit)'.format(username)
irc.connect(('irc.freenode.net', 6667))
irc.send('NICK i_am_{0}\r\n'.format(username).encode('UTF-8'))
irc.send('USER {0} {1} irc.freenode.net :{0}\r\n'.format(username, hostname).encode('UTF-8'))
while True:
read, write, error = select.select([sys.stdin, irc],[],[])
for i in read:
if i == sys.stdin:
irc.send('PRIVMSG zeekay :{0}\r\n'.format(i.readline()).encode('UTF-8'))
sys.stdout.write('> ')
sys.stdout.flush()
elif i == irc:
buffer = i.recv(1024)
for line in buffer.splitlines():
try:
_, info, line = line.decode('UTF-8').split(':', 2)
except ValueError:
break
server, type = info.split(' ')[:2]
if type == 'PRIVMSG':
sys.stdout.write('\x08' * 2)
sys.stdout.write('< {0}\n> '.format(line))
sys.stdout.flush()
elif type == 'NOTICE':
hello()
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment