Code

Add gui code for registration
authorishmal <ishmal@users.sourceforge.net>
Wed, 24 May 2006 02:13:24 +0000 (02:13 +0000)
committerishmal <ishmal@users.sourceforge.net>
Wed, 24 May 2006 02:13:24 +0000 (02:13 +0000)
src/pedro/pedrogui.cpp
src/pedro/pedrogui.h
src/pedro/pedroxmpp.cpp
src/pedro/pedroxmpp.h

index 0ef33b5c21c80201e90c4e3ed6b691b20220ad2f..b50791e25aece3ccafd12331645c732718210b1e 100644 (file)
@@ -1197,6 +1197,109 @@ bool ConnectDialog::doSetup()
     return true;
 }
 
+
+
+
+//#########################################################################
+//# P A S S W O R D      D I A L O G
+//#########################################################################
+
+
+void PasswordDialog::okCallback()
+{
+    Glib::ustring pass     = passField.get_text();
+    Glib::ustring newpass  = newField.get_text();
+    Glib::ustring confpass = confField.get_text();
+    if ((pass.size()     < 6 || pass.size()    > 12 ) ||
+        (newpass.size()  < 6 || newpass.size() > 12 ) ||
+        (confpass.size() < 6 || confpass.size()> 12 ))
+        {
+        Gtk::MessageDialog dlg(*this, "Password must be 6 to 12 characters",
+            false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
+        dlg.run();
+        }
+    else if (newpass != confpass)
+        {
+        Gtk::MessageDialog dlg(*this, "New password and confirmation do not match",
+            false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
+        dlg.run();
+        }
+    else
+        {
+        response(Gtk::RESPONSE_OK);
+        hide();
+        }
+}
+
+void PasswordDialog::cancelCallback()
+{
+    response(Gtk::RESPONSE_CANCEL);
+    hide();
+}
+
+
+bool PasswordDialog::doSetup()
+{
+    set_title("Change Password");
+    set_size_request(300,200);
+
+    Glib::RefPtr<Gtk::ActionGroup> actionGroup = Gtk::ActionGroup::create();
+    actionGroup->add( Gtk::Action::create("MenuFile", "_File") );
+    actionGroup->add( Gtk::Action::create("Change", Gtk::Stock::OK, "Change Password"),
+    sigc::mem_fun(*this, &PasswordDialog::okCallback) );
+    actionGroup->add( Gtk::Action::create("Cancel",  Gtk::Stock::CANCEL, "Cancel"),
+    sigc::mem_fun(*this, &PasswordDialog::cancelCallback) );
+
+
+    Glib::RefPtr<Gtk::UIManager> uiManager = Gtk::UIManager::create();
+
+    uiManager->insert_action_group(actionGroup, 0);
+    add_accel_group(uiManager->get_accel_group());
+
+    Glib::ustring ui_info =
+        "<ui>"
+        "  <menubar name='MenuBar'>"
+        "    <menu action='MenuFile'>"
+        "      <menuitem action='Change'/>"
+        "      <separator/>"
+        "      <menuitem action='Cancel'/>"
+        "    </menu>"
+        "  </menubar>"
+        "</ui>";
+
+    uiManager->add_ui_from_string(ui_info);
+    Gtk::Widget* pMenuBar = uiManager->get_widget("/MenuBar");
+    get_vbox()->pack_start(*pMenuBar, Gtk::PACK_SHRINK);
+
+    table.resize(3, 2);
+    get_vbox()->pack_start(table);
+
+    passLabel.set_text("Current Password");
+    table.attach(passLabel, 0, 1, 0, 1);
+    passField.set_visibility(false);
+    passField.set_text(parent.client.getPassword());
+    table.attach(passField, 1, 2, 0, 1);
+
+    newLabel.set_text("New Password");
+    table.attach(newLabel, 0, 1, 1, 2);
+    newField.set_visibility(false);
+    table.attach(newField, 1, 2, 1, 2);
+
+    confLabel.set_text("Confirm New Password");
+    table.attach(confLabel, 0, 1, 2, 3);
+    confField.set_visibility(false);
+    confField.signal_activate().connect(
+           sigc::mem_fun(*this, &PasswordDialog::okCallback) );
+    table.attach(confField, 1, 2, 2, 3);
+
+    add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
+    add_button(Gtk::Stock::OPEN,   Gtk::RESPONSE_OK);
+
+    show_all_children();
+
+    return true;
+}
+
 //#########################################################################
 //# C H A T    D I A L O G
 //#########################################################################
@@ -1773,6 +1876,8 @@ void PedroGui::doEvent(const XmppEvent &event)
             actionEnable("Chat",       true);
             actionEnable("GroupChat",  true);
             actionEnable("Disconnect", true);
+            actionEnable("RegPass",    true);
+            actionEnable("RegCancel",  true);
             DOMString title = "Pedro - ";
             title.append(client.getJid());
             set_title(title);
@@ -1785,6 +1890,8 @@ void PedroGui::doEvent(const XmppEvent &event)
             actionEnable("Chat",       false);
             actionEnable("GroupChat",  false);
             actionEnable("Disconnect", false);
+            actionEnable("RegPass",    false);
+            actionEnable("RegCancel",  false);
             DOMString title = "Pedro";
             set_title(title);
             chatDeleteAll();
@@ -2022,6 +2129,29 @@ void PedroGui::colorCallback()
         }
 }
 
+void PedroGui::regPassCallback()
+{
+    PasswordDialog dlg(*this);
+    int ret = dlg.run();
+    if (ret == Gtk::RESPONSE_OK)
+        {
+        DOMString newpass = dlg.getNewPass();
+        client.inBandRegistrationChangePassword(newpass);
+        }
+}
+
+
+void PedroGui::regCancelCallback()
+{
+    Gtk::MessageDialog dlg(*this, "Do you want to cancel your registration on the server?",
+        false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO, true);
+    int ret = dlg.run();
+    if (ret == Gtk::RESPONSE_OK)
+        {
+        client.inBandRegistrationCancel();
+        }
+}
+
 void PedroGui::sendFileCallback()
 {
     doSendFile("");
@@ -2051,6 +2181,12 @@ void PedroGui::actionEnable(const DOMString &name, bool val)
         path.append(name);
         action = uiManager->get_action(path);
         }
+    if (!action)
+        {
+        path = "/ui/MenuBar/MenuRegister/";
+        path.append(name);
+        action = uiManager->get_action(path);
+        }
     if (!action)
         {
         path = "/ui/MenuBar/MenuTransfer/";
@@ -2108,6 +2244,15 @@ bool PedroGui::doSetup()
                                   Gtk::Stock::SELECT_COLOR, "Select Color"),
     sigc::mem_fun(*this, &PedroGui::colorCallback) );
 
+    //### REGISTER MENU
+    actionGroup->add( Gtk::Action::create("MenuRegister", "_Registration") );
+    actionGroup->add( Gtk::Action::create("RegPass",
+                                  Gtk::Stock::DIALOG_AUTHENTICATION, "Change Password"),
+    sigc::mem_fun(*this, &PedroGui::regPassCallback) );
+    actionGroup->add( Gtk::Action::create("RegCancel",
+                                  Gtk::Stock::CANCEL, "Cancel Registration"),
+    sigc::mem_fun(*this, &PedroGui::regCancelCallback) );
+
     //### TRANSFER MENU
     actionGroup->add( Gtk::Action::create("MenuTransfer", "_Transfer") );
     actionGroup->add( Gtk::Action::create("SendFile",
@@ -2141,6 +2286,10 @@ bool PedroGui::doSetup()
         "      <menuitem action='SelectFont'/>"
         "      <menuitem action='SelectColor'/>"
         "    </menu>"
+        "    <menu action='MenuRegister'>"
+        "      <menuitem action='RegPass'/>"
+        "      <menuitem action='RegCancel'/>"
+        "    </menu>"
         "    <menu action='MenuTransfer'>"
         "      <menuitem action='SendFile'/>"
         "    </menu>"
@@ -2158,6 +2307,8 @@ bool PedroGui::doSetup()
     actionEnable("Chat",       false);
     actionEnable("GroupChat",  false);
     actionEnable("Disconnect", false);
+    actionEnable("RegPass",    false);
+    actionEnable("RegCancel",  false);
 
     mainBox.pack_start(vPaned);
     vPaned.add1(roster);
index 2605a894e1e003b4e150fb6f2ce531eda25eb7ae..bee20c2947f04124e7d95e6775a1f1533d4baf94 100644 (file)
@@ -408,6 +408,46 @@ private:
 };
 
 
+//#########################################################################
+//# P A S S W O R D    D I A L O G
+//#########################################################################
+class PasswordDialog : public Gtk::Dialog
+{
+public:
+
+    PasswordDialog (PedroGui &par) : parent(par)
+        { doSetup(); }
+
+    virtual ~PasswordDialog ()
+        {}
+
+   DOMString getPass()
+       { return passField.get_text(); }
+   DOMString getNewPass()
+       { return newField.get_text(); }
+   DOMString getConfirm()
+       { return confField.get_text(); }
+
+private:
+
+    void okCallback();
+    void cancelCallback();
+
+    bool doSetup();
+
+    Gtk::Table       table;
+
+    Gtk::Label       passLabel;
+    Gtk::Entry       passField;
+    Gtk::Label       newLabel;
+    Gtk::Entry       newField;
+    Gtk::Label       confLabel;
+    Gtk::Entry       confField;
+
+    PedroGui &parent;
+};
+
+
 
 //#########################################################################
 //# C H A T   D I A L O G
@@ -697,6 +737,10 @@ private:
     //# Transfer menu
     void sendFileCallback();
 
+    //# Registration menu
+    void regPassCallback();
+    void regCancelCallback();
+
     //# Help menu
     void aboutCallback();
 
index b531441f026ab2e4cf3e8e401c753ea5ca05aa92..7160a09bd59d4ff09ed131b26d393b88e510ce95 100644 (file)
@@ -2993,7 +2993,7 @@ bool XmppClient::processIq(Element *root)
             }
         else  //result
             {
-            printf("Got result\n");
+            //printf("Got result\n");
             for (int i=0 ; i<fileSendCount ; i++)
                 {
                 XmppStream *outf = fileSends[i];
@@ -3119,6 +3119,86 @@ bool XmppClient::processIq(Element *root)
         dispatchXmppEvent(event);
         }
 
+    else if (id.find("regnew") != id.npos)
+        {
+
+        }
+
+    else if (id.find("regpass") != id.npos)
+        {
+        std::vector<Element *> list = root->findElements("error");
+        if (list.size()==0)
+            {
+            XmppEvent evt(XmppEvent::EVENT_REGISTRATION_CHANGE_PASS);
+            evt.setTo(username);
+            evt.setFrom(host);
+            dispatchXmppEvent(evt);
+            return true;
+            }
+
+        Element *errElem = list[0];
+        DOMString errMsg = "Password change error: ";
+        if (errElem->findElements("bad-request").size()>0)
+            {
+            errMsg.append("password change does not contain complete information");
+            }
+        else if (errElem->findElements("not-authorized").size()>0)
+            {
+            errMsg.append("server does not consider the channel safe "
+                          "enough to enable a password change");
+            }
+        else if (errElem->findElements("not-allowed").size()>0)
+            {
+            errMsg.append("server does not allow password changes");
+            }
+        else if (errElem->findElements("unexpected-request").size()>0)
+            {
+            errMsg.append(
+             "IQ set does not contain a 'from' address because "
+             "the entity is not registered with the server");
+            }
+        error((char *)errMsg.c_str());
+        }
+
+    else if (id.find("regcancel") != id.npos)
+        {
+        std::vector<Element *> list = root->findElements("error");
+        if (list.size()==0)
+            {
+            XmppEvent evt(XmppEvent::EVENT_REGISTRATION_CANCEL);
+            evt.setTo(username);
+            evt.setFrom(host);
+            dispatchXmppEvent(evt);
+            return true;
+            }
+
+        Element *errElem = list[0];
+        DOMString errMsg = "Registration cancel error: ";
+        if (errElem->findElements("bad-request").size()>0)
+            {
+            errMsg.append("The <remove/> element was not the only child element of the <query/> element.");
+            }
+        else if (errElem->findElements("forbidden").size()>0)
+            {
+            errMsg.append("sender does not have sufficient permissions to cancel the registration");
+            }
+        else if (errElem->findElements("not-allowed").size()>0)
+            {
+            errMsg.append("not allowed to cancel registrations in-band");
+            }
+        else if (errElem->findElements("registration-required").size()>0)
+            {
+            errMsg.append("not previously registered");
+            }
+        else if (errElem->findElements("unexpected-request").size()>0)
+            {
+            errMsg.append(
+                 "IQ set does not contain a 'from' address because "
+                 "the entity is not registered with the server");
+            }
+        error((char *)errMsg.c_str());
+        }
+
     return true;
 }
 
@@ -3227,17 +3307,18 @@ bool XmppClient::write(char *fmt, ...)
 //########################################################################
 
 /**
- * Perform JEP-077 In-Band Registration
+ * Perform JEP-077 In-Band Registration.  Performed synchronously after SSL,
+ * before authentication
  */
 bool XmppClient::inBandRegistrationNew()
 {
     Parser parser;
 
     char *fmt =
-     "<iq type='get' id='reg1'>"
+     "<iq type='get' id='regnew%d'>"
          "<query xmlns='jabber:iq:register'/>"
          "</iq>\n\n";
-    if (!write(fmt))
+    if (!write(fmt, msgId++))
         return false;
 
     DOMString recbuf = readStanza();
@@ -3267,14 +3348,14 @@ bool XmppClient::inBandRegistrationNew()
 
 
     fmt =
-     "<iq type='set' id='reg2'>"
+     "<iq type='set' id='regnew%d'>"
          "<query xmlns='jabber:iq:register'>"
          "<username>%s</username>"
          "<password>%s</password>"
          "<email/><name/>"
          "</query>"
          "</iq>\n\n";
-    if (!write(fmt, toXml(username).c_str(),
+    if (!write(fmt, msgId++, toXml(username).c_str(),
                     toXml(password).c_str() ))
         return false;
 
@@ -3315,7 +3396,8 @@ bool XmppClient::inBandRegistrationNew()
 
 
 /**
- * Perform JEP-077 In-Band Registration
+ * Perform JEP-077 In-Band Registration.  Performed asynchronously, after login.
+ * See processIq() for response handling.
  */
 bool XmppClient::inBandRegistrationChangePassword(const DOMString &newpassword)
 {
@@ -3323,7 +3405,7 @@ bool XmppClient::inBandRegistrationChangePassword(const DOMString &newpassword)
 
     //# Let's try it form-style to allow the common old/new password thing
     char *fmt =
-      "<iq type='set' from='%s' to='%s' id='change1'>"
+      "<iq type='set' from='%s' to='%s' id='regpass%d'>"
       "  <query xmlns='jabber:iq:register'>"
       "    <x xmlns='jabber:x:data' type='form'>"
       "      <field type='hidden' var='FORM_TYPE'>"
@@ -3342,118 +3424,31 @@ bool XmppClient::inBandRegistrationChangePassword(const DOMString &newpassword)
       "  </query>"
       "</iq>\n\n";
 
-    if (!write(fmt, jid.c_str(), host.c_str(),
+    if (!write(fmt, msgId++, jid.c_str(), host.c_str(),
              username.c_str(), password.c_str(), newpassword.c_str()))
         return false;
 
-    DOMString recbuf = readStanza();
-    status("RECV chpass: %s", recbuf.c_str());
-    Element *elem = parser.parse(recbuf);
-    //elem->print();
-
-    std::vector<Element *> list = elem->findElements("error");
-    if (list.size()==0)
-        {
-        XmppEvent evt(XmppEvent::EVENT_REGISTRATION_CHANGE_PASS);
-        evt.setTo(username);
-        evt.setFrom(host);
-        dispatchXmppEvent(evt);
-        delete elem;
-        return true;
-        }
-
-
-    Element *errElem = list[0];
-    DOMString errMsg = "Password change error: ";
-    if (errElem->findElements("bad-request").size()>0)
-        {
-        errMsg.append("password change does not contain complete information");
-        }
-    else if (errElem->findElements("not-authorized").size()>0)
-        {
-        errMsg.append("server does not consider the channel safe "
-                      "enough to enable a password change");
-        }
-    else if (errElem->findElements("not-allowed").size()>0)
-        {
-        errMsg.append("server does not allow password changes");
-        }
-    else if (errElem->findElements("unexpected-request").size()>0)
-        {
-        errMsg.append(
-             "IQ set does not contain a 'from' address because "
-             "the entity is not registered with the server");
-        }
-
-    delete elem;
-
-    error((char *)errMsg.c_str());
+    return true;
 
-    return false;
 }
 
 
 /**
- * Perform JEP-077 In-Band Registration
+ * Perform JEP-077 In-Band Registration.  Performed asynchronously, after login.
+ * See processIq() for response handling.
  */
 bool XmppClient::inBandRegistrationCancel()
 {
     Parser parser;
 
     char *fmt =
-     "<iq type='set' from='%s' id='unreg1'>"
+     "<iq type='set' from='%s' id='regcancel%d'>"
           "<query xmlns='jabber:iq:register'><remove/></query>"
           "</iq>\n\n";  
-    if (!write(fmt, jid.c_str()))
+    if (!write(fmt, msgId++, jid.c_str()))
         return false;
 
-    DOMString recbuf = readStanza();
-    status("RECV unreg: %s", recbuf.c_str());
-    Element *elem = parser.parse(recbuf);
-    //elem->print();
-
-    std::vector<Element *> list = elem->findElements("error");
-    if (list.size()==0)
-        {
-        XmppEvent evt(XmppEvent::EVENT_REGISTRATION_CANCEL);
-        evt.setTo(username);
-        evt.setFrom(host);
-        dispatchXmppEvent(evt);
-        delete elem;
-        return true;
-        }
-
-
-    Element *errElem = list[0];
-    DOMString errMsg = "Registration cancel error: ";
-    if (errElem->findElements("bad-request").size()>0)
-        {
-        errMsg.append("The <remove/> element was not the only child element of the <query/> element.");
-        }
-    else if (errElem->findElements("forbidden").size()>0)
-        {
-        errMsg.append("sender does not have sufficient permissions to cancel the registration");
-        }
-    else if (errElem->findElements("not-allowed").size()>0)
-        {
-        errMsg.append("not allowed to cancel registrations in-band");
-        }
-    else if (errElem->findElements("registration-required").size()>0)
-        {
-        errMsg.append("not previously registered");
-        }
-    else if (errElem->findElements("unexpected-request").size()>0)
-        {
-        errMsg.append(
-             "IQ set does not contain a 'from' address because "
-             "the entity is not registered with the server");
-        }
-
-    delete elem;
-
-    error((char *)errMsg.c_str());
-
-    return false;
+    return true;
 }
 
 
index 12a69fa3b4923a0db91ff294a9daf6ebf24b16bf..d26b83b765dc77babd677c8b75fcff27fcdf6081 100644 (file)
@@ -811,19 +811,6 @@ public:
     virtual int getMsgId()
         { return msgId++; }
 
-    /**
-     *
-     */
-    virtual void setDoRegister(bool val)
-        { doRegister = val; }
-
-
-    /**
-     *
-     */
-    virtual bool getDoRegister()
-        { return doRegister; }
-
 
 
     //#######################
@@ -882,6 +869,29 @@ public:
      */
     virtual void rosterShow(const DOMString &jid, const DOMString &show);
 
+    //#######################
+    //# REGISTRATION
+    //#######################
+
+    /**
+     *  Set whether the client should to in-band registration
+     *  before authentication.  Causes inBandRegistrationNew() to be called
+     *  synchronously, before async is started.
+     */
+    virtual void setDoRegister(bool val)
+        { doRegister = val; }
+
+    /**
+     * Change the password of an existing account with a server
+     */
+    bool inBandRegistrationChangePassword(const DOMString &newPassword);
+
+    /**
+     * Cancel an existing account with a server
+     */
+    bool inBandRegistrationCancel();
+
+
     //#######################
     //# CHAT (individual)
     //#######################
@@ -1127,20 +1137,10 @@ private:
     bool iqAuthenticate(const DOMString &streamId);
 
     /**
-     * Register a new account with a server
+     * Register a new account with a server.  Not done by user
      */
     bool inBandRegistrationNew();
 
-    /**
-     * Change the password of an existing account with a server
-     */
-    bool inBandRegistrationChangePassword(const DOMString &newPassword);
-
-    /**
-     * Cancel an existing account with a server
-     */
-    bool inBandRegistrationCancel();
-
     bool keepGoing;
 
     bool doRegister;