#!/usr/bin/perl # use warnings; use strict; # created by Birl 2004oct14 for Mozilla's bookmarks.html # Last updated by Birl 2004nov10 for Mozilla 1.7.3 # # # Drop this PERL script in the same directory as your bookmarks.html file # Might work for Netscape's bookmarks file. # # By default, opens the user's bookmarks.html file # Tries to make sure that lines were not cut off prematurely # Makes sure that some of the values are strictly numbers # If a bookmark has a schedule, makes sure it isnt missing other attributes. # Not 100% sure if Errors are really fatal for bookmarks, or if Warnings should Errors, etc .... # # Line numbers are not given because Mozilla can store newlines in the Description field # which trips up PERL and text editors. # # Things to do: # Create a new file with the corrections in it. my $Input="bookmarks.html"; my $DL=0; my $AngleBrackets=0; my $ShowLineError=0; my $PersonalToolbarFolder=0; my $NewBookmarkFolder=0; my $NewSearchFolder=0; my $Output; my $LeftAngleBracketCounter=0; my $RghtAngleBracketCounter=0; my $Errors=0; my $Warnings=0; my $ShowErrorsOnly=1; my $ShowWarningsOnly=1; my $line; my $AngleBracketCounter=0; my $string; my $args; my $keyword; my @symbols; my %KeywordHash; $args=scalar @ARGV; #printf("args = $args\n"); if ($args) { if ($args == 1) { if ($ARGV[0] =~ /errors/i) { $ShowErrorsOnly=1; $ShowWarningsOnly=0; } elsif ($ARGV[0] =~ /warnings/i) { $ShowErrorsOnly=0; $ShowWarningsOnly=1; } else { $Input=$ARGV[0]; } } elsif ($args == 2) { if ($ARGV[1] =~ /errors/i) { $ShowErrorsOnly=1; $ShowWarningsOnly=0; } elsif ($ARGV[1] =~ /warnings/i) { $ShowErrorsOnly=0; $ShowWarningsOnly=1; } $Input=$ARGV[0]; } else { printf("Usage: one of \n"); printf(" $0\n"); printf(" $0 [file]\n"); printf(" $0 errors\n"); printf(" $0 warnings\n"); printf(" $0 [file] errors\n"); printf(" $0 [file] warnings\n"); exit 1; } } open(InputFH, $Input) or die "Could not open \"$Input\" for reading: $!\n"; #sprintf($Output, "%s_corrected", $Input); #open(OutputFH, ">$Output") or die "Could not open \"$Output\" for writing: $!\n"; printf("\n"); while() { $ShowLineError=0; $LeftAngleBracketCounter=0; $RghtAngleBracketCounter=0; $AngleBracketCounter=0; if (! /()/ ) # make sure the line is not part of a comment. { # # Thanks to Zeus Odin # $line=$_; # for my $position(0 .. length($line)-1) # { # $AngleBracketCounter++ if substr($line, $position, 1) eq '<'; # $AngleBracketCounter-- if substr($line, $position, 1) eq '>'; # # if ($AngleBracketCounter > 0) # { # printf("There are too many < in the following bookmark:\n"); # $ShowLineError=1; $Errors++; # } # if ($AngleBracketCounter < 0) # { # printf("There are too many > in the following bookmark:\n"); # $ShowLineError=1; $Errors++; # } # } if ($ShowErrorsOnly) { $LeftAngleBracketCounter= tr/\//; if ($LeftAngleBracketCounter > $RghtAngleBracketCounter) { printf ("There are %d too many <'s in the following line\n", $LeftAngleBracketCounter-$RghtAngleBracketCounter); $ShowLineError=1; $Errors++; } if ($LeftAngleBracketCounter < $RghtAngleBracketCounter) { printf ("There are %d too many >'s in the following line\n", $RghtAngleBracketCounter-$LeftAngleBracketCounter); $ShowLineError=1; $Errors++; } # @symbols = m/[<>]/g; # $string = join("", @symbols); # if ( ($string !~ m/^<(><)*>$/) && ($LeftAngleBracketCounter>0||$RghtAngleBracketCounter>0) ) # { # printf("###########################\n"); # printf("$LeftAngleBracketCounter < $RghtAngleBracketCounter >\n$_"); # printf("###########################\n"); # } if ( /SHORTCUTURL="(.+?)"/ ) { $keyword=$1; if (! defined $KeywordHash{$keyword}) { $KeywordHash{$keyword}=1; } else { printf("The keyword %s has already been used in an earlier bookmark and should be changed in the following line:\n", $keyword); $ShowLineError=1; $Errors++; } } else { undef $keyword; } } # ends IF ShowErrorsOnly } # ends IF (! /()/ ) if ($ShowErrorsOnly) { if ( /PERSONAL_TOOLBAR_FOLDER="true"/ ) { if ($PersonalToolbarFolder) { printf("Found another (%d) PERSONAL_TOOLBAR_FOLDER in this bookmark\n", $PersonalToolbarFolder); $ShowLineError=1; $Warnings++; } $PersonalToolbarFolder++; } if ( /NEW_BOOKMARK_FOLDER="true"/ ) { if ($NewBookmarkFolder) { printf("Found another (%d) NEW_BOOKMARK_FOLDER in this bookmark\n", $NewBookmarkFolder); $ShowLineError=1; $Warnings++; } $NewBookmarkFolder++; } if ( /NEW_SEARCH_FOLDER="true"/ ) { if ($NewSearchFolder) { printf("Found another (%d) NEW_SEARCH_FOLDER in this bookmark\n", $NewSearchFolder); $ShowLineError=1; $Warnings++; } $NewSearchFolder++; } if ( /ADD_DATE="(.+?)"/ ) { if ($1 !~ /\d+/) { printf("Bad number for ADD_DATE in this bookmark\n"); $ShowLineError=1; $Warnings++; } } if ( /LAST_VISIT="(.+?)"/ ) { if ($1 !~ /\d+/) { printf("Bad number for LAST_VISIT in this bookmark\n"); $ShowLineError=1; $Warnings++; } } if ( /LAST_MODIFIED="(.+?)"/ ) { if ($1 !~ /\d+/) { printf("Bad number for LAST_MODIFIED in this bookmark\n"); $ShowLineError=1; $Warnings++; } } } if ( /SCHEDULE="(.+?)"/ ) { my $schedule=$1; if ( (! /PING/i ) && $ShowErrorsOnly ) { printf("You have a bookmark scheduled in this bookmark, but no PINGs were established. This should be corrected.\n"); $ShowLineError=1; $Errors++; } elsif ( (! /LAST_PING/ ) && $ShowErrorsOnly ) { printf("This scheduled bookmark has no LAST_PING entry. This should be corrected\n"); $ShowLineError=1; $Errors++; } elsif ( (! /PING_CONTENT_LEN/ ) && $ShowErrorsOnly ) { printf("This scheduled bookmark has no PING_CONTENT_LEN entry. This should be corrected.\n"); $ShowLineError=1; $Errors++; } elsif ( (! /PING_STATUS/ ) && $ShowWarningsOnly ) { printf("This scheduled bookmark has no PING_STATUS entry, although it is not required.\n"); $ShowLineError=1; $Warnings++; } my @fields=split(/\|/, $schedule); my $NumFields= scalar @fields; if ($ShowErrorsOnly) { if ($NumFields != 4) { printf("The SCHEDULE in this bookmark should have only 4 fields separated by vertical bars |. Found %d.\n", $NumFields); $ShowLineError=1; $Errors++; } else { if ($fields[0] !~ /[0-6]{1,6}/) { printf("The scheduled day(s) (column 1) selected in this bookmark are out of range.\n"); printf(" The range is any sequential combination of 0 to 6. 0=Sun, 6=Sat\n"); $ShowLineError=1; $Errors++; } if ($fields[1] !~ /\d{1,2}-\d{1,2}/) { printf("The scheduled time (column 2) selected in this bookmark is incorrect.\n"); printf(" The correct time should be represented in 24-hour format (0-23) as START-FINISH.\n"); printf(" ie: 0-1 or 11-12 or 22-23, etc.\n"); $ShowLineError=1; $Errors++; } if ($fields[2] !~ /\d+/) { printf("The scheduled interval time (column 3) selected in this bookmark is incorrect\n"); printf(" The correct interval time should be represented as a number of minutes.\n"); $ShowLineError=1; $Errors++; } if ($fields[3] !~ /icon|sound|alert|open/) { printf("The scheduled action (column 4) selected in this bookmark is incorrect.\n"); printf(" The correct options for an action are any sequential combination of: icon, sound, alert, open"); $ShowLineError=1; $Errors++; } } } if ($ShowWarningsOnly) { if ( /LAST_PING="(.+?)"/ ) { if ($1 !~ /\d+/) { printf("Bad number for LAST_PING in this bookmark\n"); $ShowLineError=1; $Warnings++; } } if ( /PING_CONTENT_LEN="(.+?)"/ ) { if ($1 !~ /\d+/) { printf("Bad number for PING_CONTENT_LEN in this bookmark\n"); $ShowLineError=1; $Warnings++ } } if ( /PING_STATUS="(.+?)"/ ) { if ($1 !~ /new/i) { printf("Bad status for PING_STATUS in this bookmark\n"); $ShowLineError=1; $Warnings++ } } } } if ($ShowLineError) { printf("%s\n\n\n", $_); } else { #fprintf(OutputFH, "%s", $_); } if ( /
/i ) { $DL++; } if ( /<\/DL>/i ) { $DL--; } # Useless: All new folders being a new DL #if ( $DL > 1) { printf("Warning: Found an additional
before a terminating
\n"); } #if ( $DL < -1) { printf("Warning: Found an additional
before a beginning
\n"); } } close(InputFH); if ($ShowErrorsOnly) { if ($DL > 0) { printf("There are too many
tags present: $DL\n"); $Errors++;} if ($DL < 0) { printf("There are too few
tags present: $DL\n"); $Errors++;} } printf("%s: %d errors. %d warnings.\n", $Input, $Errors, $Warnings);