08-18-2017, 14:42 +0200
AuthorPost
thommey  10-15-2009, 01:03   | [EGG] Script to accept pastes via msg (w/coroutine example) Jabber
(Administrator)
Member since 03/2004
62 Posts
Without coroutines                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
set webdir "/home/public_www/tcl/"
set website "http://shell7.powershells.de/~tcl/"
bind msg - paste paste
proc paste {n u h t} {
    global paste
    set paste($n) ""
    putserv "PRIVMSG $n :Ok, now you may paste your script. Use\"endpaste\" if you're finished."
    return 1
}
bind msgm - * pasteapp
proc pasteapp {n u h t} {
    global paste
    if {![info exists paste($n)]} { return 0 }
    if {$t == "paste" || $t == "endpaste"} { return 0 }
    lappend paste($n) $t
}
bind msg - endpaste endpaste
proc endpaste {n u h t} {
    global paste webdir website
    if {![info exists paste($n)]} {
        putserv "PRIVMSG $n :\"endpaste\" without \"paste\". Ignoring."
        return 1
    }
    if {$paste($n) == ""} {
        putserv "PRIVMSG $n :No text stored. Please retry."
        return 1
    }
    regsub -nocase -- {[^a-zA-Z0-9|-]} $n {} n2
    set fs [open [file join $webdir $n2.html] w]
    puts $fs "<html><head><title>Paste on [clock format [clock seconds]] from $n</title></head><body>"
    set cnt 1
    foreach line $paste($n) {
        regsub -all {\t} $line {\&nbsp;\&nbsp;\&nbsp;\&nbsp;} line
        puts $fs "$cnt[str {&nbsp;} [expr {4 - [string length $cnt]}]]&nbsp;$line<br>"
        incr cnt
    }
    puts $fs "</body></html>"
    close $fs
    unset paste($n)
    putserv "PRIVMSG $n :$website$n2.html generated."
}
proc str {str tms} {
    set res ""
    for {set i 1} {$i <= $tms} {incr i} {
        append res $str
    }
    return $res
}


With coroutines
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
set webdir "/home/public_www/tcl/"
set website "http://shell7.powershells.de/~tcl/"
bind msg - paste paste
proc paste {n u h t} {
    if {[info commands coro_paste_$n] == ""} {
        coroutine coro_paste_$n paste_template $n; # create the coroutine from the template
    } else {
        coro_paste_$n $t; # continue the existing coroutine for this user
    }
}
proc paste_template {n} {
    global webdir website
    putserv "PRIVMSG $n :Ok, now you may paste your script. Use\"endpaste\" if you're finished."
    regsub -nocase -- {[^a-zA-Z0-9|-]} $n {} n2
    set fs [open [file join $webdir $n2.html] w]
    puts $fs "<html><head><title>Paste on [clock format [clock seconds]] from $n</title></head><body>"
    set linenr 0
    # the yield here pauses and waits, returns the argument with which the coroutine is continued
    while {[set text [yield]] != "endpaste"} {
        regsub -all {\t} $text {\&nbsp;\&nbsp;\&nbsp;\&nbsp;} text
        puts $fs "$linenr[str {&nbsp;} [expr {4 - [string length $linenr]}]]&nbsp;$text<br>"
        incr linenr
    }
    puts $fs "</body></html>"
    close $fs
    putserv "PRIVMSG $n :$website$n2.html generated."
}

proc str {str tms} {
    set res ""
    for {set i 1} {$i <= $tms} {incr i} {
        append res $str
    }
    return $res
}

Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen