aboutsummaryrefslogtreecommitdiff
path: root/tell.tcl
blob: 76de0b47b8243ac3e80f7f04e5054e98a23f38c4 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package require sqlite3

set tellfile "./tell.db"

if {![file exists $tellfile]} {
	sqlite3 tellDB $tellfile
	tellDB eval {CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, target TEXT, sender TEXT, channel TEXT, message TEXT)}
	tellDB close
}

bind pub * "!tell" tell

proc tell {nick uhost hand chan text} {
	global tellfile
	sqlite3 tellDB $tellfile

	set target [lindex [split $text] 0]
	set lowtarget [string tolower $target]
	set msg [join [lrange [split $text] 1 end] " "]
	set message [format "\[%s\] <%s> %s" [clock format [clock seconds] -format "%Y-%m-%dT%H:%M:%SZ" -gmt true] $nick $msg]

	if {$target eq ""} {
		putserv "NOTICE $chan :\[tell\] syntax: !tell <nick/hostmask> <message>"
		return 0
	}

	if {$msg eq ""} {
		putserv "NOTICE $chan :\[tell\] syntax: !tell <nick/hostmask> <message>"
		return 0
	}

	if {$target eq $nick} {
		putserv "NOTICE $nick :\[tell\] look at the shape you're in, talking to the walls again..."
		return 0
	}

	if {[onchan $target $chan]} {
		putserv "NOTICE $chan :\[tell\] $target is here - they should see your message :)"
		putserv "PRIVMSG #fire-trail :\[tell\] !tell requested (but skipped, they're on the channel) by $nick in $chan for $target: $msg"
	} else {
		tellDB eval {INSERT INTO messages (target, sender, channel, message) VALUES ($lowtarget, $nick, $chan, $message)}
		putserv "NOTICE $chan :\[tell\] ok, I'll tell $target when they join next"
		putserv "PRIVMSG #fire-trail :\[tell\] !tell requested by $nick in $chan for $target: $msg"
	}
	tellDB close
}

bind join * * tellJoin

proc tellJoin {nick uhost handle chan} {
	global tellfile
	sqlite3 tellDB $tellfile

	set target [string tolower $nick]
	set result [tellDB eval {SELECT message FROM messages WHERE target = $target AND channel = $chan}]

	if {[llength $result] > 0} {
		foreach row $result {
			putserv "PRIVMSG $chan :\[tell\] $nick: $row"
		}
		tellDB eval {DELETE FROM messages WHERE target = $target AND channel = $chan}
	}

	set target [string cat [string tolower $nick] "!" [string tolower $uhost]]
	set result [tellDB eval {SELECT message FROM messages WHERE $target GLOB LOWER(target) AND channel = $chan}]

	if {[llength $result] > 0} {
		foreach row $result {
			putserv "PRIVMSG $chan :\[tell\] $nick: $row"
		}
		tellDB eval {DELETE FROM messages WHERE $target GLOB LOWER(target) AND channel = $chan}
	}

	tellDB close
}

bind nick * * tellNick

proc tellNick {nick uhost handle chan newnick} {
	global tellfile
	sqlite3 tellDB $tellfile

	set target [string tolower $newnick]
	set result [tellDB eval {SELECT message FROM messages WHERE target = $target AND channel = $chan}]

	if {[llength $result] > 0} {
		foreach row $result {
			putserv "PRIVMSG $chan :\[tell\] $newnick: $row"
		}
		tellDB eval {DELETE FROM messages WHERE target = $target AND channel = $chan}
	}

	tellDB close
}