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 $nickservpassword = null;
59 public $channels = null;
60 public $transporttype = null;
61 public $encoding = null;
63 public $regcheck = null;
65 public $transport = 'irc';
69 * Get the internationalized/translated display name of this IM service
71 * @return string Name of service
73 public function getDisplayName() {
78 * Normalize a screenname for comparison
80 * @param string $screenname screenname to normalize
81 * @return string an equivalent screenname in normalized form
83 public function normalize($screenname) {
84 $screenname = str_replace(" ","", $screenname);
85 return strtolower($screenname);
89 * Get the screenname of the daemon that sends and receives messages
91 * @return string Screenname
93 public function daemon_screenname() {
98 * Validate (ensure the validity of) a screenname
100 * @param string $screenname screenname to validate
103 public function validate($screenname) {
104 if (preg_match('/\A[a-z0-9\-_]{1,1000}\z/i', $screenname)) {
112 * Load related modules when needed
114 * @param string $cls Name of the class to be loaded
115 * @return boolean hook value; true means continue processing, false means stop.
117 public function onAutoload($cls) {
118 $dir = dirname(__FILE__);
122 include_once $dir . '/'.strtolower($cls).'.php';
125 include_once $dir . '/'. $cls .'.php';
128 if (substr($cls, 0, 7) == 'Phergie') {
129 include_once str_replace('_', DIRECTORY_SEPARATOR, $cls) . '.php';
137 * Start manager on daemon start
141 public function onStartImDaemonIoManagers(&$classes) {
142 parent::onStartImDaemonIoManagers(&$classes);
143 $classes[] = new IrcManager($this); // handles sending/receiving
148 * Get a microid URI for the given screenname
150 * @param string $screenname
151 * @return string microid URI
153 public function microiduri($screenname) {
154 return 'irc:' . $screenname;
158 * Send a message to a given screenname
160 * @param string $screenname Screenname to send to
161 * @param string $body Text to send
162 * @return boolean success value
164 public function send_message($screenname, $body) {
165 $this->fake_irc->doPrivmsg($screenname, $body);
166 $this->enqueue_outgoing_raw(array('type' => 'message', 'data' => $this->fake_irc->would_be_sent));
171 * Accept a queued input message.
173 * @return true if processing completed, false if message should be reprocessed
175 public function receive_raw_message($data) {
176 $this->handle_incoming($data['sender'], $data['message']);
181 * Send a confirmation code to a user
183 * @param string $screenname screenname sending to
184 * @param string $code the confirmation code
185 * @param User $user user sending to
186 * @return boolean success value
188 public function send_confirmation_code($screenname, $code, $user, $checked = false) {
189 $body = sprintf(_('User "%s" on %s has said that your %s screenname belongs to them. ' .
190 'If that\'s true, you can confirm by clicking on this URL: ' .
192 ' . (If you cannot click it, copy-and-paste it into the ' .
193 'address bar of your browser). If that user isn\'t you, ' .
194 'or if you didn\'t request this confirmation, just ignore this message.'),
195 $user->nickname, common_config('site', 'name'), $this->getDisplayName(), common_local_url('confirmaddress', array('code' => $code)));
197 if ($this->regcheck && !$checked) {
198 return $this->checked_send_confirmation_code($screenname, $code, $user);
200 return $this->send_message($screenname, $body);
205 * Only sends the confirmation message if the nick is
208 * @param string $screenname screenname sending to
209 * @param string $code the confirmation code
210 * @param User $user user sending to
211 * @return boolean success value
213 public function checked_send_confirmation_code($screenname, $code, $user) {
214 $this->fake_irc->doPrivmsg('NickServ', 'INFO '.$screenname);
215 $this->enqueue_outgoing_raw(
217 'type' => 'nickcheck',
218 'data' => $this->fake_irc->would_be_sent,
221 'screenname' => $screenname,
235 public function initialize() {
236 if (!isset($this->host)) {
237 throw new Exception('must specify a host');
239 if (!isset($this->username)) {
240 throw new Exception('must specify a username');
242 if (!isset($this->realname)) {
243 throw new Exception('must specify a "real name"');
245 if (!isset($this->nick)) {
246 throw new Exception('must specify a nickname');
249 if (!isset($this->port)) {
252 if (!isset($this->password)) {
253 $this->password = '';
255 if (!isset($this->transporttype)) {
256 $this->transporttype = 'tcp';
258 if (!isset($this->encoding)) {
259 $this->encoding = 'UTF-8';
261 if (!isset($this->nickservpassword)) {
262 $this->nickservpassword = '';
264 if (!isset($this->channels)) {
265 $this->channels = array();
268 if (!isset($this->regcheck)) {
269 $this->regcheck = true;
272 $this->fake_irc = new Fake_Irc;
277 * Get plugin information
279 * @param array $versions array to insert information into
282 public function onPluginVersion(&$versions) {
283 $versions[] = array('name' => 'IRC',
284 'version' => STATUSNET_VERSION,
285 'author' => 'Luke Fitzgerald',
286 'homepage' => 'http://status.net/wiki/Plugin:IRC',
288 _m('The IRC plugin allows users to send and receive notices over an IRC network.'));