[Affelio-cvs 1209] CVS update: affelio/apps/diary/Diary

Back to archive index

Tadashi Okoshi slash****@users*****
2006年 4月 6日 (木) 02:23:59 JST


Index: affelio/apps/diary/Diary/PrefSet.pm
diff -u affelio/apps/diary/Diary/PrefSet.pm:1.2 affelio/apps/diary/Diary/PrefSet.pm:1.3
--- affelio/apps/diary/Diary/PrefSet.pm:1.2	Wed Mar  8 00:14:41 2006
+++ affelio/apps/diary/Diary/PrefSet.pm	Thu Apr  6 02:23:58 2006
@@ -18,7 +18,9 @@
 package Diary::PrefSet;
 {
     use strict;
+    use lib("../../../lib/");
     use Affelio::misc::Debug qw( debug_print);
+    use Diary;
 
     use Exporter;
     @Diary::PrefSet::ISA = "Exporter";
@@ -66,11 +68,26 @@
 			setRDFURL($diary, $url);
 			$output_ref->{"SET_RDF"} = '<AF_M text="RDF file was imported.">';
 		}
-	    }	else { # normal diary
+	    } else { # normal diary
 		debug_print("Diary::RdfSet::show: Unset the RDF file.");
 		unsetRDFURL($diary);
 		$output_ref->{"UNSET_RDF"} = '<AF_M text="The RDF file was unlinked.">';
 	    }
+
+	    #save Moblog config
+	    my $moblog_author = $wi->PTN_email($cgi->param("moblog_author"));
+	    my $moblog_popserv = $wi->PTN_email($cgi->param("moblog_popserv"));
+	    my $moblog_popuser = $wi->PTN_email($cgi->param("moblog_popuser"));
+	    my $moblog_poppass = $wi->PTN_password($cgi->param("moblog_poppass"));
+	    debug_print("Diary::RdfSet::show: moblog_author =[$moblog_author]");
+	    debug_print("Diary::RdfSet::show: moblog_popserv=[$moblog_popserv]");
+	    debug_print("Diary::RdfSet::show: moblog_popuser=[$moblog_popuser]");
+	    debug_print("Diary::RdfSet::show: moblog_poppass=[$moblog_poppass]");
+
+	    $diary->savePref("moblog_author",  $moblog_author);
+	    $diary->savePref("moblog_popserv", $moblog_popserv);
+	    $diary->savePref("moblog_popuser", $moblog_popuser);
+	    $diary->savePref("moblog_poppass", $moblog_poppass);
 	}
 
 	my $rdf_url = $diary->getRDFURL;
@@ -81,13 +98,17 @@
 	    $output_ref->{"default"} = 'CHECKED';
 	}
 
+	$output_ref->{"moblog_author"} =  $diary->loadPref("moblog_author");
+	$output_ref->{"moblog_popuser"} = $diary->loadPref("moblog_popuser");
+	$output_ref->{"moblog_poppass"} = $diary->loadPref("moblog_poppass");
+	$output_ref->{"moblog_popserv"} = $diary->loadPref("moblog_popserv");
+
 	debug_print("Diary::RdfSet::show end.");
     }
 
 ##############################################
 # setRDFURL
 ##############################################
-
 sub setRDFURL {
 	my ($diary, $url) = @_;
 	local (*OUT);
@@ -100,11 +121,12 @@
 ##############################################
 # unsetRDF
 ##############################################
-
 sub unsetRDFURL {
 	my $diary = shift;
 	unlink("$diary->{datadir}url") if (-f "$diary->{datadir}url");
 }
 
+
+
 }
 1;
Index: affelio/apps/diary/Diary/RetrieveMoblog.pm
diff -u /dev/null affelio/apps/diary/Diary/RetrieveMoblog.pm:1.1
--- /dev/null	Thu Apr  6 02:23:59 2006
+++ affelio/apps/diary/Diary/RetrieveMoblog.pm	Thu Apr  6 02:23:58 2006
@@ -0,0 +1,321 @@
+# Copyright (C) 2006 Affelio Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+
+package Diary::RetrieveMoblog;
+{
+    use strict;
+    use Image::Magick;
+    use Image::Size;
+    use Net::POP3;
+    use MIME::Parser;
+    use Jcode;
+    use CGI;
+    use File::Copy;
+
+    use Diary::WriteDiary qw (addEntry copyUploadImage);
+    use Affelio::misc::Debug qw( debug_print);
+
+    use Exporter;
+    @Diary::RetrieveMoblog::ISA = "Exporter";
+    @Diary::RetrieveMoblog::EXPORT = qw (retrieve_mesg_from_pop3);
+
+    ######################################################################
+    #retrieve_msg_from_pop3
+    ######################################################################
+    sub retrieve_mesg_from_pop3{
+	my %args = @_;
+	my $diary = $args{diary};
+	my $pop3_server = $args{pop3_server};
+	my $pop3_username = $args{pop3_username};
+	my $pop3_password = $args{pop3_password};
+	my $author_email = $args{author_email};
+	my $tmp_dir = $args{tmp_dir};
+
+	my @mesgs=();
+
+	debug_print("Diary::Moblog::retrieve: server=$pop3_server");
+	debug_print("Diary::Moblog::retrieve: user  =$pop3_username");
+	debug_print("Diary::Moblog::retrieve: pass  =$pop3_password");
+	debug_print("Diary::Moblog::retrieve: author=$author_email");
+	debug_print("Diary::Moblog::retrieve: tmp   =$tmp_dir"); 
+	
+	my $num_msg = 0;
+	
+	my $parser = new MIME::Parser;
+	$parser->output_dir($tmp_dir);
+	
+	my $mail_regex = setup_regex();
+	
+	#####################################
+	#Connect to POP server
+	my $popper = Net::POP3->new($pop3_server);
+	if(!$popper){
+	    debug_print("Diary::Moblog::retrieve: Creating connection to POP3 server. NG.\n");
+	    return("");
+	}else{
+	    debug_print("Diary::Moblog::retrieve: Creating connection to POP3 server. OK.\n");
+	}
+	
+	#####################################
+	#Log into POP server
+	my $num_mail = 0;
+	$num_mail = $popper->login($pop3_username, $pop3_password);
+	debug_print("Diary::Moblog::retrieve: You have [$num_mail] mails\n");
+	my $msgid_list = $popper->list();
+	my @msgid_list = sort { $a <=> $b } keys %$msgid_list;
+	
+	#####################################
+	#For each message....
+	for(my $count=0; $count < $num_mail; $count++) {
+	    
+	    my $msg_id = $msgid_list[$count];
+	    my $entity = $parser->parse_data($popper->get($msg_id));
+	    if(!$entity){
+		debug_print("Diary::Moblog::retrieve: Failed to read email from server.");
+		return("");
+	    }
+	    
+	    my $this_title ="";
+	    my $this_body ="";
+	    my $this_date ="";
+	    my $this_author="";
+	    
+	    #####################################
+	    #Authentication with "From" field
+	    my $this_author = $entity->head->get('from');
+	    if ($this_author =~ /($mail_regex)/o) {
+		if ($author_email ne $1) {
+		    $popper->delete($msg_id);
+		    debug_print("Diary::Moblog::retrieve: Denied entry from [$1]");
+		    next;
+		}
+	    }
+	    debug_print("Diary::Moblog::retrieve: Mail from : $1\n");
+	    
+	    $this_date = &conv_datetime($entity->head->get('date'));
+	    my $this_title = $entity->head->get('subject');
+	    my $this_body = "";
+	    my $this_num_files = 0;
+	    my @this_files = ();
+	    
+	    unless ($entity->is_multipart) {
+		#############################
+		#Single part mail
+		#############################
+		debug_print("Diary::Moblog::retrieve: Mail is single part.");
+		$this_body = $entity->bodyhandle->as_string;
+		
+	    }else{
+		debug_print("Diary::Moblog::retrieve: Mail is multi part.");
+
+		#############################
+		#MIME multipart mail
+		#############################
+		for (my $i=0; $i<$entity->parts; $i++) {
+		    # text or binary?
+		    my ($type, $subtype) = split('/', $entity->parts($i)->head->mime_type);
+		    
+		    if ($type =~ /^(text|message)$/) { # text
+			#text message
+			debug_print("Diary::Moblog::retrieve: part = text");
+			$this_body .= $entity->parts($i)->bodyhandle->as_string;
+			
+		    }else {
+			#binary data
+			debug_print("Diary::Moblog::retrieve: part = binary");
+
+			my $filepath = $entity->parts($i)->bodyhandle->path;
+			my $t = time; #get_timestamp();
+			my $filename = "$t-$count-$this_num_files";
+			$this_num_files++;
+			my ($width, $height, $image_type) = imgsize($filepath);
+			$filename .=  "." . lc($image_type);	
+
+			#########################
+			#check directories
+			my $tmp_imgdir = "$diary->{datadir}tmp/";
+			unless (-d $tmp_imgdir) {
+			    mkdir $tmp_imgdir;
+			}
+			my $thumbdir = "$diary->{datadir}tmp/thumb/";
+			unless (-d $thumbdir) {
+			    mkdir $thumbdir;
+			}
+			debug_print("Diary::Moblog::retrieve: after directory check.");
+
+			#########################
+			#Make distfile
+			my $distfile = $tmp_imgdir . $filename;
+			move($filepath, $distfile) or return("");
+			debug_print("Diary::Moblog::retrieve: made dist file.");
+
+			#########################
+			#Make thumnail
+			my $thumbfile = $thumbdir . $filename;
+			my $image = Image::Magick->new;
+			$image->Read( $distfile );
+			my ($wg,$hg) = $image->Get('Width','Height');
+			if ($wg>300 || $hg>300){
+			    $image->Resize( geometry=>"300x300" );
+			}
+			$image->Set( quality=>75 );
+			$image->Write( $thumbfile );
+			debug_print("Diary::Moblog::retrieve: made thumnail file.");
+
+			push(@this_files, "$filename");
+		    }
+		    #else
+		    
+		    debug_print("Diary::Moblog::retrieve: going to next part...");
+		}
+		#for each file
+		
+	    }
+	    #if(multipart)
+	    
+	    # POP3サーバのファイルを削除
+	    $popper->delete($msg_id);
+	    $num_msg++;
+	    
+	    #####################################
+	    #Several processing on title & body
+	    #####################################
+	    #Encoding
+	    $this_title = Jcode->new($this_title)->mime_decode->utf8;
+	    $this_body = Jcode->new($this_body)->utf8;
+	    
+	    #Empty title
+	    if($this_title eq ""){ $this_title =" "; }
+	    
+	    #Erase emtpy lines in the begining of the body
+	    $this_body =~ s/^\n+//s;
+	    
+	    #\n -> <BR>
+	    $this_body =~ s/\n\r/\n/g;
+	    $this_body =~ s/\r/\n/g;
+	    $this_body =~ s/\n/<br>/g;
+
+	    #####################################
+	    #Push into structure
+	    #####################################
+	    push(@mesgs,
+		 {title => $this_title,
+		  body => $this_body,
+		  files => \@this_files
+		  }
+		 );
+
+	    debug_print("Diary::Moblog::retrieve: going to next message...");
+	}
+	
+	#####################################
+	#Logoff
+	$popper->quit;
+
+	##############################################################
+	#メールサーバとのやりとりが終了... let's post them
+	##############################################################
+	foreach my $entry (@mesgs){
+	    debug_print("Diary::Moblog::retrieve: " . $entry->{title});
+	    debug_print("Diary::Moblog::retrieve: " . $entry->{body});
+	    debug_print("Diary::Moblog::retrieve: " . $entry->{files});
+
+	    foreach my $image (@{$entry->{files}}){
+		$entry->{body} .= '<p><image="' . $image . '"></p>';
+	    }
+
+	    debug_print("Diary::Write::show: before add entry");
+	    my $time = time;
+	    my $new_id = addEntry($diary, 
+				  $entry->{title}, 
+				  $entry->{body}, 
+				  1, 
+				  $diary->{afap}->get_owner_info("nickname"),
+				  $diary->{afap}->get_owner_info("afid"),
+				  $time);
+	    copyUploadImage($diary, $new_id);
+	    debug_print("Diary::Write::show: post news");
+
+	    $diary->{afap}->post_news($entry->{title}, 
+				      "", 
+				      $diary->{afap}->get_owner_info("nickname"),
+				      $diary->{afap}->get_site_info("web_root") . "/apps/". 
+				      $diary->{afap}->{install_name} . "/index.cgi?mode=show_diary&id=".$new_id);
+	    debug_print("Diary::Write::show: ping");
+
+#	    # send trackback ping
+#	    if ($tping) {
+#		sendTrackbackPing($diary, $tping, $title, $contents, $new_id);
+#	    }
+	    debug_print("Diary::Write::show: after submit");
+
+
+	}
+	#foreach
+	
+
+    }
+    
+
+    sub setup_regex{
+	return(
+q{(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\} .
+q{\[\]\000-\037\x80-\xff])|"[^\\\\\x80-\xff\n\015"]*(?:\\\\[^\x80-\xff][} .
+q{^\\\\\x80-\xff\n\015"]*)*")(?:\.(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x} .
+q{80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff])|"[^\\\\\x80-} .
+q{\xff\n\015"]*(?:\\\\[^\x80-\xff][^\\\\\x80-\xff\n\015"]*)*"))*@(?:[^(} .
+q{\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\0} .
+q{00-\037\x80-\xff])|\[(?:[^\\\\\x80-\xff\n\015\[\]]|\\\\[^\x80-\xff])*} .
+q{\])(?:\.(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,} .
+q{;:".\\\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\\\x80-\xff\n\015\[\]]|\\\\[} .
+q{^\x80-\xff])*\]))*} );
+    }
+
+
+
+}
+
+
+sub conv_datetime {
+	my %monthlist = ("Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", 
+						"May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", 
+						"Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12");
+	my $converted = "";
+	#カッコはコメントなので、無視するために削除する
+	$_[0] =~ s/\(\w*\)//g;
+	if ($_[0] =~ /\w*\s(\d{1})\s([A-Z][a-z][a-z])\s(\d+)\s(\d+):(\d+):(\d+)/) {
+		#day = $1, month = $monthlist{$2}, year = $3, $hour = $4, $minute = $5, $second = $6
+		#秒が省略されていた場合は00をセットする
+		if ($6 eq "") {
+			$6 = "00";
+		}
+		$converted = $3 . $monthlist{$2} . "0" . $1 . $4 . $5 . $6;
+	}
+	elsif ($_[0] =~ /\w*\s(\d+)\s([A-Z][a-z][a-z])\s(\d+)\s(\d+):(\d+):(\d+)/) {
+		#day = $1, month = $monthlist{$2}, year = $3, $hour = $4, $minute = $5, $second = $6
+		#秒が省略されていた場合は00をセットする
+		if ($6 eq "") {
+			$6 = "00";
+		}
+		$converted = $3 . $monthlist{$2} . $1 . $4 . $5 . $6;
+	}
+	return $converted;
+}
+
+
+
+1;
Index: affelio/apps/diary/Diary/WriteDiary.pm
diff -u affelio/apps/diary/Diary/WriteDiary.pm:1.12 affelio/apps/diary/Diary/WriteDiary.pm:1.13
--- affelio/apps/diary/Diary/WriteDiary.pm:1.12	Thu Apr  6 00:59:39 2006
+++ affelio/apps/diary/Diary/WriteDiary.pm	Thu Apr  6 02:23:58 2006
@@ -28,7 +28,7 @@
 
 use Exporter;
 @Diary::WriteDiary::ISA = "Exporter";
- @ Diary::WriteDiary::EXPORT = qw (show handler);
+ @ Diary::WriteDiary::EXPORT = qw (show handler addEntry copyUploadImage);
 
 
     #######################################################################
@@ -141,7 +141,11 @@
 		# add entry
 		debug_print("Diary::Write::show: before add entry");
 		my $time = timelocal(0, $min, $hour, $mday, $mon - 1, $year);
-		my $new_id = addEntry($diary, $title, $contents, $category,$time);
+		my $new_id = addEntry($diary, $title, $contents, $category,
+				      $diary->{nickname},
+				      $diary->{afid},
+				      $time);
+
 		copyUploadImage($diary, $new_id);
 		debug_print("Diary::Write::show: post news");
 		$diary->{afap}->post_news($title, 
@@ -189,16 +193,15 @@
     ##############################################
     # addEntry
     ##############################################
-
     sub addEntry {
 	my $diary     = shift;
 	my $title    = shift;
 	my $contents = shift;
 	my $c_id = shift;
-	my $user = $diary->{nickname};
-	my $uri = $diary->{afid};
-	my $pwd;
+	my $user = shift;
+	my $uri = shift;
 	my $time     = shift;
+	my $pwd;
 	debug_print("Diary::Write::addEntry: start.");
 	unless ($time) { $time = time; }
 
@@ -207,8 +210,8 @@
 	if ($c_id eq ""){ $c_id = "NULL";}
 	debug_print("Diary::Write::addEntry: prevent double submit.");
 	# prevent double submit
-	my @same = $diary->getall("SELECT id FROM $diary->{entry_table} WHERE title = '$title' AND contents = '$contents'");
-	if($#same >= 0) { return; }
+#	my @same = $diary->getall("SELECT id FROM $diary->{entry_table} WHERE title = '$title' AND contents = '$contents'");
+#	if($#same >= 0) { return; }
 
 	debug_print("Diary::Write::addEntry: insert new entry.");
 	$diary->{dbh}->do("INSERT INTO $diary->{entry_table} VALUES (NULL, '$title', '$contents', $year, $mon, $mday, $time, $c_id, '$user', '$uri', NULL, 0)");


Affelio-cvs メーリングリストの案内
Back to archive index