#!/usr/bin/perl
$thresh = 500;
$fwo = 1;

while (defined($buf = <STDIN>)) {
	chomp $buf;
#	print "buf:$buf\n";
	($date, $time, $line) = ($buf =~ /^\[([^ ]+) ([^\]]+)\] (.*)/);
#	print $line . "\n";
	if ($line =~ /^[<(]([^>:)]+)(?:\:[^>)]+)?[>)] (.*)/) {
		($nick, $text) = ($1, $2);
		$nick =~ s/^[@+*]//;

		$linecnt{lc($nick)}++;
		$exists{lc($nick)} = 1;
		
		$text =~ tr/,:"?.;<>'=+//d;
		
		@words = split(/ /, $text);
		foreach $word (@words) {
			if ($exists{lc($words)}) {
				$links{lc($nick)}{$words}++;
			}
			last if ($fwo == 1);
		}
	} elsif ($line =~ /^. ([^\/ ]+)(?:\/[^ ]+)? (.*)/) {
		($nick, $text) = ($1, $2);
		$nick =~ s/^[@+*]//;

		$linecnt{lc($nick)}++;
		$exists{lc($nick)} = 1;

		$text =~ tr/,:"?.;<>'=+//d;
		
		@words = split(/ /, $text);
		foreach $word (@words) {
			if ($exists{lc($words)}) {
				$links{lc($nick)}{lc($words)}++;
			}
			last if ($fwo == 1);
		}
	} elsif ($line =~ /^[^ ]+ (?:SignOff|Quit) ([^:]+):/i ||
		$line =~ /^[^ ]+ (\w+) \w+ has (?:joined|left)/i) {
		$exists{lc($1)} = 1;
	}
}

# Disqualify any nick with fewer than $thresh spoken lines
$idnum = 0;
foreach $nick (keys %exists) {
#	printf("line count %s = %d\n", $nick, $linecnt{$nick});
	if ($linecnt{$nick} < $thresh || length($nick) < 2) {
		$exists{$nick} = 0;
		next;
	}
	$id{$nick} = $idnum;
	$idnum++;
}

#printf("Found %d noteworthy nicks\n", $idnum);
print <<'EOF';
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns:viz="http:///www.gexf.net/1.1draft/viz" version="1.1" xmlns="http://www.gexf.net/1.1draft">
<meta lastmodifieddate="2011-02-21+17:30">
 <creator>ircrel.pl</creator>
</meta>
<graph defaultedgetype="directed" idtype="string" type="static">
EOF

printf("<nodes count=\"%d\">\n", $idnum);
foreach $nick (keys %id) {
	printf("<node id=\"%d\" label=\"%s\"/>\n", $id{$nick}, $nick);
}
printf("</nodes>\n");
printf("<edges>\n");
$idnum = 0;
foreach $nick (keys %linecnt) {
#	printf("For $nick...\n");
	next if (!$exists{$nick});
	foreach $rel (keys %{$links{$nick}}) {
		next if (!$exists{$rel});
#		printf("\t%s: %d\n", $rel, $links{$nick}{$rel});
#		printf("%d -> %d wt %d\n", $id{$nick}, $id{$rel}, $links{$nick}{$rel});
#		if (!defined($id{$nick}) && $exists
		printf("<edge id=\"%d\" source=\"%d\" target=\"%d\" weight=\"%d\"/>\n", $idnum, $id{$nick}, $id{$rel}, $links{$nick}{$rel});
		$idnum++;
	}
}
print <<'EOF';
</edges>
</graph>
</gexf>
EOF
