2010-08-08 13:27:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2010-08-18 15:40:19 +00:00
|
|
|
#
|
|
|
|
# Copyright 2010 Maurizio Porrato <maurizio.porrato@gmail.com>
|
|
|
|
# See LICENSE.txt for copyright info
|
2010-08-08 13:27:19 +00:00
|
|
|
|
|
|
|
from __future__ import with_statement
|
|
|
|
from frn.protocol.client import FRNClient, FRNClientFactory
|
2010-08-18 15:40:19 +00:00
|
|
|
from frn.user import FRNUser
|
2010-08-09 14:40:19 +00:00
|
|
|
from twisted.internet import reactor, task
|
|
|
|
from twisted.internet.defer import DeferredList
|
|
|
|
from twisted.python import log
|
|
|
|
import os, string
|
|
|
|
safe_chars = string.ascii_letters+string.digits+' :;.,+-=$@'
|
|
|
|
|
|
|
|
PARROT_AUDIO_DELAY = 5.0
|
|
|
|
|
|
|
|
def sanitizeFilename(name):
|
|
|
|
r = ''
|
|
|
|
for c in name:
|
|
|
|
if c in safe_chars:
|
|
|
|
r += c
|
|
|
|
return r
|
2010-08-08 13:27:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FRNParrot(FRNClient):
|
|
|
|
|
2010-08-09 14:40:19 +00:00
|
|
|
def getClientName(self, client_id):
|
|
|
|
if self.clientsById.has_key(client_id):
|
|
|
|
return self.clientsById[client_id]['on']
|
|
|
|
else:
|
|
|
|
return client_id
|
|
|
|
|
|
|
|
def textMessageReceived(self, client, message, target):
|
|
|
|
log.msg("Type %s message from %s: %s" %
|
|
|
|
(target, self.getClientName(client), message))
|
|
|
|
if target == 'P': # Only reply to private messages
|
|
|
|
if not message.startswith('play'):
|
|
|
|
self.sendTextMessage(client, message)
|
|
|
|
else:
|
|
|
|
cmd = message.split()
|
|
|
|
if len(cmd) > 1:
|
|
|
|
message = sanitizeFilename(cmd[1])
|
|
|
|
else:
|
|
|
|
message = 'monkeys'
|
|
|
|
filename = 'sounds/%s.wav' % message
|
|
|
|
if os.path.exists(filename):
|
|
|
|
log.msg("Streaming file %s" % filename)
|
|
|
|
with file(filename, 'rb') as sf:
|
|
|
|
sf.seek(0x3c) # Skip wav header
|
|
|
|
while True:
|
|
|
|
b = sf.read(325)
|
|
|
|
if len(b) < 325:
|
|
|
|
break
|
|
|
|
self.feedStreaming(b)
|
|
|
|
self.factory.reactor.callLater(0.5,
|
|
|
|
self.startStreaming)
|
|
|
|
else:
|
|
|
|
self.sendTextMessage(client, "File not found")
|
|
|
|
|
|
|
|
def stopTransmission(self):
|
|
|
|
FRNClient.stopTransmission(self)
|
|
|
|
log.msg("Stopped playback.")
|
2010-08-08 13:27:19 +00:00
|
|
|
|
2010-08-09 14:40:19 +00:00
|
|
|
def startRepeating(self, from_id):
|
|
|
|
log.msg("%s stopped talking: starting playback." %
|
|
|
|
self.clients[from_id-1]['on'])
|
|
|
|
self.startStreaming()
|
|
|
|
|
|
|
|
def audioFrameReceived(self, from_id, frames):
|
|
|
|
recname = sanitizeFilename(self.clients[from_id-1]['on'])
|
|
|
|
with file('recordings/%s.gsm' % recname, 'ab') as f:
|
2010-08-08 13:27:19 +00:00
|
|
|
f.write(frames)
|
2010-08-09 14:40:19 +00:00
|
|
|
self.feedStreaming(frames)
|
|
|
|
try:
|
|
|
|
self.parrot_timer.reset(PARROT_AUDIO_DELAY)
|
|
|
|
except:
|
|
|
|
log.msg("%s started talking" %
|
|
|
|
self.clients[from_id-1]['on'])
|
|
|
|
self.parrot_timer = self.factory.reactor.callLater(
|
|
|
|
PARROT_AUDIO_DELAY, self.startRepeating, from_id)
|
|
|
|
self.pong()
|
|
|
|
|
|
|
|
def loginResponse(self, info):
|
|
|
|
log.msg("Login: %s" % info['al'])
|
|
|
|
|
|
|
|
def clientsListUpdated(self, clients):
|
|
|
|
self.clients = clients
|
|
|
|
self.clientsById = dict([(i['id'], i) for i in clients])
|
2010-08-08 13:27:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FRNParrotFactory(FRNClientFactory):
|
|
|
|
protocol = FRNParrot
|
2010-08-09 14:40:19 +00:00
|
|
|
reactor = reactor
|
2010-08-08 13:27:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
2010-08-18 15:40:19 +00:00
|
|
|
from os.path import dirname, join as pjoin
|
2010-08-08 13:27:19 +00:00
|
|
|
from ConfigParser import ConfigParser
|
2010-08-09 14:40:19 +00:00
|
|
|
|
|
|
|
log.startLogging(sys.stderr)
|
2010-08-08 13:27:19 +00:00
|
|
|
|
2010-08-18 15:40:19 +00:00
|
|
|
basedir = dirname(__file__)
|
|
|
|
|
|
|
|
acfg = ConfigParser()
|
|
|
|
acfg.read(['/etc/grn/accounts.conf',
|
|
|
|
pjoin(basedir,'accounts.conf'), 'accounts.conf'])
|
|
|
|
|
|
|
|
scfg = ConfigParser()
|
|
|
|
scfg.read(['/etc/grn/servers.conf',
|
|
|
|
pjoin(basedir,'servers.conf'), 'servers.conf'])
|
|
|
|
|
|
|
|
argc = len(sys.argv)
|
|
|
|
if argc >= 3:
|
|
|
|
server_name, network_name = sys.argv[2].split(':',1)
|
|
|
|
account_cfg = acfg.items(sys.argv[1])+[('network', network_name)]
|
|
|
|
server_cfg = scfg.items(server_name)
|
|
|
|
server = scfg.get(server_name, 'server')
|
|
|
|
port = scfg.getint(server_name, 'port')
|
|
|
|
|
|
|
|
d = dict(account_cfg)
|
|
|
|
user = FRNUser(
|
|
|
|
EA=d['email'],
|
|
|
|
PW=d['password'], ON=d['operator'],
|
|
|
|
BC=d['transmission'], DS=d['description'],
|
|
|
|
NN=d['country'], CT=d['city'], NT=d['network'])
|
|
|
|
reactor.connectTCP(server, port, FRNParrotFactory(user))
|
|
|
|
reactor.run()
|
2010-08-08 13:27:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
# vim: set et ai sw=4 ts=4 sts=4:
|