3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Send and receive notices using an IRC network
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
37 // We bundle the Phergie library...
38 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/phergie');
45 * @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>
46 * @copyright 2010 StatusNet, Inc.
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
48 * @link http://status.net/
51 class IrcPlugin extends ImPlugin {
54 public $username = null;
55 public $realname = null;
57 public $password = null;
58 public $nickservidentifyregexp = null;
59 public $nickservpassword = null;
60 public $channels = null;
61 public $transporttype = null;
62 public $encoding = null;
64 public $regcheck = null;
65 public $unregregexp = null;
66 public $regregexp = null;
68 public $transport = 'irc';
72 * Get the internationalized/translated display name of this IM service
74 * @return string Name of service
76 public function getDisplayName() {
81 * Normalize a screenname for comparison
83 * @param string $screenname screenname to normalize
84 * @return string an equivalent screenname in normalized form
86 public function normalize($screenname) {
87 $screenname = str_replace(" ","", $screenname);
88 return strtolower($screenname);
92 * Get the screenname of the daemon that sends and receives messages
94 * @return string Screenname
96 public function daemon_screenname() {
101 * Validate (ensure the validity of) a screenname
103 * @param string $screenname screenname to validate
106 public function validate($screenname) {
107 if (preg_match('/\A[a-z0-9\-_]{1,1000}\z/i', $screenname)) {
115 * Load related modules when needed
117 * @param string $cls Name of the class to be loaded
118 * @return boolean hook value; true means continue processing, false means stop.
120 public function onAutoload($cls) {
121 $dir = dirname(__FILE__);
125 include_once $dir . '/'.strtolower($cls).'.php';
128 include_once $dir . '/'. $cls .'.php';
131 if (substr($cls, 0, 7) == 'Phergie') {
132 include_once str_replace('_', DIRECTORY_SEPARATOR, $cls) . '.php';
140 * Start manager on daemon start
144 public function onStartImDaemonIoManagers(&$classes) {
145 parent::onStartImDaemonIoManagers(&$classes);
146 $classes[] = new IrcManager($this); // handles sending/receiving
151 * Get a microid URI for the given screenname
153 * @param string $screenname
154 * @return string microid URI
156 public function microiduri($screenname) {
157 return 'irc:' . $screenname;
161 * Send a message to a given screenname
163 * @param string $screenname Screenname to send to
164 * @param string $body Text to send
165 * @return boolean success value
167 public function send_message($screenname, $body) {
168 $lines = explode("\n", $body);
169 foreach ($lines as $line) {
170 $this->fake_irc->doPrivmsg($screenname, $line);
171 $this->enqueue_outgoing_raw(array('type' => 'message', 'data' => $this->fake_irc->would_be_sent));
177 * Accept a queued input message.
179 * @return true if processing completed, false if message should be reprocessed
181 public function receive_raw_message($data) {
182 $this->handle_incoming($data['sender'], $data['message']);
187 * Send a confirmation code to a user
189 * @param string $screenname screenname sending to
190 * @param string $code the confirmation code
191 * @param User $user user sending to
192 * @return boolean success value
194 public function send_confirmation_code($screenname, $code, $user, $checked = false) {
195 $body = sprintf(_('User "%s" on %s has said that your %s screenname belongs to them. ' .
196 'If that\'s true, you can confirm by clicking on this URL: ' .
198 ' . (If you cannot click it, copy-and-paste it into the ' .
199 'address bar of your browser). If that user isn\'t you, ' .
200 'or if you didn\'t request this confirmation, just ignore this message.'),
201 $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', array('code' => $code)));
203 if ($this->regcheck && !$checked) {
204 return $this->checked_send_confirmation_code($screenname, $code, $user);
206 return $this->send_message($screenname, $body);
211 * Only sends the confirmation message if the nick is
214 * @param string $screenname screenname sending to
215 * @param string $code the confirmation code
216 * @param User $user user sending to
217 * @return boolean success value
219 public function checked_send_confirmation_code($screenname, $code, $user) {
220 $this->fake_irc->doPrivmsg('NickServ', 'INFO '.$screenname);
221 $this->enqueue_outgoing_raw(
223 'type' => 'nickcheck',
224 'data' => $this->fake_irc->would_be_sent,
227 'screenname' => $screenname,
241 public function initialize() {
242 if (!isset($this->host)) {
243 throw new Exception('must specify a host');
245 if (!isset($this->username)) {
246 throw new Exception('must specify a username');
248 if (!isset($this->realname)) {
249 throw new Exception('must specify a "real name"');
251 if (!isset($this->nick)) {
252 throw new Exception('must specify a nickname');
255 if (!isset($this->port)) {
258 if (!isset($this->transporttype)) {
259 $this->transporttype = 'tcp';
261 if (!isset($this->encoding)) {
262 $this->encoding = 'UTF-8';
265 if (!isset($this->regcheck)) {
266 $this->regcheck = true;
269 $this->fake_irc = new Fake_Irc;
274 * Get plugin information
276 * @param array $versions array to insert information into
279 public function onPluginVersion(&$versions) {
280 $versions[] = array('name' => 'IRC',
281 'version' => STATUSNET_VERSION,
282 'author' => 'Luke Fitzgerald',
283 'homepage' => 'http://status.net/wiki/Plugin:IRC',
285 _m('The IRC plugin allows users to send and receive notices over an IRC network.'));