]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
jabber send and confirmation code
authorEvan Prodromou <evan@controlezvous.ca>
Tue, 24 Jun 2008 01:32:03 +0000 (21:32 -0400)
committerEvan Prodromou <evan@controlezvous.ca>
Tue, 24 Jun 2008 01:32:03 +0000 (21:32 -0400)
darcs-hash:20080624013203-34904-fe87dc6b3b991e9753512b8b7cc192f7610eb1dc.gz

actions/imsettings.php
doc/README
lib/jabber.php
lib/mail.php

index d8680c98b4dc4daa98a797dd104142f982cb0147..2fa74514a25826a5096a186872affb1bbb8cf5a0 100644 (file)
@@ -82,12 +82,11 @@ class ImsettingsAction extends SettingsAction {
                $user->query('BEGIN');
 
                $original = clone($user);
-
-               $user->jabber = $jabber;
+               
                $user->jabbernotify = $jabbernotify;
                $user->updatefrompresence = $updatefrompresence;
 
-               $result = $user->updateKeys($original); # For key columns
+               $result = $user->update($original); # For key columns
 
                if ($result === FALSE) {
                        common_log_db_error($user, 'UPDATE', __FILE__);
@@ -95,17 +94,58 @@ class ImsettingsAction extends SettingsAction {
                        return;
                }
 
-               $result = $user->update($original); # For non-key columns
-
-               if ($result === FALSE) {
-                       common_log_db_error($user, 'UPDATE', __FILE__);
-                       common_server_error(_t('Couldnt update user.'));
-                       return;
+               $confirmation_sent = false;
+               
+               if ($user->jabber != $jabber) {
+                       
+                       if ($jabber) {
+                       $confirm = new Confirm_address();
+                       $confirm->address = $jabber;
+                       $confirm->address_type = 'jabber';
+                       $confirm->user_id = $user->id;
+                       $confirm->code = common_confirmation_code(64);
+           
+                               $result = $confirm->insert();
+
+                               if ($result === FALSE) {
+                                       common_log_db_error($confirm, 'INSERT', __FILE__);
+                                       common_server_error(_t('Couldnt insert confirmation code.'));
+                                       return;
+                               }
+                               
+                               # XXX: optionally queue for offline sending
+                               
+                               jabber_confirm_address($confirm->code,
+                                                                          $user->nickname,
+                                                                          $jabber);
+                                                                          
+                               if ($result === FALSE) {
+                                       common_log_db_error($confirm, 'INSERT', __FILE__);
+                                       common_server_error(_t('Couldnt insert confirmation code.'));
+                                       return;
+                               }
+                               
+                               $confirmation_sent = false;
+                       } else {
+                               # Clearing the ID is free
+                               $user->jabber = NULL;
+                               $result = $user->updateKeys($original);
+                               if ($result === FALSE) {
+                                       common_log_db_error($user, 'UPDATE', __FILE__);
+                                       common_server_error(_t('Couldnt update user.'));
+                                       return;
+                               }
+                       }
                }
-
+               
                $user->query('COMMIT');
 
-               $this->show_form(_t('Settings saved.'), TRUE);
+        $msg = ($confirmation_sent) ? 
+                                 _t('Settings saved. A confirmation code was ' .
+                                    ' sent to the IM address you added. ') :
+                                 _t('Settings saved.');
+                                 
+               $this->show_form($msg, TRUE);
        }
 
        function jabber_exists($jabber) {
index 070b1ec8183e657a6339e722e425c20fdd44dfc0..e34d8a2921896ffda128309c2c604682eba522e8 100644 (file)
@@ -13,5 +13,9 @@ This package requires PHP 5.x and the following PHP Pear libraries:
 - OAuth.php from http://oauth.googlecode.com/svn/code/php/
 - markdown.php from http://michelf.com/projects/php-markdown/ 
 - PEAR Mail, for sending out mail notifications
+  http://pear.php.net/package/Mail
 - PEAR Net_SMTP, if you use the SMTP factory for notifications
+  http://pear.php.net/package/Net_SMTP
+- xmpphp, the follow-up to Class.Jabber.php. Probably the best XMPP
+  library available for PHP. http://xmpphp.googlecode.com/
 
index 141b9231ff1c7bd93b28c7433c0b8137370a8462..262fbcbb907321dc5e81b316a4e3408dfc532f7d 100644 (file)
@@ -25,9 +25,75 @@ function jabber_valid_base_jid($jid) {
 }
 
 function jabber_normalize_jid($jid) {
-               preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches);
-               $node = $matches[1];
-               $server = $matches[2];
-               $resource = $matches[3];
-               return strtolower($node.'@'.$server);
+       preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $jid, $matches);
+       $node = $matches[1];
+       $server = $matches[2];
+       $resource = $matches[3];
+       return strtolower($node.'@'.$server);
 }
+
+function jabber_connect($resource=NULL) {
+       static $conn = NULL;
+       if (!$conn) {
+               $conn = new XMPP(common_config('xmpp', 'server'),
+                                            common_config('xmpp', 'port'),
+                                            common_config('xmpp', 'user'),
+                                            common_config('xmpp', 'password'),
+                                        ($resource) ? $resource : 
+                                               common_config('xmpp', 'resource'));
+                                       
+               if (!$conn) {
+                       return false;
+               }
+               $conn->connect(true); # try to get a persistent connection
+               if ($conn->disconnected) {
+                       return false;
+               }
+        $conn->processUntil('session_start');
+       }
+       return $conn;
+}
+
+function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
+       $conn = jabber_connect();
+       if (!$conn) {
+               return false;
+       }
+       $conn->message($to, $body, $type, $subject);
+       return true;
+}
+
+function jabber_send_presence($status=Null, $show='available', $to=Null) {
+       $conn = jabber_connect();
+       if (!$conn) {
+               return false;
+       }
+       $conn->presence($status, $show, $to);
+       return true;
+}
+
+function jabber_confirm_address($code, $nickname, $address) {
+
+       # FIXME: do we have to request presence first?
+       
+       $body = "Hey, $nickname.";
+       $body .= "\n\n";
+       $body .= 'Someone just entered this IM address on ';
+       $body .= common_config('site', 'name') . '.';
+       $body .= "\n\n";
+       $body .= 'If it was you, and you want to confirm your entry, ';
+       $body .= 'use the URL below:';
+       $body .= "\n\n";
+       $body .= "\t".common_local_url('confirmaddress',
+                                                                  array('code' => $code));
+       $body .= "\n\n";
+       $body .= 'If not, just ignore this message.';
+       $body .= "\n\n";
+       $body .= 'Thanks for your time, ';
+       $body .= "\n";
+       $body .= common_config('site', 'name');
+       $body .= "\n";
+
+       jabber_send_message($address, $body);
+}
+
index 53b360e3c0a64f5745c2ecf6919c19dbb64228c1..91eafa97ee28cf16dd6450cdfcae34e392e98a71 100644 (file)
@@ -55,7 +55,7 @@ function mail_notify_from() {
        }
 }
 
-# For confirming an email address
+# For confirming a Jabber address
 
 function mail_confirm_address($code, $nickname, $address) {
        $recipients = $address;