muc.vala
author Stiletto <blasux@blasux.ru>
Fri, 19 Oct 2012 00:38:54 +0400
changeset 1 76caf6a3f413
child 2 4e050075fab9
permissions -rw-r--r--
Somehow compiles and even joins a conference
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     1
abstract class Module : Object {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     2
    protected weak Config cfg;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     3
    protected weak Connection conn;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     4
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     5
    public Module(Config cfg, Connection conn) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     6
        this.cfg = cfg;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     7
        this.conn = conn;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     8
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     9
    public abstract string name();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    10
}
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    11
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    12
class ModuleMuc : Module {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    13
    public enum Affiliation {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    14
        NONE,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    15
        OUTCAST,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    16
        MEMBER,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    17
        ADMIN,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    18
        OWNER
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    19
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    20
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    21
    public enum Role {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    22
        NONE,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    23
        VISITOR,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    24
        PARTICIPANT,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    25
        MODERATOR
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    26
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    27
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    28
    static Role role_from_string(string role) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    29
        switch (role) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    30
            case "moderator": return Role.MODERATOR; break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    31
            case "participant": return Role.PARTICIPANT; break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    32
            case "visitor": return Role.VISITOR; break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    33
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    34
        return Role.NONE;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    35
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    36
    static Affiliation affil_from_string(string affil) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    37
        switch (affil) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    38
            case "owner": return Affiliation.OWNER; break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    39
            case "admin": return Affiliation.ADMIN; break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    40
            case "member": return Affiliation.MEMBER; break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    41
            case "outcast": return Affiliation.OUTCAST; break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    42
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    43
        return Affiliation.NONE;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    44
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    45
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    46
    public class Occupant : Object {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    47
        public weak Conference conference;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    48
        public string nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    49
        public string real_jid;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    50
        public Affiliation affil;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    51
        public Role role;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    52
        public bool isme;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    53
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    54
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    55
    public enum State {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    56
        CONNECTED,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    57
        DISCOVERING,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    58
        CONNECTING,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    59
        DISCONNECTING,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    60
        DISCONNECTED
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    61
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    62
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    63
    public signal void state_changed(Conference conf, State old_state, State new_state, string description);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    64
    public signal void on_join(Conference conf, Occupant occupant);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    65
    public signal void on_part(Conference conf, Occupant occupant);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    66
    public signal void on_role(Conference conf, Occupant occupant, Role prev);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    67
    public signal void on_affil(Conference conf, Occupant occupant, Affiliation prev);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    68
    public signal void on_nick(Conference conf, Occupant occupant, string prev);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    69
    public signal void on_subject(Conference conf);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    70
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    71
    public class Conference : Object {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    72
        public string jid;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    73
        public string nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    74
        public string desired_nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    75
        public string subject;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    76
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    77
        protected State _state;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    78
        public Time time;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    79
        public State state { get { return _state; } }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    80
        public Gee.HashMap<string,Occupant> occupants;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    81
        protected string presence_join_id;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    82
        protected weak ModuleMuc module;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    83
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    84
        protected void _change_state(State new_state, string description) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    85
            var old_state = this._state;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    86
            if (old_state != new_state) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    87
                this._state = new_state;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    88
                stderr.printf("MUC State changed %s -> %s : %s\n",old_state.to_string(), new_state.to_string(), description);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    89
                module.state_changed(this, old_state, new_state, description);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    90
            } else
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    91
                stderr.printf("MUC State not changed %s : %s\n",old_state.to_string(), description);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    92
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    93
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    94
        public Conference(ModuleMuc module, string jid) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    95
            this.module = module;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    96
            var section = "muc "+jid;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    97
            this.desired_nick = module.cfg[section,"nick"];
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    98
            this.nick = this.desired_nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    99
            this.jid = jid;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   100
            this._state = State.DISCONNECTED;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   101
            this.occupants = new Gee.HashMap<string,Occupant>();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   102
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   103
        public void join(string desc) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   104
            if (_state == State.DISCONNECTED) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   105
                var prs = new Lm.Message(jid+"/"+desired_nick, Lm.MessageType.PRESENCE);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   106
                presence_join_id = jid+"_"+Random.next_int().to_string();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   107
                prs.node.set_attribute("id",presence_join_id);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   108
                prs.node.add_child("x",null).set_attribute("xmlns","http://jabber.org/protocol/muc");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   109
                module.conn.cn.send(prs);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   110
                nick = desired_nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   111
                _change_state(State.CONNECTING, "requested to join");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   112
            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   113
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   114
        
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   115
        public Lm.HandlerResult muc_handler(Lm.MessageHandler handler, Lm.Connection connection, Lm.Message message) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   116
            var node = message.node;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   117
            var from = node.get_attribute("from").split("/",2);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   118
            stdout.printf("MUC<%s>: %s\n",this.jid,node.to_string());
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   119
            var type = node.get_attribute("type");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   120
            if (message.get_type()==Lm.MessageType.PRESENCE) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   121
                switch (type) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   122
                    case null:
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   123
                    case "unavailable":
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   124
                        if (from.length==2) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   125
                            var statuses = new Gee.HashSet<int>();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   126
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   127
                            var x = node.get_child("x");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   128
                            var child = x!=null ? x.children : null;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   129
                            while (child != null) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   130
                                stdout.printf("Child %s %s\n", child.name, child.get_attribute("code"));
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   131
                                if (child.name == "status") {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   132
                                    var code = child.get_attribute("code");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   133
                                    if (code!=null)
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   134
                                        statuses.add(code.to_int());
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   135
                                }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   136
                                child = child.next;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   137
                            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   138
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   139
                            if (statuses.contains(110) && (_state == State.CONNECTED))
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   140
                                log("Muc", LogLevelFlags.LEVEL_INFO, "Joined a room which I was already in: %s", from[0]);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   141
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   142
                            var occupant = occupants[from[1]];
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   143
                            var item = x != null ? x.get_child("item") : null;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   144
                            var affil = Affiliation.NONE;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   145
                            var role = Role.NONE;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   146
                            string nick = null;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   147
                            if (item != null) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   148
                                affil = affil_from_string(item.get_attribute("affiliation"));
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   149
                                role = role_from_string(item.get_attribute("role")); 
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   150
                                nick = item.get_attribute("nick");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   151
                            } else
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   152
                                log("Muc", LogLevelFlags.LEVEL_ERROR, "Your MUC server is shit. No role and affiliation info in presences: %s", node.to_string());
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   153
                            if (occupant != null) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   154
                                if (role == Role.NONE) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   155
                                    occupants.unset(from[1]);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   156
                                    log("Muc", LogLevelFlags.LEVEL_INFO, "MUC<%s> %s has parted.", this.jid, from[1]);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   157
                                    module.on_part(this, occupant);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   158
                                    if (from[1]==this.nick) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   159
                                        _change_state(State.DISCONNECTED, "we became unavailable");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   160
                                        occupants.clear();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   161
                                    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   162
                                } else {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   163
                                    if (affil != occupant.affil) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   164
                                        var prev = occupant.affil;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   165
                                        occupant.affil = affil;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   166
                                        module.on_affil(this, occupant, prev);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   167
                                    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   168
                                    if (role != occupant.role) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   169
                                        var prev = occupant.role;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   170
                                        occupant.role = role;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   171
                                        module.on_role(this, occupant, prev);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   172
                                    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   173
                                    if (statuses.contains(303) && (nick!=null)) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   174
                                        occupants[nick] = occupant;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   175
                                        occupants.unset(from[1]);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   176
                                        occupant.nick = nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   177
                                        module.on_nick(this, occupant, from[1]);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   178
                                    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   179
                                }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   180
                            } else {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   181
                                assert( role!= Role.NONE);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   182
                                occupant = new Occupant();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   183
                                occupant.role = role;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   184
                                occupant.affil = affil;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   185
                                occupant.nick = from[1];
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   186
                                occupants[from[1]] = occupant;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   187
                                occupant.isme = statuses.contains(110);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   188
                                log("Muc", LogLevelFlags.LEVEL_INFO, "MUC<%s> %s has joined as %s/%s.", this.jid, from[1], affil.to_string(), role.to_string());
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   189
                                if (statuses.contains(110))
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   190
                                    _change_state(State.CONNECTED, "got own presence. we are "+this.nick);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   191
                                module.on_join(this, occupant);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   192
                            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   193
                            
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   194
                            stdout.printf("User list: ");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   195
                            foreach (var a in occupants.keys)
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   196
                                stdout.printf("%s <%s,%s>, ", a, occupants[a].affil.to_string(), occupants[a].role.to_string());
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   197
                            stdout.printf("\n");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   198
                        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   199
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   200
                        break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   201
                    case "error":
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   202
                        if ((from.length==1)||(node.get_attribute("id")==presence_join_id)) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   203
                            _change_state(State.DISCONNECTED, node.get_child("error").to_string());
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   204
                            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   205
                        } else if (from.length==2) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   206
                            //if (from[1]==this.nick) 
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   207
                        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   208
                        break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   209
                }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   210
            } else if ((message.get_type()==Lm.MessageType.MESSAGE)&&(type=="groupchat")) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   211
                var subj = node.get_attribute("subject");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   212
                if (subj != null) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   213
                    if (subj != this.subject) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   214
                        this.subject = subj;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   215
                        module.on_subject(this);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   216
                    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   217
                }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   218
            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   219
            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   220
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   221
        public void part(string desc) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   222
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   223
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   224
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   225
    public override string name() { return "muc"; }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   226
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   227
    public Gee.HashMap<string,Conference> rooms;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   228
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   229
    public ModuleMuc(Config cfg, Connection conn) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   230
        base(cfg,conn);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   231
        this.rooms = new Gee.HashMap<string,Conference>();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   232
        
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   233
        var muc_handler = new Lm.MessageHandler((handler, connection, message) => {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   234
            var node = message.node;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   235
            var from = node.get_attribute("from");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   236
            if (from != null) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   237
                var froms = from.split("/",2);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   238
                if (rooms.has_key(froms[0])) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   239
                    return rooms[froms[0]].muc_handler(handler, connection, message);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   240
                }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   241
            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   242
            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   243
        }, null);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   244
        conn.cn.register_message_handler(muc_handler, Lm.MessageType.MESSAGE, Lm.HandlerPriority.NORMAL);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   245
        conn.cn.register_message_handler(muc_handler, Lm.MessageType.PRESENCE, Lm.HandlerPriority.NORMAL);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   246
        conn.cn.register_message_handler(muc_handler, Lm.MessageType.IQ, Lm.HandlerPriority.NORMAL);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   247
        conn.state_changed.connect( (olds, news, desc) => {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   248
            if (news == Connection.State.CONNECTED) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   249
                var room = new Conference(this, "говнохост@conference.blasux.ru"); //, "Ζαλυπα");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   250
                room.join("Oh hai");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   251
                this.rooms[room.jid] = room;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   252
            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   253
        });
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   254
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   255
}