muc_log.vala
author Stiletto <blasux@blasux.ru>
Wed, 24 Oct 2012 01:03:02 +0400
changeset 8 dfd274c668f9
parent 4 8785de25b6bd
child 9 d3260253f0bd
permissions -rw-r--r--
Join, part and participant list
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     1
using Gee;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     2
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     3
class ModuleMucLog : Module {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     4
    protected weak ModuleMuc muc;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     5
    protected HashMap<string,RoomLog> rooms;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     6
    class RoomLog : Object {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     7
        public string jid;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     8
        public string logpath;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
     9
        public string filename;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    10
        protected weak ModuleMucLog muclog;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    11
        protected FileStream file;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    12
        public string getid(Time time) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    13
            return time.format("%s");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    14
        }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    15
        protected int lastday;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    16
        protected int lastyear;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    17
        protected int lastmonth;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    18
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    19
        public RoomLog(ModuleMucLog muclog, string jid) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    20
            this.jid = jid;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    21
            this.muclog = muclog;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    22
            logpath = muclog.getconf(jid, "log-path", null);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    23
            filename = null;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    24
            write(Time.local(new time_t()), "logstart", "", "Log started");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    25
        }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    26
        ~RoomLog() {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    27
            write(Time.local(new time_t()), "logstop", "", "Log stopped");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    28
        }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    29
        public void write(Time time, string _class, string nick, string str) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    30
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    31
            if ((lastday != time.day)||(lastmonth != time.month)||(lastyear!=time.year)) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    32
                var fname = time.format(logpath).replace("<muc>",jid);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    33
                log("muc_log", LogLevelFlags.LEVEL_INFO, "Switching log file '%s' to '%s'", filename, fname);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    34
                if (file!=null) file.flush();
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    35
                //try {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    36
                    file = FileStream.open(fname,"a");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    37
                if (file==null) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    38
                    int start = 0;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    39
                    int length = fname.length;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    40
                    while (true) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    41
                        var pos = fname.index_of("/", start);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    42
                        if (pos==-1)
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    43
                            break;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    44
                        stderr.printf("--------------------- %s\n", fname[0:pos]);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    45
                        Posix.mkdir(fname[0:pos],0777);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    46
                        start = pos+1;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    47
                    } 
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    48
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    49
                    file = FileStream.open(fname,"a");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    50
                    if (file == null) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    51
                        log("muc_log", LogLevelFlags.LEVEL_WARNING, "Failed to create log file '%s'", fname);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    52
                        return;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    53
                    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    54
                }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    55
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    56
                if (file.tell()==0) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    57
                    var loghead = muclog.getconf(jid, "log-head",null);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    58
                    if (loghead!="null") {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    59
                        try {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    60
                            var headfile = new IOChannel.file(loghead,"r");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    61
                            string head;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    62
                            size_t length;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    63
                            headfile.read_to_end(out head, out length);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    64
                            headfile.shutdown(false);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    65
                            file.printf("%s\n", head.replace("<muc>",jid).replace("<date>",time.format("%d.%m.%Y")));
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    66
                        } catch (FileError fe) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    67
                            log("muc_log", LogLevelFlags.LEVEL_WARNING, "Failed to read head");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    68
                        }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    69
                    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    70
                }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    71
                filename = fname;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    72
                lastyear = time.year; lastmonth = time.month; lastday = time.day;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    73
            }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    74
            
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    75
            var id = getid(time);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    76
            var times = time.format("%H:%M:%S");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    77
            file.printf("<tr id='l_%s' class='%s'><td class='time'><a id='i_%s' href='#i_%s'>%s</a></td>", id, _class, id, id, times);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    78
            file.printf("<td class='nick'>%s</td><td class='text'>%s</td></tr>\n", nick, str);
4
8785de25b6bd Added flush()
Stiletto <blasux@blasux.ru>
parents: 3
diff changeset
    79
            file.flush();
3
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    80
        }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    81
            
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    82
    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    83
    public string getconf(string jid, string key, string? def) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    84
        var res = cfg["muc "+jid, key];
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    85
        if (res==null) res = cfg["muc", key];
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    86
        if (res==null) res = def;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    87
        return res;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    88
    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    89
    public ModuleMucLog(Config cfg, Connection conn) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    90
        base(cfg, conn);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    91
        rooms = new HashMap<string,RoomLog>();
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    92
        muc = null;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    93
        foreach (var module in conn.modules) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    94
            if (module.name()=="muc")
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    95
                muc = (ModuleMuc)module;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    96
        }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    97
        if (muc==null)
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    98
            log("muc_log", LogLevelFlags.LEVEL_ERROR, "Module 'muc' is not loaded");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    99
        muc.state_changed.connect( (conf, olds, news, desc) => {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   100
            switch (news) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   101
                case ModuleMuc.State.CONNECTED:
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   102
                    if (getconf(conf.jid, "log", "no")=="yes") {
8
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   103
                        var room = new RoomLog(this, conf.jid); 
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   104
                        rooms[conf.jid] = room;
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   105
                        var sb = new StringBuilder();
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   106
                        var notfirst = false;
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   107
                        sb.append("Participants: ");
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   108
                        foreach (var occupant in conf.occupants.values) {
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   109
                            if (notfirst)
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   110
                                sb.append(", ");
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   111
                            else
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   112
                                notfirst=true;
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   113
                            sb.append(Markup.escape_text(occupant.nick).replace(" ","&nbsp;"));
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   114
                        }
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   115
                        room.write(Time.local(new time_t()), "userlist", "", sb.str);
3
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   116
                    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   117
                    break;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   118
                case ModuleMuc.State.DISCONNECTED:
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   119
                    rooms.unset(conf.jid);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   120
                    break;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   121
            }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   122
        });
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   123
        muc.on_message.connect( (conf, user, message, body) => {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   124
            if ((user!=null)&&(body!=null)&&(message.get_child("delay")==null)) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   125
                var room = rooms[conf.jid];
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   126
                if (room!=null) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   127
                    var nick = Markup.escape_text(user.nick).replace(" ","&nbsp;");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   128
                    room.write(Time.local(new time_t()), "message", nick, Markup.escape_text(body).replace("\n","<br/>"));
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   129
                }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   130
            }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   131
        });
8
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   132
        muc.on_join.connect( (conf, user) => {
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   133
            var room = rooms[conf.jid];
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   134
            if (room!=null) {
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   135
                var nick = Markup.escape_text(user.nick).replace(" ","&nbsp;");
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   136
                room.write(Time.local(new time_t()), "join", "*", nick+" has joined the room");
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   137
            }
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   138
        });
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   139
        muc.on_part.connect( (conf, user) => {
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   140
            var room = rooms[conf.jid];
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   141
            if (room!=null) {
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   142
                var nick = Markup.escape_text(user.nick).replace(" ","&nbsp;");
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   143
                room.write(Time.local(new time_t()), "part", "*", nick+" has left the room");
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   144
            }
dfd274c668f9 Join, part and participant list
Stiletto <blasux@blasux.ru>
parents: 4
diff changeset
   145
        });
3
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   146
    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   147
    public override string name() {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   148
        return "muc_log";
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   149
    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   150
}