[siesta-commit] siesta/lib/Siesta/Plugin Subscribe.pm,1.9,1.10

[prev] [thread] [next] [lurker] [Date index for 2002/09/20]

From: muttley
Subject: [siesta-commit] siesta/lib/Siesta/Plugin Subscribe.pm,1.9,1.10
Date: 14:29 on 20 Sep 2002
Update of /cvsroot/siesta/siesta/lib/Siesta/Plugin
In directory usw-pr-cvs1:/tmp/cvs-serv4423/lib/Siesta/Plugin

Modified Files:
	Subscribe.pm 
Log Message:
Subscribe now challenges the user with a unique number.


Index: Subscribe.pm
===================================================================
RCS file: /cvsroot/siesta/siesta/lib/Siesta/Plugin/Subscribe.pm,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- Subscribe.pm	17 Sep 2002 11:31:54 -0000	1.9
+++ Subscribe.pm	20 Sep 2002 13:29:39 -0000	1.10
@@ -3,6 +3,7 @@
 use strict;
 use Siesta::Plugin;
 use base 'Siesta::Plugin';
+use Digest::MD5 qw(md5_hex);
 
 use vars qw( $DESCRIPTION );
 
@@ -17,27 +18,109 @@
 
     my $user = Siesta::User->new($user_id);
 
+    # check to see if they're already subbed
+    my %members = map { $_->id() => 1 } $list->members();
+
+
+    if ($members{$user_id}) {
+    	$self->already_subbed($mail);
+	return 1;
+    }
+
     # XXX should this be done implicitly by $list->add_member?
     unless ($user) {
         $user = Siesta::User->new_from_hash( id => $user_id );
         $user->save();
     }
 
-    if ( $list->add_member($user_id) ) {
-        $mail->reply( to   => $list->owner,
-                      body => "$user_id JOIN " . $list->id );
-        $mail->reply( body => <<END);
+    # now check to see if there's a key for them
+    $mail->user($user);
+    my $value = $self->config($mail,'request');
+
+    # and get the value from subject
+    my ($sub) = $mail->subject() =~ /SUBSCRIBE\s+(.+)/;
+
+
+
+    # if there is 
+    if (defined $sub)
+    {
+	# are they the same
+	if (defined $value && ($value eq $sub)) {
+		# tell them they've been subscribed
+		$self->successfully_subbed($mail);
+	} elsif (!(defined $value) || !($value eq $sub)) {
+		# tell them that their key is invalid
+		$self->invalid_key($mail);
+	}
+ 
+    # otherwise
+    } else {
+	# send a key
+	$self->send_key($mail);
+    }
+
+
+    # now go through the database and remove any old keys
+    my $clone = $mail->clone();
+    # remove the user and the list so that
+    # we can clean as much up as possible
+    $clone->user(undef);
+    $clone->list(undef);
+    # get the time a month ago
+    my $date = time() - (60 * 60 * 24 * 31);
+    # $self->delete_config($clone, 'request', $date);
+    # TODO     
+
+}
+
+
+
+# mail the person and the list owner and 
+# tell them they've been subbed
+sub successfully_subbed
+{
+       my $self = shift;
+       my $mail = shift;
+       my $list = $mail->list();
+       my $user = $mail->user(); 
+
+       # delete the key already in the db
+       $self->delete_config($mail,'request');
+
+
+       # add the user to the list and if that fails, send an error
+       # TODO should this be a seperate send method?
+       unless($list->add_member($user->id)) {
+		$self->already_subbed($mail);
+		return;
+       }
+       # mail the listowner and tell them that someone subbed 
+       $mail->reply( to   => $list->owner,
+                     body => $user->id."  JOIN " . $list->id );
+
+       # mail the person and tell them that they've been subbed
+       $mail->reply( body => <<END);
 Hi,
  You have been successfully subscribed to ${ \( $list->id ) }
 
 Enjoy,
 - Siesta::Plugin::Subscribe
 END
-    }
-    else {
-        $mail->reply( to   => $list->owner,
-                      body => "$user_id JOIN FAILED " . $list->id );
-        $mail->reply( body => <<END);
+
+}
+
+
+# Tell them they've already been subbed
+sub already_subbed
+{
+
+       my $self = shift;
+       my $mail = shift;
+       my $list = $mail->list();
+
+       # mail them and reject them
+       $mail->reply( body => <<END);
 Hi,
  You could not be subscribed to ${ \( $list->id ) }.
 
@@ -47,8 +130,61 @@
 Apologies,
  - Siesta::Plugin::Subscribe
 END
-    }
-    return 1;
 }
+
+
+# Tell them that the key they passed was invalid
+sub invalid_key
+{
+
+       my $self = shift;
+       my $mail = shift;
+       my $list = $mail->list();
+   
+       # mail them and reject them
+       $mail->reply( body => <<END);
+Hi,
+ You could not be subscribed to ${ \( $list->id ) }.
+
+ The subscribe key you gave was invalid.
+
+Apologies,
+ - Siesta::Plugin::Subscribe
+END
+}
+
+
+# send them a unique key
+sub send_key
+{
+	my $self = shift;
+	my $mail = shift;
+	my $list = $mail->list();
+
+	# generate a unique value 
+	my $value = md5_hex($$.time().{}.rand());
+        $self->config($mail,'request', $value);
+
+	
+        # mail them with a a request	
+	$mail->subject("SUBSCRIBE $value");
+        $mail->reply( body => <<END);
+Hi,
+ You have requested to be subscribed to  ${ \( $list->id ) }.
+
+ Please reply to this mail with the subject line
+
+ SUBSCRIBE $value
+
+ Hitting 'reply' in most mail clients should suffice. 
+
+ - Siesta::Plugin::Subscribe
+END
+
+}
+
+
+
+=cut
 
 1;



Generated at 13:57 on 01 Jul 2004 by mariachi 0.52