#!/usr/bin/perl -w

# A friendlier security level program.

use strict;

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

# 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 = 2002;
my $startmonth =1;
my $startday = 29;

my $endyear = 2005;
my $endmonth = 2;
my $endday = 23;

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

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

my $year = $startyear;
my $month = $startmonth;
my $day = $startday;

while (($year < $endyear)|| (($year == $endyear) && ($month < $endmonth)) || (($year == $endyear) && ($month == $endmonth) && $day <= $endday)) {
  my $response = LJgetDay($year, $month, $day, $content);
  my @posts = split(/\n/,$response);
  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);
  }

  ($year, $month, $day) = incrementDay($year, $month, $day);
}

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
}

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

  my @days = (
    31,
    (!($year % 4)&&!($year%400))?29:28,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31 );
   
  if ($month-1==0) {
    $last_month=12;
  } else {
    $last_month = $month - 1;
  }

  if (++$day > $days[$last_month]) {
    $day = 1;
    if (++$month > 13) {
      $month = 1;
      ++$year;
    }
  }
  
  return ($year, $month, $day);
}

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;
}

