From e72778659573267d7180d89fddef188ad0316c32 Mon Sep 17 00:00:00 2001 From: ishmal Date: Wed, 24 May 2006 02:13:24 +0000 Subject: [PATCH] Add gui code for registration --- src/pedro/pedrogui.cpp | 151 ++++++++++++++++++++++++++++++ src/pedro/pedrogui.h | 44 +++++++++ src/pedro/pedroxmpp.cpp | 199 ++++++++++++++++++++-------------------- src/pedro/pedroxmpp.h | 48 +++++----- 4 files changed, 316 insertions(+), 126 deletions(-) diff --git a/src/pedro/pedrogui.cpp b/src/pedro/pedrogui.cpp index 0ef33b5c2..b50791e25 100644 --- a/src/pedro/pedrogui.cpp +++ b/src/pedro/pedrogui.cpp @@ -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 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 uiManager = Gtk::UIManager::create(); + + uiManager->insert_action_group(actionGroup, 0); + add_accel_group(uiManager->get_accel_group()); + + Glib::ustring ui_info = + "" + " " + " " + " " + " " + " " + " " + " " + ""; + + 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() " " " " " " + " " + " " + " " + " " " " " " " " @@ -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); diff --git a/src/pedro/pedrogui.h b/src/pedro/pedrogui.h index 2605a894e..bee20c294 100644 --- a/src/pedro/pedrogui.h +++ b/src/pedro/pedrogui.h @@ -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(); diff --git a/src/pedro/pedroxmpp.cpp b/src/pedro/pedroxmpp.cpp index b531441f0..7160a09bd 100644 --- a/src/pedro/pedroxmpp.cpp +++ b/src/pedro/pedroxmpp.cpp @@ -2993,7 +2993,7 @@ bool XmppClient::processIq(Element *root) } else //result { - printf("Got result\n"); + //printf("Got result\n"); for (int i=0 ; i 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 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 element was not the only child element of the 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 = - "" + "" "" "\n\n"; - if (!write(fmt)) + if (!write(fmt, msgId++)) return false; DOMString recbuf = readStanza(); @@ -3267,14 +3348,14 @@ bool XmppClient::inBandRegistrationNew() fmt = - "" + "" "" "%s" "%s" "" "" "\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 = - "" + "" " " " " " " @@ -3342,118 +3424,31 @@ bool XmppClient::inBandRegistrationChangePassword(const DOMString &newpassword) " " "\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 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 = - "" + "" "" "\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 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 element was not the only child element of the 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; } diff --git a/src/pedro/pedroxmpp.h b/src/pedro/pedroxmpp.h index 12a69fa3b..d26b83b76 100644 --- a/src/pedro/pedroxmpp.h +++ b/src/pedro/pedroxmpp.h @@ -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; -- 2.30.2