2014-05-25 23:16:28 +02:00
var _ = require ( 'underscore' ) . _ ,
2015-04-25 12:27:22 +02:00
moment = require ( 'moment' ) ,
2018-02-23 13:30:18 +01:00
async = require ( 'async' ) ,
exec = require ( 'child_process' ) . exec ;
2013-10-21 17:40:45 +02:00
var commands = function ( dbot ) {
var commands = {
2014-05-25 22:47:30 +02:00
'~ncount' : function ( event ) {
var chanCounts = { } ,
2015-01-02 22:38:37 +01:00
typeCounts = { } ,
2014-05-25 23:16:28 +02:00
total = 0 ,
offString = event . params [ 1 ] || null ;
2015-08-13 22:59:56 +02:00
offset = moment ( ) . subtract ( offString , 1 ) . valueOf ( ) || null ,
nick = event . params [ 2 ] || event . user ;
2014-05-25 23:16:28 +02:00
/ * i f ( ! o f f s e t | | ! o f f s e t . i s V a l i d ( ) ) {
event . reply ( 'Invalid timescale. Try \'week\'' ) ;
return ;
} * /
2014-05-25 22:47:30 +02:00
2015-08-13 22:59:56 +02:00
dbot . api . users . resolveUser ( event . server , nick , function ( err , user ) {
if ( user ) {
this . db . scan ( 'notifies' , function ( notify ) {
if ( notify . user == user . id ) {
if ( ! offString ) {
2014-05-25 23:16:28 +02:00
if ( ! _ . has ( chanCounts , notify . channel ) ) chanCounts [ notify . channel ] = 0 ;
2015-01-02 22:38:37 +01:00
if ( ! _ . has ( typeCounts , notify . type ) ) typeCounts [ notify . type ] = 0 ;
2014-05-25 23:16:28 +02:00
chanCounts [ notify . channel ] ++ ;
2015-01-02 22:38:37 +01:00
typeCounts [ notify . type ] ++ ;
2014-05-25 23:16:28 +02:00
total ++ ;
2015-08-13 22:59:56 +02:00
} else {
if ( notify . time > offset ) {
if ( ! _ . has ( chanCounts , notify . channel ) ) chanCounts [ notify . channel ] = 0 ;
if ( ! _ . has ( typeCounts , notify . type ) ) typeCounts [ notify . type ] = 0 ;
chanCounts [ notify . channel ] ++ ;
typeCounts [ notify . type ] ++ ;
total ++ ;
}
2014-05-25 23:16:28 +02:00
}
}
2015-08-13 22:59:56 +02:00
} , function ( ) {
var cCounts = _ . chain ( chanCounts )
. pairs ( )
. sortBy ( function ( p ) { return p [ 1 ] ; } )
. reverse ( )
. first ( 10 )
. value ( ) ;
2014-05-25 22:47:30 +02:00
2015-08-13 22:59:56 +02:00
var cString = '' ;
for ( var i = 0 ; i < cCounts . length ; i ++ ) {
cString += cCounts [ i ] [ 0 ] + " (" + cCounts [ i ] [ 1 ] + "), " ;
}
cString = cString . slice ( 0 , - 2 ) ;
2014-05-25 22:47:30 +02:00
2015-08-13 22:59:56 +02:00
var tCounts = _ . chain ( typeCounts )
. pairs ( )
. sortBy ( function ( p ) { return p [ 1 ] ; } )
. reverse ( )
. first ( 10 )
. value ( ) ;
2015-01-02 22:38:37 +01:00
2015-08-13 22:59:56 +02:00
var tString = '' ;
for ( var i = 0 ; i < tCounts . length ; i ++ ) {
tString += tCounts [ i ] [ 0 ] + " (" + tCounts [ i ] [ 1 ] + "), " ;
}
tString = tString . slice ( 0 , - 2 ) ;
2015-01-02 22:38:37 +01:00
2015-08-13 22:59:56 +02:00
if ( offString ) {
event . reply ( dbot . t ( 'timed_notifies' , {
2015-08-13 23:03:21 +02:00
'user' : user . primaryNick ,
2015-08-13 22:59:56 +02:00
'count' : total ,
'offString' : offString ,
'cString' : cString ,
'tString' : tString
} ) ) ;
} else {
event . reply ( dbot . t ( 'total_notifies' , {
'user' : user . primaryNick ,
'count' : total ,
'cString' : cString ,
'tString' : tString
} ) ) ;
}
} ) ;
} else {
event . reply ( 'No idea who that is mate.' ) ;
}
2015-08-13 23:02:14 +02:00
} . bind ( this ) ) ;
2014-05-25 22:47:30 +02:00
} ,
2016-02-02 16:00:34 +01:00
'~batchstatus' : function ( event ) {
var nicks = event . params ,
aliases = [ ] ,
reportsFound = [ ] ;
nicks . splice ( 0 , 1 ) ;
2016-02-02 18:31:36 +01:00
_ . each ( nicks , function ( nick , i ) {
nicks [ i ] = nick . replace ( /,/g , '' ) ;
} ) ;
2016-02-02 16:00:34 +01:00
async . eachSeries ( nicks , function ( nick , next ) {
dbot . api . users . resolveUser ( event . server , nick , function ( err , user ) {
if ( ! err && user && ! _ . include ( aliases , user . primaryNick ) ) {
aliases . push ( user . primaryNick ) ;
dbot . api . users . getUserAliases ( user . id , function ( err , theseAliases ) {
if ( ! err && theseAliases ) {
aliases = _ . union ( aliases , theseAliases ) ;
}
next ( ) ;
} ) ;
2016-02-02 16:03:57 +01:00
} else {
next ( ) ;
2016-02-02 16:00:34 +01:00
}
} ) ;
} , function ( ) {
dbot . modules . report . db . search ( 'notifies' , {
'server' : event . server
} , function ( notify ) {
if ( _ . include ( aliases , notify . target ) ) {
if ( notify . type == 'ban' || notify . type == 'quiet' || notify . type == 'warn' || notify . type == 'report' ) {
if ( ! _ . include ( reportsFound , notify . target ) ) {
reportsFound . push ( notify . target ) ;
}
}
}
} , function ( ) {
if ( reportsFound . length != 0 ) {
event . reply ( 'Record found for: ' + reportsFound . join ( ', ' ) ) ;
} else {
event . reply ( 'None of these users seem to have sustatuses' ) ;
}
} ) ;
} ) ;
} ,
2018-02-10 13:35:31 +01:00
'~notes' : function ( event ) {
var tName = event . params [ 1 ] ,
server = event . server ;
dbot . api . users . resolveUser ( server , tName , function ( err , target ) {
if ( target ) {
var notes = { } ;
this . db . search ( 'warnings' , {
'server' : server ,
2018-02-10 13:47:54 +01:00
'warnee' : target . id
2018-02-10 13:35:31 +01:00
} , function ( warning ) {
if ( warning . reason . match ( '#note' ) ) {
notes [ warning . time ] = warning ;
}
} , function ( err ) {
if ( _ . size ( notes ) > 0 ) {
var nTimes = _ . keys ( notes ) . sort ( function ( a , b ) {
return parseInt ( a ) - parseInt ( b ) ;
} ) ;
var n = 0 ;
_ . each ( nTimes , function ( key ) {
2018-02-10 13:47:54 +01:00
// just couldn't be bothered doing the warner lookup, waste of time
event . reply ( '[\u00036note ' + n + '\u000f][' + moment ( parseInt ( key ) ) . format ( 'DD/MM/YYYY' ) + '][' + notes [ key ] . warner . split ( '.' ) [ 0 ] + '] ' + notes [ key ] . reason ) ;
2018-02-10 13:35:31 +01:00
n ++ ;
} ) ;
} else {
event . reply ( 'No notes found for ' + tName ) ;
}
} ) ;
} else {
event . reply ( event . params [ 1 ] + ' not found.' ) ;
}
} . bind ( this ) ) ;
} ,
2015-01-15 03:41:11 +01:00
'~sustatus' : function ( event ) {
var user = event . input [ 1 ] ;
2016-01-28 19:29:51 +01:00
if ( event . channel == '#tripsit.me' ) {
return event . reply ( '~_~ do that in #moderators ~_~' ) ;
}
2015-01-15 03:41:11 +01:00
2018-02-23 13:30:18 +01:00
function puts ( error , stdout , stderr ) {
2018-02-23 14:47:23 +01:00
console . log ( stdout ) ;
console . log ( stderr ) ;
2018-02-23 13:30:18 +01:00
var res = stdout . split ( '\n' ) ;
event . reply ( 'Link: ' + res [ res . length - 3 ] ) ;
}
2015-01-15 03:41:11 +01:00
dbot . api . users . resolveUser ( event . server , user , function ( err , user ) {
if ( user ) {
2015-04-25 12:42:30 +02:00
dbot . api . users . getUserAliases ( user . id , function ( err , aliases ) {
var ban = 0 ,
latest _ban = { 'time' : 0 } ,
latest _unban = { 'time' : 0 } ,
unban = 0 ,
quiet = 0 ,
warn = 0 ,
report = 0 ,
items = { } ;
aliases . push ( user . primaryNick ) ;
2015-01-15 03:41:11 +01:00
2015-04-25 12:42:30 +02:00
dbot . modules . report . db . search ( 'notifies' , {
'server' : event . server
} , function ( notify ) {
2016-01-28 19:29:51 +01:00
if ( _ . include ( aliases , notify . target ) ) {
if ( notify . type == 'ban' ) {
ban ++ ;
2016-01-29 18:55:03 +01:00
if ( notify . time > latest _ban . time ) {
latest _ban = notify ;
}
} else if ( notify . type == 'unban' ) {
unban ++ ;
if ( notify . time > latest _unban . time ) {
latest _unban = notify ;
2015-03-14 14:45:15 +01:00
}
2016-01-29 18:55:03 +01:00
} else if ( notify . type == 'quiet' ) {
quiet ++ ;
} else if ( notify . type == 'warn' ) {
warn ++ ;
} else if ( notify . type == 'report' ) {
report ++ ;
}
items [ notify . time ] = notify ;
}
2015-04-25 12:42:30 +02:00
} , function ( ) {
2018-02-23 14:47:23 +01:00
if ( quiet != 0 || warn != 0 || report != 0 || ban != 0 ) {
var out = '' ; // his is really stupid but i'm feeling lazy
var sumLine = user . primaryNick + ' has been warned ' + warn + ' times, quieted ' + quiet + ' times, and reported ' + report + ' times.' ;
event . reply ( sumLine ) ;
out += sumLine + '\n\n' ;
var sTimes = _ . keys ( items ) . sort ( function ( a , b ) {
return parseInt ( a ) - parseInt ( b ) ;
} ) ;
if ( sTimes . length < 70 ) event . reply ( '[\u00036reports\u000f]' ) ;
out += '\n\n[reports]\n' ;
var n = 0 ;
_ . each ( sTimes , function ( time ) {
if ( items [ time ] . type == 'report' ) {
var rEntry = '[' + n + '][' + moment ( parseInt ( time ) ) . format ( 'DD/MM/YYYY' ) + '] ' + items [ time ] . message ;
if ( sTimes . length < 70 ) event . reply ( rEntry ) ;
out += rEntry + '\n'
n ++ ;
}
} ) ;
2016-01-29 18:55:03 +01:00
2018-02-23 14:47:23 +01:00
if ( sTimes . length < 70 ) event . reply ( '[\u00037quiets\u000f]' ) ;
out += '\n\n[quiets]\n' ;
var n = 0 ;
_ . each ( sTimes , function ( time ) {
if ( items [ time ] . type == 'quiet' ) {
var qEntry = '[' + n + '][' + moment ( parseInt ( time ) ) . format ( 'DD/MM/YYYY' ) + '] ' + items [ time ] . message ;
if ( sTimes . length < 70 ) event . reply ( qEntry ) ;
out += qEntry + '\n'
n ++ ;
}
2016-01-29 18:55:03 +01:00
} ) ;
2018-02-23 14:47:23 +01:00
if ( sTimes . length < 70 ) event . reply ( '[\u00035warns\u000f]' ) ;
out += '\n\n[warns]\n'
var n = 0 ;
_ . each ( sTimes , function ( time ) {
if ( items [ time ] . type == 'warn' ) {
var wEntry = '[' + n + '][' + moment ( parseInt ( time ) ) . format ( 'DD/MM/YYYY' ) + '] ' + items [ time ] . message ;
if ( sTimes . length < 70 ) event . reply ( wEntry ) ;
out += wEntry + '\n'
n ++ ;
}
} ) ;
if ( sTimes . length < 70 ) event . reply ( '[\u00034bans\u000f]' ) ;
out += '[bans]'
var n = 0 ;
_ . each ( sTimes , function ( time ) {
if ( items [ time ] . type == 'ban' || items [ time ] . type == 'unban' ) {
var bEntry = '[' + n + '][' + moment ( parseInt ( time ) ) . format ( 'DD/MM/YYYY' ) + '] ' + items [ time ] . message ;
if ( sTimes . length < 70 ) event . reply ( bEntry ) ;
out += bEntry + '\n'
n ++ ;
}
} ) ;
if ( sTimes . length > 70 ) {
event . reply ( 'There are too many to show without killing everyone :S (wait for the link xx)' ) ;
2016-01-29 18:55:03 +01:00
}
if ( latest _ban . time != 0 ) {
if ( latest _unban . time == 0 || ( latest _unban . time < latest _ban . time ) ) {
2018-02-23 14:47:23 +01:00
var bStatus = 'Current Ban Status: \u00034Banned\u000f since ' + moment ( latest _ban . time ) . fromNow ( ) + ' (' + moment ( parseInt ( latest _ban . time ) ) . format ( 'DD/MM/YYYY' ) + ')' ;
var bReason = 'Current Ban Reason: ' + latest _ban . message ;
2016-01-29 18:55:03 +01:00
} else {
var a = moment ( latest _ban . time ) ;
var b = moment ( latest _unban . time ) ;
2018-02-23 14:47:23 +01:00
var bStatus = 'Current Ban Status: \u00037Unbanned\u000f since ' + moment ( parseInt ( latest _unban . time ) ) . format ( 'DD/MM/YYYY' ) + ' after being banned for ' + b . diff ( a , 'days' ) + ' days' ;
var bReason = 'Most recent ban reason: ' + latest _ban . message ;
2016-01-29 18:55:03 +01:00
}
2018-02-23 14:47:23 +01:00
event . reply ( bStatus )
event . reply ( bReason )
out += bStatus + '\n' + bReason
2016-01-29 18:55:03 +01:00
} else {
2018-02-23 14:47:23 +01:00
var nevBan = 'Current Ban Status: \u00033Never banned (\u00037probably\u00033)\u000f'
event . reply ( nevBan ) ;
out += nevBan
2016-01-29 18:55:03 +01:00
}
2018-02-23 14:47:23 +01:00
exec ( "/home/node/alsuti/bin/alsuti -p " + randomString ( 12 , '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ) + " /tmp/" + sTimes . length + ".txt" , puts ) ;
2016-01-29 18:55:03 +01:00
} else {
event . reply ( user . primaryNick + ' has no record.' ) ;
}
2015-04-25 12:42:30 +02:00
} ) ;
} . bind ( this ) ) ;
2015-01-15 03:41:11 +01:00
} else {
event . reply ( 'never heard of em' ) ;
}
} . bind ( this ) ) ;
} ,
2014-12-16 03:50:03 +01:00
'~ustatus' : function ( event ) {
var user = event . input [ 1 ] ;
dbot . api . users . resolveUser ( event . server , user , function ( err , user ) {
if ( user ) {
var ban = null ,
quiet = 0
warn = 0 ;
// i'll fix it later
dbot . modules . report . db . search ( 'notifies' , {
'server' : event . server
} , function ( notify ) {
if ( notify . message . match ( 'banned ' + user . primaryNick ) || notify . message . match ( 'issued a warning to ' + user . primaryNick ) || notify . message . match ( 'has quieted ' + user . primaryNick ) ) {
if ( notify . type == 'ban' ) {
ban = notify . time ;
} else if ( notify . type == 'quiet' ) {
quiet ++ ;
} else if ( notify . type == 'warn' ) {
warn ++ ;
}
}
} , function ( ) {
if ( ban ) {
event . reply ( user . primaryNick + ' was banned on ' + new Date ( ban ) . toUTCString ( ) ) ;
2016-01-28 19:29:51 +01:00
}
if ( quiet != 0 || warn != 0 ) {
2014-12-16 03:50:03 +01:00
event . reply ( user . primaryNick + ' has been warned ' + warn + ' times, and quieted ' + quiet + ' times.' ) ;
2016-01-28 19:29:51 +01:00
} else if ( ! ban ) {
2014-12-16 03:50:03 +01:00
event . reply ( user . primaryNick + ' has no record.' ) ;
}
} ) ;
} else {
event . reply ( 'never heard of em' ) ;
}
} . bind ( this ) ) ;
} ,
2013-10-21 17:40:45 +02:00
'~clearmissing' : function ( event ) {
if ( _ . has ( this . pending , event . rUser . id ) ) {
var count = this . pending [ event . rUser . id ] . length ;
delete this . pending [ event . rUser . id ] ;
event . reply ( dbot . t ( 'cleared_notifies' , { 'count' : count } ) ) ;
} else {
event . reply ( dbot . t ( 'no_missed_notifies' ) ) ;
}
} ,
'~report' : function ( event ) {
2014-09-04 17:25:21 +02:00
var channelName = ( event . input [ 1 ] || event . channel . toString ( ) ) ,
2013-10-21 17:40:45 +02:00
nick = event . input [ 2 ] ,
reason = event . input [ 3 ] . trim ( ) ;
2014-09-04 17:25:21 +02:00
channelName = channelName . trim ( ) ;
2013-10-21 17:40:45 +02:00
2014-02-04 01:58:09 +01:00
if ( channelName == event . user ) {
channelName = dbot . config . servers [ event . server ] . admin _channel ;
}
2013-10-21 17:40:45 +02:00
if ( reason . charAt ( reason . length - 1 ) != '.' ) reason += '.' ;
2014-09-04 16:54:49 +02:00
dbot . api . users . resolveUser ( event . server , nick , function ( err , reportee ) {
2013-10-21 17:40:45 +02:00
if ( _ . has ( event . allChannels , channelName ) ) {
if ( reportee ) {
this . api . notify ( 'report' , event . server , event . rUser ,
channelName , dbot . t ( 'report' , {
'reporter' : event . rUser . primaryNick ,
'reportee' : nick ,
'reason' : reason
2016-01-28 19:29:51 +01:00
} ) , false , nick ) ;
2013-10-21 17:40:45 +02:00
event . reply ( dbot . t ( 'reported' , { 'reported' : nick } ) ) ;
} else {
event . reply ( dbot . t ( 'user_not_found' , {
'reported' : nick ,
'channel' : channelName
} ) ) ;
}
} else {
event . reply ( dbot . t ( 'not_in_channel' , { 'channel' : channelName } ) ) ;
}
} . bind ( this ) ) ;
} ,
'~notify' : function ( event ) {
var channelName = event . input [ 1 ] ,
message = event . input [ 2 ] ;
if ( _ . has ( event . allChannels , channelName ) ) {
if ( this . config . firstHost ) {
var first = message . split ( ' ' ) [ 0 ] ;
2014-09-04 16:54:49 +02:00
dbot . api . users . resolveUser ( event . server , first , function ( err , user ) {
2014-01-06 19:05:44 +01:00
if ( user && _ . include ( this . config . host _lookup , channelName ) ) {
2013-10-21 17:40:45 +02:00
dbot . api . nickserv . getUserHost ( event . server , first , function ( host ) {
message = message . replace ( first , first + ' [' + host + ']' ) ;
2015-07-23 11:58:31 +02:00
this . api . notify ( 'notify' , event . server , event . rUser , channelName , message , host , first ) ;
2013-10-21 17:40:45 +02:00
} . bind ( this ) ) ;
} else {
this . api . notify ( 'notify' , event . server , event . rUser , channelName , message ) ;
}
} . bind ( this ) ) ;
} else {
this . api . notify ( 'notify' , event . server , event . rUser , channelName , message ) ;
}
event . reply ( dbot . t ( 'notified' , {
'user' : event . user ,
'channel' : channelName
} ) ) ;
} else {
event . reply ( dbot . t ( 'not_in_channel' , { 'channel' : channelName } ) ) ;
}
} ,
'~nunsub' : function ( event ) {
2014-09-15 23:12:30 +02:00
var cName = event . input [ 1 ] ,
cId = event . input [ 1 ] + '.' + event . server ;
2013-10-21 17:40:45 +02:00
2014-09-15 23:12:30 +02:00
if ( _ . has ( dbot . instance . connections [ event . server ] . channels , cName ) ) {
this . db . read ( 'nunsubs' , cId , function ( err , nunsubs ) {
if ( ! nunsubs ) {
var nunsubs = {
'id' : cId ,
'users' : [ ]
2013-10-21 17:40:45 +02:00
}
2014-09-15 23:12:30 +02:00
}
if ( ! _ . include ( nunsubs , event . rUser . id ) ) {
nunsubs . users . push ( event . rUser . id ) ;
this . db . save ( 'nunsubs' , cId , nunsubs , function ( ) {
var reply = dbot . t ( 'nunsubbed' , { 'cName' : cName } )
if ( _ . has ( this . config . chan _redirs , cName ) ) {
reply += dbot . t ( 'n_also_found' , { 'afaName' : this . config . chan _redirs [ cName ] } ) ;
}
event . reply ( reply ) ;
} . bind ( this ) ) ;
} else {
event . reply ( dbot . t ( 'already_nunsubbed' , { 'cName' : cName } ) ) ;
}
} . bind ( this ) ) ;
} else {
event . reply ( 'Channel not known.' ) ;
}
2013-10-21 17:40:45 +02:00
} ,
'~ununsub' : function ( event ) {
2014-09-15 23:12:30 +02:00
var cName = event . input [ 1 ] ,
cId = event . input [ 1 ] + '.' + event . server ;
if ( _ . has ( dbot . instance . connections [ event . server ] . channels , cName ) ) {
this . db . read ( 'nunsubs' , cId , function ( err , nunsubs ) {
if ( ! _ . isUndefined ( nunsubs ) && _ . include ( nunsubs . users , event . rUser . id ) ) {
nunsubs . users = _ . without ( nunsubs . users , event . rUser . id ) ;
this . db . save ( 'nunsubs' , cId , nunsubs , function ( ) {
event . reply ( dbot . t ( 'ununsubbed' , { 'cName' : cName } ) ) ;
} ) ;
} else {
event . reply ( dbot . t ( 'not_nunsubbed' , { 'cName' : cName } ) ) ;
}
} . bind ( this ) ) ;
} else {
event . reply ( 'Channel not known.' ) ;
}
2013-10-21 17:40:45 +02:00
}
} ;
2014-02-04 01:58:09 +01:00
commands [ '~report' ] . regex = /^report (#[^ ]+ )?([^ ]+) (.*)$/ ;
2013-12-29 19:38:24 +01:00
commands [ '~notify' ] . regex = [ /^notify ([^ ]+) (.+)$/ , 3 ] ;
commands [ '~nunsub' ] . regex = [ /^nunsub ([^ ]+)$/ , 2 ] ;
commands [ '~ununsub' ] . regex = [ /^ununsub ([^ ]+)$/ , 2 ] ;
2014-12-16 03:50:03 +01:00
commands [ '~ustatus' ] . regex = [ /^ustatus ([^ ]+)$/ , 2 ] ;
2015-01-15 03:41:11 +01:00
commands [ '~sustatus' ] . regex = [ /^sustatus ([^ ]+)$/ , 2 ] ;
2015-01-15 03:46:02 +01:00
commands [ '~ustatus' ] . access = 'power_user' ;
commands [ '~sustatus' ] . access = 'power_user' ;
2015-08-13 22:59:56 +02:00
commands [ '~ncount' ] . access = 'power_user' ;
2018-02-10 13:35:31 +01:00
commands [ '~notes' ] . access = 'power_user' ;
2013-10-21 17:40:45 +02:00
return commands ;
} ;
2018-02-23 14:47:23 +01:00
function randomString ( length , chars ) {
var result = '' ;
for ( var i = length ; i > 0 ; -- i ) result += chars [ Math . floor ( Math . random ( ) * chars . length ) ] ;
return result ;
}
2013-10-21 17:40:45 +02:00
exports . fetch = function ( dbot ) {
return commands ( dbot ) ;
} ;