1

I'm programming a bot to monitor chat on twitch.tv over IRC. Here's what a successful connection and login looks like in Telnet:

Microsoft Telnet> o irc.twitch.tv 6667
PASS <password>
NICK <username>
:tmi.twitch.tv 001 <user> :Welcome, GLHF!
:tmi.twitch.tv 002 <user> :Your host is tmi.twitch.tv
:tmi.twitch.tv 003 <user> :This server is rather new
:tmi.twitch.tv 004 <user> :-
:tmi.twitch.tv 375 <user> :-
:tmi.twitch.tv 372 <user> :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 <user> :>

I wrote a simple test script that successfully connects to the server but only receives part of the message.

# irctest.py
import socket

HOST='irc.twitch.tv'
PORT=6667

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b"PASS oauth:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n")
s.sendall(b"NICK user\r\n")
data = s.recv(2048)
s.close()
print(repr(data.decode()))

The string printed with data.decode() is only the first line of the successful connection response:

':tmi.twitch.tv 001 <user> :Welcome, GLHF!\r\n'

This means that the connection works as intended. I've tried doing a second s.recv(2048) but this hangs indefinitely with s.settimeout(None). I've also tried increasing the buffer size but this doesn't appear to have any effect. Anyone know what's going on?

Twitch has a help doc on IRC connections here.

Mac O'Brien
  • 2,407
  • 18
  • 22

1 Answers1

0

I think what you should do is something like this:

import socket

HOST='irc.twitch.tv'
PORT=6667

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(None)
s.connect((HOST, PORT))
s.sendall(b"PASS oauth:...")
s.sendall(b"USER ...")
s.sendall(b"NICK ...")
s.sendall(b"JOIN ...")

while True:
    data = irc.recv(1204)
    if data.find('PING') != -1:
        irc.send(data.replace('PING', 'PONG'))
    print(repr(data.decode()))

code from this question: IRC bot in python won't send messages

Community
  • 1
  • 1
mitnk
  • 3,127
  • 2
  • 21
  • 28
  • I guess I should probably clarify in the OP, but I'm less worried about getting a script that works (I've found workarounds) than I am about figuring out why this is a problem. The Twitch IRC help doc states that sending PASS and the NICK is the correct process for connecting, regardless of IRC standard. I'm just confused as to why the server doesn't respond the same way to what seem to be identical queries. – Mac O'Brien May 27 '14 at 19:35
  • @Ceann I didn't have a credential for twitch, cannot have a test for telnet and python ;) – mitnk May 27 '14 at 23:56
  • Gotcha. Thanks for your help though! – Mac O'Brien May 28 '14 at 00:04