summaryrefslogtreecommitdiffstats
path: root/clients/ivamPipeConnector.py
blob: 7d3cb5fa015d936a545a26211638b8f052e175bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# $Id$
#
# This file is part of ivam2.
#
# ivam2 is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# ivam2 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ivam2; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

import os, sys, time, select, signal, gzip

import ivamCore, ivamApi, ivamUtil

pc = None

def sigterm(*args):
	pc.quit = 1

class PipeConnector(ivamApi.Connector):

    BUFSIZE = 128

    stdin = sys.stdin
    stdout = sys.stdout

    playing = False
    recording = False

    timeout = 0
    quit = False
    umask = 0007

    def __init__(self, processor):

        if ivamCore.DEBUG:
            ivamCore.log("PipeConnector()")

        global pc
        pc = self
        signal.signal(signal.SIGTERM, sigterm)

        self.callerNumber = os.getenv("CALLERMSN")
        self.ringNumber = os.getenv("RINGMSN")
        self.processor = processor

        try:
            s = os.fstat(128)
            self.pipeHack = True
            ivamCore.log("Found and enabled pipe hack.")
        except OSError:
            self.pipeHack = False
            ivamCore.log("Pipe hack not detected.")

    def openDtmf(self):

        try:
            fn = os.environ["DTMFFIFO"]
        except KeyError:
            self.dtmfFifo = None
            return

        self.dtmfFifo = file(fn, "rb")

    def playClip(self, fname):

        self.stopPlayback()

        if ivamCore.DEBUG:
            ivamCore.log("playClip('%s')" % fname)

        self.playFile = ivamUtil.magicFile(fname, "rb")
        self.playName = fname
        self.playing = True
        self.playBuffer = None

    def stopPlayback(self):

        if ivamCore.DEBUG:
            ivamCore.log("stopPlayback()")

        if self.playing:
            self.playFile.close()
            self.playing = False

    def stopPlayback2(self):

        self.stopPlayback()
        self.processor.onClipFinish(self, self.playName)

    def recordClip(self, fname, z = False):

        self.stopRecording()

        if ivamCore.DEBUG:
            ivamCore.log("recordClip('%s')" % fname)

        u = os.umask(self.umask)

        try:
            if z:
                self.recordFile = gzip.open(fname, "w+b")
            else:
                self.recordFile = open(fname, "w+b")
        finally:
            os.umask(u)
            
        self.recordName = fname
        self.recording = True
        self.recordLength = 0

    def stopRecording(self):

        if ivamCore.DEBUG:
            ivamCore.log("stopRecording()")

        if self.recording:
            self.recordFile.close()
            self.recording = False

            self.processor.onRecordFinish(self, self.recordName, self.recordLength)

    def setTimeout(self, t):

        if ivamCore.DEBUG:
            ivamCore.log("timeout(%u)" % t)
        
        if t:
            self.timeout = time.time()+t
        else:
			self.timeout = 0
			

    def run(self):

        if ivamCore.DEBUG:
            ivamCore.log("run()")
            
        self.openDtmf()
        self.processor.onConnect(self, self.callerNumber, self.ringNumber)
        self.loop()
        self.stopPlayback()
        self.stopRecording()
        self.processor.onHangup(self)

        if not self.dtmfFifo is None:
            self.dtmfFifo.close()

        if ivamCore.DEBUG:
            ivamCore.log("run() finished")

    def hangup(self):

        if ivamCore.DEBUG:
            ivamCore.log("hangup()")
            
        self.quit = True

    def loop(self):

        while not self.quit:

            t = None
            now = time.time()

            if self.timeout:
                t = self.timeout - now

                if t < 0:
                    t = 0

            o = []
            i = [self.stdin]

            if not self.dtmfFifo is None:
                i += [self.dtmfFifo]

            if self.playing:
                o += [self.stdout]

            if t is None:
                i, o, x = select.select(i, o, [])
            else:
                i, o, x = select.select(i, o, [], t)

            now = time.time()

            if self.timeout and self.timeout < now:
                self.processor.onTimeout(self)
                self.timeout = 0
                if self.quit: break

			# Check DTMF fifo
            if not self.dtmfFifo is None and self.dtmfFifo in i:
                self.handleDtmf()
                if self.quit: break

			# Check STDOUT
            if self.stdout in o:
                self.writeStdout()
                if self.quit: break

			# Check STDIN
            if self.stdin in i:
                self.readStdin()
                if self.quit: break
            

    def readStdin(self):

        buf = os.read(self.stdin.fileno(), self.BUFSIZE)

        if buf == "":
            self.quit = 1
        elif self.recording:
            self.recordFile.write(buf)
            self.recordLength += len(buf)


    def	writeStdout(self):
        
        if not self.playing:
            return

        if self.playBuffer is None:

            self.playBuffer = self.playFile.read(self.BUFSIZE)

			# EOF?
            if len(self.playBuffer) == 0:
                self.stopPlayback2()
                return

        c =  os.write(self.stdout.fileno(), self.playBuffer)
        self.playBuffer = self.playBuffer[c:]
			
        if len(self.playBuffer) == 0:
            self.playBuffer = None

    def handleDtmf(self):

        if not self.dtmfFifo is None:

            d = self.dtmfFifo.read(1)
		
            if d == "":
                self.dtmfFifo.close()
                self.dtmfFifo = None
            else:
                self.processor.onDtmfEvent(self, d)

    def flushOutput(self):

        if not self.pipeHack:
            return

        try:
            b = os.read(128, 4096)
            if ivamCore.DEBUG:
                ivamCore.log("Pipe hack succeeded")
        except OSError:

            if ivamCore.DEBUG:
                ivamCore.log("Pipe hack failed")