Update /phonetic to be generic with truncation above 12 characters
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
#include <concord/discord.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <hammy/command.h>
|
||||
#include <hammy/job.h>
|
||||
#include <hammy/refdb.h>
|
||||
#include <hammy/utils.h>
|
||||
|
||||
#define HAMMY_ABBR_ECHO_MAX 16
|
||||
#define HAMMY_ABBR_CODE_MAX 3072
|
||||
#define HAMMY_ABBR_TRUNCATED " ... (truncated)"
|
||||
|
||||
void hammy_cmd_abbr(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb) {
|
||||
// Get the qcode argument from the job
|
||||
const char* qcode = hammy_job_get_arg(job, "q-code");
|
||||
if (!qcode) {
|
||||
hammy_job_respond(job, client, "Error", "No text provided for Q-Code conversion.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
char body[HAMMY_ABBR_CODE_MAX + sizeof(HAMMY_ABBR_TRUNCATED)]; // Generally, since the bottom pointers can only point to max 128-character strings (set as preprocesor header)
|
||||
// So yeah - if we ever change the above for some reason to stupid values... yeah. Technically "unsafe" but yeah.
|
||||
|
||||
const char* questionStr = NULL;
|
||||
const char* answerStr = NULL;
|
||||
|
||||
if (!hammy_refdb_get_qcode(refdb, qcode, &questionStr, &answerStr)) {
|
||||
hammy_job_respond(job, client, "Q-Code Not Found!", "Failed to find the Q-Code specified. Please check your query!", true);
|
||||
return;
|
||||
}
|
||||
|
||||
hammy_to_uppercase(qcode);
|
||||
|
||||
snprintf(body, sizeof(body), "Q-Code: `%s`\nQuestion: `%s`\nAnswer: `%s`", qcode, (questionStr ? questionStr : "Not Specified"), (answerStr ? answerStr : "Not Specified"));
|
||||
|
||||
hammy_job_respond(job, client, "Q-Code Conversion", body, false);
|
||||
}
|
||||
+10
-9
@@ -53,17 +53,14 @@ static bool phonetic_is_gap(char c) {
|
||||
|
||||
// Instant command: runs on the gateway thread, sends a fresh response.
|
||||
void hammy_cmd_phonetic(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb) {
|
||||
// Get the callsign argument from the job
|
||||
const char* text = hammy_job_get_arg(job, "callsign");
|
||||
// Get the text argument from the job
|
||||
const char* text = hammy_job_get_arg(job, "text");
|
||||
if (!text) {
|
||||
hammy_job_respond(job, client, "Callsign Missing!", "No Callsign provided for Phonetic conversion.", true);
|
||||
hammy_job_respond(job, client, "Text Missing!", "No Text provided for Phonetic conversion.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strlen(text) > 12) { // Arbitrary limit to prevent abuse and ensure reasonable output size
|
||||
hammy_job_respond(job, client, "Callsign Too Long!", "The provided Callsign exceeds the maximum allowed length.", true);
|
||||
return;
|
||||
}
|
||||
bool showPronunciation = strlen(text) < 12;
|
||||
|
||||
char phonetic[HAMMY_PHONETIC_CODE_MAX + sizeof(HAMMY_PHONETIC_TRUNCATED)];
|
||||
char pronunciation[HAMMY_PHONETIC_PRONUNCIATION_MAX + sizeof(HAMMY_PHONETIC_TRUNCATED)];
|
||||
@@ -131,7 +128,11 @@ void hammy_cmd_phonetic(const hammy_job_t* job, struct discord* client, hammy_re
|
||||
|
||||
// Reply with the phonetic words and how each of them is spoken.
|
||||
char body[sizeof(phonetic) + sizeof(pronunciation) + 48];
|
||||
snprintf(body, sizeof(body), "Callsign: `%s`\nPhonetics: `%s`\nPronunciation: `%s`", text, phonetic, pronunciation);
|
||||
if (showPronunciation) {
|
||||
snprintf(body, sizeof(body), "Text: `%s`\nPhonetics: `%s`\nPronunciation: `%s`", text, phonetic, pronunciation);
|
||||
} else {
|
||||
snprintf(body, sizeof(body), "Text: `(truncated)`\nPhonetics: `%s`", phonetic);
|
||||
}
|
||||
|
||||
hammy_job_respond(job, client, "Phonetic Code Conversion", body, false);
|
||||
hammy_job_respond(job, client, "Text to Phonetics Conversion", body, false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user