1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
#include "module.h"
#include "modules/sql.h"
class CommandCSSetChanstats : public Command
{
public:
CommandCSSetChanstats(Module *creator) : Command(creator, "chanserv/set/chanstats", 2, 2)
{
this->SetDesc(_("Turn chanstats statistics on or off"));
this->SetSyntax(_("\037channel\037 {ON | OFF}"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
ChanServ::Channel *ci = ChanServ::Find(params[0]);
if (!ci)
{
source.Reply(CHAN_X_NOT_REGISTERED, params[0].c_str());
return;
}
EventReturn MOD_RESULT;
MOD_RESULT = EventManager::Get()->Dispatch(&Event::SetChannelOption::OnSetChannelOption, source, this, ci, params[1]);
if (MOD_RESULT == EVENT_STOP)
return;
if (MOD_RESULT != EVENT_ALLOW && !source.AccessFor(ci).HasPriv("SET") && source.permission.empty() && !source.HasPriv("chanserv/administration"))
{
source.Reply(ACCESS_DENIED);
return;
}
if (params[1].equals_ci("ON"))
{
ci->Extend<bool>("CS_STATS");
source.Reply(_("Chanstats statistics are now enabled for this channel."));
Log(source.AccessFor(ci).HasPriv("SET") ? LogType::COMMAND : LogType::OVERRIDE, source, this, ci) << "to enable chanstats";
}
else if (params[1].equals_ci("OFF"))
{
Log(source.AccessFor(ci).HasPriv("SET") ? LogType::COMMAND : LogType::OVERRIDE, source, this, ci) << "to disable chanstats";
ci->Shrink<bool>("CS_STATS");
source.Reply(_("Chanstats statistics are now disabled for this channel."));
}
else
this->OnSyntaxError(source);
}
bool OnHelp(CommandSource &source, const Anope::string &) override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply("Turn Chanstats channel statistics ON or OFF.");
return true;
}
};
class CommandNSSetChanstats : public Command
{
public:
CommandNSSetChanstats(Module *creator, const Anope::string &sname = "nickserv/set/chanstats", size_t min = 1 ) : Command(creator, sname, min, min + 1)
{
this->SetDesc(_("Turn chanstats statistics on or off"));
this->SetSyntax("{ON | OFF}");
}
void Run(CommandSource &source, const Anope::string &user, const Anope::string ¶m, bool saset = false)
{
NickServ::Nick *na = NickServ::FindNick(user);
if (!na)
{
source.Reply(NICK_X_NOT_REGISTERED, user.c_str());
return;
}
EventReturn MOD_RESULT;
MOD_RESULT = EventManager::Get()->Dispatch(&Event::SetNickOption::OnSetNickOption, source, this, na->GetAccount(), param);
if (MOD_RESULT == EVENT_STOP)
return;
if (param.equals_ci("ON"))
{
Log(na->GetAccount() == source.GetAccount() ? LogType::COMMAND : LogType::ADMIN, source, this) << "to enable chanstats for " << na->GetAccount()->GetDisplay();
na->GetAccount()->Extend<bool>("NS_STATS");
if (saset)
source.Reply(_("Chanstats statistics are now enabled for %s"), na->GetAccount()->GetDisplay().c_str());
else
source.Reply(_("Chanstats statistics are now enabled for your nick."));
}
else if (param.equals_ci("OFF"))
{
Log(na->GetAccount() == source.GetAccount() ? LogType::COMMAND : LogType::ADMIN, source, this) << "to disable chanstats for " << na->GetAccount()->GetDisplay();
na->GetAccount()->Shrink<bool>("NS_STATS");
if (saset)
source.Reply(_("Chanstats statistics are now disabled for %s"), na->GetAccount()->GetDisplay().c_str());
else
source.Reply(_("Chanstats statistics are now disabled for your nick."));
}
else
this->OnSyntaxError(source);
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
this->Run(source, source.nc->GetDisplay(), params[0]);
}
bool OnHelp(CommandSource &source, const Anope::string &) override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("Turns Chanstats statistics ON or OFF."));
return true;
}
};
class CommandNSSASetChanstats : public CommandNSSetChanstats
{
public:
CommandNSSASetChanstats(Module *creator) : CommandNSSetChanstats(creator, "nickserv/saset/chanstats", 2)
{
this->ClearSyntax();
this->SetSyntax(_("\037nickname\037 {ON | OFF}"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
this->Run(source, params[0], params[1], true);
}
bool OnHelp(CommandSource &source, const Anope::string &) override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("Turns chanstats channel statistics ON or OFF for this user."));
return true;
}
};
class MySQLInterface : public SQL::Interface
{
public:
MySQLInterface(Module *o) : SQL::Interface(o) { }
void OnResult(const SQL::Result &r) override
{
}
void OnError(const SQL::Result &r) override
{
if (!r.GetQuery().query.empty())
Log(LogType::DEBUG) << "Chanstats: Error executing query " << r.finished_query << ": " << r.GetError();
else
Log(LogType::DEBUG) << "Chanstats: Error executing query: " << r.GetError();
}
};
class MChanstats : public Module
{
Serialize::Field<bool> cs_stats, ns_stats;
CommandCSSetChanstats commandcssetchanstats;
CommandNSSetChanstats commandnssetchanstats;
CommandNSSASetChanstats commandnssasetchanstats;
ServiceReference<SQL::Provider> sql;
MySQLInterface sqlinterface;
SQL::Query query;
Anope::string SmileysHappy, SmileysSad, SmileysOther, prefix;
std::vector<Anope::string> TableList, ProcedureList, EventList;
bool NSDefChanstats, CSDefChanstats;
void RunQuery(const SQL::Query &q)
{
if (sql)
sql->Run(&sqlinterface, q);
}
size_t CountWords(const Anope::string &msg)
{
size_t words = 0;
for (size_t pos = 0; pos != Anope::string::npos; pos = msg.find(" ", pos+1))
words++;
return words;
}
size_t CountSmileys(const Anope::string &msg, const Anope::string &smileylist)
{
size_t smileys = 0;
spacesepstream sep(smileylist);
Anope::string buf;
while (sep.GetToken(buf) && !buf.empty())
{
for (size_t pos = msg.find(buf, 0); pos != Anope::string::npos; pos = msg.find(buf, pos+1))
smileys++;
}
return smileys;
}
const Anope::string GetDisplay(User *u)
{
if (u && u->Account() && ns_stats.HasExt(u->Account()))
return u->Account()->GetDisplay();
else
return "";
}
void GetTables()
{
TableList.clear();
ProcedureList.clear();
EventList.clear();
if (!sql)
return;
SQL::Result r = this->sql->RunQuery(this->sql->GetTables(prefix));
for (int i = 0; i < r.Rows(); ++i)
{
const std::map<Anope::string, Anope::string> &map = r.Row(i);
for (std::map<Anope::string, Anope::string>::const_iterator it = map.begin(); it != map.end(); ++it)
TableList.push_back(it->second);
}
query = "SHOW PROCEDURE STATUS WHERE `Db` = Database();";
r = this->sql->RunQuery(query);
for (int i = 0; i < r.Rows(); ++i)
{
ProcedureList.push_back(r.Get(i, "Name"));
}
query = "SHOW EVENTS WHERE `Db` = Database();";
r = this->sql->RunQuery(query);
for (int i = 0; i < r.Rows(); ++i)
{
EventList.push_back(r.Get(i, "Name"));
}
}
bool HasTable(const Anope::string &table)
{
for (std::vector<Anope::string>::const_iterator it = TableList.begin(); it != TableList.end(); ++it)
if (*it == table)
return true;
return false;
}
bool HasProcedure(const Anope::string &table)
{
for (std::vector<Anope::string>::const_iterator it = ProcedureList.begin(); it != ProcedureList.end(); ++it)
if (*it == table)
return true;
return false;
}
bool HasEvent(const Anope::string &table)
{
for (std::vector<Anope::string>::const_iterator it = EventList.begin(); it != EventList.end(); ++it)
if (*it == table)
return true;
return false;
}
void CheckTables()
{
this->GetTables();
if (!this->HasTable(prefix +"chanstats"))
{
query = "CREATE TABLE `" + prefix + "chanstats` ("
"`id` int(11) NOT NULL AUTO_INCREMENT,"
"`chan` varchar(64) NOT NULL DEFAULT '',"
"`nick` varchar(64) NOT NULL DEFAULT '',"
"`type` ENUM('total', 'monthly', 'weekly', 'daily') NOT NULL,"
"`letters` int(10) unsigned NOT NULL DEFAULT '0',"
"`words` int(10) unsigned NOT NULL DEFAULT '0',"
"`line` int(10) unsigned NOT NULL DEFAULT '0',"
"`actions` int(10) unsigned NOT NULL DEFAULT '0',"
"`smileys_happy` int(10) unsigned NOT NULL DEFAULT '0',"
"`smileys_sad` int(10) unsigned NOT NULL DEFAULT '0',"
"`smileys_other` int(10) unsigned NOT NULL DEFAULT '0',"
"`kicks` int(10) unsigned NOT NULL DEFAULT '0',"
"`kicked` int(10) unsigned NOT NULL DEFAULT '0',"
"`modes` int(10) unsigned NOT NULL DEFAULT '0',"
"`topics` int(10) unsigned NOT NULL DEFAULT '0',"
"`time0` int(10) unsigned NOT NULL default '0',"
"`time1` int(10) unsigned NOT NULL default '0',"
"`time2` int(10) unsigned NOT NULL default '0',"
"`time3` int(10) unsigned NOT NULL default '0',"
"`time4` int(10) unsigned NOT NULL default '0',"
"`time5` int(10) unsigned NOT NULL default '0',"
"`time6` int(10) unsigned NOT NULL default '0',"
"`time7` int(10) unsigned NOT NULL default '0',"
"`time8` int(10) unsigned NOT NULL default '0',"
"`time9` int(10) unsigned NOT NULL default '0',"
"`time10` int(10) unsigned NOT NULL default '0',"
"`time11` int(10) unsigned NOT NULL default '0',"
"`time12` int(10) unsigned NOT NULL default '0',"
"`time13` int(10) unsigned NOT NULL default '0',"
"`time14` int(10) unsigned NOT NULL default '0',"
"`time15` int(10) unsigned NOT NULL default '0',"
"`time16` int(10) unsigned NOT NULL default '0',"
"`time17` int(10) unsigned NOT NULL default '0',"
"`time18` int(10) unsigned NOT NULL default '0',"
"`time19` int(10) unsigned NOT NULL default '0',"
"`time20` int(10) unsigned NOT NULL default '0',"
"`time21` int(10) unsigned NOT NULL default '0',"
"`time22` int(10) unsigned NOT NULL default '0',"
"`time23` int(10) unsigned NOT NULL default '0',"
"PRIMARY KEY (`id`),"
"UNIQUE KEY `chan` (`chan`,`nick`,`type`),"
"KEY `nick` (`nick`),"
"KEY `chan_` (`chan`),"
"KEY `type` (`type`)"
") ENGINE=MyISAM DEFAULT CHARSET=utf8;";
this->RunQuery(query);
}
/* There is no CREATE OR REPLACE PROCEDURE in MySQL */
if (this->HasProcedure(prefix + "chanstats_proc_update"))
{
query = "DROP PROCEDURE " + prefix + "chanstats_proc_update";
this->RunQuery(query);
}
query = "CREATE PROCEDURE `" + prefix + "chanstats_proc_update`"
"(chan_ VARCHAR(255), nick_ VARCHAR(255), line_ INT(10), letters_ INT(10),"
"words_ INT(10), actions_ INT(10), sm_h_ INT(10), sm_s_ INT(10), sm_o_ INT(10),"
"kicks_ INT(10), kicked_ INT(10), modes_ INT(10), topics_ INT(10))"
"BEGIN "
"DECLARE time_ VARCHAR(20);"
"SET time_ = CONCAT('time', hour(now()));"
"INSERT IGNORE INTO `" + prefix + "chanstats` (`nick`,`chan`, `type`) VALUES "
"('', chan_, 'total'), ('', chan_, 'monthly'),"
"('', chan_, 'weekly'), ('', chan_, 'daily');"
"IF nick_ != '' THEN "
"INSERT IGNORE INTO `" + prefix + "chanstats` (`nick`,`chan`, `type`) VALUES "
"(nick_, chan_, 'total'), (nick_, chan_, 'monthly'),"
"(nick_, chan_, 'weekly'),(nick_, chan_, 'daily'),"
"(nick_, '', 'total'), (nick_, '', 'monthly'),"
"(nick_, '', 'weekly'), (nick_, '', 'daily');"
"END IF;"
"SET @update_query = CONCAT('UPDATE `" + prefix + "chanstats` SET line=line+', line_, ',"
"letters=letters+', letters_, ' , words=words+', words_, ', actions=actions+', actions_, ', "
"smileys_happy=smileys_happy+', sm_h_, ', smileys_sad=smileys_sad+', sm_s_, ', "
"smileys_other=smileys_other+', sm_o_, ', kicks=kicks+', kicks_, ', kicked=kicked+', kicked_, ', "
"modes=modes+', modes_, ', topics=topics+', topics_, ', ', time_ , '=', time_, '+', line_ ,' "
"WHERE (nick='''' OR nick=''', nick_, ''') AND (chan='''' OR chan=''', chan_, ''')');"
"PREPARE update_query FROM @update_query;"
"EXECUTE update_query;"
"DEALLOCATE PREPARE update_query;"
"END";
this->RunQuery(query);
if (this->HasProcedure(prefix + "chanstats_proc_chgdisplay"))
{
query = "DROP PROCEDURE " + prefix + "chanstats_proc_chgdisplay;";
this->RunQuery(query);
}
query = "CREATE PROCEDURE `" + prefix + "chanstats_proc_chgdisplay`"
"(old_nick varchar(255), new_nick varchar(255))"
"BEGIN "
"DECLARE res_count int(10) unsigned;"
"SELECT COUNT(nick) INTO res_count FROM `" + prefix + "chanstats` WHERE nick = new_nick;"
"IF res_count = 0 THEN "
"UPDATE `" + prefix + "chanstats` SET `nick` = new_nick WHERE `nick` = old_nick;"
"ELSE "
"my_cursor: BEGIN "
"DECLARE no_more_rows BOOLEAN DEFAULT FALSE;"
"DECLARE chan_ VARCHAR(255);"
"DECLARE type_ ENUM('total', 'monthly', 'weekly', 'daily');"
"DECLARE letters_, words_, line_, actions_, smileys_happy_,"
"smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_,"
"time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_, time9_,"
"time10_, time11_, time12_, time13_, time14_, time15_, time16_, time17_, time18_,"
"time19_, time20_, time21_, time22_, time23_ INT(10) unsigned;"
"DECLARE stats_cursor CURSOR FOR "
"SELECT chan, type, letters, words, line, actions, smileys_happy,"
"smileys_sad, smileys_other, kicks, kicked, modes, topics, time0, time1,"
"time2, time3, time4, time5, time6, time7, time8, time9, time10, time11,"
"time12, time13, time14, time15, time16, time17, time18, time19, time20,"
"time21, time22, time23 "
"FROM `" + prefix + "chanstats` "
"WHERE `nick` = old_nick;"
"DECLARE CONTINUE HANDLER FOR NOT FOUND "
"SET no_more_rows = TRUE;"
"OPEN stats_cursor;"
"the_loop: LOOP "
"FETCH stats_cursor "
"INTO chan_, type_, letters_, words_, line_, actions_, smileys_happy_,"
"smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_,"
"time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_,"
"time9_, time10_, time11_, time12_, time13_, time14_, time15_, time16_,"
"time17_, time18_, time19_, time20_, time21_, time22_, time23_;"
"IF no_more_rows THEN "
"CLOSE stats_cursor;"
"LEAVE the_loop;"
"END IF;"
"INSERT INTO `" + prefix + "chanstats` "
"(chan, nick, type, letters, words, line, actions, smileys_happy, "
"smileys_sad, smileys_other, kicks, kicked, modes, topics, time0, time1, "
"time2, time3, time4, time5, time6, time7, time8, time9, time10, time11,"
"time12, time13, time14, time15, time16, time17, time18, time19, time20,"
"time21, time22, time23)"
"VALUES (chan_, new_nick, type_, letters_, words_, line_, actions_, smileys_happy_,"
"smileys_sad_, smileys_other_, kicks_, kicked_, modes_, topics_,"
"time0_, time1_, time2_, time3_, time4_, time5_, time6_, time7_, time8_, "
"time9_, time10_, time11_, time12_, time13_, time14_, time15_, time16_, "
"time17_, time18_, time19_, time20_, time21_, time22_, time23_)"
"ON DUPLICATE KEY UPDATE letters=letters+VALUES(letters), words=words+VALUES(words),"
"line=line+VALUES(line), actions=actions+VALUES(actions),"
"smileys_happy=smileys_happy+VALUES(smileys_happy),"
"smileys_sad=smileys_sad+VALUES(smileys_sad),"
"smileys_other=smileys_other+VALUES(smileys_other),"
"kicks=kicks+VALUES(kicks), kicked=kicked+VALUES(kicked),"
"modes=modes+VALUES(modes), topics=topics+VALUES(topics),"
"time1=time1+VALUES(time1), time2=time2+VALUES(time2), time3=time3+VALUES(time3),"
"time4=time4+VALUES(time4), time5=time5+VALUES(time5), time6=time6+VALUES(time6),"
"time7=time7+VALUES(time7), time8=time8+VALUES(time8), time9=time9+VALUES(time9),"
"time10=time10+VALUES(time10), time11=time11+VALUES(time11), time12=time12+VALUES(time12),"
"time13=time13+VALUES(time13), time14=time14+VALUES(time14), time15=time15+VALUES(time15),"
"time16=time16+VALUES(time16), time17=time17+VALUES(time17), time18=time18+VALUES(time18),"
"time19=time19+VALUES(time19), time20=time20+VALUES(time20), time21=time21+VALUES(time21),"
"time22=time22+VALUES(time22), time23=time23+VALUES(time23);"
"END LOOP;"
"DELETE FROM `" + prefix + "chanstats` WHERE `nick` = old_nick;"
"END my_cursor;"
"END IF;"
"END;";
this->RunQuery(query);
/* don't prepend any database prefix to events so we can always delete/change old events */
if (this->HasEvent("chanstats_event_cleanup_daily"))
{
query = "DROP EVENT chanstats_event_cleanup_daily";
this->RunQuery(query);
}
query = "CREATE EVENT `chanstats_event_cleanup_daily` "
"ON SCHEDULE EVERY 1 DAY STARTS CURRENT_DATE "
"DO UPDATE `" + prefix + "chanstats` SET letters=0, words=0, line=0, actions=0, smileys_happy=0,"
"smileys_sad=0, smileys_other=0, kicks=0, modes=0, topics=0, time0=0, time1=0, time2=0,"
"time3=0, time4=0, time5=0, time6=0, time7=0, time8=0, time9=0, time10=0, time11=0,"
"time12=0, time13=0, time14=0, time15=0, time16=0, time17=0, time18=0, time19=0,"
"time20=0, time21=0, time22=0, time23=0 "
"WHERE type='daily';";
this->RunQuery(query);
if (this->HasEvent("chanstats_event_cleanup_weekly"))
{
query = "DROP EVENT `chanstats_event_cleanup_weekly`";
this->RunQuery(query);
}
query = "CREATE EVENT `chanstats_event_cleanup_weekly` "
"ON SCHEDULE EVERY 1 WEEK STARTS ADDDATE(CURDATE(), INTERVAL 1-DAYOFWEEK(CURDATE()) DAY) "
"DO UPDATE `" + prefix + "chanstats` SET letters=0, words=0, line=0, actions=0, smileys_happy=0,"
"smileys_sad=0, smileys_other=0, kicks=0, modes=0, topics=0, time0=0, time1=0, time2=0,"
"time3=0, time4=0, time5=0, time6=0, time7=0, time8=0, time9=0, time10=0, time11=0,"
"time12=0, time13=0, time14=0, time15=0, time16=0, time17=0, time18=0, time19=0,"
"time20=0, time21=0, time22=0, time23=0 "
"WHERE type='weekly';";
this->RunQuery(query);
if (this->HasEvent("chanstats_event_cleanup_monthly"))
{
query = "DROP EVENT `chanstats_event_cleanup_monthly`;";
this->RunQuery(query);
}
query = "CREATE EVENT `chanstats_event_cleanup_monthly` "
"ON SCHEDULE EVERY 1 MONTH STARTS LAST_DAY(CURRENT_TIMESTAMP) + INTERVAL 1 DAY "
"DO BEGIN "
"UPDATE `" + prefix + "chanstats` SET letters=0, words=0, line=0, actions=0, smileys_happy=0,"
"smileys_sad=0, smileys_other=0, kicks=0, modes=0, topics=0, time0=0, time1=0, time2=0,"
"time3=0, time4=0, time5=0, time6=0, time7=0, time8=0, time9=0, time10=0, time11=0,"
"time12=0, time13=0, time14=0, time15=0, time16=0, time17=0, time18=0, time19=0, "
"time20=0, time21=0, time22=0, time23=0 "
"WHERE type='monthly';"
"OPTIMIZE TABLE `" + prefix + "chanstats`;"
"END;";
this->RunQuery(query);
}
public:
MChanstats(const Anope::string &modname, const Anope::string &creator) :
Module(modname, creator, EXTRA | VENDOR),
cs_stats(this, "CS_STATS"), ns_stats(this, "NS_STATS"),
commandcssetchanstats(this), commandnssetchanstats(this), commandnssasetchanstats(this),
sqlinterface(this)
{
}
void OnReload(Configuration::Conf *conf) override
{
Configuration::Block *block = conf->GetModule(this);
prefix = block->Get<Anope::string>("prefix", "anope_");
SmileysHappy = block->Get<Anope::string>("SmileysHappy");
SmileysSad = block->Get<Anope::string>("SmileysSad");
SmileysOther = block->Get<Anope::string>("SmileysOther");
NSDefChanstats = block->Get<bool>("ns_def_chanstats");
CSDefChanstats = block->Get<bool>("cs_def_chanstats");
Anope::string engine = block->Get<Anope::string>("engine");
this->sql = ServiceReference<SQL::Provider>("SQL::Provider", engine);
if (sql)
this->CheckTables();
else
Log(this) << "no database connection to " << engine;
}
void OnChanInfo(CommandSource &source, ChanServ::Channel *ci, InfoFormatter &info, bool show_all) override
{
if (!show_all)
return;
if (cs_stats.HasExt(ci))
info.AddOption(_("Chanstats"));
}
void OnNickInfo(CommandSource &source, NickServ::Nick *na, InfoFormatter &info, bool show_hidden) override
{
if (!show_hidden)
return;
if (ns_stats.HasExt(na->GetAccount()))
info.AddOption(_("Chanstats"));
}
void OnTopicUpdated(User *source, Channel *c, const Anope::string &user, const Anope::string &topic) override
{
if (!source || !source->Account() || !c->ci || !cs_stats.HasExt(c->ci))
return;
query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);";
query.SetValue("channel", c->name);
query.SetValue("nick", GetDisplay(source));
this->RunQuery(query);
}
EventReturn OnChannelModeSet(Channel *c, const MessageSource &setter, ChannelMode *mode, const Anope::string ¶m) override
{
this->OnModeChange(c, setter.GetUser());
return EVENT_CONTINUE;
}
EventReturn OnChannelModeUnset(Channel *c, const MessageSource &setter, ChannelMode *, const Anope::string ¶m) override
{
this->OnModeChange(c, setter.GetUser());
return EVENT_CONTINUE;
}
private:
void OnModeChange(Channel *c, User *u)
{
if (!u || !u->Account() || !c->ci || !cs_stats.HasExt(c->ci))
return;
query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0);";
query.SetValue("channel", c->name);
query.SetValue("nick", GetDisplay(u));
this->RunQuery(query);
}
public:
void OnPreUserKicked(const MessageSource &source, ChanUserContainer *cu, const Anope::string &kickmsg) override
{
if (!cu->chan->ci || !cs_stats.HasExt(cu->chan->ci))
return;
query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0);";
query.SetValue("channel", cu->chan->name);
query.SetValue("nick", GetDisplay(cu->user));
this->RunQuery(query);
query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0);";
query.SetValue("channel", cu->chan->name);
query.SetValue("nick", GetDisplay(source.GetUser()));
this->RunQuery(query);
}
void OnPrivmsg(User *u, Channel *c, Anope::string &msg) override
{
if (!c->ci || !cs_stats.HasExt(c->ci))
return;
size_t letters = msg.length();
size_t words = this->CountWords(msg);
size_t action = 0;
if (msg.find("\01ACTION")!=Anope::string::npos)
{
action = 1;
letters = letters - 7;
words--;
}
// count smileys
size_t smileys_happy = CountSmileys(msg, SmileysHappy);
size_t smileys_sad = CountSmileys(msg, SmileysSad);
size_t smileys_other = CountSmileys(msg, SmileysOther);
// do not count smileys as words
size_t smileys = smileys_happy + smileys_sad + smileys_other;
if (smileys > words)
words = 0;
else
words = words - smileys;
query = "CALL " + prefix + "chanstats_proc_update(@channel@, @nick@, 1, @letters@, @words@, @action@, "
"@smileys_happy@, @smileys_sad@, @smileys_other@, '0', '0', '0', '0');";
query.SetValue("channel", c->name);
query.SetValue("nick", GetDisplay(u));
query.SetValue("letters", letters);
query.SetValue("words", words);
query.SetValue("action", action);
query.SetValue("smileys_happy", smileys_happy);
query.SetValue("smileys_sad", smileys_sad);
query.SetValue("smileys_other", smileys_other);
this->RunQuery(query);
}
void OnDelCore(NickServ::Account *nc) override
{
query = "DELETE FROM `" + prefix + "chanstats` WHERE `nick` = @nick@;";
query.SetValue("nick", nc->GetDisplay());
this->RunQuery(query);
}
void OnChangeCoreDisplay(NickServ::Account *nc, const Anope::string &newdisplay) override
{
query = "CALL " + prefix + "chanstats_proc_chgdisplay(@old_display@, @new_display@);";
query.SetValue("old_display", nc->GetDisplay());
query.SetValue("new_display", newdisplay);
this->RunQuery(query);
}
void OnDelChan(ChanServ::Channel *ci) override
{
query = "DELETE FROM `" + prefix + "chanstats` WHERE `chan` = @channel@;";
query.SetValue("channel", ci->GetName());
this->RunQuery(query);
}
void OnChanRegistered(ChanServ::Channel *ci)
{
if (CSDefChanstats)
ci->Extend<bool>("CS_STATS");
}
void OnNickRegister(User *user, NickAlias *na, const Anope::string &)
{
if (NSDefChanstats)
na->GetAccount()->Extend<bool>("NS_STATS");
}
};
MODULE_INIT(MChanstats)
|