muc_log.vala
author Stiletto <blasux@blasux.ru>
Tue, 23 Oct 2012 23:50:28 +0400
changeset 3 dd7a02c6d476
child 4 8785de25b6bd
permissions -rw-r--r--
muc log, updated example config
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
            file.flush();
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    29
        }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    30
        public void write(Time time, string _class, string nick, string str) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    31
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    32
            if ((lastday != time.day)||(lastmonth != time.month)||(lastyear!=time.year)) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    33
                var fname = time.format(logpath).replace("<muc>",jid);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    34
                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
    35
                if (file!=null) file.flush();
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    36
                //try {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    37
                    file = FileStream.open(fname,"a");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    38
                if (file==null) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    39
                    int start = 0;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    40
                    int length = fname.length;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    41
                    while (true) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    42
                        var pos = fname.index_of("/", start);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    43
                        if (pos==-1)
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    44
                            break;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    45
                        stderr.printf("--------------------- %s\n", fname[0:pos]);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    46
                        Posix.mkdir(fname[0:pos],0777);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    47
                        start = pos+1;
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
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    50
                    file = FileStream.open(fname,"a");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    51
                    if (file == null) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    52
                        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
    53
                        return;
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
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    57
                if (file.tell()==0) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    58
                    var loghead = muclog.getconf(jid, "log-head",null);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    59
                    if (loghead!="null") {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    60
                        try {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    61
                            var headfile = new IOChannel.file(loghead,"r");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    62
                            string head;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    63
                            size_t length;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    64
                            headfile.read_to_end(out head, out length);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    65
                            headfile.shutdown(false);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    66
                            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
    67
                        } catch (FileError fe) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    68
                            log("muc_log", LogLevelFlags.LEVEL_WARNING, "Failed to read head");
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
                }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    72
                filename = fname;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    73
                lastyear = time.year; lastmonth = time.month; lastday = time.day;
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
            
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    76
            var id = getid(time);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    77
            var times = time.format("%H:%M:%S");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
    78
            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
    79
            file.printf("<td class='nick'>%s</td><td class='text'>%s</td></tr>\n", nick, str);
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") {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   103
                        rooms[conf.jid] = new RoomLog(this, conf.jid);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   104
                    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   105
                    break;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   106
                case ModuleMuc.State.DISCONNECTED:
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   107
                    rooms.unset(conf.jid);
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   108
                    break;
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   109
            }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   110
        });
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   111
        muc.on_message.connect( (conf, user, message, body) => {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   112
            if ((user!=null)&&(body!=null)&&(message.get_child("delay")==null)) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   113
                var room = rooms[conf.jid];
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   114
                if (room!=null) {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   115
                    var nick = Markup.escape_text(user.nick).replace(" ","&nbsp;");
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   116
                    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
   117
                }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   118
            }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   119
        });
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   120
    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   121
    public override string name() {
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   122
        return "muc_log";
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   123
    }
dd7a02c6d476 muc log, updated example config
Stiletto <blasux@blasux.ru>
parents:
diff changeset
   124
}