#!/usr/bin/perl package LJ; use Getopt::Long; use IO::File; require '/home/lj/cgi-bin/ljlib.pl'; require '/home/lj/cgi-bin/ljprotocol.pl'; my $MAINTAINER = "jaffray\@pobox.com"; my $VERSION = "0.1"; my ($user, $name, $addr, $password, $userpic, $communities, $valid, $paid, $email); sub help { die <<"EOF"; Creates a new pre-validated LiveJournal user, with username USER, email address ADDR, name NAME, and optionally the specified community memberships and user pictures. Prompts for the LiveJournal database password if not given on the command line. -p, --password=PASS give USER the password PASS (default random) -u, --userpic=FILE upload FILE as USER's default picture -c, --community=COMM1,COMM2,... make USER belong to communities COMM1,COMM2,... -a, --paid give user paid status -e, --email send email with account details to ADDR -h, --help this message This is $0 $VERSION. Report bugs to <$MAINTAINER>. EOF } sub usage { die "usage: $0 [OPTIONS...] USER ADDR NAME\n"; } sub get_options { GetOptions( "password|p=s" => \$password, "userpic|u=s" => \$userpic, "community|communities|c=s" => \$communities, "paid|a" => \$paid, "email|e" => \$email, "help|h" => \&help, ) or usage(); @ARGV == 3 or usage(); ($user, $addr, $name) = @ARGV; $password ||= int rand 1000000; } sub create_user { my $paid = (($LJ::EVERYONE_PAID || $paid) ? "on" : "off"); my $status = 'A'; my ($quser, $qname, $qemail, $qpassword) = map {$dbh->quote($_)} ($user, $name, $addr, $password); $dbh->do("INSERT INTO user (user, email, password, status, paidfeatures, name, timecreate, lastn_style, friends_style, calendar_style, day_style) VALUES ($quser, $qemail, $qpassword, '$status', '$paid', $qname, NOW(), 1, 6, 2, 5)"); not $dbh->err and return 1 or die $dbh->errstr; } sub upload_pic { undef $/; my $image_file = new IO::File($userpic) or die "Couldn't open $userpic: $!\n"; my $image_data = $image_file->getline; length $image_data <= 40960 or die "$filename exceeds maximum file size of 40K."; $/ = "\n"; my $req = { mode => "uploadpic", user => $user, password => $password, data => $image_data, default => 1, }; my $res = {}; &LJ::do_request($dbh, $req, $res, {}); $res->{success} eq "OK" and return 1 or die $res->{errmsg}; } sub send_email { my $template = new IO::File("$BASEDIR/inc/email-welcome-intranet") or die "couldn't read template: $!"; my $msg = join "", $template->getlines; my %replace = ( "email" => $addr, "username" => $user, "password" => $password, "sitename" => $SITENAME, "siteroot" => $SITEROOT, "admin_email" => $ADMIN_EMAIL, ); $msg =~ s/%%(\w+?)%%/$replace{$1}/ge; print "Sending message:\n$msg\n\n"; open MAIL, "|$SENDMAIL" or die "couldn't open sendmail: $!"; print MAIL $msg; close MAIL; } sub join_communities { my @communities = split /,/, $communities; my $userid = get_userid($dbh, $user); foreach my $community (@communities) { my $commid = get_userid($dbh, $community) or die "invalid community name $community"; $dbh->do("INSERT INTO friends (userid, friendid, fgcolor, bgcolor, groupmask) VALUES ($userid, $commid, '#000000', '#ffffff', 1)"); die $dbh->errstr if $dbh->err; $dbh->do("INSERT INTO friends (userid, friendid, fgcolor, bgcolor, groupmask) VALUES ($commid, $userid, '#000000', '#ffffff', 1)"); die $dbh->errstr if $dbh->err; $dbh->do("INSERT INTO logaccess (ownerid, posterid) VALUES ($commid, $userid)"); die $dbh->errstr if $dbh->err; } return 1; } get_options(); connect_db(); eval {create_user()} or die "error creating user: $@\n"; if ($userpic) { eval {upload_pic()} or die "error uploading userpic: $@\n"; } if ($email) { eval {send_email()} or die "error sending email: $@\n"; } if ($communities) { eval {join_communities()} or die "error joining communities: $@\n"; }