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; |
9
|
24 |
write(Time.local(time_t()), "logstart", "", "Log started"); |
3
|
25 |
} |
|
26 |
~RoomLog() { |
9
|
27 |
write(Time.local(time_t()), "logstop", "", "Log stopped"); |
|
28 |
} |
|
29 |
|
|
30 |
public static uint nick_hash(string s) { |
|
31 |
uint sum = 0; |
|
32 |
const uint k = 102, m = 54, l = 140; |
|
33 |
for( var j = 0, iTop = s.length; j < iTop; j++ ) { |
|
34 |
sum += k ^ s[j]; |
|
35 |
} |
|
36 |
return ((sum ^ m)+l)%10; |
3
|
37 |
} |
12
|
38 |
|
|
39 |
protected void real_write(Time time, string _class, string nick, string str) { |
|
40 |
var id = getid(time); |
|
41 |
var times = time.format("%H:%M:%S"); |
|
42 |
var nicklass = (_class=="message") ? (" nick%u".printf(nick_hash(nick))) : ""; |
|
43 |
|
|
44 |
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); |
|
45 |
file.printf("<td class='nick%s'>%s</td><td class='text'>%s</td></tr>\n", nicklass, nick, str); |
|
46 |
file.flush(); |
|
47 |
} |
|
48 |
|
3
|
49 |
public void write(Time time, string _class, string nick, string str) { |
|
50 |
if ((lastday != time.day)||(lastmonth != time.month)||(lastyear!=time.year)) { |
|
51 |
var fname = time.format(logpath).replace("<muc>",jid); |
|
52 |
log("muc_log", LogLevelFlags.LEVEL_INFO, "Switching log file '%s' to '%s'", filename, fname); |
12
|
53 |
bool wasntnull = false; |
|
54 |
if (file!=null) { |
|
55 |
wasntnull = true; |
|
56 |
real_write(time, "logstop", "", "Log stopped. See you in the next episode."); |
|
57 |
} |
3
|
58 |
//try { |
|
59 |
file = FileStream.open(fname,"a"); |
|
60 |
if (file==null) { |
|
61 |
int start = 0; |
|
62 |
int length = fname.length; |
|
63 |
while (true) { |
|
64 |
var pos = fname.index_of("/", start); |
|
65 |
if (pos==-1) |
|
66 |
break; |
|
67 |
stderr.printf("--------------------- %s\n", fname[0:pos]); |
|
68 |
Posix.mkdir(fname[0:pos],0777); |
|
69 |
start = pos+1; |
|
70 |
} |
|
71 |
|
|
72 |
file = FileStream.open(fname,"a"); |
|
73 |
if (file == null) { |
|
74 |
log("muc_log", LogLevelFlags.LEVEL_WARNING, "Failed to create log file '%s'", fname); |
|
75 |
return; |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
if (file.tell()==0) { |
|
80 |
var loghead = muclog.getconf(jid, "log-head",null); |
|
81 |
if (loghead!="null") { |
|
82 |
try { |
|
83 |
var headfile = new IOChannel.file(loghead,"r"); |
|
84 |
string head; |
|
85 |
size_t length; |
|
86 |
headfile.read_to_end(out head, out length); |
|
87 |
headfile.shutdown(false); |
|
88 |
file.printf("%s\n", head.replace("<muc>",jid).replace("<date>",time.format("%d.%m.%Y"))); |
|
89 |
} catch (FileError fe) { |
|
90 |
log("muc_log", LogLevelFlags.LEVEL_WARNING, "Failed to read head"); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
filename = fname; |
|
95 |
lastyear = time.year; lastmonth = time.month; lastday = time.day; |
12
|
96 |
if (wasntnull) |
|
97 |
real_write(time, "logstart", "", "Log started. This soap opera will never end."); |
3
|
98 |
} |
9
|
99 |
|
12
|
100 |
real_write(time, _class, nick, str); |
3
|
101 |
} |
|
102 |
|
|
103 |
} |
|
104 |
public string getconf(string jid, string key, string? def) { |
|
105 |
var res = cfg["muc "+jid, key]; |
|
106 |
if (res==null) res = cfg["muc", key]; |
|
107 |
if (res==null) res = def; |
|
108 |
return res; |
|
109 |
} |
|
110 |
public ModuleMucLog(Config cfg, Connection conn) { |
|
111 |
base(cfg, conn); |
|
112 |
rooms = new HashMap<string,RoomLog>(); |
|
113 |
muc = null; |
|
114 |
foreach (var module in conn.modules) { |
|
115 |
if (module.name()=="muc") |
|
116 |
muc = (ModuleMuc)module; |
|
117 |
} |
|
118 |
if (muc==null) |
|
119 |
log("muc_log", LogLevelFlags.LEVEL_ERROR, "Module 'muc' is not loaded"); |
|
120 |
muc.state_changed.connect( (conf, olds, news, desc) => { |
|
121 |
switch (news) { |
|
122 |
case ModuleMuc.State.CONNECTED: |
|
123 |
if (getconf(conf.jid, "log", "no")=="yes") { |
8
|
124 |
var room = new RoomLog(this, conf.jid); |
|
125 |
rooms[conf.jid] = room; |
|
126 |
var sb = new StringBuilder(); |
|
127 |
var notfirst = false; |
|
128 |
sb.append("Participants: "); |
|
129 |
foreach (var occupant in conf.occupants.values) { |
|
130 |
if (notfirst) |
|
131 |
sb.append(", "); |
|
132 |
else |
|
133 |
notfirst=true; |
|
134 |
sb.append(Markup.escape_text(occupant.nick).replace(" "," ")); |
|
135 |
} |
9
|
136 |
room.write(Time.local(time_t()), "userlist", "", sb.str); |
3
|
137 |
} |
|
138 |
break; |
|
139 |
case ModuleMuc.State.DISCONNECTED: |
|
140 |
rooms.unset(conf.jid); |
|
141 |
break; |
|
142 |
} |
|
143 |
}); |
|
144 |
muc.on_message.connect( (conf, user, message, body) => { |
|
145 |
if ((user!=null)&&(body!=null)&&(message.get_child("delay")==null)) { |
|
146 |
var room = rooms[conf.jid]; |
|
147 |
if (room!=null) { |
|
148 |
var nick = Markup.escape_text(user.nick).replace(" "," "); |
9
|
149 |
if (body.has_prefix("/me ")) { |
|
150 |
room.write(Time.local(time_t()), "action", "*", nick+Markup.escape_text(body[3:body.length]).replace("\n","<br/>")); |
|
151 |
} else |
|
152 |
room.write(Time.local(time_t()), "message", nick, Markup.escape_text(body).replace("\n","<br/>")); |
3
|
153 |
} |
|
154 |
} |
|
155 |
}); |
8
|
156 |
muc.on_join.connect( (conf, user) => { |
|
157 |
var room = rooms[conf.jid]; |
|
158 |
if (room!=null) { |
|
159 |
var nick = Markup.escape_text(user.nick).replace(" "," "); |
9
|
160 |
room.write(Time.local(time_t()), "join", "*", nick+" has joined the room"); |
8
|
161 |
} |
|
162 |
}); |
9
|
163 |
muc.on_part.connect( (conf, user, status) => { |
8
|
164 |
var room = rooms[conf.jid]; |
|
165 |
if (room!=null) { |
|
166 |
var nick = Markup.escape_text(user.nick).replace(" "," "); |
9
|
167 |
room.write(Time.local(time_t()), "part", "*", |
|
168 |
nick+" has left the room"+((status!=null) ? (": "+Markup.escape_text(status)) : "")); |
|
169 |
} |
|
170 |
}); |
|
171 |
muc.on_nick.connect( (conf, user, prev) => { |
|
172 |
var room = rooms[conf.jid]; |
|
173 |
if (room!=null) { |
|
174 |
var nick = Markup.escape_text(user.nick).replace(" "," "); |
|
175 |
var nickp = Markup.escape_text(prev).replace(" "," "); |
|
176 |
room.write(Time.local(time_t()), "nickchange", "*", nickp+" is now known as "+nick); |
8
|
177 |
} |
|
178 |
}); |
3
|
179 |
} |
|
180 |
public override string name() { |
|
181 |
return "muc_log"; |
|
182 |
} |
|
183 |
} |