Skip to content

Instantly share code, notes, and snippets.

@cthom06
Created July 22, 2011 12:31
Show Gist options
  • Save cthom06/1099358 to your computer and use it in GitHub Desktop.
Save cthom06/1099358 to your computer and use it in GitHub Desktop.
import pyaudio, threading, base64, speex, audioop, socket, sys
class Packet:
""" Use this class to parse dAmn packets.
Data is stored in the attributes cmd, param,
args, body and raw.
"""
def __init__(self, data=None, sep='='):
self.cmd, self.param, self.args, self.body, self.raw = None, None, {}, None, data
if not bool(data): return
if '\n\n' in data:
self.body = data[data.find('\n\n')+2:]
data = data[:data.find('\n\n')]
breaks = data.split('\n')
if not bool(breaks): return
if len(breaks) >= 1 and sep not in breaks[0]:
head = breaks.pop(0).split(' ')
self.cmd = head[0] or None
self.param = None if len(head) < 2 else head[1]
for line in breaks:
if sep not in line: continue
self.args[line[:line.find(sep)]] = line[line.find(sep)+len(sep):]
# And that's the end of that chapter.
a = pyaudio.PyAudio()
inp = a.open(41000 / 4, 1, pyaudio.paInt16, input=True, start=False)
outp = a.open(41000 / 4, 1, pyaudio.paInt16, output=True)
user = sys.argv[1].lower()
pk = sys.argv[2]
other = sys.argv[3].lower()
wlock = threading.Lock()
room = ""
if user < other:
room = "pchat:"+user+":"+other
else:
room = "pchat:"+other+":"+user
def helper(data, lastT):
if lastT: lastT.join()
outp.write(speex.new().decode(data, True))
def reader(conn):
buffer = ""
t = None
while 1:
buffer += conn.recv(4096)
parts = buffer.split("\0")
while len(parts) > 1:
pkt = Packet(parts[0])
if pkt.cmd == "ping":
wlock.aquire()
conn.sendall("pong\n\0")
wlock.release()
elif pkt.cmd == "disconnect" or pkt.cmd == "part":
print "connection gone"
sys.exit(1)
elif pkt.cmd == "recv":
pkt = Packet(pkt.body)
if pkt.cmd == "msg" and pkt.args["from"].lower() == other:
t = threading.Thread(target=helper, args=(base64.b64decode(pkt.body), t))
t.start()
else:
pass#ignore
parts = parts[1:]
buffer = parts[0]
# main
empty = "\0" * 4000
s = socket.socket()
s.connect(("chat.deviantart.com", 3900))
print "send:", s.sendall("dAmnClient 0.3\nagent=speakerbox\n\0")
tmp = s.recv(1024)
p = Packet(tmp)
while not p.cmd:
p = Packet(s.recv(1024))
if p.cmd != "dAmnServer":
print "invalid handshake : " + p.cmd
sys.exit(1)
print "connected"
s.sendall("login "+user+"\npk="+pk+"\n\0")
if Packet(s.recv(4096)).args['e'] != "ok":
print "invalid login"
sys.exit(1)
s.sendall("join "+room+"\n\0")
if Packet(s.recv(4096)).args['e'] != "ok":
print "couldn't join "+room
sys.exit(1)
threading.Thread(target=reader, args=(s,)).start()
inp.start_stream()
while 1:
tmp = ""
have = False
i = 0
while i < 5:
d = inp.read(2000)
if audioop.max(d, 2) > 375:
have = True
tmp += d
elif have:
tmp += empty
i += 1
if tmp:
d = base64.b64encode(speex.new().encode(tmp, True))
wlock.acquire()
s.sendall("send "+room+"\n\nnpmsg main\n\n"+d+"\0")
wlock.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment