/**
 * If user has confirmed and allowed address email
 * then add him/her to privatedomains user group.
 */
function pd_UserLoginComplete( $user ) {
	if( $user->isEmailConfirmed() ) {
		$domainsStr = PrivateDomains::getParam( 'privatedomains-domains' );
		if( $domainsStr != '' ) {
			$email = strtolower( $user->mEmail );
			// get suffix domain name
			preg_match( "/([^@]+)@(.+)$/i", $email, $matches );
			$emailDomain = $matches[2];
			$domainsArr = explode( "\n", $domainsStr );
			foreach ( $domainsArr as $allowedDomain ) {
				$allowedDomain = strtolower( $allowedDomain );
				if ( preg_match( "/.*?$allowedDomain$/", $emailDomain ) ) {
					$user->addGroup( 'privatedomains' );
					return true;
				}
			}
		}
	}
	$user->removeGroup( 'privatedomains' );
	return true;
}
	/**
	 * Custom version of SpecialPage::displayRestrictionError for PrivateDomains.
	 * This is OutputPage::permissionRequired with some modifications.
	 * The big change here is that we display 'privatedomains-ifcontact'
	 * message if user doesn't have the permission to access the special page.
	 */
	function displayRestrictionError() {
		global $wgUser, $wgLang, $wgOut;

		$wgOut->setPageTitle( wfMsgHtml( 'badaccess' ) );
		$wgOut->setHTMLTitle( wfMsgHtml( 'errorpagetitle' ) );
		$wgOut->setRobotPolicy( 'noindex,nofollow' );
		$wgOut->setArticleRelated( false );
		$wgOut->mBodytext = '';

		$groups = array_map( array( 'User', 'makeGroupLinkWiki' ),
			User::getGroupsWithPermission( $this->mRestriction ) );
		$privatedomains_emailadmin = PrivateDomains::getParam( 'privatedomains-emailadmin' );
		if( $groups ) {
			$wgOut->addWikiMsg( 'badaccess-groups',
				$wgLang->commaList( $groups ),
				count( $groups ) );
			if( $privatedomains_emailadmin != '' ) {
				$wgOut->addWikiMsg( 'privatedomains-ifemailcontact', $privatedomains_emailadmin );
			}
		} else {
			$wgOut->addWikiMsg( 'badaccess-group0' );
		}
		$wgOut->returnToMain();
	}