Skip to content

Commit

Permalink
finalized google free apps support
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuto committed Sep 28, 2012
1 parent 14ef68e commit f2dd3b6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 11 deletions.
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,13 @@ int import_message(char *filename, struct session_data *sdata, struct __data *da

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);

break;

case ERR_EXISTS:
//printf("discarding duplicate message: %s\n", filename);
rc = OK;
break;

Expand Down
20 changes: 17 additions & 3 deletions src/pilerimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -293,7 +297,7 @@ int import_from_imap_server(char *imapserver, char *username, char *password, st


void usage(){
printf("usage: pilerimport [-c <config file>] -e <eml file> | -m <mailbox file> | -d <directory> | -i <imap server> -u <imap username> -p <imap password> [-F <foldername>] [-R]\n");
printf("usage: pilerimport [-c <config file>] -e <eml file> | -m <mailbox file> | -d <directory> | -i <imap server> -u <imap username> -p <imap password> [-F <foldername>] [-R] [-r] [-q]\n");
exit(0);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
59 changes: 59 additions & 0 deletions util/gmail-imap-import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

$webuidir = "";

if(isset($_SERVER['argv'][1])) { $webuidir = $_SERVER['argv'][1]; }

require_once($webuidir . "/config.php");

ini_set("session.save_path", DIR_TMP);

require(DIR_SYSTEM . "/startup.php");

require_once 'Zend/Mail/Protocol/Imap.php';
require_once 'Zend/Mail/Storage/Imap.php';

$request = new Request();
Registry::set("request", $request);


session_start();

Registry::set('document', new Document());


$loader = new Loader();
Registry::set('load', $loader);


$language = new Language();
Registry::set('language', $language);


$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PREFIX);
Registry::set('DB_DATABASE', DB_DATABASE);

Registry::set('db', $db);

Registry::set('DB_DRIVER', DB_DRIVER);


$loader->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);
}
}

?>
5 changes: 3 additions & 2 deletions webui/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@
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);
Expand Down Expand Up @@ -176,7 +177,7 @@
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)
Expand Down
11 changes: 8 additions & 3 deletions webui/model/google/google.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function try_imap_login($email, $accessToken) {
public function download_users_emails($email, $accessToken) {
$last_msg_id = -1;
$from = 1;
$downloaded = 0;

if(!$email || !$accessToken) { return 0; }

Expand Down Expand Up @@ -82,11 +83,15 @@ public function download_users_emails($email, $accessToken) {
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";
}
Expand All @@ -97,7 +102,7 @@ public function download_users_emails($email, $accessToken) {
}



syslog(LOG_INFO, "downloaded $downloaded messages for $email");
}

}
Expand Down
25 changes: 25 additions & 0 deletions webui/model/user/google.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ public function update_tokens($email = '', $id = 0, $token = array()) {
}


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 '';
}


}

?>

0 comments on commit f2dd3b6

Please sign in to comment.