Distributed programming/AutoHotkey: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
m (Fixed syntax highlighting.)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{libheader|WinSock2.ahk}}
{{collection|Distributed Program}}
Library: {WinSock2.ahk}


WinSock2 library created by derRaphael. Basic code structure created by Trikster. Untested. If someone reading this has two computers and AutoHotkey installed on both, please test. My other computers are all in states of semi-decay.
WinSock2 library created by derRaphael. Basic code structure created by Trikster. Everything else created by B R (skylord5816 on IRC).
<div style="clear:both;width:full;overflow:scroll"><lang autohotkey>#Include WinSock2.ahk
<div style="clear:both;width:full;overflow:scroll"><syntaxhighlight lang="autohotkey">#Include WinSock2.ahk
#Persistent
#Persistent
#SingleInstance, force
#SingleInstance, force
#Include WinSock2.ahk ; derRaphaels WinSock Rewrite
#Include, WinSock2.ahk
SetBatchLines, -1
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%
SetWorkingDir, %A_ScriptDir%
Line 14: Line 13:
Gui, Add, DropDownList, vDDL1 gDDL1, Select something!||One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten
Gui, Add, DropDownList, vDDL1 gDDL1, Select something!||One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten
Gui, Add, Button, gB3, Click me!
Gui, Add, Button, gB3, Click me!

Server := "irc.freenode.net"
Server := "irc.freenode.net"
Port := "6667"
Port := "6667"

Channel := "#UIHjbfilgbafLIEfbAIJCICBGncaiJBHiwehFIUHEIbnjKCINJEhiUAHEJcKLACui" ; Choose something long and random - a channel sure to be deserted.
Channel := "#aokjf" ; Choose something long and random - a channel sure to be deserted.

Nick := "somenick" ; Register this! You can drop it later if you want, but register it!
Nick := "aokjfONE" ; Register this! You can drop it later if you want, but register it!
Pass := "somepass" ; It can be any nick and any pass.
Pass := "aokjfONE" ; It can be any nick and any pass.

Name := "distributed program"
Name := "distributed program"

BotCmd := "!"
BotCmd := "!"
OtherBotCmd := "~"
OtherBotCmd := "~"

OnExit, CleanUp
OnExit, CleanUp
WS2_CleanUp()
WS2_CleanUp()
Line 34: Line 33:
MsgBox, 16, Error!, An error occured wilist connecting to %Connection%
MsgBox, 16, Error!, An error occured wilist connecting to %Connection%
}
}

; Set OnMessage function
; Set OnMessage function
WS2_AsyncSelect(Socket, "DataProcess")
WS2_AsyncSelect(Socket, "DataProcess")
; Send AUTH info to the server
; Send AUTH info to the server
; USER A-trivial-nickname a-domain-like-google.com some-trivial-stuff :your-real-name
; USER A-trivial-nickname a-domain-like-google.com some-trivial-stuff :your-real-name
WS2_SendData(Socket, "USER " . Nick . " google.com AHKBOT :" . Name . "`n") ; All data send to the server must be
WS2_SendData(Socket, "USER " . Nick . " google.com AHKBOT :" . Name . "`n") ; All data send to the server must be
; followed by a newline.
; followed by a newline.
; PASS A-trivial-pass
; PASS A-trivial-pass
WS2_SendData(Socket, "PASS " . Pass . "`n")
WS2_SendData(Socket, "PASS " . Pass . "`n")
; NICK A-trivial-nick
; NICK A-trivial-nick
WS2_SendData(Socket, "NICK " . Nick . "`n")
WS2_SendData(Socket, "NICK " . Nick . "`n")
; Join channel
; Join channel
; JOIN A-trivial-channel
; JOIN A-trivial-channel
WS2_SendData(Socket, "JOIN " . Channel . "`n")
WS2_SendData(Socket, "JOIN " . Channel . "`n")
Gui, Show
Gui, Show
Return
Return

DataProcess(Socket, Data) ; OnMessage function
DataProcess(Socket, Data) ; OnMessage function
{
{
Line 63: Line 62:
StringReplace, Command, Param5, % Chr(10),, All
StringReplace, Command, Param5, % Chr(10),, All
StringReplace, Command, Command, % Chr(13),, All
StringReplace, Command, Command, % Chr(13),, All
If (Param1 == "PING")
If (Param1 == "PING")
WS2_SendData(Socket, "PONG " . Param2 . "`n")
WS2_SendData(Socket, "PONG " . Param2 . "`n")
Else If (RegExMatch(Data, ":\" . BotCMD . " "))
Else If (RegExMatch(Data, ":\" . BotCMD . " "))
{
{
If (Command == "B1"
If (Command == "B1")
MsgBox, The other person pressed Button Number One
MsgBox, The other person pressed Button Number One
Else If (Command == "B2E1")
Else If (Command == "E1B2")
{
{
out :=
MsgBox, The other person typed in Edit One then pressed Button Two
Loop
Loop
{
{
If (A_Index <= 6)
If (A_Index < 6)
continue
continue
If Param%A_Index%
If Param%A_Index%
Line 82: Line 81:
break
break
}
}
MsgBox, The other person typed:%out%
StringTrimRight, out, out, 1
MsgBox, The other person typed:`n%out%
}
}
Else If (Command == "DDL1")
Else If (Command == "DDL1")
MsgBox, The other person selected %Param7% in the drop-down-list.
MsgBox, The other person selected %Param6% %Param7% in the drop-down-list.
Else If (Command == "B3")
Else If (Command == "B3")
MsgBox, The other person clicked Button Three.
MsgBox, The other person clicked Button Three.
Line 92: Line 92:
}
}
B1:
B1:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B1"
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B1`n")
return
return
B2:
B2:
Gui, Submit, NoHide
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B2E1 " . E1 . " | " . B2
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " E1B2 " . E1 . "`n")
return
return
DDL1:
DDL1:
Gui, Submit, NoHide
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " DDL1 " . DDL1
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " DDL1 " . DDL1 . "`n")
return
return
B3:
B3:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B3"
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B3" . "`n")
return
return
CleanUp:
CleanUp:
GuiClose:
GuiClose:
WS2_CleanUp()
WS2_CleanUp()
ExitApp</lang></div>
ExitApp</syntaxhighlight></div>


Put the above on one computer, and run it. Then put the below on a different computer (no need for a network, just add Internet!) and run it.
Put the above on one computer, and run it. Then put the below on a different computer (no need for a network, just add Internet!) and run it.
<div style="width:full;overflow:scroll"><lang autohotkey>#Include WinSock2.ahk
<div style="width:full;overflow:scroll"><syntaxhighlight lang="autohotkey">#Include WinSock2.ahk
#Persistent
#Persistent
#SingleInstance, force
#SingleInstance, force
#Include WinSock2.ahk ; derRaphaels WinSock Rewrite
#Include WinSock2.ahk
SetBatchLines, -1
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%
SetWorkingDir, %A_ScriptDir%
Line 122: Line 122:
Gui, Add, DropDownList, vDDL1 gDDL1, Select something!||One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten
Gui, Add, DropDownList, vDDL1 gDDL1, Select something!||One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten
Gui, Add, Button, gB3, Click me!
Gui, Add, Button, gB3, Click me!

Server := "irc.freenode.net"
Server := "irc.freenode.net"
Port := "6667"
Port := "6667"

Channel := "#UIHjbfilgbafLIEfbAIJCICBGncaiJBHiwehFIUHEIbnjKCINJEhiUAHEJcKLACui" ; Choose something long and random - a channel sure to be deserted.
Channel := "#aokjf" ; Choose something long and random - a channel sure to be deserted.

Nick := "someothernick" ; Register this! You can drop it later if you want, but register it!
Nick := "aokjfTWO" ; Register this! You can drop it later if you want, but register it!
Pass := "someotherpass" ; It can be any nick and any pass.
Pass := "aokjfTWO" ; It can be any nick and any pass.

Name := "distributed program"
Name := "distributed program"

BotCmd := "~"
BotCmd := "~"
OtherBotCmd := "!"
OtherBotCmd := "!"

OnExit, CleanUp
OnExit, CleanUp
WS2_CleanUp()
WS2_CleanUp()
Line 142: Line 142:
MsgBox, 16, Error!, An error occured wilist connecting to %Connection%
MsgBox, 16, Error!, An error occured wilist connecting to %Connection%
}
}

; Set OnMessage function
; Set OnMessage function
WS2_AsyncSelect(Socket, "DataProcess")
WS2_AsyncSelect(Socket, "DataProcess")
; Send AUTH info to the server
; Send AUTH info to the server
; USER A-trivial-nickname a-domain-like-google.com some-trivial-stuff :your-real-name
; USER A-trivial-nickname a-domain-like-google.com some-trivial-stuff :your-real-name
WS2_SendData(Socket, "USER " . Nick . " google.com AHKBOT :" . Name . "`n") ; All data send to the server must be
WS2_SendData(Socket, "USER " . Nick . " google.com AHKBOT :" . Name . "`n") ; All data send to the server must be
; followed by a newline.
; followed by a newline.
; PASS A-trivial-pass
; PASS A-trivial-pass
WS2_SendData(Socket, "PASS " . Pass . "`n")
WS2_SendData(Socket, "PASS " . Pass . "`n")
; NICK A-trivial-nick
; NICK A-trivial-nick
WS2_SendData(Socket, "NICK " . Nick . "`n")
WS2_SendData(Socket, "NICK " . Nick . "`n")
; Join channel
; Join channel
; JOIN A-trivial-channel
; JOIN A-trivial-channel
WS2_SendData(Socket, "JOIN " . Channel . "`n")
WS2_SendData(Socket, "JOIN " . Channel . "`n")
Gui, Show
Gui, Show
Return
Return

DataProcess(Socket, Data) ; OnMessage function
DataProcess(Socket, Data) ; OnMessage function
{
{
Line 171: Line 171:
StringReplace, Command, Param5, % Chr(10),, All
StringReplace, Command, Param5, % Chr(10),, All
StringReplace, Command, Command, % Chr(13),, All
StringReplace, Command, Command, % Chr(13),, All
If (Param1 == "PING")
If (Param1 == "PING")
WS2_SendData(Socket, "PONG " . Param2 . "`n")
WS2_SendData(Socket, "PONG " . Param2 . "`n")
Else If (RegExMatch(Data, ":\" . BotCMD . " "))
Else If (RegExMatch(Data, ":\" . BotCMD . " "))
{
{
If (Command == "B1"
If (Command == "B1")
MsgBox, The other person pressed Button Number One
MsgBox, The other person pressed Button Number One
Else If (Command == "B2E1")
Else If (Command == "E1B2")
{
{
out :=
MsgBox, The other person typed in Edit One then pressed Button Two
Loop
Loop
{
{
If (A_Index <= 6)
If (A_Index < 6)
continue
continue
If Param%A_Index%
If Param%A_Index%
Line 190: Line 190:
break
break
}
}
MsgBox, The other person typed:%out%
StringTrimRight, out, out, 1
MsgBox, The other person typed:`n%out%
}
}
Else If (Command == "DDL1")
Else If (Command == "DDL1")
MsgBox, The other person selected %Param7% in the drop-down-list.
MsgBox, The other person selected %Param6% %Param7% in the drop-down-list.
Else If (Command == "B3")
Else If (Command == "B3")
MsgBox, The other person clicked Button Three.
MsgBox, The other person clicked Button Three.
Line 200: Line 201:
}
}
B1:
B1:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B1"
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B1`n")
return
return
B2:
B2:
Gui, Submit, NoHide
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B2E1 " . E1 . " | " . B2
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " E1B2 " . E1 . "`n")
return
return
DDL1:
DDL1:
Gui, Submit, NoHide
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " DDL1 " . DDL1
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " DDL1 " . DDL1 . "`n")
return
return
B3:
B3:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B3"
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B3" . "`n")
return
return
CleanUp:
CleanUp:
GuiClose:
GuiClose:
WS2_CleanUp()
WS2_CleanUp()
ExitApp</lang></div>
ExitApp</syntaxhighlight></div>

Latest revision as of 13:04, 1 September 2022

Library: WinSock2.ahk

WinSock2 library created by derRaphael. Basic code structure created by Trikster. Everything else created by B R (skylord5816 on IRC).

#Include WinSock2.ahk
#Persistent
#SingleInstance, force
#Include, WinSock2.ahk
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%
Gui, Add, Button, gB1, Button number One!
Gui, Add, Edit, vE1, Type in me!
Gui, Add, Button, gB2, Press me after you're done typing!
Gui, Add, DropDownList, vDDL1 gDDL1, Select something!||One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten
Gui, Add, Button, gB3, Click me!
 
Server  := "irc.freenode.net"
Port    := "6667"             
 
Channel := "#aokjf" ; Choose something long and random - a channel sure to be deserted.
 
Nick := "aokjfONE"  ; Register this! You can drop it later if you want, but register it!
Pass := "aokjfONE"  ; It can be any nick and any pass.
 
Name := "distributed program"
 
BotCmd := "!"
OtherBotCmd := "~"
 
OnExit, CleanUp
WS2_CleanUp()
If (!Socket := WS2_Connect(Connection := Server . ":" . Port))
{
   MsgBox, 16, Error!, An error occured wilist connecting to %Connection%
}
 
   ; Set OnMessage function
   WS2_AsyncSelect(Socket, "DataProcess")
 
   ; Send AUTH info to the server
   ; USER A-trivial-nickname a-domain-like-google.com some-trivial-stuff :your-real-name
   WS2_SendData(Socket, "USER " . Nick . " google.com AHKBOT :" . Name . "`n") ; All data send to the server must be
                                                                               ; followed by a newline.
 
   ; PASS A-trivial-pass
   WS2_SendData(Socket, "PASS " . Pass . "`n")
 
   ; NICK A-trivial-nick
   WS2_SendData(Socket, "NICK " . Nick . "`n")
 
   ; Join channel
   ; JOIN A-trivial-channel
   WS2_SendData(Socket, "JOIN " . Channel . "`n")
 
   Gui, Show
Return
 
DataProcess(Socket, Data) ; OnMessage function
{
   global Server,Port,Channel,Nick,Pass,Name,BotCMD
   StringSplit, Param, Data, %A_Space%
   Name := SubStr(Data, 2, InStr(Data, "!")-2)
   StringReplace, Command, Param5, % Chr(10),, All
   StringReplace, Command, Command, % Chr(13),, All
 
   If (Param1 == "PING")
      WS2_SendData(Socket, "PONG " . Param2 . "`n")
   Else If (RegExMatch(Data, ":\" . BotCMD . " "))
   {
      If (Command == "B1")
         MsgBox, The other person pressed Button Number One
      Else If (Command == "E1B2")
      {
         out :=
         Loop
         {
            If (A_Index < 6)
               continue
            If Param%A_Index%
               out := out . " " . Param%A_Index%
            Else
               break
         }
         StringTrimRight, out, out, 1
         MsgBox, The other person typed:`n%out%
      }
      Else If (Command == "DDL1")
         MsgBox, The other person selected %Param6% %Param7% in the drop-down-list.
      Else If (Command == "B3")
         MsgBox, The other person clicked Button Three.
   }
   return
}
B1:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B1`n")
return
B2:
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " E1B2 " . E1 . "`n")
return
DDL1:
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " DDL1 " . DDL1 . "`n")
return
B3:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B3" . "`n")
return
CleanUp:
GuiClose:
WS2_CleanUp()
ExitApp

Put the above on one computer, and run it. Then put the below on a different computer (no need for a network, just add Internet!) and run it.

#Include WinSock2.ahk
#Persistent
#SingleInstance, force
#Include WinSock2.ahk
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%
Gui, Add, Button, gB1, Button number One!
Gui, Add, Edit, vE1, Type in me!
Gui, Add, Button, gB2, Press me after you're done typing!
Gui, Add, DropDownList, vDDL1 gDDL1, Select something!||One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten
Gui, Add, Button, gB3, Click me!
 
Server  := "irc.freenode.net"
Port    := "6667"             
 
Channel := "#aokjf" ; Choose something long and random - a channel sure to be deserted.
 
Nick := "aokjfTWO"  ; Register this! You can drop it later if you want, but register it!
Pass := "aokjfTWO"  ; It can be any nick and any pass.
 
Name := "distributed program"
 
BotCmd := "~"
OtherBotCmd := "!"
 
OnExit, CleanUp
WS2_CleanUp()
If (!Socket := WS2_Connect(Connection := Server . ":" . Port))
{
   MsgBox, 16, Error!, An error occured wilist connecting to %Connection%
}
 
   ; Set OnMessage function
   WS2_AsyncSelect(Socket, "DataProcess")
 
   ; Send AUTH info to the server
   ; USER A-trivial-nickname a-domain-like-google.com some-trivial-stuff :your-real-name
   WS2_SendData(Socket, "USER " . Nick . " google.com AHKBOT :" . Name . "`n") ; All data send to the server must be
                                                                               ; followed by a newline.
 
   ; PASS A-trivial-pass
   WS2_SendData(Socket, "PASS " . Pass . "`n")
 
   ; NICK A-trivial-nick
   WS2_SendData(Socket, "NICK " . Nick . "`n")
 
   ; Join channel
   ; JOIN A-trivial-channel
   WS2_SendData(Socket, "JOIN " . Channel . "`n")
 
   Gui, Show
Return
 
DataProcess(Socket, Data) ; OnMessage function
{
   global Server,Port,Channel,Nick,Pass,Name,BotCMD
   StringSplit, Param, Data, %A_Space%
   Name := SubStr(Data, 2, InStr(Data, "!")-2)
   StringReplace, Command, Param5, % Chr(10),, All
   StringReplace, Command, Command, % Chr(13),, All
 
   If (Param1 == "PING")
      WS2_SendData(Socket, "PONG " . Param2 . "`n")
   Else If (RegExMatch(Data, ":\" . BotCMD . " "))
   {
      If (Command == "B1")
         MsgBox, The other person pressed Button Number One
      Else If (Command == "E1B2")
      {
         out :=
         Loop
         {
            If (A_Index < 6)
               continue
            If Param%A_Index%
               out := out . " " . Param%A_Index%
            Else
               break
         }
         StringTrimRight, out, out, 1
         MsgBox, The other person typed:`n%out%
      }
      Else If (Command == "DDL1")
         MsgBox, The other person selected %Param6% %Param7% in the drop-down-list.
      Else If (Command == "B3")
         MsgBox, The other person clicked Button Three.
   }
   return
}
B1:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B1`n")
return
B2:
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " E1B2 " . E1 . "`n")
return
DDL1:
Gui, Submit, NoHide
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " DDL1 " . DDL1 . "`n")
return
B3:
WS2_SendData(Socket, "PRIVMSG " . Channel . " :" . OtherBotCmd . " B3" . "`n")
return
CleanUp:
GuiClose:
WS2_CleanUp()
ExitApp