aboutsummaryrefslogtreecommitdiff
path: root/lib/python/Plugins/Extensions/FritzCall/plugin.py
blob: 631cb4c03c7827247bb742b3cfadbdc5ea64eeac (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
from Screens.Screen import Screen
from Screens.MessageBox import MessageBox
from Components.ActionMap import ActionMap
from Components.Label import Label
from Plugins.Plugin import PluginDescriptor
from Tools import Notifications

from twisted.internet import reactor
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.protocols.basic import LineReceiver

from enigma import eTimer

my_global_session = None

from Components.config import config, ConfigSubsection, ConfigIP, ConfigEnableDisable, getConfigListEntry
from Components.ConfigList import ConfigList, ConfigListScreen

config.FritzCall = ConfigSubsection()
config.FritzCall.hostname = ConfigIP(default = [192,168,178,254])
config.FritzCall.enable = ConfigEnableDisable(default = False)

class FritzCallSetup(ConfigListScreen, Screen):
	skin = """
		<screen position="100,100" size="550,400" title="FritzCall Setup" >
		<widget name="config" position="20,10" size="460,350" scrollbarMode="showOnDemand" />
		</screen>"""


	def __init__(self, session, args = None):
		from Tools.BoundFunction import boundFunction
		
		Screen.__init__(self, session)
		self.onClose.append(self.abort)
		
		# nun erzeugen wir eine liste von elementen fuer die menu liste.
		self.list = [ ]
		self.list.append(getConfigListEntry(_("Call monitoring"), config.FritzCall.enable))
		self.list.append(getConfigListEntry(_("Fritz!Box FON IP address"), config.FritzCall.hostname))
		ConfigListScreen.__init__(self, self.list)

		# DO NOT ASK.
		self["setupActions"] = ActionMap(["SetupActions"],
		{
			"save": self.save,
			"cancel": self.cancel,
			"ok": self.save,
		}, -2)

	def abort(self):
		print "aborting"

	def save(self):
		for x in self["config"].list:
			x[1].save()
		if fritz_call is not None:
			fritz_call.connect()
		self.close()

	def cancel(self):
		for x in self["config"].list:
			x[1].cancel()
		self.close()

class FritzProtocol(LineReceiver):
	delimiter = "\r\n"
	
	def lineReceived(self, line):

#15.07.06 00:38:54;CALL;1;4;<provider>;<callee>;
#15.07.06 00:38:58;DISCONNECT;1;0;
#15.07.06 00:39:22;RING;0;<caller>;<outgoing msn>;
#15.07.06 00:39:27;DISCONNECT;0;0;

		a = line.split(';')
		(date, event) = a[0:2]
		
		if event == "RING":
			phone = a[4]
			number = a[3]
			text = _("incoming call!\n%s calls on %s!") % (number, phone)
			timeout = 10
		else:	
			return
		
		Notifications.AddNotification(MessageBox, text, type=MessageBox.TYPE_INFO, timeout=timeout)

class FritzClientFactory(ReconnectingClientFactory):

	initialDelay = 20
	maxDelay = 500
	
	def __init__(self):
		self.hangup_ok = False

	def startedConnecting(self, connector):
		Notifications.AddNotification(MessageBox, _("Connecting to Fritz!Box..."), type=MessageBox.TYPE_INFO, timeout=2)
	
	def buildProtocol(self, addr):
		Notifications.AddNotification(MessageBox, _("Connected to Fritz!Box!"), type=MessageBox.TYPE_INFO, timeout=2)
		self.resetDelay()
		return FritzProtocol()
	
	def clientConnectionLost(self, connector, reason):
		if not self.hangup_ok:
			Notifications.AddNotification(MessageBox, _("Disconnected from\nFritz!Box! (%s)\nretrying...") % reason.getErrorMessage(), type=MessageBox.TYPE_INFO, timeout=4)
		ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
	
	def clientConnectionFailed(self, connector, reason):
		Notifications.AddNotification(MessageBox, _("Connection to Fritz!Box\nfailed! (%s)\nretrying...") % reason.getErrorMessage(), type=MessageBox.TYPE_INFO, timeout=4)
		ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)

class FritzCall:
	def __init__(self):
		self.dialog = None
		self.d = None
		self.connect()
		
	def connect(self):	
		self.abort()
		if config.FritzCall.enable.value:
			f = FritzClientFactory()
			self.d = (f, reactor.connectTCP("%d.%d.%d.%d" % tuple(config.FritzCall.hostname.value), 1012, f))

	def shutdown(self):
		self.abort()

	def abort(self):
		if self.d is not None:
			self.d[0].hangup_ok = True 
			self.d[0].stopTrying()
			self.d[1].disconnect()
			self.d = None

def main(session):
	session.open(FritzCallSetup)

fritz_call = None

def autostart(reason, **kwargs):
	global fritz_call
	
	# ouch, this is a hack	
	if kwargs.has_key("session"):
		global my_global_session
		my_global_session = kwargs["session"]
		return
	
	print "autostart"
	if reason == 0:
		fritz_call = FritzCall()
	elif reason == 1:
		fritz_call.shutdown()
		fritz_call = None

def Plugins(**kwargs):
 	return [ PluginDescriptor(name="FritzCall", description="Display Fritzbox-Fon calls on screen", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=main),
 		PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart) ]