Changeset cc27aec in Renga


Ignore:
Timestamp:
Jun 22, 2019, 2:39:29 PM (5 years ago)
Author:
Adrien Destugues <pulkomandy@…>
Branches:
main
Children:
8229a0c
Parents:
dfb2026
Message:

Implement adding/removing contacts, and creating account

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jabber/JabberSpeak.cpp

    rdfb2026 rcc27aec  
    44
    55#include <gloox/jid.h>
     6#include <gloox/registration.h>
    67#include <gloox/rostermanager.h>
    78
     
    609610
    610611void JabberSpeak::_AcceptPresence(string username) {
    611         XMLEntity *entity;
    612        
    613         char **atts = CreateAttributeMemory(4);
    614        
    615         // assemble attributes
    616         strcpy(atts[0], "to");
    617         strcpy(atts[1], username.c_str());
    618         strcpy(atts[2], "type");
    619         strcpy(atts[3], "subscribed");
    620 
    621         entity = new XMLEntity("presence", (const char **)atts);
    622 
    623         // send XML command
    624         char *str = entity->ToString();
    625         free(str);
    626        
    627         DestroyAttributeMemory(atts, 4);
    628         delete entity;
     612        gloox::Subscription subscription(gloox::Subscription::Subscribed, gloox::JID(username));
     613        fClient->send(subscription);
    629614}
    630615
    631616void JabberSpeak::_RejectPresence(string username) {
    632         XMLEntity *entity;
    633        
    634         char **atts = CreateAttributeMemory(4);
    635        
    636         // assemble attributes
    637         strcpy(atts[0], "to");
    638         strcpy(atts[1], username.c_str());
    639         strcpy(atts[2], "type");
    640         strcpy(atts[3], "unsubscribed");
    641 
    642         entity = new XMLEntity("presence", (const char **)atts);
    643 
    644         // send XML command
    645         char *str = entity->ToString();
    646         free(str);
    647        
    648         DestroyAttributeMemory(atts, 4);
    649         delete entity;
     617        gloox::Subscription subscription(gloox::Subscription::Unsubscribed, gloox::JID(username));
     618        fClient->send(subscription);
    650619}
    651620
     
    776745
    777746void JabberSpeak::AddToRoster(const UserID *new_user) {
    778         XMLEntity *entity, *entity_query, *entity_item;
    779        
    780         char **atts       = CreateAttributeMemory(2);
    781         char **atts_query = CreateAttributeMemory(2);
    782         char **atts_item  = CreateAttributeMemory(6);
    783        
    784         // assemble attributes
    785         strcpy(atts[0], "type");
    786         strcpy(atts[1], "set");
    787         entity = new XMLEntity("iq", (const char **)atts);
    788 
    789         strcpy(atts_query[0], "xmlns");
    790         strcpy(atts_query[1], "jabber:iq:roster");
    791         entity_query = new XMLEntity("query", (const char **)atts_query);
    792 
    793         strcpy(atts_item[0], "jid");
    794         strcpy(atts_item[1], new_user->Handle().c_str());
    795         strcpy(atts_item[2], "name");
    796         strcpy(atts_item[3], new_user->FriendlyName().c_str());
    797         strcpy(atts_item[4], "subscription");
    798         strcpy(atts_item[5], "to");
    799         entity_item = new XMLEntity("item", (const char **)atts_item);
    800 
    801         entity_query->AddChild(entity_item);
    802         entity->AddChild(entity_query);
    803 
    804         DestroyAttributeMemory(atts, 2);
    805         DestroyAttributeMemory(atts_query, 2);
    806         DestroyAttributeMemory(atts_item, 6);
    807        
    808         delete entity;
     747        gloox::StringList groups;
     748        fClient->rosterManager()->add(gloox::JID(new_user->Handle()),
     749                new_user->FriendlyName(), groups);
    809750}
    810751
    811752void JabberSpeak::RemoveFromRoster(const UserID *removed_user) {
    812         XMLEntity *entity, *entity_query, *entity_item;
    813 
    814         char **atts       = CreateAttributeMemory(4);
    815         char **atts_query = CreateAttributeMemory(2);
    816         char **atts_item  = CreateAttributeMemory(6);
    817        
    818         // assemble attributes
    819         strcpy(atts[0], "type");
    820         strcpy(atts[1], "set");
    821         strcpy(atts[2], "id");
    822         strcpy(atts[3], GenerateUniqueID().c_str());
    823 
    824         strcpy(atts_query[0], "xmlns");
    825         strcpy(atts_query[1], "jabber:iq:roster");
    826 
    827         strcpy(atts_item[0], "jid");
    828         strcpy(atts_item[1], removed_user->Handle().c_str());
    829         strcpy(atts_item[2], "name");
    830         strcpy(atts_item[3], removed_user->FriendlyName().c_str());
    831         strcpy(atts_item[4], "subscription");
    832         strcpy(atts_item[5], "remove");
    833 
    834         entity = new XMLEntity("iq", (const char **)atts);
    835         entity_query = new XMLEntity("query", (const char **)atts_query);
    836         entity_item = new XMLEntity("item", (const char **)atts_item);
    837 
    838         entity_query->AddChild(entity_item);
    839         entity->AddChild(entity_query);
    840 
    841         // log command
    842         _iq_map[atts[3]] = ROSTER;
    843 
    844         // send XML command
    845         char *str = entity->ToString();
    846         free(str);
    847        
    848         DestroyAttributeMemory(atts, 4);
    849         DestroyAttributeMemory(atts_query, 2);
    850         DestroyAttributeMemory(atts_item, 6);
    851        
    852         delete entity;
     753        fClient->rosterManager()->remove(gloox::JID(removed_user->Handle()));
    853754}
    854755
     
    916817}
    917818
    918 void JabberSpeak::_SendUserRegistration(string username, string password, string resource) {
    919         XMLEntity   *entity_iq, *entity_query;
    920         char **atts_iq    = CreateAttributeMemory(4);
    921         char **atts_query = CreateAttributeMemory(2);
    922 
    923         // assemble attributes;
    924         strcpy(atts_iq[0], "id");
    925 
    926         strcpy(atts_iq[1], GenerateUniqueID().c_str());
    927 
    928         strcpy(atts_iq[2], "type");
    929         strcpy(atts_iq[3], "set");
    930 
    931         strcpy(atts_query[0], "xmlns");
    932         strcpy(atts_query[1], "jabber:iq:register");
    933 
    934         // construct XML tagset
    935         entity_iq    = new XMLEntity("iq", (const char **)atts_iq);
    936         entity_query = new XMLEntity("query", (const char **)atts_query);
    937 
    938         entity_iq->AddChild(entity_query);
    939        
    940         entity_query->AddChild("username", NULL, username.c_str());
    941         entity_query->AddChild("password", NULL, password.c_str());
    942         entity_query->AddChild("resource", NULL, resource.c_str());
    943 
    944         // log command
    945         _iq_map[atts_iq[1]] = NEW_USER;
    946        
    947         // send XML command
    948         char *str = entity_iq->ToString();
    949         free(str);
    950 
    951         DestroyAttributeMemory(atts_iq, 4);
    952         DestroyAttributeMemory(atts_query, 2);
    953        
    954         delete entity_iq;
     819void JabberSpeak::_SendUserRegistration(string username, string password, string resource __attribute__((unused))) {
     820        gloox::Registration registration(fClient);
     821        gloox::RegistrationFields fields;
     822        fields.username = username;
     823        fields.password = password;
     824        // TODO this can return an error
     825        registration.createAccount(
     826                gloox::Registration::FieldUsername | gloox::Registration::FieldPassword,
     827                fields);
    955828}
    956829
Note: See TracChangeset for help on using the changeset viewer.