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