repos
/
irgramd
/ annotate_shade
service: Disable by now the help for subcommands "archive" and "delete" from
Annotate for file /service.py
2022-03-02 E.
1
# irgramd: IRC-Telegram gateway
19:18:03 '
2
# service.py: IRC service/control command handlers
'
3
#
2023-03-29 E.
4
# Copyright (c) 2022,2023 E. Bosch <presidev@AT@gmail.com>
2022-03-02 E.
5
#
19:18:03 '
6
# Use of this source code is governed by a MIT style license that
'
7
# can be found in the LICENSE file included in this project.
'
8
2023-06-25 E.
9
from utils import compact_date, command, HELP
2023-03-18 E.
10
from telethon import utils as tgutils
2022-03-19 E.
11
2023-06-22 E.
12
class service(command):
2022-03-08 E.
13
def __init__(self, settings, telegram):
2022-03-02 E.
14
self.commands = \
2023-06-25 E.
15
{ # Command Handler Arguments Min Max Maxsplit
22:17:22 '
16
'code': (self.handle_command_code, 1, 1, -1),
'
17
'dialog': (self.handle_command_dialog, 1, 2, -1),
'
18
'get': (self.handle_command_get, 2, 2, -1),
'
19
'help': (self.handle_command_help, 0, 1, -1),
'
20
'history': (self.handle_command_history, 1, 3, -1),
'
21
'mark_read': (self.handle_command_mark_read, 1, 1, -1),
2022-03-02 E.
22
}
2022-03-08 E.
23
self.ask_code = settings['ask_code']
21:58:55 '
24
self.tg = telegram
2023-05-04 E.
25
self.irc = telegram.irc
2022-03-19 E.
26
self.tmp_ircnick = None
22:26:56 '
27
2022-03-08 E.
28
async def handle_command_code(self, code=None, help=None):
21:58:55 '
29
if not help:
'
30
if self.ask_code:
'
31
reply = ('Code will be asked on console',)
'
32
elif code.isdigit():
'
33
try:
'
34
await self.tg.telegram_client.sign_in(code=code)
'
35
except:
'
36
reply = ('Invalid code',)
'
37
else:
'
38
reply = ('Valid code', 'Telegram account authorized')
'
39
await self.tg.continue_auth()
'
40
else: # not isdigit
'
41
reply = ('Code must be numeric',)
'
42
'
43
else: # HELP.brief or HELP.desc (first line)
2023-05-07 E.
44
reply = (' code Enter authorization code',)
2022-03-08 E.
45
if help == HELP.desc: # rest of HELP.desc
21:58:55 '
46
reply += \
'
47
(
'
48
' code <code>',
'
49
'Enter authorization code sent by Telegram to the phone or to',
'
50
'another client connected.',
'
51
'This authorization code usually is only needed the first time',
'
52
'that irgramd connects to Telegram with a given account.',
2022-03-19 E.
53
)
22:26:56 '
54
return reply
'
55
'
56
async def handle_command_dialog(self, command=None, id=None, help=None):
'
57
if not help:
'
58
if command == 'archive':
'
59
pass
'
60
elif command == 'delete':
'
61
pass
'
62
elif command == 'list':
'
63
reply = \
'
64
(
'
65
'Dialogs:',
2023-04-11 E.
66
' {:<11} {:<9} {:<9} {:5} {:<3} {:<4} {:<6} {}'.format(
2022-03-19 E.
67
'Id', 'Unread', 'Mentions', 'Type', 'Pin', 'Arch', 'Last', 'Name'),
22:26:56 '
68
)
'
69
async for dialog in self.tg.telegram_client.iter_dialogs():
2023-03-18 E.
70
id, type = tgutils.resolve_id(dialog.id)
2022-03-19 E.
71
unr = dialog.unread_count
22:26:56 '
72
men = dialog.unread_mentions_count
2023-04-11 E.
73
ty = self.tg.get_entity_type(dialog.entity, format='short')
2022-03-19 E.
74
pin = 'Yes' if dialog.pinned else 'No'
22:26:56 '
75
arch = 'Yes' if dialog.archived else 'No'
2023-05-07 E.
76
last = compact_date(dialog.date, self.tg.timezone)
2023-03-18 E.
77
if id == self.tg.id:
04:12:31 '
78
name_in_irc = self.tmp_ircnick
'
79
else:
2023-12-02 E.
80
name_in_irc = self.tg.get_irc_name_from_telegram_id(id)
19:41:44 '
81
2023-04-11 E.
82
reply += (' {:<11d} {:<9d} {:<9d} {:5} {:<3} {:<4} {:<6} {}'.format(
2022-03-19 E.
83
id, unr, men, ty, pin, arch, last, name_in_irc),
22:26:56 '
84
)
'
85
'
86
else: # HELP.brief or HELP.desc (first line)
2023-05-07 E.
87
reply = (' dialog Manage conversations (dialogs)',)
2022-03-19 E.
88
if help == HELP.desc: # rest of HELP.desc
22:26:56 '
89
reply += \
'
90
(
'
91
' dialog <subcommand> [id]',
'
92
'Manage conversations (dialogs) established in Telegram, the',
'
93
'following subcommands are available:',
2024-04-18 E.
94
# ' archive <id> Archive the dialog specified by id',
23:11:38 '
95
# ' delete <id> Delete the dialog specified by id',
2022-03-19 E.
96
' list Show all dialogs',
2023-05-13 E.
97
)
22:39:42 '
98
return reply
'
99
'
100
async def handle_command_get(self, peer=None, mid=None, help=None):
'
101
if not help:
2023-05-16 E.
102
msg = None
2023-05-13 E.
103
peer_id, reply = self.get_peer_id(peer.lower())
22:39:42 '
104
if reply: return reply
2023-05-16 E.
105
else: reply = ()
2023-05-13 E.
106
2023-11-28 E.
107
# If the ID starts with '=' is absolute ID, not compact ID
21:38:01 '
108
# character '=' is not used by compact IDs
'
109
if mid[0] == '=':
'
110
id = int(mid[1:])
'
111
else:
'
112
id = self.tg.mid.id_to_num_offset(peer_id, mid)
2023-05-16 E.
113
if id is not None:
21:13:51 '
114
msg = await self.tg.telegram_client.get_messages(entity=peer_id, ids=id)
'
115
if msg is not None:
'
116
await self.tg.handle_telegram_message(event=None, message=msg, history=True)
'
117
else:
2023-06-25 E.
118
reply = ('Message not found',)
2023-05-13 E.
119
return reply
22:39:42 '
120
'
121
else: # HELP.brief or HELP.desc (first line)
'
122
reply = (' get Get a message by id and peer',)
'
123
if help == HELP.desc: # rest of HELP.desc
'
124
reply += \
'
125
(
2023-11-28 E.
126
' get <peer> <compact_id|=ID>',
21:38:01 '
127
'Get one message from peer with the compact or absolute ID',
2022-03-08 E.
128
)
21:58:55 '
129
return reply
'
130
'
131
async def handle_command_help(self, help_command=None, help=None):
2022-03-06 E.
132
01:36:51 '
133
start_help = ('*** Telegram Service Help ***',)
'
134
end_help = ('*** End of Help ***',)
'
135
2022-03-08 E.
136
if help == HELP.brief:
2023-05-07 E.
137
help_text = (' help This help',)
2022-03-08 E.
138
elif not help_command or help_command == 'help':
2022-03-06 E.
139
help_text = start_help
01:36:51 '
140
help_text += \
'
141
(
'
142
'This service contains specific Telegram commands that irgramd',
'
143
'cannot map to IRC commands. The following commands are available:',
'
144
)
'
145
for command in self.commands.values():
'
146
handler = command[0]
2022-03-08 E.
147
help_text += await handler(help=HELP.brief)
2022-03-06 E.
148
help_text += \
01:36:51 '
149
(
2023-06-25 E.
150
'The commands begining with ! (exclamation) must be used directly',
22:17:22 '
151
'in channels or chats. The following ! commands are available:',
'
152
)
'
153
for command in self.irc.exclam.commands.values():
'
154
handler = command[0]
'
155
help_text += await handler(help=HELP.brief)
'
156
help_text += \
'
157
(
2022-03-06 E.
158
'If you need more information about a specific command you can use',
01:36:51 '
159
'help <command>',
'
160
)
'
161
help_text += end_help
2023-06-25 E.
162
elif help_command in (all_commands := dict(**self.commands, **self.irc.exclam.commands)).keys():
22:17:22 '
163
handler = all_commands[help_command][0]
2022-03-06 E.
164
help_text = start_help
2022-03-08 E.
165
help_text += await handler(help=HELP.desc)
2022-03-06 E.
166
help_text += end_help
01:36:51 '
167
else:
'
168
help_text = ('help: Unknown command',)
'
169
return help_text
'
170
2023-05-04 E.
171
async def handle_command_history(self, peer=None, limit='10', add_unread=None, help=None):
22:23:04 '
172
if not help:
'
173
async def get_unread(tgt):
'
174
async for dialog in self.tg.telegram_client.iter_dialogs():
'
175
id, type = tgutils.resolve_id(dialog.id)
'
176
if id in self.tg.tid_to_iid.keys():
'
177
name = self.tg.tid_to_iid[id]
'
178
if tgt == name.lower():
'
179
count = dialog.unread_count
'
180
reply = None
'
181
break
'
182
else:
'
183
count = None
'
184
reply = ('Unknown unread',)
'
185
return count, reply
'
186
'
187
def conv_int(num_str):
'
188
if num_str.isdigit():
'
189
n = int(num_str)
'
190
err = None
'
191
else:
'
192
n = None
'
193
err = ('Invalid argument',)
'
194
return n, err
'
195
'
196
tgt = peer.lower()
2023-05-07 E.
197
peer_id, reply = self.get_peer_id(tgt)
2023-05-04 E.
198
if reply: return reply
22:23:04 '
199
'
200
if limit == 'unread':
'
201
add_unread = '0' if add_unread is None else add_unread
'
202
add_unread_int, reply = conv_int(add_unread)
'
203
if reply: return reply
'
204
'
205
li, reply = await get_unread(tgt)
'
206
if reply: return reply
'
207
li += add_unread_int
'
208
elif add_unread is not None:
'
209
reply = ('Wrong number of arguments',)
'
210
return reply
'
211
elif limit == 'all':
'
212
li = None
'
213
else:
'
214
li, reply = conv_int(limit)
'
215
if reply: return reply
'
216
'
217
his = await self.tg.telegram_client.get_messages(peer_id, limit=li)
'
218
for msg in reversed(his):
2023-05-06 E.
219
await self.tg.handle_telegram_message(event=None, message=msg, history=True)
2023-05-04 E.
220
reply = ()
22:23:04 '
221
return reply
'
222
'
223
else: # HELP.brief or HELP.desc (first line)
2023-05-07 E.
224
reply = (' history Get messages from history',)
2023-05-04 E.
225
if help == HELP.desc: # rest of HELP.desc
22:23:04 '
226
reply += \
'
227
(
'
228
' history <peer> [<limit>|all|unread [<plusN>]]',
'
229
'Get last <limit> number of messages already sent to <peer>',
'
230
'(channel or user). If not set <limit> is 10.',
'
231
'Instead of <limit>, "unread" is for messages not marked as read,',
'
232
'optionally <plusN> number of previous messages to the first unread.',
'
233
'Instead of <limit>, "all" is for retrieving all available messages',
'
234
'in <peer>.',
'
235
)
'
236
return reply
2022-03-06 E.
237
2023-05-07 E.
238
async def handle_command_mark_read(self, peer=None, help=None):
23:04:52 '
239
if not help:
'
240
peer_id, reply = self.get_peer_id(peer.lower())
'
241
if reply: return reply
'
242
'
243
await self.tg.telegram_client.send_read_acknowledge(peer_id, clear_mentions=True)
'
244
reply = ('',)
'
245
else: # HELP.brief or HELP.desc (first line)
2023-05-07 E.
246
reply = (' mark_read Mark messages as read',)
2023-05-07 E.
247
if help == HELP.desc: # rest of HELP.desc
23:04:52 '
248
reply += \
'
249
(
'
250
' mark_read <peer>',
'
251
'Mark all messages on <peer> (channel or user) as read, this also will',
'
252
'reset the number of mentions to you on <peer>.'
'
253
)
'
254
return reply
'
255
'
256
def get_peer_id(self, tgt):
'
257
if tgt in self.irc.users or tgt in self.irc.irc_channels:
'
258
peer_id = self.tg.get_tid(tgt)
'
259
reply = None
'
260
else:
'
261
peer_id = None
'
262
reply = ('Unknown user or channel',)
'
263
return peer_id, reply