muc.vala
author Stiletto <blasux@blasux.ru>
Mon, 05 Nov 2012 23:54:44 +0400
changeset 12 d3e36b368fc5
parent 11 0f0cf428409f
permissions -rw-r--r--
iq, commands, makefile up
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 {
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
    13
    public static const string NS_MUC_USER = "http://jabber.org/protocol/muc#user";
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
    14
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    15
    public enum Affiliation {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    16
        NONE,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    17
        OUTCAST,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    18
        MEMBER,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    19
        ADMIN,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    20
        OWNER
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    21
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    22
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    23
    public enum Role {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    24
        NONE,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    25
        VISITOR,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    26
        PARTICIPANT,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    27
        MODERATOR
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    28
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    29
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    30
    static Role role_from_string(string role) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    31
        switch (role) {
3
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    32
            case "moderator": return Role.MODERATOR;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    33
            case "participant": return Role.PARTICIPANT;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    34
            case "visitor": return Role.VISITOR;
1
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
        return Role.NONE;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    37
    }
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    38
    static string role_to_string(Role role) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    39
        switch (role) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    40
            case Role.MODERATOR: return "moderator";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    41
            case Role.PARTICIPANT: return "participant";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    42
            case Role.VISITOR: return "visitor";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    43
        }
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    44
        return "none";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    45
    }
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    46
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    47
    static Affiliation affil_from_string(string affil) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    48
        switch (affil) {
3
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    49
            case "owner": return Affiliation.OWNER;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    50
            case "admin": return Affiliation.ADMIN;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    51
            case "member": return Affiliation.MEMBER;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    52
            case "outcast": return Affiliation.OUTCAST;
1
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
        return Affiliation.NONE;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    55
    }
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    56
    static string affil_to_string(Affiliation affil) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    57
        switch (affil) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    58
            case Affiliation.OWNER: return "owner";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    59
            case Affiliation.ADMIN: return "admin";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    60
            case Affiliation.MEMBER: return "member";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    61
            case Affiliation.OUTCAST: return "outcast";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    62
        }
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    63
        return "none";
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    64
    }
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    65
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    66
    public class Occupant : Object {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    67
        public weak Conference conference;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    68
        public string nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    69
        public string real_jid;
9
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
    70
        public string status;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    71
        public Affiliation affil;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    72
        public Role role;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    73
        public bool isme;
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    74
        public void public_message(string text) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    75
            var msg = new Lm.Message(conference.jid, Lm.MessageType.MESSAGE);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    76
            msg.node.set_attribute("type","groupchat");
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    77
            msg.node.add_child("body",nick+": "+text);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    78
            conference.module.conn.cn.send(msg);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
    79
        }
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    80
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    81
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    82
    public enum State {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    83
        CONNECTED,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    84
        DISCOVERING,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    85
        CONNECTING,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    86
        DISCONNECTING,
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    87
        DISCONNECTED
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    88
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    89
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    90
    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
    91
    public signal void on_join(Conference conf, Occupant occupant);
9
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
    92
    public signal void on_part(Conference conf, Occupant occupant, string? status);
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    93
    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
    94
    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
    95
    public signal void on_nick(Conference conf, Occupant occupant, string prev);
3
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    96
    public signal void on_message(Conference conf, Occupant? occupant, Lm.MessageNode message, string? body);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents: 2
diff changeset
    97
    public signal void on_subject(Conference conf, Occupant? occupant);
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    98
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    99
    public class Conference : Object {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   100
        public string jid;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   101
        public string nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   102
        public string desired_nick;
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   103
        public bool enabled;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   104
        public string subject;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   105
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   106
        protected State _state;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   107
        public Time time;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   108
        public State state { get { return _state; } }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   109
        public Gee.HashMap<string,Occupant> occupants;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   110
        protected string presence_join_id;
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   111
        public string pingcheck_id;
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   112
        public time_t last_check;
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   113
        public weak ModuleMuc module;
1
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
        protected void _change_state(State new_state, string description) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   116
            var old_state = this._state;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   117
            if (old_state != new_state) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   118
                this._state = new_state;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   119
                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
   120
                module.state_changed(this, old_state, new_state, description);
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   121
                if (new_state == State.DISCONNECTED)
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   122
                    occupants.clear();
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   123
            } else
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   124
                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
   125
        }
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
        public Conference(ModuleMuc module, string jid) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   128
            this.module = module;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   129
            var section = "muc "+jid;
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   130
            this.desired_nick = module.cfg.get_default(section,"nick",module.cfg.get_default("muc","nick",Random.next_int().to_string()));
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   131
            this.nick = this.desired_nick;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   132
            this.jid = jid;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   133
            this._state = State.DISCONNECTED;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   134
            this.occupants = new Gee.HashMap<string,Occupant>();
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
        public void join(string desc) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   137
            if (_state == State.DISCONNECTED) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   138
                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
   139
                presence_join_id = jid+"_"+Random.next_int().to_string();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   140
                prs.node.set_attribute("id",presence_join_id);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   141
                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
   142
                module.conn.cn.send(prs);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   143
                nick = desired_nick;
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   144
                _change_state(State.CONNECTING, "requested to join: "+desc);
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   145
            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   146
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   147
        
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   148
        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
   149
            var node = message.node;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   150
            var from = node.get_attribute("from").split("/",2);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   151
            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
   152
            var type = node.get_attribute("type");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   153
            if (message.get_type()==Lm.MessageType.PRESENCE) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   154
                switch (type) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   155
                    case null:
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   156
                    case "unavailable":
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   157
                        if (from.length==2) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   158
                            var statuses = new Gee.HashSet<int>();
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   159
                            var occupant = occupants[from[1]];
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   160
                            var affil = Affiliation.NONE;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   161
                            var role = Role.NONE;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   162
                            string prs_nick = null;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   163
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   164
                            var chn = node.children;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   165
                            while (chn != null) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   166
                                switch (chn.name) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   167
                                    
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   168
                                    case "x":
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   169
                                        switch (chn.get_attribute("xmlns")) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   170
                                            case NS_MUC_USER:
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   171
                                                var child = chn.children;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   172
                                                while (child != null) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   173
                                                    stdout.printf("Child %s %s\n", child.name, child.get_attribute("code"));
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   174
                                                    if (child.name == "status") {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   175
                                                        var code = child.get_attribute("code");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   176
                                                        if (code!=null)
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   177
                                                            statuses.add(code.to_int());
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   178
                                                    }
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   179
                                                    child = child.next;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   180
                                                }
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   181
                                                var item = chn.get_child("item");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   182
                                                if (item == null)
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   183
                                                    log("Muc", LogLevelFlags.LEVEL_ERROR, "Your MUC server is shit. No role and affiliation info in presences: %s", node.to_string());
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   184
                                                affil = affil_from_string(item.get_attribute("affiliation"));
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   185
                                                role = role_from_string(item.get_attribute("role")); 
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   186
                                                prs_nick = item.get_attribute("nick");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   187
                                                break;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   188
                                        }
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   189
                                        break;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   190
                                }
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   191
                                chn = chn.next;
1
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
                            if (statuses.contains(110) && (_state == State.CONNECTED))
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   195
                                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
   196
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   197
                            if (occupant != null) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   198
                                if (role == Role.NONE) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   199
                                    occupants.unset(from[1]);
9
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   200
                                    var status_child = node.get_child("status");
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   201
                                    log("Muc", LogLevelFlags.LEVEL_INFO, "MUC<%s> %s has parted.", this.jid, from[1]);
9
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   202
                                    module.on_part(this, occupant, (status_child!=null) ? status_child.get_value() : null);
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   203
                                    if (occupant.isme) {
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   204
                                        _change_state(State.DISCONNECTED, "we became unavailable");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   205
                                    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   206
                                } else {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   207
                                    if (affil != occupant.affil) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   208
                                        var prev = occupant.affil;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   209
                                        occupant.affil = affil;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   210
                                        module.on_affil(this, occupant, prev);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   211
                                    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   212
                                    if (role != occupant.role) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   213
                                        var prev = occupant.role;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   214
                                        occupant.role = role;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   215
                                        module.on_role(this, occupant, prev);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   216
                                    }
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   217
                                    if (statuses.contains(303) && (prs_nick!=null)) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   218
                                        occupants[prs_nick] = occupant;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   219
                                        occupants.unset(from[1]);
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   220
                                        occupant.nick = prs_nick;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   221
                                        module.on_nick(this, occupant, from[1]);
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
                            } else {
9
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   225
                                if( role!= Role.NONE) {
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   226
                                    occupant = new Occupant();
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   227
                                    occupant.role = role;
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   228
                                    occupant.affil = affil;
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   229
                                    occupant.nick = from[1];
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   230
                                    occupant.conference = this;
9
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   231
                                    occupants[from[1]] = occupant;
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   232
                                    occupant.isme = statuses.contains(110);
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   233
                                    log("Muc", LogLevelFlags.LEVEL_INFO, "MUC<%s> %s has joined as %s/%s.", this.jid, from[1], affil.to_string(), role.to_string());
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   234
                                    if (statuses.contains(110)) {
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   235
                                        this.nick = from[1];
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   236
                                        _change_state(State.CONNECTED, "got own presence. we are "+this.nick);
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   237
                                    }
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   238
                                    module.on_join(this, occupant);
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   239
                                } else {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   240
                                    if (statuses.contains(110))
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   241
                                        _change_state(State.DISCONNECTED, "got role none while joining");
9
d3260253f0bd Moved nick colors to vala code
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
   242
                                    log("Muc", LogLevelFlags.LEVEL_WARNING, "Got NONE role for new participant. Maybe we are reconnecting.");
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   243
                                }
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   244
                            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   245
                            
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   246
                            stdout.printf("User list: ");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   247
                            foreach (var a in occupants.keys)
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   248
                                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
   249
                            stdout.printf("\n");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   250
                        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   251
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   252
                        break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   253
                    case "error":
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   254
                        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
   255
                            _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
   256
                            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   257
                        } else if (from.length==2) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   258
                            //if (from[1]==this.nick) 
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   259
                        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   260
                        break;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   261
                }
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   262
            } else if (message.get_type()==Lm.MessageType.MESSAGE)
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   263
                switch (type) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   264
                    case "groupchat":
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   265
                        var subj = node.get_attribute("subject");
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   266
                        var occupant = (from.length > 1) ? occupants[from[1]] : null;
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   267
                        if (subj != null) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   268
                            if (subj != this.subject) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   269
                                this.subject = subj;
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   270
                                module.on_subject(this, occupant);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   271
                            }
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   272
                        } else {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   273
                            var body = node.find_child("body");
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   274
                            module.on_message(this, occupant, node, (body!=null) ? body.get_value() : null);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   275
                        }
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   276
                        break;
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   277
                    case "error":
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   278
                        if (node.get_attribute("id")==pingcheck_id) {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   279
                            _change_state(State.DISCONNECTED, node.get_child("error").to_string());
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   280
                        }
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   281
                        break;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   282
                }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   283
            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   284
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   285
        public void part(string desc) {
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   286
            if (_state != State.DISCONNECTED) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   287
                var prs = new Lm.Message(jid+"/"+desired_nick, Lm.MessageType.PRESENCE);
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   288
                presence_join_id = jid+"_"+Random.next_int().to_string();
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   289
                prs.node.set_attribute("type","unavailable");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   290
                prs.node.add_child("x",null).set_attribute("xmlns","http://jabber.org/protocol/muc");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   291
                module.conn.cn.send(prs);
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   292
                nick = desired_nick;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   293
            }
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   294
        }
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   295
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   296
        public void connection_lost() {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   297
            _change_state(State.DISCONNECTED, "connection lost");
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   298
        }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   299
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   300
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   301
    public override string name() { return "muc"; }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   302
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   303
    public Gee.HashMap<string,Conference> rooms;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   304
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   305
    public ModuleMuc(Config cfg, Connection conn) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   306
        base(cfg,conn);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   307
        this.rooms = new Gee.HashMap<string,Conference>();
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   308
        
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   309
        var muc_handler = new Lm.MessageHandler((handler, connection, message) => {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   310
            var node = message.node;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   311
            var from = node.get_attribute("from");
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   312
            if (from != null) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   313
                var froms = from.split("/",2);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   314
                if (rooms.has_key(froms[0])) {
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   315
                    return rooms[froms[0]].muc_handler(handler, connection, message);
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   316
                }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   317
            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   318
            return Lm.HandlerResult.ALLOW_MORE_HANDLERS;
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   319
        }, null);
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   320
        var names = cfg.get_default("muc","mucs","").split(" ");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   321
        foreach (var name in names) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   322
            var room = new Conference(this, name); //, "Ζαλυπα");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   323
            //room.join("Oh hai");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   324
            room.enabled = true;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   325
            this.rooms[room.jid] = room;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   326
        }
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   327
        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
   328
        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
   329
        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
   330
        conn.state_changed.connect( (olds, news, desc) => {
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   331
            switch (news) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   332
                case Connection.State.CONNECTED:
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   333
                    break;
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   334
                case Connection.State.DISCONNECTED:
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   335
                    foreach(var room in rooms.values)
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   336
                        room.connection_lost();
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   337
                    break;
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   338
            }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   339
        });
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   340
        conn.check_time.connect( () => {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   341
            if (conn.state == Connection.State.CONNECTED)
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   342
                foreach (var room in rooms.values) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   343
                    if (room.enabled) {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   344
                        if (room.state == State.DISCONNECTED)
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   345
                            room.join("Reconnect");
11
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   346
                        else {
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   347
                            time_t curtime = time_t();
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   348
                            if ((curtime - room.last_check) > 60) { // crappy check for netsplits
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   349
                                room.last_check = curtime;
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   350
                                var msg = new Lm.Message(room.jid, Lm.MessageType.MESSAGE);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   351
                                room.pingcheck_id = room.jid+"_"+Random.next_int().to_string();
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   352
                                msg.node.set_attribute("id",room.pingcheck_id);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   353
                                log("Muc", LogLevelFlags.LEVEL_DEBUG, "Ping check to %s", room.jid);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   354
                                room.module.conn.cn.send(msg);
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   355
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   356
                            }
0f0cf428409f Crappy reconnector
Stiletto <blasux@blasux.ru>
parents: 9
diff changeset
   357
                        }
2
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   358
                    } else {
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   359
                        if (room.state == State.CONNECTED)
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   360
                            room.part("Disabled");
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   361
                    }
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   362
                }
4e050075fab9 Level of stubbornness skyrocketed
Stiletto <blasux@blasux.ru>
parents: 1
diff changeset
   363
        });
1
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   364
    }
76caf6a3f413 Somehow compiles and even joins a conference
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   365
}