Update /phonetic to be generic with truncation above 12 characters

This commit is contained in:
2026-09-01 17:45:06 +02:00
parent ae5389a7d5
commit ee045cd1dd
4 changed files with 1180 additions and 19 deletions
+39
View File
@@ -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);
}
+11 -10
View File
@@ -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);
hammy_job_respond(job, client, "Phonetic Code Conversion", body, false);
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, "Text to Phonetics Conversion", body, false);
}
+9 -8
View File
@@ -6,11 +6,12 @@
// Handlers live in src/hammy/commands/. Declared here rather than in a header
// so adding a command touches exactly two places: its own .c file and this
// table.
void hammy_cmd_ping(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb);
void hammy_cmd_morse(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb);
void hammy_cmd_freq(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb);
void hammy_cmd_q(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb);
void hammy_cmd_phonetic(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb);
void hammy_cmd_ping(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb); // Ping
void hammy_cmd_morse(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb); // Text -> Morse
void hammy_cmd_freq(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb); // Frequency lookup
void hammy_cmd_q(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb); // Q-Code lookup
void hammy_cmd_abbr(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb); // Abbreviation lookup
void hammy_cmd_phonetic(const hammy_job_t* job, struct discord* client, hammy_refdb_t* refdb); // Callsign -> Phonetics
// TODO: Temporary - move to a proper options system; e.g. a vector of heap-allocated options or something. For now, just point at static storage.
static struct discord_application_command_option morse_opts[] = {
@@ -46,8 +47,8 @@ static struct discord_application_command_options q_opts_struct = {
};
static struct discord_application_command_option phonetic_opts[] = {
{ .type = DISCORD_APPLICATION_OPTION_STRING, .name = "callsign",
.description = "Callsign to convert to Phonetics", .required = true },
{ .type = DISCORD_APPLICATION_OPTION_STRING, .name = "text",
.description = "Text to convert to Phonetics", .required = true },
};
static struct discord_application_command_options phonetic_opts_struct = {
@@ -65,7 +66,7 @@ static const hammy_command_t hammy_builtin_commands[] = {
{ "morse", "Text to and from Morse code", &morse_opts_struct, &hammy_cmd_morse, true },
{ "freq", "Band, segment and who can transmit", &freq_opts_struct, &hammy_cmd_freq, true },
{ "q", "Convert Q-Code to the corresponding question and answer", &q_opts_struct, &hammy_cmd_q, true },
{ "phonetic", "Convert Callsign to Phonetics", &phonetic_opts_struct, &hammy_cmd_phonetic, true }
{ "phonetic", "Convert Text to Phonetics", &phonetic_opts_struct, &hammy_cmd_phonetic, true }
// Tier 0 commands go here as they land. All pure computation, so instant:
// { "grid", "Maidenhead locator conversions", &grid_opts, &hammy_cmd_grid, true },
+1122 -2
View File
File diff suppressed because it is too large Load Diff