/*
xchat2mirc - Copyright (c) 2002 Teemu Toivola <tst at iki dot fi>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 dated June, 1991.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program;  if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave., Cambridge, MA 02139, USA.
*/

/*

	gcc -O2 xchat2mirc.c -o xchat2mirc

*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	FILE *log;
	char logline[1024], line[1024], time[10], nick[64], temp[10];
	
	if ((argc == 1) || (argc >= 3)) {
		printf("xchat2mirc 1.0 by Teemu Toivola <tst@iki.fi>\n\n");
		printf("Usage: %s <xchatlog>\n",argv[0]); 
		exit(0);
	}

	if ((log=fopen(argv[1],"r"))==NULL) {
		fprintf(stderr,"\nError:\nUnable to open logfile \"%s\" for reading.\n\n",argv[1]);
		exit(1);
	}
	
	while (fgets(logline,1024,log)!=NULL) {
		if (logline[9]==':' && logline[12]==':') {
			if (logline[16]=='<') {
				sscanf(logline+7,"%s %s",time,nick);
				if (nick[strlen(nick)-1]=='>') {
					strcpy(line,logline+17+strlen(nick));
					time[strlen(time)-3]='\0';
					printf("[%s] %s %s",time,nick,line);
				} else {
					sscanf(logline+7,"%s",time);
					sscanf(logline+20,"%s",nick);
					time[strlen(time)-3]='\0';
					sscanf(logline+25+strlen(nick),"%s",temp);
					if (strcmp("kicked",temp)==0) {
						strcpy(line,logline+20);
						printf("[%s] *** %s",time,line);
					}
				}

			} else if (logline[16]=='-' && logline[17]=='-' && logline[18]=='>') {
				sscanf(logline+7,"%s",time);
				sscanf(logline+20,"%s",nick);
				time[strlen(time)-3]='\0';
				printf("[%s] *** %s (null@null) has joined the channel\n",time,nick);

			} else if (logline[16]=='*') {
				sscanf(logline+7,"%s",time);
				sscanf(logline+18,"%s",nick);
				time[strlen(time)-3]='\0';
				strcpy(line,logline+19+strlen(nick));
				printf("[%s] * %s %s",time,nick,line);

			} else if (logline[16]=='-' && logline[17]=='-' && logline[18]=='-') {
				sscanf(logline+7,"%s",time);
				sscanf(logline+20,"%s",nick);
				time[strlen(time)-3]='\0';
				sscanf(logline+20+strlen(nick),"%s",temp);
				if (strcmp("has",temp)==0) {
					strcpy(line,logline+47+strlen(nick));
					printf("[%s] *** %s changes topic to %s",time,nick,line);
				}
			}
		}
	}

	fclose(log);
	return 0;
}
