User:Lost Labyrinth/chatban.js

// if (wgUserGroups && ((wgUserGroups.indexOf('sysop')!=-1||wgUserGroups.indexOf('chatmoderator')!=-1)&&(wgTitle=='Chat/Bans'||wgTitle.indexOf('Chat/Bans/Archive')!=-1))) { $(function {	var banpage = wgTitle.indexOf('Chat/Bans/Archive/')==-1	var d = new Date	var monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"];	function callAPI(data, method, callback) {		data['format'] = 'json';		$.ajax({ data: data, dataType: 'json', url: wgScriptPath + '/api.php', type: method, cache: false, success: function(response) { if (response.error) alert('API error: ' + response.error.info); else callback(response); },			error: function(xhr, error) { alert('AJAX error: ' + error) } });	}	$('#ChatBanAdd').html(' Add new banned user '+(banpage?'See Template:Bans for documentation and shorthand inputs. ':)+' Banned user:  Banning moderator: '+(banpage?' (default: yourself)':)+'  Kicks given:  '+(banpage?:' ')+'Date of the ban: '+(banpage?'  Today  ':)+''+(banpage?' Other':'')+'  Ban expiry:   Ban reason:   Other notes (optional):   Submit  ')	$('#btoday').click(function { if ($(this).is(':checked')) { $('#bdate #bother').attr('disabled','disabled') } else { $('#bdate #bother').removeAttr('disabled') }	})	$('#bother').keyup(function { if ($(this).val!='') { $('#bdate #btoday').attr('disabled','disabled') } else { $('#bdate #btoday').removeAttr('disabled') }	}) }) function submitBan { var banpage = wgTitle.indexOf('Chat/Bans/Archive/')==-1 $('#bsubmit').attr('disabled','disabled').html('Loading...') var bandate = $('#bdate input[disabled]').length!=0||!banpage?$('#bdate input:not([disabled])').val:'' var template = '' callAPI({		'action': 'query',		'prop': 'info|revisions',		'intoken': 'edit',		'titles': wgPageName,		'rvprop': 'content',		'rvlimit': '1',		'indexpageids': 'true',	}, 'GET', function(response) {		var page = response.query.pages[response.query.pageids[0]];		var content = page.revisions[0]['*'];		content = content+'\n'+template;		callAPI({ 'minor': 'yes', 'summary': (banpage?'Added template for banned user.':'Adding to the bans archive.'), 'action': 'edit', 'title': wgPageName, 'basetimestamp': page.revisions[0].timestamp, 'startimestamp': page.starttimestamp, 'token': page.edittoken, 'text': content }, 'POST', function(response) { alert('Your edit has been submitted. The page will now be reloaded.') document.location.href = wgArticlePath.replace('$1', wgPageName)+'?action=purge' });	}); } //makes it possible to unban an user with one click, and then a click to confirm the summary. $(function {	$('#banexpired #removeban').html('(remove ban)')	$('#removeban').click(function(e) { e.preventDefault var ubReason = prompt('Please enter a summary. Press cancel if you do not want to unban this user','Ban has expired.') if (!ubReason) { return null; }		var ubUsername = $(this).parents('tr').find('a[href*="Special:Contributions/"]').html; callAPI({			'action': 'query',			'list': 'users',			'ustoken': 'userrights',			'ususers': ubUsername		}, 'GET', function(response) {			var ubToken = response.query.users["0"].userrightstoken			callAPI({ 'action': 'userrights', 'user': ubUsername, 'token': ubToken, 'remove': 'bannedfromchat', 'reason': ubReason, }, 'POST', function(response) { $.post(wgScript + '?action=ajax&rs=ChatAjax&method=blockOrBanChat', {				userToBan: ubUsername, 				time: '0',				reason: ubReason, 				mode: 'global' 			}, $.proxy(function(data) { if (confirm('Should the bans page and the archive page be updated?')) { var d = new Date callAPI({						'action': 'query',						'prop': 'info|revisions',						'intoken': 'edit',						'titles': wgPageName+'|'+wgPageName+'/Archive/'+d.getUTCFullYear,						'rvprop': 'content',						'indexpageids': 'true',					}, 'GET', function(response) {						var page = response.query.pages[response.query.pageids[0]];						var archivePage = response.query.pages[response.query.pageids[1]];						var content = page.revisions[0]['*'];						var regex = new RegExp('\\|-\\n\\|\\[\\[Special:Contributions/'+ubUsername+'\\|.*(\\n.*){3}(\\n.*)(\\n.*){1,2}\\n?')						var banTemplate = content.match(regex)[0].replace(/\{\{Bans1\|([^}]*)\}\}/i, '$1').replace(/ \(\[\[Special:UserRights[^\]]*remove ban\]\]\)/,).replace(/\n\|[\d\s]+/,)						content = content.replace(regex,'')						callAPI({ 'minor': 'yes', 'summary': 'Archiving ban, ban has expired.', 'action': 'edit', 'title': wgPageName, 'basetimestamp': page.revisions[0].timestamp, 'startimestamp': page.starttimestamp, 'token': page.edittoken, 'text': content }, 'POST', function(response) { });						callAPI({ 'minor': 'yes', 'summary': 'Archiving ban.', 'action': 'edit', 'title': wgPageName+'/Archive/'+d.getUTCFullYear, 'basetimestamp': archivePage.revisions[0].timestamp, 'startimestamp': archivePage.starttimestamp, 'token': archivePage.edittoken, 'appendtext': '\n'+banTemplate }, 'POST', function(response) { alert('"'+ubUsername+'" has been unbanned, and the templates have been moved.') document.location.href = wgArticlePath.replace('$1', wgPageName)+'?action=purge' });					})				} else { alert('"'+ubUsername+' has been unbanned. Please do archive the ban template though.')				}			}));			})		})	}) }) } else if (wgTitle == 'Chat/Reports' && wgUserGroups) { if (wgUserGroups.indexOf('sysop')!=-1||wgUserGroups.indexOf('chatmoderator')!=-1) {	$(function {		$('#ChatBanAdd').html(' Ban user Username:  Ban expiry:   Minutes  Hours  Days    Reason: <input type="text" id="reason">  Other notes (optional): <input type="text" id="notes">  Submit  ')	})	function banUser {		$('#bsubmit').attr('disabled','disabled').html('Loading...')		var user = $('#ChatBanAdd #user').val;		var time = parseInt($('#ChatBanAdd #time').val) * parseInt($('#ChatBanAdd #timeformat').val); var reason = $('#ChatBanAdd #reason').val; var notes = $('#ChatBanAdd #notes').val; var now = +(new Date).toUTCString+; exdate = +(new Date((+new Date) + (time * 1000))).toUTCString+; var template = '' $.post(wgScript + '?action=ajax&rs=ChatAjax&method=blockOrBanChat', {			userToBan: user, 			time: time,			reason: reason, 			mode: 'global' 		}, $.proxy(function(data) { callAPI({				'action': 'query',				'prop': 'info|revisions',				'intoken': 'edit',				'titles': 'Project:Chat/Bans',				'rvprop': 'content',				'rvlimit': '1',				'indexpageids': 'true',			}, 'GET', function(response) {				var page = response.query.pages[response.query.pageids[0]];				var content = page.revisions[0]['*'];				content = content+'\n'+template;				callAPI({ 'minor': 'yes', 'summary': 'Added template for banned user.', 'action': 'edit', 'title': 'Project:Chat/Bans', 'basetimestamp': page.revisions[0].timestamp, 'startimestamp': page.starttimestamp, 'token': page.edittoken, 'text': content }, 'POST', function(response) { alert('The user has been banned, and the bans page has been updated.') $('#bsubmit').removeAttr('disabled').html('Submit'); });			});		}));	} } else { $(function {		$('#ChatBanAdd').html('<form action="javascript:reportUser" style="background:#9FB;border:1px solid #00FF00;padding:15px;"> Report user Username: <input type="text" id="user"> Reason: <input type="text" id="reason">  Submit  ')	}) function reportUser { $('#bsubmit').attr('disabled','disabled').html('Loading...') var template = '|'+$('#ChatBanAdd #user').val+'\n|'+$('#ChatBanAdd #reason').val+'\n|-'; callAPI({			'action': 'query',			'prop': 'info|revisions',			'intoken': 'edit',			'titles': wgPageName,			'rvprop': 'content',			'rvlimit': '1',			'indexpageids': 'true',		}, 'GET', function(response) {			var page = response.query.pages[response.query.pageids[0]];			var content = page.revisions[0]['*'];			content = content+'\n'+template;			callAPI({ 'minor': 'yes', 'summary': 'Reported new user.', 'action': 'edit', 'title': wgPageName, 'basetimestamp': page.revisions[0].timestamp, 'startimestamp': page.starttimestamp, 'token': page.edittoken, 'text': content }, 'POST', function(response) { alert('Your edit has been submitted. The page will now be reloaded.') document.location.href = wgArticlePath.replace('$1', wgPageName)+'?action=purge' });		});	} } }