Add time formatting and clean up stuff, fix one "feature" where read would block and messages could be delayed
This commit is contained in:
parent
10c2b6f966
commit
b669fc1e54
29
common.c
29
common.c
|
@ -1,10 +1,6 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#define UIRC_IRCV3
|
|
||||||
#include "uirc/functions.h"
|
char* mIRC_COLORS[16] = {"97", "30", "34", "32", "91", "31", "35", "33", "93", "92", "96", "36", "94", "95", "90", "37"};
|
||||||
#include "uirc/types.h"
|
|
||||||
|
|
||||||
ssize_t get_buffer_line(char* buf, IRC_Message* parsed)
|
ssize_t get_buffer_line(char* buf, IRC_Message* parsed)
|
||||||
{
|
{
|
||||||
|
@ -19,3 +15,24 @@ ssize_t get_buffer_line(char* buf, IRC_Message* parsed)
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_local_time(const char* irc_time)
|
||||||
|
{
|
||||||
|
if (irc_time != NULL) {
|
||||||
|
struct tm time;
|
||||||
|
char* tz;
|
||||||
|
char buffer[32];
|
||||||
|
strptime(irc_time, "%Y-%m-%dT%H:%M:%S", &time);
|
||||||
|
tz = getenv("TZ");
|
||||||
|
setenv("TZ", "", 1);
|
||||||
|
tzset();
|
||||||
|
time_t ctime = mktime(&time);
|
||||||
|
if (tz != NULL)
|
||||||
|
setenv("TZ", tz, 1);
|
||||||
|
else
|
||||||
|
unsetenv("TZ");
|
||||||
|
tzset();
|
||||||
|
strftime(buffer, sizeof(buffer), "%c", localtime(&ctime));
|
||||||
|
printf(" " ANSI_COLOR_BLUE "(%s)" ANSI_COLOR_RESET, buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
18
common.h
18
common.h
|
@ -1,7 +1,23 @@
|
||||||
|
#include <ctype.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#define UIRC_IRCV3
|
||||||
|
#include "uirc/functions.h"
|
||||||
|
#include "uirc/types.h"
|
||||||
|
|
||||||
|
#define ANSI_COLOR_RED "\x1b[31m"
|
||||||
|
#define ANSI_COLOR_GREEN "\x1b[32m"
|
||||||
|
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
||||||
|
#define ANSI_COLOR_BLUE "\x1b[34m"
|
||||||
|
#define ANSI_COLOR_GRAY "\x1b[90m"
|
||||||
|
#define ANSI_COLOR_RESET "\x1b[0m"
|
||||||
|
|
||||||
#define UIRC_IRCV3
|
#define UIRC_IRCV3
|
||||||
#include "uirc/types.h"
|
#include "uirc/types.h"
|
||||||
|
|
||||||
|
extern char* mIRC_COLORS[16];
|
||||||
ssize_t get_buffer_line(char* buf, IRC_Message* parsed);
|
ssize_t get_buffer_line(char* buf, IRC_Message* parsed);
|
||||||
|
void print_local_time(const char* irc_time);
|
||||||
|
|
45
formatirc.c
45
formatirc.c
|
@ -9,13 +9,6 @@
|
||||||
#include "uirc/mappings.h"
|
#include "uirc/mappings.h"
|
||||||
#include "uirc/types.h"
|
#include "uirc/types.h"
|
||||||
|
|
||||||
#define ANSI_COLOR_RED "\x1b[31m"
|
|
||||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
|
||||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
|
||||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
|
||||||
#define ANSI_COLOR_GRAY "\x1b[90m"
|
|
||||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
ssize_t bread = 1, tok;
|
ssize_t bread = 1, tok;
|
||||||
|
@ -23,28 +16,28 @@ int main(void)
|
||||||
char buffer[513];
|
char buffer[513];
|
||||||
IRC_Message out;
|
IRC_Message out;
|
||||||
for (; bread > 0;) {
|
for (; bread > 0;) {
|
||||||
|
if ((tok = get_buffer_line(buffer, &out)) > 0) {
|
||||||
|
if (out.cmd == PRIVMSG || out.cmd == NOTICE) {
|
||||||
|
printf((out.name.nick != NULL) ? ANSI_COLOR_RED "%s" ANSI_COLOR_RESET : ANSI_COLOR_GRAY "%s" ANSI_COLOR_RESET,
|
||||||
|
(out.name.nick != NULL) ? out.name.nick : "unknown");
|
||||||
|
putchar('/');
|
||||||
|
if (out.args[0] != NULL) {
|
||||||
|
printf(ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET,
|
||||||
|
strchr("#+&!", *out.args[0]) != NULL ? out.args[0] : "direct");
|
||||||
|
}
|
||||||
|
if (out.args[1] != NULL) printf(": %s", out.args[1]);
|
||||||
|
print_local_time(out.tags.time.value);
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
for (long unsigned int x = 0; x < sizeof(buffer) && *(buffer + tok + x); x++) *(buffer + x) = *(buffer + x + tok);
|
||||||
|
pos -= (unsigned long)tok;
|
||||||
|
*(buffer + pos) = '\0';
|
||||||
|
memset(&out, '\0', sizeof(out));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if ((bread = read(0, buffer + pos, sizeof(buffer) - 1 - pos)) > 0) {
|
if ((bread = read(0, buffer + pos, sizeof(buffer) - 1 - pos)) > 0) {
|
||||||
pos += (size_t)bread;
|
pos += (size_t)bread;
|
||||||
buffer[pos] = '\0';
|
buffer[pos] = '\0';
|
||||||
if ((tok = get_buffer_line(buffer, &out)) > 0) {
|
|
||||||
if (out.cmd == PRIVMSG || out.cmd == NOTICE) {
|
|
||||||
printf((out.name.nick != NULL) ? ANSI_COLOR_RED "%s" ANSI_COLOR_RESET : ANSI_COLOR_GRAY "%s" ANSI_COLOR_RESET,
|
|
||||||
(out.name.nick != NULL) ? out.name.nick : "unknown");
|
|
||||||
if (out.name.host != NULL) printf("@" ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET, out.name.host);
|
|
||||||
putchar('\n');
|
|
||||||
if (out.args[0] != NULL) {
|
|
||||||
printf(ANSI_COLOR_YELLOW "%s" ANSI_COLOR_RESET,
|
|
||||||
strchr("#+&!", *out.args[0]) != NULL ? out.args[0] : "direct");
|
|
||||||
}
|
|
||||||
if (out.args[1] != NULL) printf(ANSI_COLOR_GREEN " >" ANSI_COLOR_RESET " %s\n", out.args[1]);
|
|
||||||
if (out.tags.time.value != NULL) printf(ANSI_COLOR_BLUE "(%s)\n" ANSI_COLOR_RESET, out.tags.time.value);
|
|
||||||
putchar('\n');
|
|
||||||
}
|
|
||||||
for (long unsigned int x = 0; x < sizeof(buffer) && *(buffer + tok + x); x++) *(buffer + x) = *(buffer + x + tok);
|
|
||||||
pos -= (unsigned long)tok;
|
|
||||||
*(buffer + pos) = '\0';
|
|
||||||
memset(&out, '\0', sizeof(out));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
32
joinpart.c
32
joinpart.c
|
@ -9,13 +9,6 @@
|
||||||
#include "uirc/mappings.h"
|
#include "uirc/mappings.h"
|
||||||
#include "uirc/types.h"
|
#include "uirc/types.h"
|
||||||
|
|
||||||
#define ANSI_COLOR_RED "\x1b[31m"
|
|
||||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
|
||||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
|
||||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
|
||||||
#define ANSI_COLOR_GRAY "\x1b[90m"
|
|
||||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
ssize_t bread = 1, tok;
|
ssize_t bread = 1, tok;
|
||||||
|
@ -23,10 +16,8 @@ int main(void)
|
||||||
char buffer[513];
|
char buffer[513];
|
||||||
IRC_Message out;
|
IRC_Message out;
|
||||||
for (; bread > 0;) {
|
for (; bread > 0;) {
|
||||||
if ((bread = read(0, buffer + pos, sizeof(buffer) - 1 - pos)) > 0) {
|
if ((tok = get_buffer_line(buffer, &out)) > 0) {
|
||||||
pos += (size_t)bread;
|
if (out.cmd == JOIN || out.cmd == PART || out.cmd == QUIT) {
|
||||||
buffer[pos] = '\0';
|
|
||||||
if ((tok = get_buffer_line(buffer, &out)) > 0) {
|
|
||||||
if (out.cmd == JOIN || out.cmd == PART) {
|
if (out.cmd == JOIN || out.cmd == PART) {
|
||||||
printf((out.cmd == JOIN)
|
printf((out.cmd == JOIN)
|
||||||
? ANSI_COLOR_GREEN "+++" ANSI_COLOR_RESET " %s " ANSI_COLOR_GREEN "joined" ANSI_COLOR_RESET
|
? ANSI_COLOR_GREEN "+++" ANSI_COLOR_RESET " %s " ANSI_COLOR_GREEN "joined" ANSI_COLOR_RESET
|
||||||
|
@ -34,18 +25,21 @@ int main(void)
|
||||||
: ANSI_COLOR_YELLOW "---" ANSI_COLOR_RESET " %s " ANSI_COLOR_YELLOW "left" ANSI_COLOR_RESET
|
: ANSI_COLOR_YELLOW "---" ANSI_COLOR_RESET " %s " ANSI_COLOR_YELLOW "left" ANSI_COLOR_RESET
|
||||||
" " ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET,
|
" " ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET,
|
||||||
out.name.nick, out.args[0]);
|
out.name.nick, out.args[0]);
|
||||||
if (out.tags.time.value != NULL) printf(" at %s", out.tags.time.value);
|
|
||||||
putchar('\n');
|
|
||||||
} else if (out.cmd == QUIT) {
|
} else if (out.cmd == QUIT) {
|
||||||
printf(ANSI_COLOR_RED "***" ANSI_COLOR_RESET " %s " ANSI_COLOR_RED "quit" ANSI_COLOR_RESET, out.name.nick);
|
printf(ANSI_COLOR_RED "***" ANSI_COLOR_RESET " %s " ANSI_COLOR_RED "quit" ANSI_COLOR_RESET, out.name.nick);
|
||||||
if (out.tags.time.value != NULL) printf(" at %s", out.tags.time.value);
|
|
||||||
putchar('\n');
|
|
||||||
}
|
}
|
||||||
for (long unsigned int x = 0; x < sizeof(buffer) && *(buffer + tok + x); x++) *(buffer + x) = *(buffer + x + tok);
|
print_local_time(out.tags.time.value);
|
||||||
pos -= (unsigned long)tok;
|
putchar('\n');
|
||||||
*(buffer + pos) = '\0';
|
|
||||||
memset(&out, '\0', sizeof(out));
|
|
||||||
}
|
}
|
||||||
|
for (long unsigned int x = 0; x < sizeof(buffer) && *(buffer + tok + x); x++) *(buffer + x) = *(buffer + x + tok);
|
||||||
|
pos -= (unsigned long)tok;
|
||||||
|
*(buffer + pos) = '\0';
|
||||||
|
memset(&out, '\0', sizeof(out));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ((bread = read(0, buffer + pos, sizeof(buffer) - 1 - pos)) > 0) {
|
||||||
|
pos += (size_t)bread;
|
||||||
|
buffer[pos] = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Reference in New Issue