Skip to content

Commit

Permalink
Merge pull request #11 from manilal/0016835
Browse files Browse the repository at this point in the history
Feature to add users to issue monitoring list #16835
  • Loading branch information
SL-Gundam committed Jan 29, 2014
2 parents 852a727 + 51139b9 commit d15443e
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 82 deletions.
3 changes: 3 additions & 0 deletions EmailReporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ function config()

//Strip Gmail style replies from body of the message
'mail_strip_gmail_style_replies' => OFF,

// Add users from Cc and To field in mail header
'mail_add_users_from_cc_to' => OFF,
);
}

Expand Down
37 changes: 37 additions & 0 deletions core/Mail/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ERP_Mail_Parser
private $_body;
private $_parts = array();
private $_ctype = array();
private $_cc = array();
private $_to = array();

private $_mb_list_encodings = array();

Expand Down Expand Up @@ -230,6 +232,16 @@ public function from()
return( $this->_from );
}

public function to()
{
return( $this->_to );
}

public function cc()
{
return( $this->_cc );
}

public function subject()
{
return( $this->_subject );
Expand Down Expand Up @@ -275,6 +287,13 @@ private function parseStructure( &$structure )
{
$this->setParts( $structure->parts );
}

$this->setTo( $structure->headers['to'] );

if ( isset( $structure->headers['cc'] ) )
{
$this->setCc( $structure->headers['cc'] );
}
}

private function setFrom( $from )
Expand All @@ -287,6 +306,24 @@ private function setSubject( $subject )
$this->_subject = $this->process_header_encoding( $subject );
}

private function setTo( $p_to )
{
$regex = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})';
if( preg_match_all ("/".$regex."/is", $p_to, $matches) )
{
$this->_to = $matches[1];
}
}

private function setCc( $p_cc )
{
$regex = '([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})';
if( preg_match_all ("/".$regex."/is", $p_cc, $matches) )
{
$this->_cc = $matches[1];
}
}

private function setPriority( $priority )
{
$this->_priority = $priority;
Expand Down
215 changes: 133 additions & 82 deletions core/mail_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class ERP_mailbox_api
private $_mail_subject_id_regex;
private $_mail_use_bug_priority;
private $_mail_use_reporter;
private $_mail_add_users_from_cc_to;

private $_mp_options = array();

Expand Down Expand Up @@ -120,6 +121,7 @@ public function __construct( $p_test_only = FALSE )
$this->_mail_subject_id_regex = plugin_config_get( 'mail_subject_id_regex' );
$this->_mail_use_bug_priority = plugin_config_get( 'mail_use_bug_priority' );
$this->_mail_use_reporter = plugin_config_get( 'mail_use_reporter' );
$this->_mail_add_users_from_cc_to = plugin_config_get( 'mail_add_users_from_cc_to' );

$this->_mp_options[ 'add_attachments' ] = config_get( 'allow_file_upload' );
$this->_mp_options[ 'debug' ] = $this->_mail_debug;
Expand Down Expand Up @@ -572,6 +574,9 @@ private function parse_content( &$p_msg )

$t_email[ 'Subject' ] = trim( $t_mp->subject() );

$t_email[ 'To' ] = $t_mp->to();
$t_email[ 'Cc' ] = $t_mp->cc();

$t_email[ 'X-Mantis-Body' ] = trim( $t_mp->body() );

$t_email[ 'X-Mantis-Parts' ] = $t_mp->parts();
Expand Down Expand Up @@ -599,88 +604,88 @@ private function parse_content( &$p_msg )

# --------------------
# return the user id for the mail reporting user
private function get_user( $p_parsed_from )
{
if ( $this->_mail_use_reporter )
{
// Always report as mail_reporter
$t_reporter_id = $this->_mail_reporter_id;
}
else
{
// Try to get the reporting users id
$t_reporter_id = $this->get_userid_from_email( $p_parsed_from[ 'email' ] );

if ( !$t_reporter_id )
{
if ( $this->_mail_auto_signup )
{
// So, we have to sign up a new user...
$t_new_reporter_name = $this->prepare_username( $p_parsed_from );

if ( $t_new_reporter_name !== FALSE && $this->validate_email_address( $p_parsed_from[ 'email' ] ) )
{
if( user_signup( $t_new_reporter_name, $p_parsed_from[ 'email' ] ) )
{
# notify the selected group a new user has signed-up
email_notify_new_account( $t_new_reporter_name, $p_parsed_from[ 'email' ] );

$t_reporter_id = user_get_id_by_email( $p_parsed_from[ 'email' ] );
$t_reporter_name = $t_new_reporter_name;

$t_realname = $this->prepare_realname( $p_parsed_from, $t_reporter_name );

if ( $t_realname !== FALSE )
{
user_set_realname( $t_reporter_id, $t_realname );
}
}
}

if ( !$t_reporter_id )
{
$this->custom_error( 'Failed to create user based on: ' . $p_parsed_from[ 'From' ] );
}
}
}

if ( ( !$t_reporter_id || !user_is_enabled( $t_reporter_id ) ) && $this->_mail_fallback_mail_reporter )
{
// Fall back to the default mail_reporter
$t_reporter_id = $this->_mail_reporter_id;
}
}

if ( $t_reporter_id && user_is_enabled( $t_reporter_id ) )
{
if ( !isset( $t_reporter_name ) )
{
$t_reporter_name = user_get_field( $t_reporter_id, 'username' );
}

$t_authattemptresult = auth_attempt_script_login( $t_reporter_name );

# last attempt for fallback
if ( $t_authattemptresult === FALSE && $this->_mail_fallback_mail_reporter && $t_reporter_id != $this->_mail_reporter_id && user_is_enabled( $this->_mail_reporter_id ) )
{
$t_reporter_id = $this->_mail_reporter_id;
$t_reporter_name = user_get_field( $t_reporter_id, 'username' );
$t_authattemptresult = auth_attempt_script_login( $t_reporter_name );
}

if ( $t_authattemptresult === TRUE )
{
user_update_last_visit( $t_reporter_id );

return( (int) $t_reporter_id );
}
}

// Normally this function does not get here unless all else failed
$this->custom_error( 'Could not get a valid reporter. Email will be ignored' );

return( FALSE );
}
private function get_user( $p_parsed_from )
{
if ( $this->_mail_use_reporter )
{
// Always report as mail_reporter
$t_reporter_id = $this->_mail_reporter_id;
}
else
{
// Try to get the reporting users id
$t_reporter_id = $this->get_userid_from_email( $p_parsed_from[ 'email' ] );

if ( !$t_reporter_id )
{
if ( $this->_mail_auto_signup )
{
// So, we have to sign up a new user...
$t_new_reporter_name = $this->prepare_username( $p_parsed_from );

if ( $t_new_reporter_name !== FALSE && $this->validate_email_address( $p_parsed_from[ 'email' ] ) )
{
if( user_signup( $t_new_reporter_name, $p_parsed_from[ 'email' ] ) )
{
# notify the selected group a new user has signed-up
email_notify_new_account( $t_new_reporter_name, $p_parsed_from[ 'email' ] );

$t_reporter_id = user_get_id_by_email( $p_parsed_from[ 'email' ] );
$t_reporter_name = $t_new_reporter_name;

$t_realname = $this->prepare_realname( $p_parsed_from, $t_reporter_name );

if ( $t_realname !== FALSE )
{
user_set_realname( $t_reporter_id, $t_realname );
}
}
}

if ( !$t_reporter_id )
{
$this->custom_error( 'Failed to create user based on: ' . $p_parsed_from[ 'From' ] );
}
}
}

if ( ( !$t_reporter_id || !user_is_enabled( $t_reporter_id ) ) && $this->_mail_fallback_mail_reporter )
{
// Fall back to the default mail_reporter
$t_reporter_id = $this->_mail_reporter_id;
}
}

if ( $t_reporter_id && user_is_enabled( $t_reporter_id ) )
{
if ( !isset( $t_reporter_name ) )
{
$t_reporter_name = user_get_field( $t_reporter_id, 'username' );
}

$t_authattemptresult = auth_attempt_script_login( $t_reporter_name );

# last attempt for fallback
if ( $t_authattemptresult === FALSE && $this->_mail_fallback_mail_reporter && $t_reporter_id != $this->_mail_reporter_id && user_is_enabled( $this->_mail_reporter_id ) )
{
$t_reporter_id = $this->_mail_reporter_id;
$t_reporter_name = user_get_field( $t_reporter_id, 'username' );
$t_authattemptresult = auth_attempt_script_login( $t_reporter_name );
}

if ( $t_authattemptresult === TRUE )
{
user_update_last_visit( $t_reporter_id );

return( (int) $t_reporter_id );
}
}

// Normally this function does not get here unless all else failed
$this->custom_error( 'Could not get a valid reporter. Email will be ignored' );

return( FALSE );
}

# --------------------
# Try to obtain an existing userid based on an email address
Expand Down Expand Up @@ -923,6 +928,9 @@ private function add_bug( &$p_email, $p_overwrite_project_id = FALSE )
}
}

//Add the users in Cc and To list in mail header
$this->add_monitors( $t_bug_id, $p_email );

ERP_set_temporary_overwrite( 'project_override', NULL );

$this->show_memory_usage( 'Finished processing attachments' );
Expand Down Expand Up @@ -1380,6 +1388,49 @@ private function show_memory_usage( $p_location )
}
}

// --------------------
// Add monitors from Cc and To fields in mail header
private function add_monitors( $p_bug_id, $p_email )
{
if ( $this->_mail_add_users_from_cc_to)
{
$t_emails = array_merge($p_email[ 'To' ], $p_email[ 'Cc' ] );

print_r($t_emails);
foreach( $t_emails as $t_email )
{
$t_user_id = $this->get_userid_from_email( $t_email );
print $t_email.": ".$t_user_id. "<hr>" ;
if( $t_user_id !== FALSE)
{
// Make sure that mail_reporter_id and reporter_id are not added as a monitors.
if( ($this->_mail_reporter_id != $t_user_id) && ($p_email['Reporter_id'] != $t_user_id) )
{
bug_monitor( $p_bug_id, $t_user_id );
}
}
}
}
}


# --------------------
# This function formats the bytes so that they are easily readable.
# Not part of a class
function ERP_formatbytes( $p_bytes )
{
$t_units = array( ' B', ' KiB', ' MiB', ' GiB', ' TiB' );

$t_bytes = $p_bytes;

for ( $i = 0; $t_bytes > 1024; $i++ )
{
$t_bytes /= 1024;
}

return( round( $t_bytes, 2 ) . $t_units[ $i ] );
}

/**
* Gets the username from LDAP given the email address
*
Expand Down
1 change: 1 addition & 0 deletions doc/CHANGELOG.bug_report_mail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Nov 2013 - EmailReporting-0.9.0-DEV
- Parser fix for uppercase ctypes (based on work from rolfkleef)
- Attachment blocking added based on a md5 hash list
- Added feature to remove Gmail style replies in notes
- Added feature to add users from Cc and To list of mail header to issue monitoring list

Nov 2010 - EmailReporting-0.8.4
- Improved the project category list
Expand Down
2 changes: 2 additions & 0 deletions lang/strings_english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ $s_plugin_EmailReporting_mail_preferred_realname = 'Preferred realname for new u
$s_plugin_EmailReporting_mail_remove_mantis_email = 'Remove MantisBT notification emails from replies.';
$s_plugin_EmailReporting_mail_remove_replies = 'Remove all replies from notes';
$s_plugin_EmailReporting_mail_strip_gmail_style_replies = 'Remove Gmail style replies (English only) from notes (mail_remove_replies should be YES)';
$s_plugin_EmailReporting_mail_add_users_from_cc_to = 'Add users to issue monitoring list from Cc and To fields in mail header';

$s_plugin_EmailReporting_mail_remove_replies_after = 'Use this text to identify the start of a reply';
$s_plugin_EmailReporting_mail_removed_reply_text = 'Use this text if replies have been removed from the email';
$s_plugin_EmailReporting_mail_reporter_id = 'The default / fallback reporter user for issues created by email';
Expand Down
1 change: 1 addition & 0 deletions pages/manage_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
ERP_output_config_option( 'mail_save_from', 'boolean' );
ERP_output_config_option( 'mail_save_subject_in_note', 'boolean' );
ERP_output_config_option( 'mail_subject_id_regex', 'dropdown', NULL, 'print_descriptions_option_list', array( 'strict', 'balanced', 'relaxed' ) );
ERP_output_config_option( 'mail_add_users_from_cc_to', 'boolean' );

ERP_output_config_option( NULL, 'empty' );
ERP_output_config_option( 'priority_feature_options', 'header' );
Expand Down
1 change: 1 addition & 0 deletions pages/manage_config_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'mail_subject_id_regex' => gpc_get_string( 'mail_subject_id_regex' ),
'mail_use_bug_priority' => gpc_get_int( 'mail_use_bug_priority' ),
'mail_use_reporter' => gpc_get_int( 'mail_use_reporter' ),
'mail_add_users_from_cc_to' => gpc_get_int( 'mail_add_users_from_cc_to' ),
);

$f_mail_bug_priority = 'array (' . "\n" . gpc_get_string( 'mail_bug_priority' ) . "\n" . ')';
Expand Down

0 comments on commit d15443e

Please sign in to comment.