/* licq2gaim 2002 Phil "Crazy" Spencer
 * this will convert a licq icq list to gaim buddy list format
 * just do something like ls ~/.licq/users/ > licq.list and then
 * licq2gaim licq.list and it will do the rest DON'T FORGET TO SET
 * LICQUINPATH to the proper location of .licq/users/ =)
 */

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

#define LICQUINPATH "/home/spencep/.licq/users/"

int main(int argc,char *argv[]) {
	char *licqlist;
	char backup[255];
	char buf[255];
	char *parse[2];
	char *s;
	char fnamebuf[255];
	FILE *from;
	FILE *to;
	FILE *uin;

	if (argc < 2) {
		printf("USAGE: licq2gaim <textfile>\n");
		return 1;
	}
	strcpy(licqlist,argv[1]);

	if ((from = fopen(licqlist,"r")) == NULL) {
		perror("fopen");
		return 1;
	}
	if ((to = fopen("youruin.blist","w+")) == NULL) {
		perror("fopen");
		return 1;
	}
	printf("Files opened....\n");
	fputs("m 1\n",to);
	fputs("g ICQ\n",to);

	while (fgets(buf,255,from) != NULL) {
		if (buf[0] != '-' ) { //- users are evil spam guys, atleast on my list
   			strcpy(backup,buf);
			s = strtok(backup,".\n");
			parse[0] = s;
			printf("Parsing %s\n",parse[0]);
			sprintf(fnamebuf,"%s%s.uin",LICQUINPATH,parse[0]);
			fputs("b ",to);
			fputs(parse[0],to);
			fputs(":",to);
			if ((uin = fopen(fnamebuf,"r")) != NULL) {
				printf("UIN file opened successfully\n");
				while (fgets(buf,255,uin) != NULL) {
					if (strncmp(buf,"Alias",5) == 0) {
						/* Gaim buddly lists work as
						 * UIN:alias so once we find the
						 * Alias line in the uin file
						 * we can parse it and break the loop
						 */
						strcpy(backup,buf);
						s = strtok(backup," ");
						s = strtok(NULL," ");
						s = strtok(NULL,"\n");
						parse[1] = s;
						printf("alias: %s\n",parse[1]);
						strcat(parse[1],"\n");
						fputs(parse[1],to);
						break;
					}
				}
				fclose(uin);
			}

		}
	}

	fclose(from);
	fclose(to);
	return 0;
}

