diff options
Diffstat (limited to 'xmpp-vala/src/module/xep/0045_muc')
-rw-r--r-- | xmpp-vala/src/module/xep/0045_muc/flag.vala | 119 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0045_muc/module.vala | 101 |
2 files changed, 107 insertions, 113 deletions
diff --git a/xmpp-vala/src/module/xep/0045_muc/flag.vala b/xmpp-vala/src/module/xep/0045_muc/flag.vala index da469a41..87e0930f 100644 --- a/xmpp-vala/src/module/xep/0045_muc/flag.vala +++ b/xmpp-vala/src/module/xep/0045_muc/flag.vala @@ -1,128 +1,123 @@ using Gee; -using Xmpp.Core; - namespace Xmpp.Xep.Muc { public class Flag : XmppStreamFlag { public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "muc"); - private HashMap<string, Gee.List<Feature>> room_features = new HashMap<string, Gee.List<Feature>>(); - private HashMap<string, string> room_names = new HashMap<string, string>(); + private HashMap<Jid, Gee.List<Feature>> room_features = new HashMap<Jid, Gee.List<Feature>>(Jid.hash_bare_func, Jid.equals_bare_func); + private HashMap<Jid, string> room_names = new HashMap<Jid, string>(Jid.hash_bare_func, Jid.equals_bare_func); - private HashMap<string, string> enter_ids = new HashMap<string, string>(); - private HashMap<string, string> own_nicks = new HashMap<string, string>(); - private HashMap<string, string> subjects = new HashMap<string, string>(); - private HashMap<string, string> subjects_by = new HashMap<string, string>(); + private HashMap<Jid, string> enter_ids = new HashMap<Jid, string>(Jid.hash_bare_func, Jid.equals_bare_func); + private HashMap<Jid, string> own_nicks = new HashMap<Jid, string>(Jid.hash_bare_func, Jid.equals_bare_func); + private HashMap<Jid, string> subjects = new HashMap<Jid, string>(Jid.hash_bare_func, Jid.equals_bare_func); + private HashMap<Jid, Jid> subjects_by = new HashMap<Jid, Jid>(Jid.hash_bare_func, Jid.equals_bare_func); - private HashMap<string, string> occupant_real_jids = new HashMap<string, string>(); - private HashMap<string, HashMap<string, Affiliation>> affiliations = new HashMap<string, HashMap<string, Affiliation>>(); - private HashMap<string, Role> occupant_role = new HashMap<string, Role>(); + private HashMap<Jid, Jid> occupant_real_jids = new HashMap<Jid, Jid>(Jid.hash_func, Jid.equals_bare_func); + private HashMap<Jid, HashMap<Jid, Affiliation>> affiliations = new HashMap<Jid, HashMap<Jid, Affiliation>>(Jid.hash_bare_func, Jid.equals_bare_func); + private HashMap<Jid, Role> occupant_role = new HashMap<Jid, Role>(Jid.hash_func, Jid.equals_func); - public string? get_room_name(string jid) { return room_names.has_key(jid) ? room_names[jid] : null; } + public string? get_room_name(Jid muc_jid) { return room_names.has_key(muc_jid.bare_jid) ? room_names[muc_jid.bare_jid] : null; } - public bool has_room_feature(string jid, Feature feature) { - return room_features.has_key(jid) && room_features[jid].contains(feature); + public bool has_room_feature(Jid muc_jid, Feature feature) { + return room_features.has_key(muc_jid.bare_jid) && room_features[muc_jid.bare_jid].contains(feature); } - public string? get_real_jid(string full_jid) { return occupant_real_jids[full_jid]; } + public Jid? get_real_jid(Jid full_jid) { return occupant_real_jids[full_jid]; } - public Gee.List<string> get_offline_members(string muc_jid) { - Gee.List<string> ret = new ArrayList<string>(); - HashMap<string, Affiliation>? muc_affiliations = affiliations[muc_jid]; + public Gee.List<Jid> get_offline_members(Jid muc_jid) { + Gee.List<Jid> ret = new ArrayList<Jid>(Jid.equals_func); + HashMap<Jid, Affiliation>? muc_affiliations = affiliations[muc_jid.bare_jid]; if (muc_affiliations != null) { - foreach (string jid in muc_affiliations.keys) { - if (!jid.has_prefix(muc_jid)) ret.add(jid); + foreach (Jid jid in muc_affiliations.keys) { + if (!jid.equals_bare(muc_jid)) ret.add(jid); } } return ret; } - public Affiliation get_affiliation(string muc_jid, string full_jid) { - HashMap<string, Affiliation>? muc_affiliations = affiliations[muc_jid]; + public Affiliation get_affiliation(Jid muc_jid, Jid full_jid) { + HashMap<Jid, Affiliation>? muc_affiliations = affiliations[muc_jid.bare_jid]; if (muc_affiliations != null) return muc_affiliations[full_jid]; return Affiliation.NONE; } - public Role? get_occupant_role(string full_jid) { + public Role? get_occupant_role(Jid full_jid) { if (occupant_role.has_key(full_jid)) return occupant_role[full_jid]; return Role.NONE; } - public string? get_muc_nick(string bare_jid) { return own_nicks[bare_jid]; } + public string? get_muc_nick(Jid muc_jid) { return own_nicks[muc_jid.bare_jid]; } - public string? get_enter_id(string bare_jid) { return enter_ids[bare_jid]; } + public string? get_enter_id(Jid muc_jid) { return enter_ids[muc_jid.bare_jid]; } - public bool is_muc(string jid) { return own_nicks[jid] != null; } + public bool is_muc(Jid jid) { return own_nicks[jid] != null; } - public bool is_occupant(string jid) { - string bare_jid = get_bare_jid(jid); - return own_nicks.has_key(bare_jid) || enter_ids.has_key(bare_jid); + public bool is_occupant(Jid jid) { + return own_nicks.has_key(jid.bare_jid) || enter_ids.has_key(jid.bare_jid); } public bool is_muc_enter_outstanding() { return enter_ids.size != 0; } - public string? get_muc_subject(string bare_jid) { return subjects[bare_jid]; } + public string? get_muc_subject(Jid muc_jid) { return subjects[muc_jid.bare_jid]; } - internal void set_room_name(string jid, string name) { - room_names[jid] = name; + internal void set_room_name(Jid muc_jid, string name) { + room_names[muc_jid.bare_jid] = name; } - internal void set_room_features(string jid, Gee.List<Feature> features) { - room_features[jid] = features; + internal void set_room_features(Jid muc_jid, Gee.List<Feature> features) { + room_features[muc_jid.bare_jid] = features; } - internal void set_real_jid(string full_jid, string real_jid) { occupant_real_jids[full_jid] = real_jid; } + internal void set_real_jid(Jid full_jid, Jid real_jid) { occupant_real_jids[full_jid] = real_jid; } - internal void set_offline_member(string muc_jid, string real_jid, Affiliation affiliation) { - set_affiliation(muc_jid, real_jid, affiliation); + internal void set_offline_member(Jid muc_jid, Jid real_jid, Affiliation affiliation) { + set_affiliation(muc_jid.bare_jid, real_jid, affiliation); } - internal void set_affiliation(string muc_jid, string full_jid, Affiliation affiliation) { - if (!affiliations.has_key(muc_jid)) affiliations[muc_jid] = new HashMap<string, Affiliation>(); + internal void set_affiliation(Jid muc_jid, Jid full_jid, Affiliation affiliation) { + if (!affiliations.has_key(muc_jid.bare_jid)) affiliations[muc_jid.bare_jid] = new HashMap<Jid, Affiliation>(Jid.hash_func, Jid.equals_func); if (affiliation == Affiliation.NONE) { - affiliations[muc_jid].unset(full_jid); + affiliations[muc_jid.bare_jid].unset(full_jid); } else { - affiliations[muc_jid][full_jid] = affiliation; + affiliations[muc_jid.bare_jid][full_jid] = affiliation; } } - internal void set_occupant_role(string full_jid, Role role) { + internal void set_occupant_role(Jid full_jid, Role role) { occupant_role[full_jid] = role; } - internal void set_muc_subject(string full_jid, string? subject) { - string bare_jid = get_bare_jid(full_jid); - subjects[bare_jid] = subject; - subjects_by[bare_jid] = full_jid; + internal void set_muc_subject(Jid full_jid, string? subject) { + subjects[full_jid.bare_jid] = subject; + subjects_by[full_jid.bare_jid] = full_jid; } - internal void start_muc_enter(string bare_jid, string presence_id) { - enter_ids[bare_jid] = presence_id; + internal void start_muc_enter(Jid jid, string presence_id) { + enter_ids[jid.bare_jid] = presence_id; } - internal void finish_muc_enter(string bare_jid, string? nick = null) { - if (nick != null) own_nicks[bare_jid] = nick; - enter_ids.unset(bare_jid); + internal void finish_muc_enter(Jid jid, string? nick = null) { + if (nick != null) own_nicks[jid.bare_jid] = nick; + enter_ids.unset(jid.bare_jid); } - internal void left_muc(XmppStream stream, string muc) { - own_nicks.unset(muc); - subjects.unset(muc); - subjects_by.unset(muc); - Gee.List<string>? occupants = stream.get_flag(Presence.Flag.IDENTITY).get_resources(muc); + internal void left_muc(XmppStream stream, Jid muc_jid) { + own_nicks.unset(muc_jid); + subjects.unset(muc_jid); + subjects_by.unset(muc_jid); + Gee.List<Jid>? occupants = stream.get_flag(Presence.Flag.IDENTITY).get_resources(muc_jid); if (occupants != null) { - foreach (string occupant in occupants) { + foreach (Jid occupant in occupants) { remove_occupant_info(occupant); } } } - internal void remove_occupant_info(string full_jid) { - occupant_real_jids.unset(full_jid); - string bare_jid = get_bare_jid(full_jid); - if (affiliations.has_key(full_jid)) affiliations[bare_jid].unset(full_jid); - occupant_role.unset(full_jid); + internal void remove_occupant_info(Jid jid) { + occupant_real_jids.unset(jid); + if (affiliations.has_key(jid)) affiliations[jid].unset(jid); + occupant_role.unset(jid); } internal override string get_ns() { return NS_URI; } diff --git a/xmpp-vala/src/module/xep/0045_muc/module.vala b/xmpp-vala/src/module/xep/0045_muc/module.vala index faaf1901..f0ae0ed6 100644 --- a/xmpp-vala/src/module/xep/0045_muc/module.vala +++ b/xmpp-vala/src/module/xep/0045_muc/module.vala @@ -1,7 +1,5 @@ using Gee; -using Xmpp.Core; - namespace Xmpp.Xep.Muc { private const string NS_URI = "http://jabber.org/protocol/muc"; @@ -58,20 +56,20 @@ public enum Feature { public class Module : XmppStreamModule { public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0045_muc_module"); - public signal void received_occupant_affiliation(XmppStream stream, string jid, Affiliation? affiliation); - public signal void received_occupant_jid(XmppStream stream, string jid, string? real_jid); - public signal void received_occupant_role(XmppStream stream, string jid, Role? role); - public signal void subject_set(XmppStream stream, string subject, string jid); - public signal void room_configuration_changed(XmppStream stream, string jid, StatusCode code); + public signal void received_occupant_affiliation(XmppStream stream, Jid jid, Affiliation? affiliation); + public signal void received_occupant_jid(XmppStream stream, Jid jid, Jid? real_jid); + public signal void received_occupant_role(XmppStream stream, Jid jid, Role? role); + public signal void subject_set(XmppStream stream, string? subject, Jid jid); + public signal void room_configuration_changed(XmppStream stream, Jid jid, StatusCode code); - public signal void room_entered(XmppStream stream, string jid, string nick); - public signal void room_enter_error(XmppStream stream, string jid, MucEnterError? error); // TODO "?" shoudln't be necessary (vala bug), remove someday - public signal void self_removed_from_room(XmppStream stream, string jid, StatusCode code); - public signal void removed_from_room(XmppStream stream, string jid, StatusCode? code); + public signal void room_entered(XmppStream stream, Jid jid, string nick); + public signal void room_enter_error(XmppStream stream, Jid jid, MucEnterError? error); // TODO "?" shoudln't be necessary (vala bug), remove someday + public signal void self_removed_from_room(XmppStream stream, Jid jid, StatusCode code); + public signal void removed_from_room(XmppStream stream, Jid jid, StatusCode? code); - public void enter(XmppStream stream, string bare_jid, string nick, string? password, DateTime? history_since) { + public void enter(XmppStream stream, Jid bare_jid, string nick, string? password, DateTime? history_since) { Presence.Stanza presence = new Presence.Stanza(); - presence.to = bare_jid + "/" + nick; + presence.to = bare_jid.with_resource(nick); StanzaNode x_node = new StanzaNode.build("x", NS_URI).add_self_xmlns(); if (password != null) { x_node.put_node(new StanzaNode.build("password", NS_URI).put_node(new StanzaNode.text(password))); @@ -89,47 +87,47 @@ public class Module : XmppStreamModule { stream.get_module(Presence.Module.IDENTITY).send_presence(stream, presence); } - public void exit(XmppStream stream, string jid) { + public void exit(XmppStream stream, Jid jid) { string nick = stream.get_flag(Flag.IDENTITY).get_muc_nick(jid); Presence.Stanza presence = new Presence.Stanza(); - presence.to = jid + "/" + nick; + presence.to = jid.with_resource(nick); presence.type_ = Presence.Stanza.TYPE_UNAVAILABLE; stream.get_module(Presence.Module.IDENTITY).send_presence(stream, presence); } - public void change_subject(XmppStream stream, string jid, string subject) { - Message.Stanza message = new Message.Stanza(); + public void change_subject(XmppStream stream, Jid jid, string subject) { + MessageStanza message = new MessageStanza(); message.to = jid; - message.type_ = Message.Stanza.TYPE_GROUPCHAT; + message.type_ = MessageStanza.TYPE_GROUPCHAT; message.stanza.put_node((new StanzaNode.build("subject")).put_node(new StanzaNode.text(subject))); - stream.get_module(Message.Module.IDENTITY).send_message(stream, message); + stream.get_module(MessageModule.IDENTITY).send_message(stream, message); } - public void change_nick(XmppStream stream, string jid, string new_nick) { + public void change_nick(XmppStream stream, Jid jid, string new_nick) { Presence.Stanza presence = new Presence.Stanza(); - presence.to = jid + "/" + new_nick; + presence.to = jid.with_resource(new_nick); stream.get_module(Presence.Module.IDENTITY).send_presence(stream, presence); } - public void invite(XmppStream stream, string to_muc, string jid) { - Message.Stanza message = new Message.Stanza(); + public void invite(XmppStream stream, Jid to_muc, Jid jid) { + MessageStanza message = new MessageStanza(); message.to = to_muc; StanzaNode invite_node = new StanzaNode.build("x", NS_URI_USER).add_self_xmlns() - .put_node(new StanzaNode.build("invite", NS_URI_USER).put_attribute("to", jid)); + .put_node(new StanzaNode.build("invite", NS_URI_USER).put_attribute("to", jid.to_string())); message.stanza.put_node(invite_node); - stream.get_module(Message.Module.IDENTITY).send_message(stream, message); + stream.get_module(MessageModule.IDENTITY).send_message(stream, message); } - public void kick(XmppStream stream, string jid, string nick) { + public void kick(XmppStream stream, Jid jid, string nick) { change_role(stream, jid, nick, "none"); } /* XEP 0046: "A user cannot be kicked by a moderator with a lower affiliation." (XEP 0045 8.2) */ - public bool kick_possible(XmppStream stream, string occupant) { - string muc_jid = get_bare_jid(occupant); + public bool kick_possible(XmppStream stream, Jid occupant) { + Jid muc_jid = occupant.bare_jid; Flag flag = stream.get_flag(Flag.IDENTITY); string own_nick = flag.get_muc_nick(muc_jid); - Affiliation my_affiliation = flag.get_affiliation(muc_jid, muc_jid + "/" + own_nick); + Affiliation my_affiliation = flag.get_affiliation(muc_jid, muc_jid.with_resource(own_nick)); Affiliation other_affiliation = flag.get_affiliation(muc_jid, occupant); switch (my_affiliation) { case Affiliation.MEMBER: @@ -142,22 +140,22 @@ public class Module : XmppStreamModule { return true; } - public void change_role(XmppStream stream, string jid, string nick, string new_role) { + public void change_role(XmppStream stream, Jid jid, string nick, string new_role) { StanzaNode query = new StanzaNode.build("query", NS_URI_ADMIN).add_self_xmlns(); query.put_node(new StanzaNode.build("item", NS_URI_ADMIN).put_attribute("nick", nick, NS_URI_ADMIN).put_attribute("role", new_role, NS_URI_ADMIN)); Iq.Stanza iq = new Iq.Stanza.set(query) { to=jid }; stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); } - public void change_affiliation(XmppStream stream, string jid, string nick, string new_affiliation) { + public void change_affiliation(XmppStream stream, Jid jid, string nick, string new_affiliation) { StanzaNode query = new StanzaNode.build("query", NS_URI_ADMIN).add_self_xmlns(); query.put_node(new StanzaNode.build("item", NS_URI_ADMIN).put_attribute("nick", nick, NS_URI_ADMIN).put_attribute("affiliation", new_affiliation, NS_URI_ADMIN)); Iq.Stanza iq = new Iq.Stanza.set(query) { to=jid }; stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq); } - public delegate void OnConfigFormResult(XmppStream stream, string jid, DataForms.DataForm data_form); - public void get_config_form(XmppStream stream, string jid, owned OnConfigFormResult listener) { + public delegate void OnConfigFormResult(XmppStream stream, Jid jid, DataForms.DataForm data_form); + public void get_config_form(XmppStream stream, Jid jid, owned OnConfigFormResult listener) { Iq.Stanza get_iq = new Iq.Stanza.get(new StanzaNode.build("query", NS_URI_OWNER).add_self_xmlns()) { to=jid }; stream.get_module(Iq.Module.IDENTITY).send_iq(stream, get_iq, (stream, form_iq) => { StanzaNode? x_node = form_iq.stanza.get_deep_subnode(NS_URI_OWNER + ":query", DataForms.NS_URI + ":x"); @@ -176,7 +174,7 @@ public class Module : XmppStreamModule { public override void attach(XmppStream stream) { stream.add_flag(new Muc.Flag()); - stream.get_module(Message.Module.IDENTITY).received_message.connect(on_received_message); + stream.get_module(MessageModule.IDENTITY).received_message.connect(on_received_message); stream.get_module(Presence.Module.IDENTITY).received_presence.connect(check_for_enter_error); stream.get_module(Presence.Module.IDENTITY).received_available.connect(on_received_available); stream.get_module(Presence.Module.IDENTITY).received_unavailable.connect(on_received_unavailable); @@ -192,7 +190,7 @@ public class Module : XmppStreamModule { } public override void detach(XmppStream stream) { - stream.get_module(Message.Module.IDENTITY).received_message.disconnect(on_received_message); + stream.get_module(MessageModule.IDENTITY).received_message.disconnect(on_received_message); stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(check_for_enter_error); stream.get_module(Presence.Module.IDENTITY).received_available.disconnect(on_received_available); stream.get_module(Presence.Module.IDENTITY).received_unavailable.disconnect(on_received_unavailable); @@ -201,8 +199,8 @@ public class Module : XmppStreamModule { public override string get_ns() { return NS_URI; } public override string get_id() { return IDENTITY.id; } - private void on_received_message(XmppStream stream, Message.Stanza message) { - if (message.type_ == Message.Stanza.TYPE_GROUPCHAT) { + private void on_received_message(XmppStream stream, MessageStanza message) { + if (message.type_ == MessageStanza.TYPE_GROUPCHAT) { StanzaNode? subject_node = message.stanza.get_subnode("subject"); if (subject_node != null) { string subject = subject_node.get_string_content(); @@ -215,7 +213,7 @@ public class Module : XmppStreamModule { private void check_for_enter_error(XmppStream stream, Presence.Stanza presence) { Flag flag = stream.get_flag(Flag.IDENTITY); if (presence.is_error() && flag.is_muc_enter_outstanding() && flag.is_occupant(presence.from)) { - string bare_jid = get_bare_jid(presence.from); + Jid bare_jid = presence.from.bare_jid; ErrorStanza? error_stanza = presence.get_error(); if (flag.get_enter_id(bare_jid) == presence.id) { MucEnterError error = MucEnterError.NONE; @@ -258,20 +256,21 @@ public class Module : XmppStreamModule { if (x_node != null) { ArrayList<int> status_codes = get_status_codes(x_node); if (status_codes.contains(StatusCode.SELF_PRESENCE)) { - string bare_jid = get_bare_jid(presence.from); + Jid bare_jid = presence.from.bare_jid; if (flag.get_enter_id(bare_jid) != null) { - room_entered(stream, bare_jid, get_resource_part(presence.from)); - flag.finish_muc_enter(bare_jid, get_resource_part(presence.from)); + room_entered(stream, bare_jid, presence.from.resourcepart); + flag.finish_muc_enter(bare_jid, presence.from.resourcepart); } } string? affiliation_str = x_node.get_deep_attribute("item", "affiliation"); if (affiliation_str != null) { Affiliation affiliation = parse_affiliation(affiliation_str); - flag.set_affiliation(get_bare_jid(presence.from), presence.from, affiliation); + flag.set_affiliation(presence.from.bare_jid, presence.from, affiliation); received_occupant_affiliation(stream, presence.from, affiliation); } - string? jid = x_node.get_deep_attribute("item", "jid"); - if (jid != null) { + string? jid_ = x_node.get_deep_attribute("item", "jid"); + if (jid_ != null) { + Jid? jid = Jid.parse(jid_); flag.set_real_jid(presence.from, jid); received_occupant_jid(stream, presence.from, jid); } @@ -301,10 +300,10 @@ public class Module : XmppStreamModule { foreach (StatusCode code in USER_REMOVED_CODES) { if (code in status_codes) { if (StatusCode.SELF_PRESENCE in status_codes) { - flag.left_muc(stream, get_bare_jid(presence.from)); + flag.left_muc(stream, presence.from.bare_jid); self_removed_from_room(stream, presence.from, code); Presence.Flag presence_flag = stream.get_flag(Presence.Flag.IDENTITY); - presence_flag.remove_presence(get_bare_jid(presence.from)); + presence_flag.remove_presence(presence.from.bare_jid); } else { removed_from_room(stream, presence.from, code); } @@ -312,7 +311,7 @@ public class Module : XmppStreamModule { } } - private void query_room_info(XmppStream stream, string jid) { + private void query_room_info(XmppStream stream, Jid jid) { stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, jid, (stream, query_result) => { Gee.List<Feature> features = new ArrayList<Feature>(); @@ -351,8 +350,8 @@ public class Module : XmppStreamModule { }); } - public delegate void OnAffiliationResult(XmppStream stream, Gee.List<string> jids); - private void query_affiliation(XmppStream stream, string jid, string affiliation, owned OnAffiliationResult? listener) { + public delegate void OnAffiliationResult(XmppStream stream, Gee.List<Jid> jids); + private void query_affiliation(XmppStream stream, Jid jid, string affiliation, owned OnAffiliationResult? listener) { Iq.Stanza iq = new Iq.Stanza.get( new StanzaNode.build("query", NS_URI_ADMIN) .add_self_xmlns() @@ -364,9 +363,9 @@ public class Module : XmppStreamModule { StanzaNode? query_node = iq.stanza.get_subnode("query", NS_URI_ADMIN); if (query_node == null) return; Gee.List<StanzaNode> item_nodes = query_node.get_subnodes("item", NS_URI_ADMIN); - Gee.List<string> ret_jids = new ArrayList<string>(); + Gee.List<Jid> ret_jids = new ArrayList<Jid>(Jid.equals_func); foreach (StanzaNode item in item_nodes) { - string? jid_ = item.get_attribute("jid"); + Jid? jid_ = Jid.parse(item.get_attribute("jid")); string? affiliation_ = item.get_attribute("affiliation"); if (jid_ != null && affiliation_ != null) { stream.get_flag(Muc.Flag.IDENTITY).set_offline_member(iq.from, jid_, parse_affiliation(affiliation_)); |