| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Nickname lameness meter. Ported from the perl script by Adam Spiers.
|
| 7 |
*
|
| 8 |
* @author David Kent Norman
|
| 9 |
* @link http://deekayen.net
|
| 10 |
* @link http://www.adamspiers.org/computing/nickometer/
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_help().
|
| 15 |
*/
|
| 16 |
function nickometer_help($path, $arg) {
|
| 17 |
switch ($path) {
|
| 18 |
case 'nickometer':
|
| 19 |
return t('When online chatters use a nick that is especially lame, let them know by putting a score to it.');
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Impelementation of hook_perm().
|
| 25 |
*
|
| 26 |
* @return array
|
| 27 |
*/
|
| 28 |
function nickometer_perm() {
|
| 29 |
return array('access nickometer');
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Implementation of hook_menu().
|
| 34 |
*
|
| 35 |
* @return array
|
| 36 |
*/
|
| 37 |
function nickometer_menu() {
|
| 38 |
$items = array();
|
| 39 |
$items['nickometer'] = array(
|
| 40 |
'page callback' => 'drupal_get_form',
|
| 41 |
'page arguments' => array('nickometer_form'),
|
| 42 |
'access callback' => 'user_access',
|
| 43 |
'access arguments' => array('access nickometer'),
|
| 44 |
'title' => 'Lame-O-Nickometer',
|
| 45 |
'title callback' => 't',
|
| 46 |
'title arguments' => array(),
|
| 47 |
'type' => MENU_CALLBACK
|
| 48 |
);
|
| 49 |
return $items;
|
| 50 |
}
|
| 51 |
|
| 52 |
function nickometer_form() {
|
| 53 |
$form = array();
|
| 54 |
$form['nick'] = array(
|
| 55 |
'#type' => 'textfield',
|
| 56 |
'#size' => 20
|
| 57 |
);
|
| 58 |
$form['submit'] = array(
|
| 59 |
'#type' => 'submit',
|
| 60 |
'#value' => t('Check nick'),
|
| 61 |
);
|
| 62 |
return $form;
|
| 63 |
}
|
| 64 |
|
| 65 |
function nickometer_form_submit($form, &$form_state) {
|
| 66 |
$nick = $form_state['values']['nick'];
|
| 67 |
|
| 68 |
drupal_set_message(t('Original nick: @nick', array('@nick' => $nick)));
|
| 69 |
|
| 70 |
$score = 0;
|
| 71 |
|
| 72 |
// Deal with special cases (precede with \ to prevent de-k3wlt0k)
|
| 73 |
$special_cost = array(
|
| 74 |
'69' => 500,
|
| 75 |
'dea?th' => 500,
|
| 76 |
'dark' => 400,
|
| 77 |
'n[i1]ght' => 300,
|
| 78 |
'n[i1]te' => 500,
|
| 79 |
'fuck' => 500,
|
| 80 |
'sh[i1]t' => 500,
|
| 81 |
'coo[l1]' => 500,
|
| 82 |
'kew[l1]' => 500,
|
| 83 |
'lame' => 500,
|
| 84 |
'dood' => 500,
|
| 85 |
'dude' => 500,
|
| 86 |
'rool[sz]' => 500,
|
| 87 |
'rule[sz]' => 500,
|
| 88 |
'[l1](oo?|u)[sz]er' => 500,
|
| 89 |
'[l1]eet' => 500,
|
| 90 |
'e[l1]ite' => 500,
|
| 91 |
'[l1]ord' => 500,
|
| 92 |
'k[i1]ng' => 500,
|
| 93 |
'pron' => 1000,
|
| 94 |
'warez' => 1000,
|
| 95 |
'xx' => 100,
|
| 96 |
'\[rkx]0' => 1000,
|
| 97 |
'\0[rkx]' => 1000,
|
| 98 |
);
|
| 99 |
|
| 100 |
foreach ($special_cost as $special_pattern => $value) {
|
| 101 |
if (preg_match("/$special_pattern/i", $nick, $reg)) {
|
| 102 |
$score = _nickometer_punish($score, $value, t('matched special case !match ', array('!match' => $reg[0])));
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
// Punish consecutive non-alphas
|
| 107 |
preg_match_all("/([^A-Za-z]{2,})/", $nick, $regs);
|
| 108 |
|
| 109 |
$consecutive = sizeof($regs[0]);
|
| 110 |
if ($consecutive) {
|
| 111 |
$score = _nickometer_punish($score, _nickometer_slow_pow(10, $consecutive), t("!num total consecutive non-alphas", array('!num' => $consecutive)));
|
| 112 |
}
|
| 113 |
|
| 114 |
// Remove one layer of balanced brackets and punish for rest
|
| 115 |
// Porting to PHP made this messy
|
| 116 |
// ...room to improve here later
|
| 117 |
if (strcmp(preg_replace("/^([^{}]*)(\{)(.*)(\})([^{}]*)$/x", "\\1\\3\\5", $nick), $nick) != 0) {
|
| 118 |
$newnick = preg_replace("/^([^{}]*)(\{)(.*)(\})([^{}]*)$/x", "\\1\\3\\5", $nick);
|
| 119 |
drupal_set_message(t('Removed {} outside parentheses; nick now !newnick', array('!newnick' => $newnick)));
|
| 120 |
}
|
| 121 |
else if (strcmp(preg_replace("/^([^\[\]]*)(\[)(.*)(\])([^\[\]]*)$/x", "\\1\\3\\5", $nick), $nick) != 0) {
|
| 122 |
$newnick = preg_replace("/^([^\[\]]*)(\[)(.*)(\])([^\[\]]*)$/x", "\\1\\3\\5", $nick);
|
| 123 |
drupal_set_message(t('Removed [] outside parentheses; nick now !newnick', array('!newnick' => $newnick)));
|
| 124 |
}
|
| 125 |
else if (strcmp(preg_replace("/^([^()]*)(\()(.*)(\))([^()]*)$/", "\\1\\3\\5", $nick), $nick) != 0) {
|
| 126 |
$newnick = preg_replace("/^([^()]*)(\()(.*)(\))([^()]*)$/", "\\1\\3\\5", $nick);
|
| 127 |
drupal_set_message(t('Removed () outside parentheses; nick now !newnick', array('!newnick' => $newnick)));
|
| 128 |
}
|
| 129 |
else {
|
| 130 |
$newnick = $nick;
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
// the following lines were done in perl with just: $parentheses = tr/(){}[]/(){}[]/;
|
| 135 |
$parens = array('(', ')', '{', '}', '[', ']');
|
| 136 |
for ($i=0; $i<sizeof($parens); $i++) {
|
| 137 |
$parentheses += substr_count($newnick, $parens[$i]);
|
| 138 |
$newnick = str_replace($parens[$i], '', $newnick);
|
| 139 |
}
|
| 140 |
|
| 141 |
if ($parentheses) {
|
| 142 |
$score = _nickometer_punish($score, _nickometer_slow_pow(10, $parentheses), "$parentheses extraneous "
|
| 143 |
. ($parentheses == 1 ? 'parenthesis' : 'parentheses'));
|
| 144 |
}
|
| 145 |
|
| 146 |
// Punish k3wlt0k
|
| 147 |
$k3wlt0k_weights = array(5, 5, 2, 5, 2, 3, 1, 2, 2, 2);
|
| 148 |
for ($digit=0; $digit<=9; $digit++) {
|
| 149 |
$occurrences = substr_count($newnick, $digit);
|
| 150 |
|
| 151 |
if ($occurrences) {
|
| 152 |
$score = _nickometer_punish($score, $k3wlt0k_weights[$digit] * $occurrences * 30,
|
| 153 |
$occurrences . ' '
|
| 154 |
. (($occurrences == 1) ? 'occurrence' : 'occurrences')
|
| 155 |
. " of $digit");
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
/*
|
| 160 |
This needs to be fixed so there aren't too many extraneous caps
|
| 161 |
# An alpha caps is not lame in middle or at end, provided the first
|
| 162 |
# alpha is caps.
|
| 163 |
$capnick = $newnick;
|
| 164 |
$newnick = preg_replace ("/^([^A-Za-z]*[A-Z].*[a-z].*?)[_-]?([A-Z])/e", "'\\1'.strtolower('\\2')", $newnick);
|
| 165 |
|
| 166 |
# A caps first alpha is sometimes not lame
|
| 167 |
$newnick = preg_replace ("/^([^A-Za-z]*)([A-Z])([a-z])/e", "'\\1'.strtolower('\\2').strtolower('\\3')", $newnick);
|
| 168 |
*/
|
| 169 |
|
| 170 |
// Punish uppercase to lowercase shifts and vice-versa, modulo
|
| 171 |
// exceptions above
|
| 172 |
$case_shifts = _nickometer_case_shifts($newnick);
|
| 173 |
|
| 174 |
if ($case_shifts > 1 && preg_match("/[A-Z]/", $newnick))
|
| 175 |
$score = _nickometer_punish($score, _nickometer_slow_pow(9, $case_shifts),
|
| 176 |
$case_shifts . ' case ' .
|
| 177 |
(($case_shifts == 1) ? 'shift' : 'shifts'));
|
| 178 |
|
| 179 |
// Punish lame endings (TorgoX, WraithX et al. might kill me for this :-)
|
| 180 |
if (preg_match("/[XZ][^a-zA-Z]*$/", $newnick)) {
|
| 181 |
$score = _nickometer_punish($score, 50, 'last alpha lame');
|
| 182 |
}
|
| 183 |
|
| 184 |
// Punish letter to numeric shifts and vice-versa
|
| 185 |
$number_shifts = _nickometer_number_shifts($newnick);
|
| 186 |
|
| 187 |
if ($number_shifts > 1) {
|
| 188 |
$score = _nickometer_punish($score, _nickometer_slow_pow(9, $number_shifts),
|
| 189 |
$number_shifts . ' letter/number ' .
|
| 190 |
(($number_shifts == 1) ? 'shift' : 'shifts'));
|
| 191 |
}
|
| 192 |
|
| 193 |
$newnick = preg_replace("/[a-z]/", '', $newnick);
|
| 194 |
|
| 195 |
// Punish extraneous caps
|
| 196 |
$newnick = preg_split('//', $newnick);
|
| 197 |
|
| 198 |
$caps = 0;
|
| 199 |
for ($i=0; $i<sizeof($newnick); $i++) {
|
| 200 |
if (preg_match("/^([A-Z])$/", $newnick[$i])) {
|
| 201 |
$caps++;
|
| 202 |
}
|
| 203 |
}
|
| 204 |
|
| 205 |
if ($caps) {
|
| 206 |
$score = _nickometer_punish($score, _nickometer_slow_pow(7, $caps), t("!num extraneous caps", array('!num' => $caps)));
|
| 207 |
}
|
| 208 |
|
| 209 |
// Now punish anything that's left
|
| 210 |
$remains = preg_replace("/[A-Za-z0-9]/", '', $newnick);
|
| 211 |
if (is_array($remains)) {
|
| 212 |
$remains = implode('', $remains);
|
| 213 |
}
|
| 214 |
$remains_length = strlen($remains);
|
| 215 |
|
| 216 |
if ($remains) {
|
| 217 |
$score = _nickometer_punish($score, 50 * $remains_length + _nickometer_slow_pow(9, $remains_length),
|
| 218 |
$remains_length . ' extraneous ' .
|
| 219 |
(($remains_length == 1) ? 'symbol' : 'symbols'));
|
| 220 |
}
|
| 221 |
|
| 222 |
drupal_set_message(t("Raw lameness score is !score", array('!score' => sprintf("%.2f", $score))));
|
| 223 |
|
| 224 |
$percentage = 100 * (1 + tanh(($score-400)/400)) * (1 - 1/(1+$score/5)) / 2;
|
| 225 |
|
| 226 |
$digits = 2 * (2 - ceil(log(100 - $percentage) / log(10)));
|
| 227 |
|
| 228 |
drupal_set_message(t("!percent is the percentage of suckage", array('!percent' => sprintf("%.${digits}f", $percentage))));
|
| 229 |
|
| 230 |
}
|
| 231 |
|
| 232 |
function _nickometer_number_shifts($shift) {
|
| 233 |
|
| 234 |
$shift = preg_split('//', $shift);
|
| 235 |
|
| 236 |
$shifts = 0;
|
| 237 |
for ($i=0; $i<sizeof($shift); $i++) {
|
| 238 |
if (preg_match("/^([A-Za-z])$/", $shift[$i])) {
|
| 239 |
$shifts += ($case == 'n' ? 1 : 0);
|
| 240 |
$case = 'l';
|
| 241 |
}
|
| 242 |
else if (preg_match("/^([0-9])$/", $shift[$i])) {
|
| 243 |
$shifts += ($case == 'l' ? 1 : 0);
|
| 244 |
$case = 'n';
|
| 245 |
}
|
| 246 |
}
|
| 247 |
return $shifts - 1;
|
| 248 |
}
|
| 249 |
|
| 250 |
function _nickometer_case_shifts($shift) {
|
| 251 |
|
| 252 |
$shift = preg_replace("/\d/", '', $shift);
|
| 253 |
$shift = preg_split('//', $shift);
|
| 254 |
|
| 255 |
$shifts = 0;
|
| 256 |
for ($i=0; $i<sizeof($shift); $i++) {
|
| 257 |
if (preg_match("/^([A-Z])$/", $shift[$i])) {
|
| 258 |
$shifts += ($case == 'l' ? 1 : 0);
|
| 259 |
$case = 'U';
|
| 260 |
}
|
| 261 |
elseif (preg_match("/^([a-z])$/", $shift[$i])) {
|
| 262 |
$shifts += ($case == 'U' ? 1 : 0);
|
| 263 |
$case = 'l';
|
| 264 |
}
|
| 265 |
}
|
| 266 |
return $shifts - 1;
|
| 267 |
}
|
| 268 |
|
| 269 |
function _nickometer_slow_pow($x, $y) {
|
| 270 |
return pow($x, _nickometer_slow_exponent($y));
|
| 271 |
}
|
| 272 |
|
| 273 |
function _nickometer_slow_exponent($x) {
|
| 274 |
return 1.3 * $x * (1 - atan($x/6) *2/pi());
|
| 275 |
}
|
| 276 |
|
| 277 |
function _nickometer_punish($score, $damage, $reason) {
|
| 278 |
$score += $damage;
|
| 279 |
|
| 280 |
if ($damage) {
|
| 281 |
drupal_set_message(t('!num lameness points awarded: !reason', array('!num' => sprintf("%.2f", $damage), '!reason' => $reason)));
|
| 282 |
}
|
| 283 |
return $score;
|
| 284 |
}
|