"Fossies" - the Fresh Open Source Software Archive 
Member "polysh-polysh-0.13/polysh/callbacks.py" (11 May 2020, 2888 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "callbacks.py" see the
Fossies "Dox" file reference documentation.
1 """Polysh - Callbacks
2
3 Polysh uses specially crafted strings to communicate out of band data with
4 remote shells. This includes detecting the shell prompt, and other events to
5 detect.
6
7 These strings are built and sent in two parts, the remote shell should send
8 back the concatenation of these two strings to trigger the callback. This is
9 to insure that the sending of the trigger to the remote shell does not
10 trigger the callback.
11
12 Example: The trigger FOOBAR could be split into FOO and BAR and sent as
13 echo "FOO""BAR" so that the sent string does not contain FOOBAR.
14
15 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
16 Copyright (c) 2018 InnoGames GmbH
17 """
18 # This program is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU General Public License as published by
20 # the Free Software Foundation, either version 2 of the License, or
21 # (at your option) any later version.
22 #
23 # This program is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU General Public License for more details.
27 #
28 # You should have received a copy of the GNU General Public License
29 # along with this program. If not, see <http://www.gnu.org/licenses/>.
30
31 import random
32 from typing import Callable
33 from typing import Tuple
34
35 DIGITS_LETTERS = list(map(str, list(range(10)))) + \
36 list(map(chr, list(range(ord('a'), ord('z') + 1)))) + \
37 list(map(chr, list(range(ord('A'), ord('Z') + 1))))
38
39
40 def random_string(length: int) -> str:
41 def random_char() -> str:
42 return DIGITS_LETTERS[random.randint(0, len(DIGITS_LETTERS) - 1)]
43 return ''.join([random_char() for i in range(length)])
44
45
46 COMMON_PREFIX = 'polysh-{}:'.format(random_string(5)).encode()
47 NR_GENERATED_TRIGGERS = 0
48
49 # {'random_string()': (function, repeat)}
50 CALLBACKS = {}
51
52
53 def add(name: bytes, function: Callable, repeat: bool) -> Tuple[bytes, bytes]:
54 name = name.replace(b'/', b'_')
55 global NR_GENERATED_TRIGGERS
56 nr = NR_GENERATED_TRIGGERS
57 NR_GENERATED_TRIGGERS += 1
58 trigger = (COMMON_PREFIX + name + b':' + random_string(5).encode() + b':' +
59 str(nr).encode() + b'/')
60 CALLBACKS[trigger] = (function, repeat)
61 trigger1 = trigger[:int(len(COMMON_PREFIX) / 2)]
62 trigger2 = trigger[len(trigger1):]
63 return trigger1, trigger2
64
65
66 def any_in(data: bytes) -> bool:
67 return COMMON_PREFIX in data
68
69
70 def process(line: bytes) -> bool:
71 start = line.find(COMMON_PREFIX)
72 if start < 0:
73 return False
74
75 end = line.find(b'/', start) + 1
76 if end <= 0:
77 return False
78
79 trigger = line[start:end]
80 callback, repeat = CALLBACKS.get(trigger, (None, True))
81 if not callback:
82 return False
83
84 if not repeat:
85 del CALLBACKS[trigger]
86
87 callback(line[end:].strip())
88 return True