9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie
23 * Data structure for a hostmask.
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie
31 class Phergie_Hostmask
55 * Regular expression used to parse a hostmask
59 protected static $regex = '/^([^!@]+)!(?:[ni]=)?([^@]+)@([^ ]+)/';
62 * Constructor to initialize components of the hostmask.
64 * @param string $nick Nick component
65 * @param string $username Username component
66 * @param string $host Host component
70 public function __construct($nick, $username, $host)
73 $this->username = $username;
78 * Returns whether a given string appears to be a valid hostmask.
80 * @param string $string Alleged hostmask string
82 * @return bool TRUE if the string appears to be a valid hostmask, FALSE
85 public static function isValid($string)
87 return (preg_match(self::$regex, $string) > 0);
91 * Parses a string containing the entire hostmask into a new instance of
94 * @param string $hostmask Entire hostmask including the nick, username,
97 * @return Phergie_Hostmask New instance populated with data parsed from
98 * the provided hostmask string
99 * @throws Phergie_Hostmask_Exception
101 public static function fromString($hostmask)
103 if (preg_match(self::$regex, $hostmask, $match)) {
104 list(, $nick, $username, $host) = $match;
105 return new self($nick, $username, $host);
108 throw new Phergie_Hostmask_Exception(
109 'Invalid hostmask specified: "' . $hostmask . '"',
110 Phergie_Hostmask_Exception::ERR_INVALID_HOSTMASK
117 * @param string $host Hostname
119 * @return Phergie_Hostmask Provides a fluent interface
121 public function setHost($host)
129 * Returns the hostname.
133 public function getHost()
139 * Sets the username of the user.
141 * @param string $username Username
143 * @return Phergie_Hostmask Provides a fluent interface
145 public function setUsername($username)
147 $this->username = $username;
153 * Returns the username of the user.
157 public function getUsername()
159 return $this->username;
163 * Sets the nick of the user.
165 * @param string $nick User nick
167 * @return Phergie_Hostmask Provides a fluent interface
169 public function setNick($nick)
177 * Returns the nick of the user.
181 public function getNick()
187 * Returns the hostmask for the originating server or user.
191 public function __toString()
193 return $this->nick . '!' . $this->username . '@' . $this->host;
197 * Returns whether a given hostmask matches a given pattern.
199 * @param string $pattern Pattern using conventions of a ban mask where
200 * represents a wildcard
201 * @param string $hostmask Optional hostmask to match against, if not
202 * the current hostmask instance
204 * @return bool TRUE if the hostmask matches the pattern, FALSE otherwise
205 * @link http://irchelp.org/irchelp/rfc/chapter4.html#c4_2_3 Examples
207 public function matches($pattern, $hostmask = null)
210 $hostmask = (string) $this;
213 $pattern = str_replace('*', '.*', $pattern);
215 return (preg_match('#^' . $pattern . '$#', $hostmask) > 0);