This repository was archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexample-hash.c
More file actions
307 lines (250 loc) · 8.26 KB
/
Copy pathexample-hash.c
File metadata and controls
307 lines (250 loc) · 8.26 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// Created by dxing97 on 2018-12-14.
//
#include <libdiscord.h>
#include <getopt.h>
#include <signal.h>
#include <openssl/evp.h> //openssl EVP crypto API
/*
* bot that hashes stuff using a specified hashing function
* bot arguments (in Discord):
* !hash <hash function> <stuff to hash>
* i.e.
* !hash md5 MY MESSAGE HERE
*/
int bot_exit = 0;
char *trigger = NULL;
char halt_trigger[] = "hcf";
uint64_t target_channel = 0;
struct buffer {
char *buf;
unsigned int len;
};
char *binary2hex(unsigned char *buf,int buflen) {
int i = 0;
char tmp[3];
char *hex = malloc(sizeof(char) * (buflen * 2 + 1));
for(; i < buflen; i++) {
sprintf(tmp, "%02x", buf[i]);
strcat(hex, tmp);
}
// printf("hex: %s\n", hex);
return hex;
}
char *to_hash2(const char *input, const char *hashfun) {
EVP_MD_CTX *mdctx;
const EVP_MD *md;
const char *mess1 = input;
char mess2[] = "";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;
OpenSSL_add_all_digests();
md = EVP_get_digestbyname(hashfun);
if(!md) {
//printf("Unknown message digest %s\n", hashfun);
return NULL;
}
#if OPENSSL_VERSION_NUMBER > 0x1010000fL //1.1.0release
mdctx = EVP_MD_CTX_new();
#else
mdctx = EVP_MD_CTX_create();
#endif
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(mdctx, md_value, (unsigned int *) &md_len);
#if OPENSSL_VERSION_NUMBER > 0x1010000fL //1.1.0release
EVP_MD_CTX_free(mdctx);
#else
EVP_MD_CTX_cleanup(mdctx);
#endif
return binary2hex(md_value, md_len);
}
//takes message and extracts the message we want to hash
char *to_hash(struct ld_json_message *message, struct ld_context *context) {
char *message_content = message->content;
if(strlen(message_content) < strlen(trigger) + 4) { //message is too short, doesn't include trigger
return NULL;
}
if(strncmp(message_content, trigger, strlen(trigger)) != 0) { //message doesn't contain trigger
return NULL;
}
//get hash function
char *head;
head = message_content + strlen(trigger); //head is at space after trigger
if(*head != ' ') {
return NULL;
}
head++;
char hash_function[strlen(message_content)];
if(sscanf(head, "%s", hash_function) != 1) { //couldn't get hash function
return NULL;
}
char *ret, tmp[100];
// sprintf(tmp, "hashing (%s)", head + strlen(hash_function) + 1);
// ld_send_basic_message(context, message->channel_id, tmp);
ret = to_hash2(head + strlen(hash_function) + 1, hash_function);
if(ret == NULL) {
sprintf(tmp, "invalid hash function (%s)", hash_function);
ld_send_basic_message(context, message->channel_id, tmp);
}
target_channel = message->channel_id;
return ret;
}
int callback(struct ld_context *context, enum ld_callback_reason reason, void *data, int len) {
if(reason == LD_CALLBACK_MESSAGE_CREATE) {
struct ld_json_message message;
ld_json_message_init(&message);
ld_json_pack_message(&message, (json_t *) data);
char *next;
if(message.content == NULL) {
ld_debug("empty message!");
return LDS_OK;
}
if(strcmp(message.content, halt_trigger) == 0) {
//halt
target_channel = message.channel_id; //reply to hcf message
bot_exit = 1;
return LDS_OK;
}
if(message.author->id == context->current_user->id) {
ld_debug("callback: can't respond to ourselves");
return LDS_OK;
}
if(message.author->bot == 1) {
ld_debug("callback: shouldn't respond to another bot");
return LDS_OK;
}
char *hash = to_hash(&message, context);
if(hash == NULL) {
//message wasn't formatted correctly, do nothing
return LDS_OK;
}
int ret = ld_send_basic_message(context, message.channel_id, hash);
free(hash);
if(ret != LDS_OK) {
ld_error("%s: error sending hash\n", __FUNCTION__);
return LDS_CURL_ERR;
}
return LDS_OK;
}
return LDS_OK;
}
void sig_handler(int i) {
bot_exit = 1;
}
void print_help(char *executable_name) {
printf("libdiscord hash bot\n"
"Returns the hash of some ASCII text using a user-specified digest\n"
"Example: !hash md5 my message here\n\n"
"Usage:\n"
"%s [-t bot_token]\n\n"
"Options: \n\t"
"-t, --bot-token [bot_token]\n\t\t"
"Required. Discord bot token. See Discord developer pages on how to obtain one.\n\t"
"-h, --help\n\t\t"
"Displays this help dialog\n", executable_name);
}
int main(int argc, char *argv[]) {
char *bot_token = NULL;
unsigned long log_level = 63;
if(argc == 1) {
goto HELP;
}
while(1) {
int option_index = 0, c;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"bot-token", required_argument, 0, 't'},
{"log-level", required_argument, 0, 'l'},
{"prefix" , required_argument, 0, 'p'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv, "ht:l:p:", long_options, &option_index);
if(c == -1) {
break;
}
switch(c) {
case 'h':
HELP:
print_help(argv[0]);
return 0;
case 't':
if(strlen(optarg) > 65) { //basic overflow check
printf("invalid bot token detected");
return 0;
}
bot_token = strdup(optarg);
break;
case 'l':
log_level = strtoul(optarg, NULL, 10);
break;
case 'p':
trigger = strdup(optarg);
break;
default:
return 1; //this should never happen
}
}
printf("Example bot 5 \"hash\" starting up using libdiscord v%s\n", LD_VERSION);
if(bot_token == NULL) {
printf("bot token is not set! see %s -h for details on how to pass your bot token in\n", argv[0]);
return 0;
}
if(trigger == NULL) {
trigger = strdup("!hash");
}
signal(SIGINT, sig_handler);
printf("Initializing libdiscord at log level %lu\n", log_level);
ld_set_logging_level(log_level);
struct ld_context_info info;
if(ld_init_context_info(&info) != 0) {
ld_error("%s: error initalizing context info");
goto ABORT;
}
info.bot_token = bot_token;
info.user_callback = callback;
struct ld_json_status_update presence;
struct ld_json_activity game;
presence.status = LD_PRESENCE_ONLINE;
presence.game = &game;
presence.game->name = "example-bot-hash";
presence.game->type = LD_PRESENCE_ACTIVITY_LISTENING;
presence.roles = NULL; /// \todo: presences should be initialized by a function
info.init_presence = &presence;
struct ld_context *context = malloc(sizeof(struct ld_context));
if(context == NULL) {
ld_error("example-bot-hash: couldn't initalize context");
goto ABORT;
}
if(ld_init_context(&info, context) != 0) {
ld_error("example-bot-hash: error initalizing context");
goto ABORT;
}
int ret;
int bot_state = 0; //not connected initially
while(!bot_exit) {
if(bot_state == 0) {
ret = ld_connect(context);
if(ret != 0) {
ld_error("example-bot-hash: error connecting to discord (error %d)", ret);
bot_exit = 1;
}
bot_state = 1;
} else {
ret = ld_service(context, 20);
if(ret != 0) {
ld_error("example-bot-hash: ld_service returned error (error %d)", ret);
bot_exit = 1;
}
}
}
if(target_channel != 0)
ld_send_basic_message(context, target_channel, "example-bot-hash: received SIGINT or HALT_TRIGGER, cleaning up");
ABORT:
ld_cleanup_context(context);
free(context);
context = NULL;
free(bot_token);
return 0;
}