forked from saghul/sipsimple-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotchachinese.py
143 lines (111 loc) · 4.1 KB
/
gotchachinese.py
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
#!/usr/bin/python
import os, time
from cmd import Cmd
from random import randint
from application.notification import NotificationCenter, IObserver
from application.python import Null
from threading import Event
from zope.interface import implements
from sipsimple.account import AccountManager
from sipsimple.application import SIPApplication
from sipsimple.storage import FileStorage
from sipsimple.core import ToHeader, SIPURI
from sipsimple.lookup import DNSLookup, DNSLookupError
from sipsimple.session import Session
from sipsimple.streams import AudioStream
from sipsimple.threading.green import run_in_green_thread
class TenFifty(object):
implements(IObserver)
def __init__(self):
self.application = SIPApplication()
self.quit_event = Event()
notification_center = NotificationCenter()
notification_center.add_observer(self, sender=self.application)
def start(self):
self.application.start(FileStorage(os.path.realpath('test-config')))
def stop(self):
self.application.stop()
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification)
def _NH_SIPApplicationDidStart(self, notification):
print 'Clem\'s gonna stress...'
def _NH_SIPApplicationDidEnd(self, notification):
self.quit_event.set()
class OutgoingCallHandler(object):
implements(IObserver)
def __init__(self):
self.session = None
@run_in_green_thread
def call(self, destination):
if self.session is not None:
print 'Another session is in progress'
return
callee = ToHeader(SIPURI.parse(destination))
try:
routes = DNSLookup().lookup_sip_proxy(callee.uri, ['udp']).wait()
except DNSLookupError, e:
print 'DNS lookup failed: %s' % str(e)
else:
account = AccountManager().default_account
self.session = Session(account)
NotificationCenter().add_observer(self, sender=self.session)
self.session.connect(callee, routes, [AudioStream()])
def hangup(self):
if self.session is None:
print 'There is no session to hangup'
return
self.session.end()
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification)
def _NH_SIPSessionGotRingIndication(self, notification):
print 'Ringing!'
def _NH_SIPSessionWillStart(self, notification):
print 'connessione...'
def _NH_SIPSessionDidStart(self, notification):
print 'Cioccato :('
global catched
catched = catched + 1
def _NH_SIPSessionDidFail(self, notification):
#print 'Failed to connect'
self.session = None
NotificationCenter().remove_observer(self, sender=notification.sender)
def _NH_SIPSessionDidEnd(self, notification):
#print 'Session ended'
self.session = None
NotificationCenter().remove_observer(self, sender=notification.sender)
def ringToClem():
call_handler.call("sip:victim_number@voip.provider.it") # clem
time.sleep(6)
call_handler.hangup()
application = TenFifty()
application.start()
call_handler = OutgoingCallHandler()
rings = 0
catched = 0
interval_from = 60
interval_to = 180
interval_reset = 1
try:
while True:
if catched >= (10 * interval_reset):
interval_from = interval_from * 2
interval_to = interval_to * 2
interval_reset = interval_reset + 1
if rings > 0: # skip timeout for the first run
time.sleep(randint(interval_from,interval_to))
else:
time.sleep(5)
ringToClem()
rings = rings + 1
os.system('clear')
print 'Clem is under goccia cinese now, press CTRL+C to let him free' + '\n\n'
print 'Rings total: ', rings
print 'Rings catched: ', catched
except KeyboardInterrupt:
pass
class Stop(object):
pass
application.stop()
application.quit_event.wait()