2
0
Fork 0
forked from zynk/zynk

Add !changelog command, reduce GITLOG_MAX and CHANGELOG_MAX (0.38.0)

- Added !changelog [count] command: displays the latest changelog entries
  from CHANGELOG.md. Defaults to 5 entries (CHANGELOG_COUNT), accepts 1-10
  as count. Each entry title is sent as a separate IRC message with a
  200ms delay to avoid IRC server flood protection.
- Reduced GITLOG_MAX and CHANGELOG_MAX from 20 to 10.
- Rewrote cmd_changelog to use getline() for robust line reading and
  isdigit() checks for reliable date pattern detection.
- Updated !help to list !changelog.
- Updated README.md with !changelog documentation and corrected max values.
- Added CHANGELOG.md entry for v0.38.0.
- Version bumped to 0.38.0.
This commit is contained in:
Johannes Findeisen 2026-07-21 02:44:54 +02:00
commit e254b64745
3 changed files with 200 additions and 134 deletions

63
zynk.c
View file

@ -29,7 +29,7 @@ int asprintf(char **, const char *, ...);
/* ---- constants ---- */
#define VERSION "0.37.0"
#define VERSION "0.38.0"
#define DEFAULT_SERVER "irc.libera.chat"
#define OPENCODE_BIN "/usr/bin/opencode"
#define CLR_RESET "\033[0m"
@ -53,7 +53,9 @@ enum {
RATE_LIMIT_MAX = 8,
MAX_HISTORY = 100,
GITLOG_COUNT = 5,
GITLOG_MAX = 20,
GITLOG_MAX = 10,
CHANGELOG_COUNT = 5,
CHANGELOG_MAX = 10,
};
/* ---- IRC session ---- */
@ -2305,7 +2307,7 @@ static int cmd_help(Session *s, const char *msg, const char *reply_target, const
if (rate_limit_check(src_nick) < 0) return 1;
char buf[512];
snprintf(buf, sizeof buf,
"Commands: !help, !version, !ping, !uptime, !weather [city], !forecast [city], !stock <SYMBOL>, !calc <expr>, !time [tz], !seen <nick>, !tell <nick> <msg>, !gitlog [count], !ai <question>, !code <change> (ops), !rebuild (ops), !reload (ops), !restart (ops), !quit/!die (ops)");
"Commands: !help, !version, !ping, !uptime, !weather [city], !forecast [city], !stock <SYMBOL>, !calc <expr>, !time [tz], !seen <nick>, !tell <nick> <msg>, !gitlog [count], !changelog [count], !ai <question>, !code <change> (ops), !rebuild (ops), !reload (ops), !restart (ops), !quit/!die (ops)");
irc_msg(s, reply_target, buf);
log_stamp(); fprintf(stderr, CLR_CYAN "HELP from %s in %s" CLR_RESET "\n", src_nick, reply_target);
return 1;
@ -3089,6 +3091,60 @@ static int cmd_gitlog(Session *s, const char *msg, const char *reply_target, con
return 1;
}
/*
* cmd_changelog - Handle the "!changelog" command.
*
* Reads CHANGELOG.md from the project root and displays the last N date/title
* lines (default: CHANGELOG_COUNT). Lines starting with a date (YYYY.MM.DD)
* are treated as entries. Accepts an optional count argument (1–CHANGELOG_MAX).
* Rate-limited.
*
* Returns: 1 if the command was matched, 0 otherwise.
*/
static int cmd_changelog(Session *s, const char *msg, const char *reply_target, const char *src_nick) {
if (strncmp(msg, "!changelog", 10) != 0) return 0;
if (msg[10] != ' ' && msg[10] != '\0') return 0;
if (rate_limit_check(src_nick) < 0) return 1;
int count = CHANGELOG_COUNT;
const char *rest = msg[10] == ' ' ? msg + 11 : "";
while (*rest == ' ') rest++;
if (*rest) {
count = atoi(rest);
if (count <= 0) count = CHANGELOG_COUNT;
if (count > CHANGELOG_MAX) count = CHANGELOG_MAX;
}
FILE *f = fopen("CHANGELOG.md", "r");
if (!f) { irc_msg(s, reply_target, "changelog: CHANGELOG.md not found"); return 1; }
char *line = NULL;
size_t cap = 0;
const char *headers[256];
int total = 0;
while (getline(&line, &cap, f) != -1) {
size_t len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) line[--len] = 0;
if (len > 10 && isdigit((unsigned char)line[0]) && isdigit((unsigned char)line[1]) &&
isdigit((unsigned char)line[2]) && isdigit((unsigned char)line[3]) &&
line[4] == '.' && isdigit((unsigned char)line[5]) && isdigit((unsigned char)line[6]) &&
line[7] == '.' && isdigit((unsigned char)line[8]) && isdigit((unsigned char)line[9]) &&
line[10] == ' ') {
if (total < 256) headers[total++] = strdup(line);
}
}
free(line);
fclose(f);
int showed = count < total ? count : total;
struct timespec ts_msg = { .tv_sec = 0, .tv_nsec = 200000000 };
for (int i = 0; i < showed; i++) {
irc_msg(s, reply_target, headers[i]);
free((void *)headers[i]);
if (i + 1 < showed) nanosleep(&ts_msg, NULL);
}
for (int i = showed; i < total; i++) free((void *)headers[i]);
if (total == 0) { irc_msg(s, reply_target, "changelog: no entries found"); return 1; }
log_stamp(); fprintf(stderr, CLR_YELLOW "CHANGELOG %s from %s (count=%d, showed=%d)" CLR_RESET "\n", reply_target, src_nick, count, showed);
return 1;
}
/*
* cmd_rebuild - Handle the "!rebuild" command.
*
@ -3257,6 +3313,7 @@ static void handle_privmsg(Session *s, const char *params, const char *src_nick)
if (cmd_seen(s, msg, reply_target, src_nick)) return;
if (cmd_tell(s, msg, reply_target, src_nick)) return;
if (cmd_gitlog(s, msg, reply_target, src_nick)) return;
if (cmd_changelog(s, msg, reply_target, src_nick)) return;
if (cmd_ai(s, msg, reply_target, src_nick)) return;
if (cmd_code(s, msg, reply_target, src_nick, tgt)) return;
if (cmd_rebuild(s, msg, reply_target, src_nick, tgt)) return;