-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathshell.c
160 lines (132 loc) · 4.29 KB
/
shell.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>
#include <inttypes.h> /* SCNu64 */
#include "discord.h"
#include "log.h"
u64snowflake g_sudo_id;
void
print_usage(void)
{
printf("\n\nThis bot allows navigating its host machine like"
" a shell terminal.\n\n"
"DISCLAIMER: This bot is potentially dangerous if not"
" used with care.\nOnly give admin privileges to yourself"
" or someone trustworthy.\n\n\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Shell-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
on_cd(struct discord *client, const struct discord_message *event)
{
if (event->author->id != g_sudo_id) return;
char path[PATH_MAX];
chdir(*event->content ? event->content : ".");
struct discord_create_message params = {
.content = getcwd(path, sizeof(path)),
};
discord_create_message(client, event->channel_id, ¶ms, NULL);
}
void
on_less_like(struct discord *client, const struct discord_message *event)
{
if (event->author->id != g_sudo_id) return;
if (!event->content || !*event->content) {
struct discord_create_message params = { .content =
"No file specified" };
discord_create_message(client, event->channel_id, ¶ms, NULL);
}
else {
struct discord_embed embed = { .title = event->content };
struct discord_attachment attachment = { .filename = event->content };
char text[512];
snprintf(text, sizeof(text), "attachment://%s", event->content);
struct discord_create_message params = {
.content = text,
.embeds =
&(struct discord_embeds){
.size = 1,
.array = &embed,
},
.attachments =
&(struct discord_attachments){
.size = 1,
.array = &attachment,
},
};
discord_create_message(client, event->channel_id, ¶ms, NULL);
}
}
void
on_fallback(struct discord *client, const struct discord_message *event)
{
const size_t MAX_FSIZE = 5e6; // 5 mb
const size_t MAX_CHARS = 2000;
FILE *fp;
if (event->author->id != g_sudo_id) return;
if (NULL == (fp = popen(event->content, "r"))) {
perror("Failed to run command");
return;
}
char *path = calloc(1, MAX_FSIZE);
char *pathtmp = calloc(1, MAX_FSIZE);
while (NULL != fgets(path, MAX_FSIZE, fp)) {
strncat(pathtmp, path, MAX_FSIZE - 1);
}
const size_t fsize = strlen(pathtmp);
if (fsize <= MAX_CHARS) {
struct discord_create_message params = { .content = pathtmp };
discord_create_message(client, event->channel_id, ¶ms, NULL);
}
else {
struct discord_attachment attachment = {
.content = pathtmp,
.size = fsize,
};
struct discord_create_message params = {
.attachments =
&(struct discord_attachments){
.size = 1,
.array = &attachment,
}
};
discord_create_message(client, event->channel_id, ¶ms, NULL);
}
pclose(fp);
free(path);
free(pathtmp);
}
int
main(int argc, char *argv[])
{
const char *config_file;
if (argc > 1)
config_file = argv[1];
else
config_file = "../config.json";
ccord_global_init();
struct discord *client = discord_config_init(config_file);
assert(NULL != client && "Couldn't initialize client");
discord_set_prefix(client, "$");
discord_set_on_command(client, NULL, &on_fallback);
discord_set_on_command(client, "cd", &on_cd);
char *cmds[] = { "less", "cat", "hexdump" };
discord_set_on_commands(client, cmds, sizeof(cmds) / sizeof *cmds,
&on_less_like);
print_usage();
do {
printf("User ID to have sudo privileges\n");
fscanf(stdin, "%" SCNu64, &g_sudo_id);
} while (!g_sudo_id || errno == ERANGE);
discord_run(client);
discord_cleanup(client);
ccord_global_cleanup();
}