IRC gateway: Difference between revisions

From Rosetta Code
Content added Content deleted
m (moved IRC Gateway to IRC gateway: capitalization policy)
(→‎Tcl: Added implementation)
Line 1: Line 1:
{{draft task}} Create an IRC Gateway capable of connecting an IRC server with another IRC server or [[Chat server]]
{{draft task}} Create an IRC Gateway capable of connecting an IRC server with another IRC server or [[Chat server]]

=={{header|Tcl}}==
This code is called as a complete script, perhaps like this:
<lang sh>./ircgateway.tcl irc://hostA.org/fishing bait irc://hostB.com:6667/haxors botfly</lang>
{{tcllib|picoirc}}
<lang tcl>#!/bin/env tclsh8.5
package require picoirc

### Parse script arguments
# URL form: irc://foobar.org/secret
if {$argc != 4} {
puts stderr "wrong # args: should be \"$argv0 ircA nickA ircB nickB\""
exit 1
}
lassign $argv url1 nick1 url2 nick2

### How to do the forwarding from one side to the other
proc handle {from to -> state args} {
upvar #0 conn($from) f conn($to) t chan($to) chan
switch -exact -- $state {
"chat" {
lassign $args target nick message type
if {![string match "*>>*<<*" $message]} {
picoirc::post $t $chan ">>$nick said<< $message"
}
}
"traffic" {
lassign $args action channel nick newnick
switch -exact -- $action {
"entered" - "left" {
picoirc::post $t $chan ">>$nick has $action<<"
}
}
}
"close" {
exit
}
}
}

### Connect and run the event loop
set chan(1) [lindex [picoirc::splituri $url1] 2]
set chan(2) [lindex [picoirc::splituri $url1] 2]
interp alias {} handle1to2 {} handle 1 2
interp alias {} handle2to1 {} handle 2 1
set conn(1) [picoirc::connect handle1to2 $nick1 $url1]
set conn(2) [picoirc::connect handle2to1 $nick2 $url2]
vwait forever</lang>

Revision as of 16:21, 19 June 2011

IRC gateway is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Create an IRC Gateway capable of connecting an IRC server with another IRC server or Chat server

Tcl

This code is called as a complete script, perhaps like this: <lang sh>./ircgateway.tcl irc://hostA.org/fishing bait irc://hostB.com:6667/haxors botfly</lang>

Library: Tcllib (Package: picoirc)

<lang tcl>#!/bin/env tclsh8.5 package require picoirc

      1. Parse script arguments
  1. URL form: irc://foobar.org/secret

if {$argc != 4} {

   puts stderr "wrong # args: should be \"$argv0 ircA nickA ircB nickB\""
   exit 1

} lassign $argv url1 nick1 url2 nick2

      1. How to do the forwarding from one side to the other

proc handle {from to -> state args} {

   upvar #0 conn($from) f conn($to) t chan($to) chan
   switch -exact -- $state {

"chat" { lassign $args target nick message type if {![string match "*>>*<<*" $message]} { picoirc::post $t $chan ">>$nick said<< $message" } } "traffic" { lassign $args action channel nick newnick switch -exact -- $action { "entered" - "left" { picoirc::post $t $chan ">>$nick has $action<<" } } } "close" { exit }

   }

}

      1. Connect and run the event loop

set chan(1) [lindex [picoirc::splituri $url1] 2] set chan(2) [lindex [picoirc::splituri $url1] 2] interp alias {} handle1to2 {} handle 1 2 interp alias {} handle2to1 {} handle 2 1 set conn(1) [picoirc::connect handle1to2 $nick1 $url1] set conn(2) [picoirc::connect handle2to1 $nick2 $url2] vwait forever</lang>