I'm no longer maintaining this code. The last update I made was 11/23/2006. This is the same code I linked on the first page. I am posting it here in response to a request by a user. I hope this will be useful to someone.
functions.ahk
Code:
TimeVar() {
FormatTime, output,, time
StringSplit array, output, %A_Space%
/* Uncomment this to remove the space between the time and am/pm
loop {
index:=A_Index-1
array:=% array array%A_Index%
msgbox % array
if (index=array0) {
output:= array
break
}
}
*/
output:=strtolower(output)
return output
}
RunScript() {
tmpAHK=update.ahk
FileDelete,%A_Temp%\%tmpAHK%
FileAppend,
(
#NoTrayIcon
var:=%input%
UrlDownloadToFile,%url%%RunUrl%,%A_Temp%\tmp.txt
FileDelete,%A_Temp%\%tmpAHK%
),%A_Temp%\%tmpAHK%
return
}
strtolower(input) {
StringLower, input, input
return input
}
RemoveFromQueue(array) { ;array param must be passed as a string
Global
Local index
loop {
if (A_Index < 2) {
continue
}
index:=A_Index-1
%array%%index%=% %array%%A_Index%
if (index=%array%0) {
%array%%index%=
%array%0--
return
}
}
}
RemoveFromStack(array) { ;array param must be passed as a string
Global
var= % array %array%0
%array%0--
}
RTrim(input) {
loop {
StringRight, char, input, 1
if (char=A_Space) {
StringTrimRight, input, input, 1
} else {
return input
}
}
}
LTrim(input) {
loop {
StringLeft, char, input, 1
if (char=A_Space) {
StringTrimLeft, input, input, 1
} else {
return input
}
}
}
Trim(input) {
input:=RTrim(input)
input:=LTrim(input)
return input
}
PrintArray(array) { ;array param must be passed as a string
Global
Local msg
if (%array%0=0) {
return
}
msg:="The array " array " has " %array%0 " elements`n"
loop {
msg:=msg array A_Index "= " %array%%A_Index% "`n"
if (A_Index=%array%0)
{
msgbox %msg%
return
}
}
}
CollapseArray(array) { ;array param must be passed as a string
Global
Local output
Local index
loop {
index:=A_Index-1
%array%:=% %array% %array%%A_Index%
if (index=%array%0) {
output= % %array%
return output
}
}
}
CreateGetKeys(input) { ;converts a string containing keys and values to variables
Global
StringSplit Get, %input%, &
GetKey0=0
loop {
if (GetKey0=Get0) {
;EmptyArray("Get")
KeyVal1=
KeyVal2=
return
}
StringSplit KeyVal, Get%A_Index%, =
GetKey0++
GetKey%A_Index%=%KeyVal1%
%KeyVal1%=%KeyVal2%
}
}
PrintGetKeys(array) {
Global
Local output
loop %GetKey0% {
value:=GetKey%A_Index%
output:=output GetKey%A_Index% " => " %value% "`n"
}
output:=output "`nTotal keys: " GetKey0
msgbox %output%
}
Get(key) {
Global GetKey0
loop %GetKey0% {
getkey:=GetKey%A_Index%
getvalue:=%getkey%
if (%getkey%= %key%) {
return %getvalue%
}
}
}
EmptyArray(array) {
;add support for passed parameters 0, 1, 2..
}
OptimizeArray(array) { ;removes empty indexes from an array
}
event.php
Code:
<?php
$event = $_GET['event'];
###########################
# 1=Yes, 0=No #
$chatevent = "1"; #
###########################
if ($event == 'eventquery') {
if ($chatevent == 1) {
echo 'Yes';
} else {
echo 'No';
}
}
?>
db.sql
Code:
-- phpMyAdmin SQL Dump
-- version 2.7.0-pl2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 23, 2006 at 01:06 PM
-- Server version: 5.0.18
-- PHP Version: 5.1.2
--
-- Database: `ahkchat`
--
-- --------------------------------------------------------
--
-- Table structure for table `ahkchatchat`
--
CREATE TABLE `ahkchatchat` (
`msgid` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL default '',
`date` timestamp NOT NULL default CURRENT_TIMESTAMP,
`msg` text NOT NULL,
PRIMARY KEY (`msgid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `ahkchatchat`
--
-- --------------------------------------------------------
--
-- Table structure for table `ahkchatusers`
--
CREATE TABLE `ahkchatusers` (
`userid` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL default '',
`Alias` varchar(50) NOT NULL default 'no',
`loggedin` char(3) NOT NULL default 'yes',
`ip` varchar(50) NOT NULL default '',
`lastping` timestamp NOT NULL default CURRENT_TIMESTAMP,
`lastmsg` int(11) NOT NULL default '0',
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `ahkchatusers`
--
chat.php
Code:
<?php
/* Warning codes, put to errorlog
W001: Username already in use
*/
$event = $_GET['event'];
$msg = $_GET['msg'];
$username = $_GET['loggedusername'];
$loggedusername = $_GET['loggedusername'];
$alias = trim($_GET['alias']);
if ($db = mysql_connect('localhost', 'root', 'password')) {
mysql_select_db('ahkchat', $db);
//echo 'Connected to the database.';
} else {
die('Could not connect: ' . mysql_error());
}
if ($event == 'chat') {
$sql_events = mysql_query("SELECT * FROM ahkchatusers where username='$loggedusername' and loggedin='yes'") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$lastmsg = $row["lastmsg"];
}
$sql_events2 = mysql_query("SELECT * FROM ahkchatchat where msgid>'$lastmsg' order by msgid desc") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events2)) {
$username = $row["username"];
$msg = $row["msg"];
$loggedin = $row["loggedin"];
if ($loggedin == "no") {
echo 'You have been logged out';
exit;
}
if ($msgid <= $row["msgid"]) {
$msgid = $row["msgid"];
}
if ($username <> "SERVER_MSG") {
if ($username <> $loggedusername) {
$msgout[]= "&()[%time%] « $username » $msg";
}
} else {
$msgout[]= "&()[%time%] $msg";
}
}
if ($msgout[0] != '') {
for ($i=count($msgout)-1; $i>=0; $i--) {
echo $msgout[$i];
}
mysql_query("UPDATE ahkchatusers SET lastmsg='$msgid' where username='$loggedusername' and loggedin='yes'") or die('Error, insert query failed');
}
}
if ($event == 'login') {
$sql_events3 = mysql_query("SELECT * FROM ahkchatusers where username='$username' limit 1") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events3)) {
$loggedin = $row["loggedin"];
}
if ($loggedin == 'yes') {
echo "Warning: W001";
}
if ($loggedin == 'no') {
$ip = GetHostByName($REMOTE_ADDR);
$query2 = "UPDATE ahkchatusers SET loggedin='yes', ip='$ip' where username='$username'";
mysql_query($query2) or die("Could not connect: " . mysql_error());
echo "success";
}
if ($loggedin == '') {
$ip = GetHostByName($REMOTE_ADDR);
$query3 = "INSERT INTO ahkchatusers (username, loggedin, ip) VALUES ('$username', 'yes', '$ip')";
mysql_query($query3) or die('Error, insert query failed');
echo "success";
}
$result = mysql_query("SELECT * FROM ahkchatchat") or die (mysql_error());
$num_rows = mysql_num_rows($result);
$query4 = "UPDATE ahkchatusers SET lastmsg='$num_rows'";
mysql_query($query4) or die("Could not connect: " . mysql_error());
}
if ($event == 'logout') {
$query5 = "UPDATE ahkchatusers SET loggedin='no' where username='$username'";
mysql_query($query5) or die("Could not connect: " . mysql_error());
}
if ($event == 'sendtext') {
$query6 = "INSERT INTO ahkchatchat (username, msg) VALUES ('$username', '$msg')";
mysql_query($query6) or die('Error, insert query failed');
}
if ($event == 'userlist') {
$sql_events4 = mysql_query("SELECT * FROM ahkchatusers where loggedin='yes'") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events4)) {
$username = $row["username"];
echo $username;
echo ' &()';
}
}
if ($event == 'ping') {
mysql_query("UPDATE ahkchatusers SET lastping=CURRENT_TIMESTAMP WHERE username='$username'") or die('Error, insert query failed');
}
if ($event == 'setalias') {
$query = mysql_query("SELECT * FROM ahkchatusers WHERE username='$alias'") or die (mysql_error());
while ($row = mysql_fetch_array($query)) {
$loggedin = $row["loggedin"];
}
if ($loggedin == 'no' || $loggedin == '') {
mysql_query("UPDATE ahkchatusers SET username='$alias' where username='$username'") or die('Error, insert query failed');
echo $alias;
}
}
if ($event == 'whoisactive') {
$query_myping = mysql_query("SELECT * FROM ahkchatusers where username='$username' limit 1") or die (mysql_error());
while ($row = mysql_fetch_array($query_myping)) {
$myping = $row["lastping"];
}
$query_username = mysql_query("SELECT * FROM ahkchatusers where loggedin='yes'") or die (mysql_error());
while ($row = mysql_fetch_array($query_username)) {
$username = $row["username"];
$query_userping = mysql_query("SELECT * FROM ahkchatusers where username='$username' limit 1") or die (mysql_error());
while ($row = mysql_fetch_array($query_userping)) {
$theirping = $row["lastping"];
}
$mytime=strtotime($myping);
$theirtime=strtotime($theirping);
$diff = $mytime-$theirtime;
if ($diff >= 30) {
mysql_query("UPDATE ahkchatusers SET loggedin='no' where username='$username'") or die('Error, insert query failed');
mysql_query("INSERT INTO ahkchatchat (username, msg) VALUES ('SERVER_MSG', 'Quits: $username (Connection timed out)')") or die('Error, insert query failed');
}
}
}
mysql_close($db);
?>
chat.ahk
Code:
#NoTrayIcon
#SingleInstance off
url=http://localhost/ ;You must include the ending forward slash
chatname=chat
if 0 > 1
{
MsgBox,, Error, This script allows only one incoming parameter but it received %0%.
ExitApp
}
if 0 = 1
{
CreateGetKeys("1")
event:=strtolower(Get("event"))
msg:=Get("msg")
username:=Get("username")
alias:=Get("alias")
chat_id:=Get("chat_id")
userlist_id:=Get("userlist_id")
ahk_id:=Get("ahk_id")
if (event = "logout") {
URLDownloadToFile, %url%chat.php?event=%event%&loggedusername=%username%, %A_Temp%\logout.txt
ExitApp
}
if (event = "sendtext") {
URLDownloadToFile, %url%chat.php?event=%event%&loggedusername=%username%&msg=%msg%, %A_Temp%\sendtxt.txt
ExitApp
}
if (event = "chat") {
URLDownloadToFile, %url%chat.php?event=userlist, %A_Temp%\userlist.txt
FileReadLine, userlist, %A_Temp%\userlist.txt, 1
StringReplace, userlist, userlist, &(), `r`n, 1
ControlSetText, %userlist_id%, %userlist%, ahk_id %ahk_id%
URLDownloadToFile, %url%chat.php?event=chat&loggedusername=%username%, %A_Temp%\chat.txt
FileReadLine, chatnew, %A_Temp%\chat.txt, 1
chatnew:=trim(chatnew)
if (chatnew <> "") {
ControlGetText, chat, %chat_id%, ahk_id %ahk_id%
chat:= chat chatnew
StringReplace, chat, chat,% "%time%",% TimeVar(), 1
if (chat <> "") {
StringLeft, First3Char, chat, 3
if (First3Char="&()") {
StringTrimLeft chat, chat, 3
}
}
StringReplace, chat, chat, &(), `r`n, 1
ControlSetText, %chat_id%, %chat%, ahk_id %ahk_id%
PostMessage, 0x115, 7, 0, %chat_id%, ahk_id %ahk_id%
}
URLDownloadToFile, %url%chat.php?event=ping&loggedusername=%username%, %A_Temp%\tmp.txt
ExitApp
}
if (event = "whoisactive") {
URLDownloadToFile, %url%chat.php?event=whoisactive&loggedusername=%username%, %A_Temp%\tmp.txt
ExitApp
}
MsgBox Error: Script broke from parameter definitions.
ExitApp
}
URLDownloadToFile, %url%event.php?event=eventquery, %A_Temp%\tmp.txt
FileReadLine, result, %A_Temp%\tmp.txt, 1
result = %result%
if (result = "No") {
msgbox,, Alert, The server is not allowing connections at this time
ExitApp
}
else if (result <> "Yes") {
msgbox,, Error, Could not connect to server.
ExitApp
}
Gui, 2:Add, Edit, x76 y10 w100 h20 vusername ,
Gui, 2:Add, Text, x6 y10 w70 h20 , User Name:
Gui, 2:Add, Button, x43 y40 w100 h30 glogin +default, Login
Gui, 2:Show, h83 w188, Login
Return
login:
Login()
return
send:
Send()
return
updatechat:
Updatechat()
return
whoisactive:
Run %comspec% /c %A_ScriptName% "event=whoisactive&username=%username%",, Hide
return
2GuiClose:
gui 2:hide
menu, tray, NoIcon
URLDownloadToFile, %url%chat.php?event=sendtext&loggedusername=SERVER_MSG&msg=Quits: %username% (User left), %A_Temp%\sendtxt.txt
URLDownloadToFile, %url%chat.php?event=logout&loggedusername=%username%, %A_Temp%\logout.txt
FileDelete, %A_Temp%\tmp.txt
FileDelete, %A_Temp%\logout.txt
FileDelete, %A_Temp%\userlist.txt
FileDelete, %A_Temp%\chat.txt
FileDelete, %A_Temp%\login.txt
FileDelete, %A_Temp%\send.txt
;Gui 2:Destroy
ExitApp
Login() {
Global
gui 2:submit
gui 2:destroy
Local loginresult, uNum
URLDownloadToFile, %url%chat.php?event=login&loggedusername=%username%, %A_Temp%\login.txt
FileReadLine, loginresult, %A_Temp%\login.txt, 1
loginresult = %loginresult%
;Check for warnings
IfInString, loginresult, Warning: W001
{
uNum = 1
loop
{
username = %username%%uNum%
URLDownloadToFile, %url%chat.php?event=login&loggedusername=%username%, %A_Temp%\login.txt
FileReadLine, loginresult, %A_Temp%\login.txt, 1
loginresult = %loginresult%
IfNotInString, loginresult, Warning: W001
{
break
}
StringTrimRight username,username,1
uNum := uNum + 1
}
msgbox,, Warning, The username you entered is already in use and has been changed to %username% instead.
}
if loginresult <> success
{
FileRead, loginresult, %A_Temp%\login.txt
;Check for errors
IfInString, loginresult, Could not connect
{
msgbox,, Error, Could not connect to server.
return
}
IfInString, loginresult, Too many connections
{
msgbox,, Error, Login failed: Too many connections
return
}
msgbox,,Warning, There was a problem logging in. Please try again.
return
}
if loginresult = success
{
Run %comspec% /c %A_ScriptName% "event=sendtext&username=SERVER_MSG&msg=%username% has joined the chat.,, Hide
URLDownloadToFile, %url%chat.php?event=userlist, %A_Temp%\userlist.txt
URLDownloadToFile, %url%chat.php?event=chat&loggedusername=%username%, %A_Temp%\chat.txt
FileReadLine, userlist, %A_Temp%\userlist.txt, 1
StringReplace, userlist, userlist, &(), `n, 1
FileReadLine, chat, %A_Temp%\chat.txt, 1
StringReplace, chat, chat, &(), `n, 1
SetTimer, updatechat, 5000
SetTimer, whoisactive, 30000
Gui, 2:Add, Edit, x6 y310 w350 h20 vsendtext,
Gui, 2:Add, Button, x366 y310 w100 h20 gsend +default, Send
Gui, 2:font,, Arial
Gui, 2:Add, Edit, x6 y10 w350 h290 +readonly vchat,
Gui, 2:Add, Edit, x366 y10 w100 h290 +readonly vuserlist, %userlist%
Gui, 2:Show, h342 w477, %chatname%
WinWaitActive, %chatname%
ahk_id := WinExist("A") ; Chat window ID
WinGetTitle, AHK_TITLE, ahk_id %ahk_id% ; Chat window title
GuiControl Focus, chat
ControlGetFocus chat_id, ahk_id %ahk_id%
GuiControl Focus, userlist
ControlGetFocus userlist_id, ahk_id %ahk_id%
GuiControl Focus, sendtext
}
return
}
Send() {
Global sendtext, username, chat_id, ahk_id, url
gui 2:submit, nohide
guicontrol,, sendtext,
StringReplace, sendtext, sendtext, &(),,
StringReplace, sendtext, sendtext, `%time`%, % TimeVar()
StringLeft, firstchar, sendtext, 1
if (sendtext <> "") {
if (firstchar <> "/") {
GuiControlGet, chat,, chat
if (chat = "") {
chat = % chat "[" TimeVar() "] « " username " » " sendtext
} else {
chat = % chat "`n[" TimeVar() "] « " username " » " sendtext
}
guiControl,, chat, %chat%
PostMessage, 0x115, 7, 0, %chat_id%, ahk_id %ahk_id%
}
StringLower, CheckString, sendtext
StringReplace, CheckString, sendtext, /,, All
StringSplit, CheckString, CheckString, %A_Space%,, All
firstword = %CheckString1%
StringLower firstword, firstword
Loop, %CheckString0%
{
If a_index < 2 ; Skip the first element
Continue
query:=trim(query A_Space CheckString%a_index%)
}
if (firstchar = "/") {
if (firstword="nick") {
alias=%query%
;setalias
URLDownloadToFile, %url%chat.php?event=setalias&loggedusername=%username%&alias=%alias%, %A_Temp%\tmp.txt
FileReadLine, newalias, %A_Temp%\tmp.txt, 1
newalias:=trim(newalias)
if (alias=newalias)
{
;login
URLDownloadToFile, %url%chat.php?event=login&loggedusername=%username%, %A_Temp%\login.txt
FileReadLine, loginresult, %A_Temp%\login.txt, 1
loginresult = %loginresult%
if (loginresult <> "success") {
msgbox Error
ExitApp
}
Run %comspec% /c %A_ScriptName% "event=sendtext&username=SERVER_MSG&msg=%username% is now known as %newalias%",, Hide
Run %comspec% /c %A_ScriptName% "event=logout&username=%username%",, Hide
username=%alias%
UpdateChat()
}
}
}
else {
Run %comspec% /c %A_ScriptName% "event=sendtext&username=%username%&msg=%sendtext%",, Hide
}
}
return
}
UpdateChat() {
Global
Run %comspec% /c %A_ScriptName% "event=chat&username=%username%&chat_id=%chat_id%&userlist_id=%userlist_id%&ahk_id=%ahk_id%",, Hide
return
}
#Include functions.ahk