public static function get_response($in_input) { if (JxBotConverse::user_input_looks_strange($in_input)) { return 'Your last comment looks a bit strange.'; } // ** configurable? /* cap general server requests (safety); should be configurable as people have different host specs; 300 recommended for small shared host */ $cap_bot_ipm = JxBotConfig::option('sys_cap_bot_ipm', 300); if ($cap_bot_ipm > 0) { $stmt = JxBotDB::$db->prepare('SELECT COUNT(*) FROM log WHERE stamp >= DATE_SUB(NOW(), INTERVAL 1 MINUTE)'); $stmt->execute(); $last_min_total = intval($stmt->fetchAll(PDO::FETCH_NUM)[0][0]); if ($last_min_total >= $cap_bot_ipm) { return 'Sorry, I\'m too busy to chat right now. Please come back later.'; } } /* count interaction */ JxBotDB::$db->exec('UPDATE stats SET interactions = interactions + 1'); /* start timer */ $start_time = microtime(true); /* initalize tracking variables */ JxBotConverse::$match_time = 0.0; JxBotConverse::$service_time = 0.0; JxBotConverse::$iq_score = 0.0; JxBotConverse::$category_stack = array(); $fault = false; /* run the bot */ try { $output = JxBotConverse::srai($in_input); } catch (Exception $err) { $output = $err->getMessage(); $fault = true; } /* end timer */ $end_time = microtime(true); //print 'IQ '.JxBotConverse::$iq_score. '<br>'; /* log this interaction */ if (trim($output) == '') { $fault = true; } if ($fault) { JxBotConverse::$iq_score = -1; } JxBotConverse::log($in_input, $output, $end_time - $start_time, JxBotConverse::$match_time, JxBotConverse::$service_time, JxBotConverse::$iq_score); /* return the bot response */ if ($fault) { return 'I do apologise. I seem to be experiencing a positronic malfunction.'; } return $output; }
} else { if (isset($_POST['bot_name'])) { //JxBotConfig::set_option('bot_name', $_POST['bot_name']); foreach ($_POST as $key => $value) { if (substr($key, 0, 4) == 'bot_') { JxBotConfig::set_option($key, $value); } } JxBotConfig::save_configuration(); } ?> <p><div class="field"><label for="bot_name">Bot Name: </label> <input type="text" name="bot_name" id="bot_name" size="40" value="<?php print JxBotConfig::option('bot_name'); ?> "></div></p> <div class="field"><label for="bot_birthday">Birthday: </label> <input type="text" name="bot_birthday" id="bot_birthday" size="12" value="<?php print JxBotConfig::option('bot_birthday'); ?> "> (YYYY/MM/DD)</div> <p><input type="submit" value="Save"></p> <?php }
?> </div> <h2>Language Processing</h2> <div class="field"><label for="pre_strip_accents">Strip Accents:</label> <?php JxWidget::toggle_switch('pre_strip_accents', JxBotConfig::option('pre_strip_accents')); ?> <br><small>(strip accents during normalisation; good for English)</small></div> <h2>Security</h2> <div class="field"><label for="sys_cap_bot_ipm">Bot Load Maximum: </label> <input type="text" name="sys_cap_bot_ipm" id="sys_cap_bot_ipm" size="6" value="<?php print JxBotConfig::option('sys_cap_bot_ipm'); ?> "><br><small>(interactions per minute; 0 = unlimited)</small></div> <div class="field"><label for="admin_timeout">Administration Timeout: </label> <input type="text" name="admin_timeout" id="admin_timeout" size="6" value="<?php print JxBotConfig::option('admin_timeout'); ?> "><br><small>(minutes; 0 = no timeout)</small></div> <p class="left" id="buttons"><button type="submit" name="action" value="save">Save</button></p>
} JxBotConfig::save_configuration(); } ?> <div class="field"><label for="bot_active">Online:</label> <?php JxWidget::toggle_switch('bot_active', JxBotConfig::option('bot_active')); ?> </div> <div class="field"><label for="admin_user">Administration Username: </label> <input type="text" name="admin_user" id="admin_user" size="20" value="<?php print JxBotConfig::option('admin_user'); ?> "></div> <div class="field"><label for="bot_password">Change Password: </label> <input type="text" name="bot_password" id="bot_password" size="20"></div> <p class="left" id="buttons"><button type="submit" name="action" value="save">Save</button></p> <?php } }
private static function get_configuration() { $inputs = JxBotUtil::inputs('db_host,db_name,db_prefix,db_username,db_password,bot_tz,bot_name,admin_password'); $config = ''; $config .= "<?php\n\n"; $config .= '$jxbot[\'db_host\'] = "' . $inputs['db_host'] . "\";\n"; $config .= '$jxbot[\'db_name\'] = "' . $inputs['db_name'] . "\";\n"; $config .= '$jxbot[\'db_prefix\'] = "' . $inputs['db_prefix'] . "\";\n"; $config .= '$jxbot[\'db_username\'] = "' . $inputs['db_username'] . "\";\n"; $config .= '$jxbot[\'db_password\'] = "' . $inputs['db_password'] . "\";\n\n"; $config .= '$jxbot[\'bot_url\'] = "' . JxBotConfig::option('bot_url') . "\";\n\n"; return $config; }
public static function widget_timezone() { ?> <select name="bot_tz" id="bot_tz"> <option value=""></option> <?php $timezone_identifiers = DateTimeZone::listIdentifiers(); foreach ($timezone_identifiers as $tz) { print '<option value="' . $tz . '" ' . (JxBotConfig::option('bot_tz') == $tz ? ' selected="true"' : '') . '>' . $tz . '</option>'; } ?> </select> <?php }
public static function check_and_login() { $inputs = JxBotUtil::inputs('username,password'); /* check the user hasn't logged in too often recently */ $stmt = JxBotDB::$db->prepare('SELECT COUNT(*) FROM login WHERE stamp > DATE_SUB(NOW(), INTERVAL 1 MINUTE) AND username=?'); $stmt->execute(array($inputs['username'])); $recent_logins = intval($stmt->fetchAll(PDO::FETCH_NUM)[0][0]); if ($recent_logins > 5) { return false; } /* are credentials wrong? */ if (JxBotConfig::option('admin_user') != $inputs['username'] || JxBotConfig::option('admin_hash') != hash('sha256', $inputs['password'])) { $stmt = JxBotDB::$db->prepare('INSERT INTO login (username, note) VALUES (?, ?)'); $stmt->execute(array($inputs['username'], 'failure')); return false; } /* do the login */ $_SESSION['jxbot-admin'] = 1; $stmt = JxBotDB::$db->prepare('INSERT INTO login (username, note) VALUES (?, ?)'); $stmt->execute(array($inputs['username'], 'success')); $_SESSION['jxbot-last'] = time(); /* generate the admin page */ JxBotAdmin::admin_generate(); return true; }
public static function normalise($in_input) { //if ($in_keep_wildcards) return JxBotNL::normalise_pattern($in_input); /* preparation */ $output = ' ' . $in_input . ' '; // leading & trailing space to help with substitution matching // consider replacing vertical & unusual horizontal whitespace (tabs) with all spaces here $output = JxBotNL::upper($output); /* do `tagging` in a different routine; really a pre-normalisation function */ /* `substitution` normalisations; substitutions, abbreviations, spelling */ $output = JxBotNL::apply_substitutions($output); /* `pattern fitting` normalisations */ if (JxBotConfig::option('pre_strip_accents', 0) == 1) { $output = JxBotNL::strip_accents($output); } $output = JxBotNL::strip_punctuation($output); $output = JxBotNL::split_words($output); return $output; }