--daemon long-polls getUpdates, runs one turn per message and replies. The token comes from ~/.splinter/telegram.token (0600) or SPLINTER_TELEGRAM_TOKEN, never from an argument: arguments end up in ps and in shell history. telegram_allow lists who is answered, comma separated, nobody by default - an empty list is a refusal, not an open door. Unknown senders are logged without their text. The loop survives a failed request instead of dying on it, and keeps no state beyond the session: a restart re-delivers the last update, which costs one duplicated answer and never a lost message. test_telegram covers the token paths (file wins, environment as fallback, empty file is not a token). Verified against api.telegram.org: the token works and getMe answers hbx_splinter_bot. Sending needs the user to write first - a bot cannot start a chat, the API says 'chat not found'.
78 lines
2.1 KiB
C
78 lines
2.1 KiB
C
/*
|
|
* splinter -- gateway bits that can be tested without Telegram.
|
|
*
|
|
* The token must come from a file (0600) or the environment, never from an
|
|
* argument, so both paths are checked here. The network loop is not tested
|
|
* against api.telegram.org: it is a loop over one HTTP call, and a test that
|
|
* needs the internet fails for reasons that have nothing to do with the code.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char dir[512], path[600], buf[256];
|
|
const char *tmp = getenv("TMPDIR");
|
|
FILE *f;
|
|
|
|
if (tmp == NULL || *tmp == '\0')
|
|
tmp = "/var/tmp";
|
|
snprintf(dir, sizeof(dir), "%s/splinter-tg-XXXXXX", tmp);
|
|
CHECK(mkdtemp(dir) != NULL);
|
|
snprintf(path, sizeof(path), "%s/telegram.token", dir);
|
|
|
|
/* no file, no environment: refuse instead of using an empty token */
|
|
unsetenv("SPLINTER_TELEGRAM_TOKEN");
|
|
CHECK(sp_telegram_token(buf, sizeof(buf), path) != 0);
|
|
|
|
/* the file wins, trailing newline stripped */
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("123:abc\n", f);
|
|
fclose(f);
|
|
}
|
|
CHECK(sp_telegram_token(buf, sizeof(buf), path) == 0);
|
|
CHECK(strcmp(buf, "123:abc") == 0);
|
|
|
|
/* environment is the fallback when there is no file */
|
|
CHECK(sp_telegram_token(buf, sizeof(buf), "/nonexistent/token") != 0);
|
|
CHECK(setenv("SPLINTER_TELEGRAM_TOKEN", "456:def", 1) == 0);
|
|
CHECK(sp_telegram_token(buf, sizeof(buf), "/nonexistent/token") == 0);
|
|
CHECK(strcmp(buf, "456:def") == 0);
|
|
|
|
/* an empty file is not a token */
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f != NULL)
|
|
fclose(f);
|
|
unsetenv("SPLINTER_TELEGRAM_TOKEN");
|
|
CHECK(sp_telegram_token(buf, sizeof(buf), path) != 0);
|
|
|
|
{
|
|
char cmd[600];
|
|
|
|
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
|
|
if (system(cmd) != 0)
|
|
fprintf(stderr, "cleanup failed\n");
|
|
}
|
|
if (failures == 0)
|
|
printf("test_telegram: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|