Skip to content

Commit 59e5e8e

Browse files
author
V V
committed
added 'msg @username' command, fixed small bugs
1 parent c61c8ba commit 59e5e8e

File tree

3 files changed

+128
-9
lines changed

3 files changed

+128
-9
lines changed

‎interface.c

+124-5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@
9595

9696
#include <errno.h>
9797

98+
#include "tgl/tree.h"
99+
100+
struct username_peer_pair {
101+
const char *username;
102+
tgl_peer_t *peer;
103+
};
104+
105+
#define username_peer_pair_cmp(a,b) strcmp (a->username, b->username)
106+
DEFINE_TREE (username_peer_pair, struct username_peer_pair *, username_peer_pair_cmp, NULL)
107+
struct tree_username_peer_pair *username_peer_pair;
108+
109+
struct username_peer_pair *current_map;
110+
98111
#define ALLOW_MULT 1
99112
char *default_prompt = "> ";
100113

@@ -478,6 +491,19 @@ tgl_peer_id_t parse_input_peer_id (const char *s, int l, int mask) {
478491
return res;
479492
}
480493

494+
if (*s == '@') {
495+
s ++;
496+
l --;
497+
char *tmp = strndup (s, l);
498+
struct username_peer_pair *p = tree_lookup_username_peer_pair (username_peer_pair, (void *)&tmp);
499+
free (tmp);
500+
if (p && (!mask || tgl_get_peer_type (p->peer->id) == mask)) {
501+
return p->peer->id;
502+
} else {
503+
return TGL_PEER_NOT_FOUND;
504+
}
505+
}
506+
481507
const char *ss[] = {"user#id", "user#", "chat#id", "chat#", "secret_chat#id", "secret_chat#", "channel#id", "channel#"};
482508
int tt[] = {TGL_PEER_USER, TGL_PEER_USER, TGL_PEER_CHAT, TGL_PEER_CHAT, TGL_PEER_ENCR_CHAT, TGL_PEER_ENCR_CHAT, TGL_PEER_CHANNEL, TGL_PEER_CHANNEL};
483509

@@ -2050,6 +2076,44 @@ int complete_chat_command (tgl_peer_t *P, int index, const char *text, int len,
20502076
}
20512077
}
20522078

2079+
int complete_username (int mode, int index, const char *text, int len, char **R) {
2080+
*R = NULL;
2081+
if (len > 0 && *text == '@') {
2082+
text ++;
2083+
len --;
2084+
}
2085+
index ++;
2086+
while (index < TLS->peer_num) {
2087+
tgl_peer_t *P = TLS->Peers[index];
2088+
if (mode && tgl_get_peer_type (P->id) != mode) {
2089+
index ++;
2090+
continue;
2091+
}
2092+
char *u = NULL;
2093+
if (tgl_get_peer_type (P->id) == TGL_PEER_USER) {
2094+
u = P->user.username;
2095+
} else if (tgl_get_peer_type (P->id) == TGL_PEER_CHANNEL) {
2096+
u = P->channel.username;
2097+
}
2098+
if (!u) {
2099+
index ++;
2100+
continue;
2101+
}
2102+
if ((int)strlen (u) < len || memcmp (u, text, len)) {
2103+
index ++;
2104+
continue;
2105+
}
2106+
*R = malloc (strlen (u) + 2);
2107+
*R[0] = '@';
2108+
memcpy (*R + 1, u, strlen (u) + 1);
2109+
break;
2110+
}
2111+
if (index == TLS->peer_num) {
2112+
return -1;
2113+
}
2114+
return index;
2115+
}
2116+
20532117
char *command_generator (const char *text, int state) {
20542118
#ifndef DISABLE_EXTF
20552119
static int len;
@@ -2094,11 +2158,19 @@ char *command_generator (const char *text, int state) {
20942158
if (c) { rl_line_buffer[rl_point] = c; }
20952159
return R;
20962160
case ca_user:
2097-
index = tgl_complete_user_list (TLS, index, command_pos, command_len, &R);
2161+
if (command_len && command_pos[0] == '@') {
2162+
index = complete_username (TGL_PEER_USER, index, command_pos, command_len, &R);
2163+
} else {
2164+
index = tgl_complete_user_list (TLS, index, command_pos, command_len, &R);
2165+
}
20982166
if (c) { rl_line_buffer[rl_point] = c; }
20992167
return R;
21002168
case ca_peer:
2101-
index = tgl_complete_peer_list (TLS, index, command_pos, command_len, &R);
2169+
if (command_len && command_pos[0] == '@') {
2170+
index = complete_username (0, index, command_pos, command_len, &R);
2171+
} else {
2172+
index = tgl_complete_peer_list (TLS, index, command_pos, command_len, &R);
2173+
}
21022174
if (c) { rl_line_buffer[rl_point] = c; }
21032175
return R;
21042176
case ca_file_name:
@@ -2115,7 +2187,11 @@ char *command_generator (const char *text, int state) {
21152187
if (c) { rl_line_buffer[rl_point] = c; }
21162188
return R;
21172189
case ca_channel:
2118-
index = tgl_complete_channel_list (TLS, index, command_pos, command_len, &R);
2190+
if (command_len && command_pos[0] == '@') {
2191+
index = complete_username (TGL_PEER_CHANNEL, index, command_pos, command_len, &R);
2192+
} else {
2193+
index = tgl_complete_channel_list (TLS, index, command_pos, command_len, &R);
2194+
}
21192195
if (c) { rl_line_buffer[rl_point] = c; }
21202196
return R;
21212197
case ca_modifier:
@@ -3073,6 +3149,39 @@ void json_peer_update (struct in_ev *ev, tgl_peer_t *P, unsigned flags) {
30733149
#endif
30743150
}
30753151

3152+
void peer_update_username (tgl_peer_t *P, const char *username) {
3153+
if (!username) {
3154+
if (P->extra) {
3155+
struct username_peer_pair *p = tree_lookup_username_peer_pair (username_peer_pair, (void *)&P->extra);
3156+
assert (p);
3157+
username_peer_pair = tree_delete_username_peer_pair (username_peer_pair, p);
3158+
tfree_str (P->extra);
3159+
tfree (p, sizeof (*p));
3160+
P->extra = NULL;
3161+
}
3162+
return;
3163+
}
3164+
assert (username);
3165+
if (P->extra && !strcmp (P->extra, username)) {
3166+
return;
3167+
}
3168+
if (P->extra) {
3169+
struct username_peer_pair *p = tree_lookup_username_peer_pair (username_peer_pair, (void *)&P->extra);
3170+
assert (p);
3171+
username_peer_pair = tree_delete_username_peer_pair (username_peer_pair, p);
3172+
tfree_str (P->extra);
3173+
tfree (p, sizeof (*p));
3174+
P->extra = NULL;
3175+
}
3176+
3177+
P->extra = tstrdup (username);
3178+
struct username_peer_pair *p = talloc (sizeof (*p));
3179+
p->peer = P;
3180+
p->username = P->extra;
3181+
3182+
username_peer_pair = tree_insert_username_peer_pair (username_peer_pair, p, rand ());
3183+
}
3184+
30763185
void user_update_gw (struct tgl_state *TLSR, struct tgl_user *U, unsigned flags) {
30773186
assert (TLSR == TLS);
30783187
#ifdef USE_LUA
@@ -3081,6 +3190,8 @@ void user_update_gw (struct tgl_state *TLSR, struct tgl_user *U, unsigned flags)
30813190
#ifdef USE_PYTHON
30823191
py_user_update (U, flags);
30833192
#endif
3193+
3194+
peer_update_username ((void *)U, U->username);
30843195

30853196
if (disable_output && !notify_ev) { return; }
30863197
if (!binlog_read) { return; }
@@ -3149,8 +3260,6 @@ void secret_chat_update_gw (struct tgl_state *TLSR, struct tgl_secret_chat *U, u
31493260
#ifdef USE_PYTHON
31503261
py_secret_chat_update (U, flags);
31513262
#endif
3152-
3153-
31543263

31553264
if ((flags & TGL_UPDATE_WORKING) || (flags & TGL_UPDATE_DELETED)) {
31563265
write_secret_chat_file ();
@@ -3188,6 +3297,15 @@ void secret_chat_update_gw (struct tgl_state *TLSR, struct tgl_secret_chat *U, u
31883297
}
31893298
}
31903299

3300+
void channel_update_gw (struct tgl_state *TLSR, struct tgl_channel *U, unsigned flags) {
3301+
assert (TLSR == TLS);
3302+
3303+
peer_update_username ((void *)U, U->username);
3304+
3305+
if (disable_output && !notify_ev) { return; }
3306+
if (!binlog_read) { return; }
3307+
}
3308+
31913309
void print_card_gw (struct tgl_state *TLSR, void *extra, int success, int size, int *card) {
31923310
assert (TLSR == TLS);
31933311
struct in_ev *ev = extra;
@@ -3295,6 +3413,7 @@ struct tgl_update_callback upd_cb = {
32953413
.user_update = user_update_gw,
32963414
.chat_update = chat_update_gw,
32973415
.secret_chat_update = secret_chat_update_gw,
3416+
.channel_update = channel_update_gw,
32983417
.msg_receive = print_message_gw,
32993418
.our_id = our_id_gw,
33003419
.user_status_update = user_status_upd,

‎lua-tg.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ void push_update_types (unsigned flags) {
226226
void push_peer (tgl_peer_id_t id, tgl_peer_t *P) {
227227
lua_newtable (luaState);
228228

229-
lua_add_string_field ("id", print_permanent_peer_id (P->id));
229+
lua_add_string_field ("id", print_permanent_peer_id (P ? P->id : id));
230230
lua_pushstring (luaState, "peer_type");
231-
push_tgl_peer_type (tgl_get_peer_type (P->id));
231+
push_tgl_peer_type (tgl_get_peer_type (id));
232232
lua_settable (luaState, -3);
233-
lua_add_num_field ("peer_id", tgl_get_peer_id (P->id));
233+
lua_add_num_field ("peer_id", tgl_get_peer_id (id));
234234

235235
if (!P || !(P->flags & TGLPF_CREATED)) {
236236
lua_pushstring (luaState, "print_name");

‎tgl

Submodule tgl updated 1 file

0 commit comments

Comments
 (0)