hirc

[archived] IRC client
git clone https://hhvn.uk/hirc
git clone git://hhvn.uk/hirc
Log | Files | Refs

hirc2txt.c (2863B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <time.h>
      5 
      6 char *topic = NULL;
      7 
      8 void
      9 convert(char *line) {
     10 	char *tok[8];
     11 	char *msg, *p;
     12 	char time[64];
     13 	time_t timestamp;
     14 	int i;
     15 
     16 	if (*line == 'v')
     17 		line = strchr(line, '\t');
     18 	tok[0] = strtok_r(line, "\t", &msg);
     19 	for (i = 1; i < (sizeof(tok) / sizeof(tok[0])); i++)
     20 		tok[i] = strtok_r(NULL, "\t", &msg);
     21 
     22 	if (!tok[0] || !tok[1] || !tok[2] ||
     23 			!tok[3] || !tok[4] || !tok[5] ||
     24 			!tok[6] || !tok[7] || !msg)
     25 		return;
     26 
     27 	timestamp = (time_t)strtoll(tok[0], NULL, 10);
     28 
     29 	strftime(time, sizeof(time), "[%Y-%m-%d %H:%M:%S]", localtime(&timestamp));
     30 
     31 	if (strncmp(msg, "PRIVMSG ", 8) == 0 && strstr(msg, ":\01ACTION")) {
     32 		if (msg[strlen(msg) - 1] == 1)
     33 			msg[strlen(msg) - 1] = '\0';
     34 		p = strchr(msg, ':') + 9;
     35 		printf("%s *%s %s\n", time, tok[5], p);
     36 	} else if (strncmp(msg, "PRIVMSG ", 8) == 0 && strstr(msg, ":\01")) {
     37 		if (msg[strlen(msg) - 1] == 1)
     38 			msg[strlen(msg) - 1] = '\0';
     39 		p = strchr(msg, ':') + 2;
     40 		printf("%s %s requested %s via CTCP\n", time, tok[5], p);
     41 	} else if (strncmp(msg, "NOTICE ", 7) == 0 && strstr(msg, ":\01")) {
     42 		if (msg[strlen(msg) - 1] == 1)
     43 			msg[strlen(msg) - 1] = '\0';
     44 		p = strchr(msg, ':') + 2;
     45 		if (msg = strchr(p, ' ')) {
     46 			*msg = '\0';
     47 			msg++;
     48 		} else msg = "";
     49 		printf("%s %s replied to the CTCP request for %s: %s\n", time, tok[5], p, msg);
     50 	} else if (strncmp(msg, "NOTICE ", 7) == 0 && (p = strchr(msg, ':'))) {
     51 		printf("%s -%s%s- %s\n", time, *tok[4] != ' ' ? tok[4] : "", tok[5], p + 1);
     52 	} else if (strncmp(msg, "PRIVMSG ", 8) == 0 && (p = strchr(msg, ':'))) {
     53 		printf("%s <%s%s> %s\n", time, *tok[4] != ' ' ? tok[4] : "", tok[5], p + 1);
     54 	} else if (strncmp(msg, "JOIN ", 5) == 0) {
     55 		printf("%s %s (%s@%s) joined.\n", time, tok[5], tok[6], tok[7]);
     56 	} else if (strncmp(msg, "PART ", 5) == 0) {
     57 		printf("%s %s (%s@%s) parted.\n", time, tok[5], tok[6], tok[7]);
     58 	} else if (strncmp(msg, "QUIT ", 5) == 0) {
     59 		printf("%s %s (%s@%s) quit.\n", time, tok[5], tok[6], tok[7]);
     60 	} else if (strncmp(msg, "332 ", 4) == 0 && (p = strchr(msg, ':'))) {
     61 		free(topic);
     62 		topic = strdup(p + 1);
     63 	} else if (strncmp(msg, "TOPIC ", 6) == 0 && (p = strchr(msg, ':'))) {
     64 		if (topic)
     65 			printf("%s %s changed the topic from \"%s\" to \"%s\"\n", time, tok[5], topic, p + 1);
     66 		else
     67 			printf("%s %s set the topic to \"%s\"\n", time, tok[5], p + 1);
     68 		free(topic);
     69 		topic = strdup(p + 1);
     70 	} else if (strncmp(msg, "NICK ", 5) == 0 && (p = strchr(msg, ':'))) {
     71 		printf("%s %s is now known as %s\n", time, tok[5], p + 1);
     72 	} else if (strncmp(msg, "MODE ", 5) == 0 && (p = msg + 5)) {
     73 		if ((p = strchr(p, ' ')))
     74 			printf("%s %s set mode(s) %s\n", time, tok[5], p + 1);
     75 	}
     76 
     77 }
     78 
     79 int
     80 main(void) {
     81 	char buf[2048], *p;
     82 
     83 	while (fgets(buf, sizeof(buf), stdin)) {
     84 		if ((p = strchr(buf, '\n')) && *(p+1) == '\0')
     85 			*p = '\0';
     86 		convert(buf);
     87 	}
     88 }