diff --git a/Makefile.in b/Makefile.in index 3b6dc158..a9769bce 100644 --- a/Makefile.in +++ b/Makefile.in @@ -73,6 +73,7 @@ installdirs: mkinstalldirs $(INSTALL) -d -m 0755 -o $(RUNNING_USER) -g $(RUNNING_GROUP) $(DESTDIR)$(localstatedir)/run/piler $(INSTALL) -d -m 0700 -o $(RUNNING_USER) -g $(RUNNING_GROUP) $(DESTDIR)$(localstatedir)/piler/store + $(INSTALL) -d -m 0700 -o $(RUNNING_USER) -g $(RUNNING_GROUP) $(DESTDIR)$(localstatedir)/piler/imap $(INSTALL) -d -m 0755 -o $(RUNNING_USER) -g $(RUNNING_GROUP) $(DESTDIR)$(localstatedir)/piler/stat $(INSTALL) -d -m 0711 -o $(RUNNING_USER) -g $(RUNNING_GROUP) $(DESTDIR)$(localstatedir)/piler/tmp $(INSTALL) -d -m 0700 -o $(RUNNING_USER) -g $(RUNNING_GROUP) $(DESTDIR)$(localstatedir)/piler/sphinx diff --git a/src/import.c b/src/import.c index 2d6d01c1..d4c02838 100644 --- a/src/import.c +++ b/src/import.c @@ -98,8 +98,6 @@ ENDE: switch(rc) { case OK: - //printf("imported: %s\n", filename); - bzero(&counters, sizeof(counters)); counters.c_size += sdata->tot_len; update_counters(sdata, data, &counters, cfg); @@ -107,7 +105,6 @@ ENDE: break; case ERR_EXISTS: - //printf("discarding duplicate message: %s\n", filename); rc = OK; break; diff --git a/src/pilerimport.c b/src/pilerimport.c index 4fd9e5a2..ec2d427e 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -28,6 +28,8 @@ extern char *optarg; extern int optind; int quiet=0; +int remove_after_successful_import = 0; + int connect_to_imap_server(int sd, int *seq, char *imapserver, char *username, char *password); int list_folders(int sd, int *seq, char *folders, int foldersize); @@ -211,6 +213,8 @@ int import_from_maildir(char *directory, struct session_data *sdata, struct __da if(rc == OK) (*tot_msgs)++; else ret = ERR; + if(remove_after_successful_import == 1 && ret != ERR) unlink(fname); + i++; if(quiet == 0) printf("processed: %7d\r", *tot_msgs); fflush(stdout); @@ -293,7 +297,7 @@ int import_from_imap_server(char *imapserver, char *username, char *password, st void usage(){ - printf("usage: pilerimport [-c ] -e | -m | -d | -i -u -p [-F ] [-R]\n"); + printf("usage: pilerimport [-c ] -e | -m | -d | -i -u -p [-F ] [-R] [-r] [-q]\n"); exit(0); } @@ -328,16 +332,18 @@ int main(int argc, char **argv){ {"password", required_argument, 0, 'p' }, {"skiplist", required_argument, 0, 'x' }, {"folder", required_argument, 0, 'F' }, + {"quiet", required_argument, 0, 'q' }, {"recursive", required_argument, 0, 'R' }, + {"remove-after-import", required_argument, 0, 'r' }, {"help", no_argument, 0, 'h' }, {0,0,0,0} }; int option_index = 0; - c = getopt_long(argc, argv, "c:m:M:e:d:i:u:p:x:F:Rh?", long_options, &option_index); + c = getopt_long(argc, argv, "c:m:M:e:d:i:u:p:x:F:Rrqh?", long_options, &option_index); #else - c = getopt(argc, argv, "c:m:M:e:d:i:u:p:x:F:Rh?"); + c = getopt(argc, argv, "c:m:M:e:d:i:u:p:x:F:Rrqh?"); #endif if(c == -1) break; @@ -393,6 +399,14 @@ int main(int argc, char **argv){ data.recursive_folder_names = 1; break; + case 'r' : + remove_after_successful_import = 1; + break; + + case 'q' : + quiet = 1; + break; + case 'h' : case '?' : usage(); diff --git a/util/gmail-imap-import.php b/util/gmail-imap-import.php new file mode 100644 index 00000000..d05ab5c4 --- /dev/null +++ b/util/gmail-imap-import.php @@ -0,0 +1,59 @@ +model('user/google'); +$loader->model('google/google'); + +openlog("piler-gmail-import", LOG_PID, LOG_MAIL); + +$ug = new ModelUserGoogle(); +$g = new ModelGoogleGoogle(); + + + +$query = $db->query("SELECT email FROM " . TABLE_GOOGLE); +if(isset($query->rows)) { + foreach($query->rows as $q) { + $access_token = $ug->refresh_access_token($q['email']); + $g->download_users_emails($q['email'], $access_token); + } +} + +?> diff --git a/webui/config.php b/webui/config.php index 432bbc21..68d4ecfd 100644 --- a/webui/config.php +++ b/webui/config.php @@ -65,10 +65,11 @@ define('DIR_APPLICATION', DIR_BASE . 'controller/'); define('DIR_THEME', DIR_BASE . 'view/theme/'); define('DIR_REPORT', DIR_BASE . 'reports/'); define('DIR_LOG', DIR_BASE . 'log/'); -define('DIR_TMP', DIR_BASE . 'tmp/'); define('DIR_STORE', '/var/piler/store'); define('DIR_STAT', '/var/piler/stat'); +define('DIR_IMAP', '/var/piler/imap'); +define('DIR_TMP', '/var/piler/tmp'); define('DECRYPT_BINARY', '/usr/local/bin/pilerget'); define('DECRYPT_BUFFER_LENGTH', 65536); @@ -176,7 +177,7 @@ define('CGI_INPUT_FIELD_WIDTH', 50); define('CGI_INPUT_FIELD_HEIGHT', 7); define('MEMCACHED_PREFIX', '_piler:'); -define('MEMCACHED_TTL', 3600); +define('MEMCACHED_TTL', 600); $memcached_servers = array( array('127.0.0.1', 11211) diff --git a/webui/model/google/google.php b/webui/model/google/google.php index e65b2a19..0ce60b5e 100644 --- a/webui/model/google/google.php +++ b/webui/model/google/google.php @@ -48,6 +48,7 @@ class ModelGoogleGoogle extends Model { public function download_users_emails($email, $accessToken) { $last_msg_id = -1; $from = 1; + $downloaded = 0; if(!$email || !$accessToken) { return 0; } @@ -82,11 +83,15 @@ class ModelGoogleGoogle extends Model { while(list($k, $v) = each($messages)) { $uuid = $storage->getUniqueId($k); - $tmpname = DIR_TMP . "piler-" . $email . "-" . $k . "-" . $uuid . ".eml"; - $f = fopen($tmpname, "w+"); + $tmpname = "piler-" . $email . "-" . $k . "-" . $uuid . ".eml"; + $f = fopen(DIR_TMP . "/" . $tmpname, "w+"); if($f){ fwrite($f, $v['RFC822.HEADER'] . $v['RFC822.TEXT']); fclose($f); + + rename(DIR_TMP . "/" . $tmpname, DIR_IMAP . "/" . $tmpname); + + $downloaded++; } //print "k: $k\n"; } @@ -97,7 +102,7 @@ class ModelGoogleGoogle extends Model { } - + syslog(LOG_INFO, "downloaded $downloaded messages for $email"); } } diff --git a/webui/model/user/google.php b/webui/model/user/google.php index 763f4ff5..174a286a 100644 --- a/webui/model/user/google.php +++ b/webui/model/user/google.php @@ -80,6 +80,31 @@ class ModelUserGoogle extends Model { } + public function refresh_access_token($email = '') { + if($email == '') { return ''; } + + $query = $this->db->query("SELECT refresh_token FROM " . TABLE_GOOGLE . " WHERE email=?", array($email)); + + if(!isset($query->row['refresh_token'])) { return ''; } + + $client = new apiClient(); + $client->setApplicationName(GOOGLE_APPLICATION_NAME); + + $client->setClientId(GOOGLE_CLIENT_ID); + $client->setClientSecret(GOOGLE_CLIENT_SECRET); + $client->setRedirectUri(GOOGLE_REDIRECT_URL); + $client->setDeveloperKey(GOOGLE_DEVELOPER_KEY); + + $client->refreshToken($query->row['refresh_token']); + $s = $client->getAccessToken(); + $a = json_decode($s); + + if(isset($a->{'access_token'})) { return $a->{'access_token'}; } + + return ''; + } + + } ?>