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
|
import os, sys, time, select, signal
import ivamCore, ivamApi, ivamUtil
pc = None
def sigterm(*args):
pc.quit = 1
class PipeConnector(ivamApi.Connector):
BUFSIZE = 128
sdtin = sys.stdin
stdout = sys.stdout
playing = False
recording = False
timeout = 0
quit = False
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
def openDtmf(self):
try:
fn = os.environ["DTMFFIFO"]
except KeyError:
self.dtmfFifo = None
return
self.dtmfFifo = file.open(fn, "rb")
def playClip(self, fname):
if ivamCore.DEBUG:
ivamCore.log("playClip('%s')" % fname)
self.stopPlayback()
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
processor.onClipFinish(self, self.playName)
def recordClip(self, fname, gzip = False):
if ivamCore.DEBUG:
ivamCore.log("recordClip('%s')" % fname)
self.stopRecording()
if gzip:
self.recordFile = gzip.open(fname, "w+b")
else:
self.recordFile = open(fname, "w+b")
self.recordName = fname
self.recording = True
def stopRecording(self):
if ivamCore.DEBUG:
ivamCore.log("stopRecording()")
if self.recording:
self.recordFile.close()
self.recording = False
processor.onRecordFinish(self, self.recordName)
def timeout(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)
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:
if ivamCore.DEBUG:
ivamCore.log("iteration")
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 = select.select(i, o, [])
else:
i, o = select.select(i, o, [], t)
now = time.time()
if self.timeout < now:
processor.onTimeout()
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 o:
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)
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.stopPlayback()
return
c = os.write(self.stdout.fileno(), self.playBuffer)
self.playBuffer = self.playBuffer[c:]
if len(self.playBuffer) == 0:
self.playBuffer = None
def dtmfCheck(self):
if not self.dtmfFifo is None:
d = self.dtmfFifoFd.read(1)
if d == "":
self.dtmfFifo.close()
self.dtmfFifo = None
else:
self.onDtmfEvent(self, d)
|