-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchannelcmd.uc
More file actions
executable file
·167 lines (161 loc) · 5.92 KB
/
Copy pathchannelcmd.uc
File metadata and controls
executable file
·167 lines (161 loc) · 5.92 KB
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
159
160
161
162
163
164
165
166
167
import * as channel from "channel";
import * as router from "router";
import * as message from "message";
import * as textmessage from "textmessage";
import * as node from "node";
function getPublicChannels()
{
const channels = [];
const all = channel.getAllChannelNamekeys();
for (let i = 0; i < length(all); i++) {
const namekey = all[i];
if (channel.isMeshtasticPreset(namekey) || channel.isMeshcorePreset(namekey) || channel.isAREDNPreset(namekey)) {
push(channels, `<div class="cj">${split(namekey, " ")[0]}</div>`);
}
else if (ord(namekey) === 35 /* # */ || ord(namekey) === 37 /* % */ || channel.isAREDNOnly(namekey)) {
push(channels, `<div class="cj" onclick='cmd("/channels join ${namekey}")'>${split(namekey, " ")[0]}</div>`);
}
}
return sort(channels);
}
function getBridge()
{
const services = platform.getTargetsByIdAndNamekey(null, null, true);
for (let i = 0; i < length(services); i++) {
const bridges = services[i].bridge;
for (let j = 0; j < length(bridges); j++) {
if (bridges[j].meship) {
return services[i].id;
}
}
}
return null;
}
export function setup()
{
};
export function tick()
{
};
export function process(msg)
{
if (msg.data?.command && node.toMe(msg)) {
switch (msg.data.command.cmd) {
case "get_public_channels":
{
router.queue(message.createMessage(msg.from, null,null, "command", {
id: msg.data.command.id,
cmd: "reply_public_channels",
channels: getPublicChannels(),
}, {
hop_limit: 0
}));
break;
}
case "reply_public_channels":
{
const reply = [
"Public channels on world network:", " ",
...msg.data.command.channels
];
event.queue({ cmd: "/reply", reply: reply, socket: msg.data.command.id });
}
default:
break;
}
}
};
export function cmd(msg, reply)
{
const cmd = msg.command;
switch (cmd[0]) {
case "help":
{
push(reply,
"/channels [local] - list channels on local mesh",
"/channels world - list channels on all meshes",
"/channels join <name> [key] - join named channel (with key if necessary)",
"/channels leave <name> - leave named channel"
);
break;
}
case "channels":
{
switch (cmd[1] ?? "local") {
case "world":
{
const bridge = getBridge();
if (bridge) {
router.queue(message.createMessage(bridge, null,null, "command", {
id: msg.socket,
cmd: "get_public_channels"
}, {
hop_limit: 0
}));
break;
}
// Fall through
}
case "local":
{
push(reply,
"Public channels on local network:", " ",
...getPublicChannels()
);
break;
}
case "join":
{
const name = cmd[2];
let key;
if (ord(name) === 35) { // #
key = b64enc(struct.pack("16B", ...crypto.sha256hash(name)));
}
else if (ord(name) === 37) { // %
key = "og==";
}
else {
key = cmd[3];
}
if (name && key) {
let join = true;
const namekey = `${name} ${key}`;
const newchannel = { namekey: namekey, max: 100, badge: true, images: false, telemetry: false, winlink: false };
const currchannels = map(channel.getAllLocalChannels(), c => {
const s = textmessage.state(c.namekey);
if (c.namekey === namekey) {
join = false;
}
return { namekey: c.namekey, max: s.max, badge: s.badge, images: s.images, telemetry: c.telemetry, winlink: s.winlink };
});
if (join) {
event.queue({ cmd: "newchannels", channels: [ ...currchannels, newchannel ] });
push(reply, `Joined channel ${name}`);
}
}
break;
}
case "leave":
{
if (cmd[2]) {
const name = `${cmd[2]} `;
const currchannels = channel.getAllLocalChannels();
const newchannels = map(filter(currchannels, c => index(c.namekey, name) !== 0), c => {
const s = textmessage.state(c.namekey);
return { namekey: c.namekey, max: s.max, badge: s.badge, images: s.images, telemetry: c.telemetry, winlink: s.winlink };
});
if (length(currchannels) !== length(newchannels)) {
event.queue({ cmd: "newchannels", channels: newchannels });
push(reply, `Left channel ${name}`);
}
}
break;
}
default:
break;
}
}
default:
break;
}
};