Index: maiadbtool.pl
===================================================================
--- maiadbtool.pl       (revision 1111)
+++ maiadbtool.pl       (working copy)
@@ -118,8 +118,8 @@
 sub unconflict($$$);
 sub valid_address($);
 sub valid_domain($);
+sub reset_password($$$$);

-
 # name of this script
 my $script_name = "maiadbtool";

@@ -142,6 +142,7 @@
 my @add_domains = ();
 my @delete_domains = ();
 my @link_addresses = ();
+my @reset_passwords = ();
 my $link_account = undef;
 my $add_from_file = undef;
 my $delete_from_file = undef;
@@ -163,6 +164,7 @@
            "add-from-file=s" => \$add_from_file,
            "delete-from-file|del-from-file=s" => \$delete_from_file,
            "link-from-file=s" => \$link_from_file,
+           "reset-passwords=s" => \@reset_passwords,
            "address-rewriting-type=i" => \$address_rewriting_type,
            "routing-domain=s" => \$routing_domain,
            "auth-method|authentication-method=s" => \$auth_method,
@@ -180,6 +182,7 @@
 @add_domains = split(/,/, join(',', @add_domains));
 @delete_domains = split(/,/, join(',', @delete_domains));
 @link_addresses = split(/,/, join(',', @link_addresses));
+@reset_passwords = split(/,/, join(',', @reset_passwords));

 # Display usage information
 if ($help) {
@@ -203,6 +206,7 @@
            "   --delete-from-file file            : delete users and/or domains from a file\n" .
            "   --del-from-file file               : same as --delete-from-file\n" .
            "   --link-from-file file              : link addresses to user accounts from a file\n" .
+           "   --reset-passwords user1,user2,...  : reset password on accounts and send new user email\n" .
            "   --address-rewriting-type code      : address rewriting method [0..5] (see config.php)\n" .
            "   --routing-domain domain            : routing domain (see config.php)\n" .
            "   --auth-method method               : authentication method (see config.php)\n" .
@@ -276,6 +280,7 @@
 dedupe(\@link_addresses);
 dedupe(\@add_domains);
 dedupe(\@delete_domains);
+dedupe(\@reset_passwords);

 # Identify any conflicting addresses and domains
 unconflict(\@add_addresses, \@delete_addresses, $debug);
@@ -307,6 +312,10 @@
     link_address($dbh, $address, $account, $debug, $quiet);
 }

+# Reset passwords
+foreach my $user_name (@reset_passwords) {
+    reset_password($dbh, $user_name, $debug, $quiet);
+}
 # Bayes clear/expire
 if ($clear_bayes) {
     clear_bayes($dbh, $debug, $quiet);
@@ -1592,3 +1601,35 @@
 }


+sub reset_password($$$$) {
+       my($dbh, $user_name, $debug, $quiet) = @_;
+    my($sth, $query, @row, $user_id, $user_primary_email_id, $user_email);
+
+    # get the id's of the account
+    $query = "SELECT id, primary_email_id FROM maia_users WHERE user_name = ?";
+    $sth = $dbh->prepare($query)
+               or fatal(sprintf("Couldn't prepare query: %s", $dbh->errstr));
+    $sth->execute($user_name)
+        or fatal(sprintf("Couldn't execute query: %s", $dbh->errstr));
+    if (!((@row) = $sth->fetchrow_array())) {
+        fatal("Couldn't find user '$user_name' in the maia_users table");
+    }
+       $user_id = $row[0];
+       $user_primary_email_id = $row[1];
+    $sth->finish;
+
+       # get the account's primary email
+       $query = "SELECT email FROM users WHERE id = ?";
+    $sth = $dbh->prepare($query)
+               or fatal(sprintf("Couldn't prepare query: %s", $dbh->errstr));
+    $sth->execute($user_primary_email_id)
+        or fatal(sprintf("Couldn't execute query: %s", $dbh->errstr));
+    if (!(($user_email) = $sth->fetchrow())) {
+        fatal("Couldn't find email for id '$user_id' (with user_name '$user_name' and primary_email_id '$user_primary_email_id')");
+    }
+    $sth->finish;
+
+    output("Issuing new password to user account '$user_name' with id '$user_id' and primary address '$user_email'") if ($debug);
+
+       issue_password($dbh, $user_email, $user_id);
+}
\ No newline at end of file

