Skip to content

Commit

Permalink
Add JMAP server management issue #180
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmunro committed Jan 25, 2019
1 parent 5009bdd commit 0453cf3
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 55 deletions.
3 changes: 3 additions & 0 deletions modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class Hm_IMAP extends Hm_IMAP_Cache {
/* query the server for it's CAPABILITY response */
public $no_caps = false;

/* server type */
public $server_type = 'IMAP';

/* IMAP ID client information */
public $app_name = 'Hm_IMAP';
public $app_version = '3.0';
Expand Down
36 changes: 16 additions & 20 deletions modules/imap/hm-jmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Hm_JMAP {
public $folder_state = array();
public $use_cache = true;
public $read_only = false;
public $server_type = 'JMAP';

/**
* PUBLIC INTERFACE
Expand Down Expand Up @@ -279,7 +280,7 @@ public function get_state() {
public function show_debug() {
return array(
'commands' => $this->requests,
'responses' => $htis->responses
'responses' => $this->responses
);
}

Expand Down Expand Up @@ -396,7 +397,6 @@ public function connect($cfg) {
$cfg['username'],
$cfg['password'],
$cfg['server'],
$cfg['port']
);
}

Expand All @@ -413,22 +413,21 @@ public function disconnect() {
* @param string $username user to login
* @param string $password user password
* @param string $url JMAP url
* @param integer $port JMAP port
* @return boolean
*/
public function authenticate($username, $password, $url, $port) {
public function authenticate($username, $password, $url) {
if (is_array($this->session)) {
$res = $this->session;
}
else {
$auth_url = $this->prep_url($url, $port);
$auth_url = $this->prep_url($url);
$res = $this->send_command($auth_url, array(), 'GET');
}
if (is_array($res) &&
array_key_exists('apiUrl', $res) &&
array_key_exists('accounts', $res)) {

$this->init_session($res, $url, $port);
$this->init_session($res, $url);
return true;
}
return false;
Expand Down Expand Up @@ -835,30 +834,28 @@ private function normalize_headers($msg) {
* Start a JMAP session
* @param array $data JMAP auth response
* @param string $url url to access JMAP
* @param integer $port port to access JMAP
* @return void
*/
private function init_session($data, $url, $port) {
private function init_session($data, $url) {
$this->state = 'authenticated';
$this->session = $data;
$this->api_url = sprintf(
'%s:%s%s',
'%s%s',
preg_replace("/\/$/", '', $url),
$port,
$data['apiUrl']
);
$this->download_url = sprintf(
'%s:%s%s',
'%s%s',
preg_replace("/\/$/", '', $url),
$port,
$data['downloadUrl']
);
$this->upload_url = sprintf(
'%s:%s%s',
'%s%s',
preg_replace("/\/$/", '', $url),
$port,
$data['uploadUrl']
);
/* TODO: get account listed as "primary" */
/* TODO: support > 1 account from a JMAP source */
$this->account_id = array_keys($data['accounts'])[0];
if (count($this->folder_list) == 0) {
$this->reset_folders();
Expand Down Expand Up @@ -943,6 +940,9 @@ private function search_response($data, $key_path, $default=false) {
$data = $data[$key];
}
else {
Hm_Debug::add('Failed to find key path in response');
Hm_Debug::add('key path: '.print_r($keypath, true));
Hm_Debug::add('data: '.print_r($data, true));
return $default;
}
}
Expand Down Expand Up @@ -995,15 +995,11 @@ private function build_headers($user, $pass) {
/**
* Prep a URL for JMAP discover
* @param string $url JMAP url
* @param integer $port JMAP port
* @return string
*/
private function prep_url($url, $port) {
private function prep_url($url) {
$url = preg_replace("/\/$/", '', $url);
if ($port == 80 || $port == 443) {
return sprintf('%s/.well-known/jmap/', $url);
}
return sprintf('%s:%s/.well-known/jmap/', $url, $port);
return sprintf('%s/.well-known/jmap/', $url);
}

/**
Expand Down
Loading

0 comments on commit 0453cf3

Please sign in to comment.