#!/usr/bin/perl -w

# A friendlier security level program.
# Original Program by Kevin Jackson-Mead.
# Modified by Sepideh Baghaii for speed and 
# kindness to the LJ Server.

use strict;

my $user = 'xxxx';
my $pw = 'yyyy';

# For friends
#my $allowmask = 1;

# For custom, use friendgroups.pl to determine the allow mask
# my $allowmask = 10;

# For private
my $allowmask = 0;

my $startyear = 2004;
my $startmonth =6;
my $startday = 1;

my $endyear = 2004;
my $endmonth = 6;
my $endday = 5;

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;

my $content = 
"ver=1&user=$user&password=$pw&lineendings=unix";

my @dates = LJPostDates($startyear, $startmonth, $startday, $endyear, $endmonth,$endday);

foreach my $date (@dates) {
  my ($year, $month, $day) = split('-',$date);
  my $response = LJgetDay($year, $month, $day, $content);
  my @posts = split(/\n/,$response);
  print @posts;
  my %posts;			# Holds all posts
  my $p_listening = 0;		# True if next line is a %posts value
  my %meta;			# Holds all meta information
  my $m_listening = 0;		# True is next line is a %meta value

  my ($id, $flavor);
  foreach (@posts) {
    if (/^events_(\d+)_(\w+)$/) {
      ($id, $flavor) = ($1, $2);
      $posts{$id} ||= {};
      $p_listening = 1;
    } elsif ($p_listening == 1) {
      $posts{$id}{$flavor} = $_;
      $p_listening = 0;
    } elsif ($m_listening == 1) {
      $meta{$flavor} = $_;
      $m_listening = 0;
    } else {
      # Must be a meta-key.
      $flavor = $_;
      $m_listening = 1;
    }
  }

  foreach my $id (sort(keys(%posts))) {
    if (defined($posts{$id}{security})) {
    # Not public

      if ($posts{$id}{security} eq 'private') {
        # It's private, so we skip
        next;
      }

      if ($posts{$id}{allowmask} > 1) {
        # It's a custom group
        # Skip it if the min security level is friends
        # Otherwise, we'll want to edit this one
        next if $allowmask == 1;
      } elsif ($posts{$id}{allowmask} == 1) {
        # It's friends
        # Skip it if the min security level is friends
        # Otherwise, we'll want to edit this one
        next if $allowmask == 1;
      } else {
        # Here we have a case where th security is 'usemask'
        # and the allowmask is 0.
        # This happens when a post is initially set to a
        # certain friend group but the friend group is
        # then deleted.
        # let's just skip these posts.
        next;
      }
    }

    $posts{$id}{eventtime} =~ m/^(....)-(..)-(..) (..):(..):..$/;
    my $y = $1;
    my $m = $2;
    my $d = $3;
    my $h = $4;
    my $min = $5;

    my $security;
    if ($allowmask == 0) {
      $security = 'private';
    } else {
      $security = 'usemask';
    }

    LJeditEvent($posts{$id}{itemid}, $posts{$id}{event}, 
$posts{$id}{subject} || '', $y, $m, $d, $h, $min, $security, $allowmask, 
$content);
  }
    print "Year: $year Month: $month Day: $day\n";

}

sub LJeditEvent {
  my ($itemid, $event, $subject, $year, $mon, $day, $hour, $min, $security, 
$allowmask, $content) = @_;

  $content .= 
"&mode=editevent&itemid=$itemid&event=$event&subject=$subject&year=$year&mon=$mon&day=$day&hour=$hour&min=$min&security=$security";

  if ($security eq 'usemask') {
    $content .= "&allowmask=$allowmask";
  }

  my $req =
  HTTP::Request->new(POST=>"http://www.livejournal.com/interface/flat");
  $req->content_type('application/x-www-form-urlencoded');
  $req->content($content);

  my $result = $ua->request($req);
  # Think about some kind of logging here
  print $result;
}

sub LJgetDay {
  my ($year, $month, $day, $content) = @_;

  $content .= 
"&mode=getevents&selecttype=day&year=$year&month=$month&day=$day";

  my $req = 
  HTTP::Request->new(POST=>"http://www.livejournal.com/interface/flat");
  $req->content_type('application/x-www-form-urlencoded');
  $req->content($content);

  my $result = $ua->request($req);

  return $result->content;
}

sub LJPostDates {
	my ($startyear, $startmonth, $startday, $endyear, $endmonth, $endday) = @_;	
	my $day_contents = $content."&mode=getdaycounts";

	my $req = 
	HTTP::Request->new(POST=>"http://www.livejournal.com/interface/flat");
	$req->content_type('application/x-www-form-urlencoded');
	$req->content($day_contents);
	my $result = $ua->request($req)->content;
	@dates = split('\n',$result);
	@dates = grep(/\d{4}\-\d{2}\-\d{2}/,@dates);
	my @valid_dates=();
	foreach my $date (@dates) {
		my ($year, $month, $day) = split('-',$date);
		if (($year < $startyear || $year > $endyear) ||
		($year == $startyear && $month < $startmonth) ||
		($year == $endyear && $month > $endmonth) ||
		($year == $startyear && $month == $startmonth && $day < $startday) ||
		($year == $endyear && $month == $endmonth && $day > $endday))
		{ next; }
		else {
			push(@valid_dates, $date);
		}
	}
	return @valid_dates;
}
