answer.py :  » Web-Services » Shtoom » shtoom-0.2 » shtoom » app » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Services » Shtoom 
Shtoom » shtoom 0.2 » shtoom » app » answer.py
# Copyright (C) 2004 Anthony Baxter

# The Answering Machine app. Accepts all calls, plays a message 
# (based on the 'to' address) then records a message for them.

from shtoom.app.interfaces import Application
from shtoom.app.base import BaseApplication
from twisted.internet import defer
from twisted.python import log
from twisted.protocols import sip
import sys, time, os

from shtoom.app.base import STATE_SENDING,STATE_RECEIVING

from shtoom.audio import FMT_PCMU,FMT_GSM,FMT_SPEEX,FMT_DVI4
from shtoom.audio.fileaudio import getFileAudio

from message import Message

class AnsweringMachine(Message):
    __implements__ = ( Application, )

    def __init__(self, *args, **kwargs):
        self._playingAnnounce = {}
        Message.__init__(self, *args, **kwargs)

    def acceptCall(self, call, **calldesc):
        print "acceptCall for %r"%calldesc
        calltype = calldesc.get('calltype')
        if calldesc.get('toAddr'):
            comment, toAddress, tag = tpsip.parseAddress(calldesc['toAddr'][0])
            toAddress = (toAddress.username, toAddress.host)
        else:
            toAddress = None
        if calldesc.get('fromAddr'):
            comment, fromAddress, tag = tpsip.parseAddress(calldesc['fromAddr'][0])
            fromAddress = (fromAddress.username, fromAddress.host)
        else:
            fromAddress = None
        d = defer.Deferred()
        cookie = self.getCookie()
        self.openAudioDevice(cookie, toAddress, fromAddress)
        d.addCallback(lambda x: self._createRTP(cookie,
                                                calldesc['fromIP'],
                                                calldesc['withSTUN']))
        self._calls[cookie] = call
        if calltype == 'inbound':
            # Otherwise we chain callbacks
            log.msg("accepting incoming call from %s"%calldesc['desc'])
            d.callback('ok')
        else:
            raise ValueError, "unknown call type %s"%(calltype)
        return cookie, d

    def openAudioDevice(self, callcookie, toAddress, fromAddress):
        import os, os.path
        announceDir = self.getPref('announce_directory')
        messageDir = self.getPref('message_directory')
        if not os.path.isdir(announceDir):
            log.err("ANNOUNCEMENTS DIRECTORY IS MISSING: %s"%(announceDir))
            self.dropCall(callcookie)
        if not os.path.isdir(messageDir):
            log.err("MESSAGE DIRECTORY IS MISSING: %s"%(messageDir))
            self.dropCall(callcookie)
        touser, todomain = toAddress
        fromuser, fromdomain = fromAddress
        da = os.path.join(announceDir, todomain)
        if os.path.isdir(da):
            announceFile = os.path.join(da, '%s.wav'%touser)
            if os.path.exists(announceFile):
                messageFile = os.path.join(messageDir, 
                        '%s@%s_%s@%s_%d.raw'%(touser, todomain,
                                              fromuser, fromdomain,
                                              time.time()))
                
            else:
                announceFile = self.getPref('fallback_message')
                messageFile = None
        else:
            log.err("announcements directory for domain %s is missing"%(todomain))
            announceFile = self.getPref('fallback_message')
            messageFile = None

        self._audios[callcookie] = getFileAudio(announceFile, messageFile)
        self._audioStates[callcookie] = STATE_SENDING
        self._playingAnnounce[callcookie] = messageFile

    def closeAudioDevice(self, callcookie):
        self._audios[callcookie].close()
        del self._audios[callcookie]
        if self._audioStates[callcookie] == STATE_SENDING:
            # They didn't get to record a message
            if self._playingAnnounce[callcookie] is not None:
                os.remove(self._playingAnnounce[callcookie])
        del self._audioStates[callcookie]
        del self._playingAnnounce[callcookie]

    def finishedAudio(self, callcookie):
        if self._playingAnnounce[callcookie] is None:
            self.dropCall(callcookie)
        else:
            self._audioStates[callcookie] = STATE_RECEIVING
        
    def appSpecificOptions(self, opts):
        import os.path
        from shtoom.Options import OptionGroup,StringOption,ChoiceOption
        app = OptionGroup('shtam', 'Shtam')
        app.addOption(StringOption('announce_directory','announcements live here', default='/tmp/recordings'))
        app.addOption(StringOption('message_directory','saved messages live here', default='/tmp/messages'))
        app.addOption(StringOption('fallback_message','fallback message when no announcement can be found', default='/tmp/fallback.wav'))
        opts.addGroup(app)
        opts.setOptsFile('.shtamrc')

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.