summaryrefslogtreecommitdiff
path: root/lib/send.sh
blob: 9cbe2472c6fedc640743c598f7db5f09de46c865 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# -*- coding: utf-8 -*-
###########################################################################
#                                                                         #
#  envbot - an IRC bot in bash                                            #
#  Copyright (C) 2007-2008  Arvid Norlander                               #
#                                                                         #
#  This program is free software: you can redistribute it and/or modify   #
#  it under the terms of the GNU General Public License as published by   #
#  the Free Software Foundation, either version 3 of the License, or      #
#  (at your option) any later version.                                    #
#                                                                         #
#  This program is distributed in the hope that it will be useful,        #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of         #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
#  GNU General Public License for more details.                           #
#                                                                         #
#  You should have received a copy of the GNU General Public License      #
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
#                                                                         #
###########################################################################
#---------------------------------------------------------------------
## Functions for sending data to server
#---------------------------------------------------------------------

#---------------------------------------------------------------------
## Simple flood limiting.
## Note that this doesn't handle this very well:<br />
## seconds:milliseconds message<br />
## 01:999 message<br />
## 02:001 other message<br />
## Then they get too close.<br />
## I think this won't flood us off though.<br />
## @Type Private
#---------------------------------------------------------------------
send_last=0

#---------------------------------------------------------------------
## Send a "raw" line to the server.
## @Type API
## @param Line to send
#---------------------------------------------------------------------
send_raw() {
	# Do the flood limiting
	local now=
	time_get_current 'now'
	if [[ "$send_last" == "$now" ]]; then
		sleep 1
	fi
	time_get_current 'send_last'
	send_raw_flood "$*"
}

#---------------------------------------------------------------------
## Send a PRIVMSG
## @Type API
## @param Who (channel or nick)
## @param Message
#---------------------------------------------------------------------
send_msg() {
	# Don't do anything if no message
	[[ -z $2 ]] && return 0
	send_raw "PRIVMSG ${1} :${2}"
}

#---------------------------------------------------------------------
## Send a NOTICE
## @Type API
## @param Who (channel or nick)
## @param Message
#---------------------------------------------------------------------
send_notice() {
	# Don't do anything if no message
	[[ -z $2 ]] && return 0
	send_raw "NOTICE ${1} :${2}"
}

#---------------------------------------------------------------------
## Send a CTCP
## @Type API
## @param Who (channel or nick)
## @param Message
#---------------------------------------------------------------------
send_ctcp() {
	# Don't do anything if no message
	[[ -z $2 ]] && return 0
	send_msg "$1" $'\1'"${2}"$'\1'
}

#---------------------------------------------------------------------
## Send a NCTCP (ctcp reply)
## @Type API
## @param Who (channel or nick)
## @param Message
#---------------------------------------------------------------------
send_nctcp() {
	# Don't do anything if no message
	[[ -z $2 ]] && return 0
	send_notice "$1" $'\1'"${2}"$'\1'
}

#---------------------------------------------------------------------
## Send a NICK to change nick
## @Type API
## @param New nick
#---------------------------------------------------------------------
send_nick() {
	send_raw "NICK ${1}"
}

#---------------------------------------------------------------------
## Send a MODE to change umodes.
## @Type API
## @param Modes to send
#---------------------------------------------------------------------
send_umodes() {
	send_raw "MODE $server_nick_current $1"
}

#---------------------------------------------------------------------
## Send a MODE to change channel modes.
## @Type API
## @param Target channel
## @param Modes to set
#---------------------------------------------------------------------
send_modes() {
	send_raw "MODE $1 $2"
}

#---------------------------------------------------------------------
## Send a TOPIC to change channel topic.
## @Type API
## @param Channel to change topic of
## @param New topic.
#---------------------------------------------------------------------
send_topic() {
	send_raw "TOPIC $1 :$2"
}

#---------------------------------------------------------------------
## This is semi-internal only
## This may flood ourself off. Use send_raw instead in most cases.
## Also this doesn't log the actual line, so used for passwords.
## @Type API
## @param What to log instead (example could be: "NickServ IDENTIFY (password)")
## @param The line to send
#---------------------------------------------------------------------
send_raw_flood_nolog() {
	log_raw_out "<hidden line from logs>: $1"
	transport_write_line "$2"$'\r'
}

#---------------------------------------------------------------------
## This is semi-internal only
## This may flood ourself off. Use send_raw instead in most cases.
## Same syntax as send_raw
## @Type Semi-private
#---------------------------------------------------------------------
send_raw_flood() {
	log_raw_out "$*"
	transport_write_line "$*"$'\r'
}

###########################################################################
# Internal functions to core or this file below this line!                #
# Module authors: go away                                                 #
###########################################################################

#---------------------------------------------------------------------
## Module authors: use the wrapper: bot_quit in misc.sh instead!
## @Type Private
## @param If set, a quit reason
#---------------------------------------------------------------------
send_quit() {
	local reason=""
	[[ -n "$1" ]] && reason=" :$1"
	send_raw_flood "QUIT${reason}"
}

send_ipc_msg()
{
  if [[ -z "${IPC_NOTICE_CHANNEL}" ]]
  then echo '[SEND]: no target channel set in IPC_NOTICE_CHANNEL'
  elif ! touch "${IPC_QUEUE_FILE}" 2> /dev/null
  then echo "[SEND]: no such file set in IPC_QUEUE_FILE: '${IPC_QUEUE_FILE}'"
  else
    local delay_secs=$1 ; shift ;
    local message=$*
    local delivery_time=$(( $(date +%s) + ${delay_secs} ))

    echo "${delivery_time} ${IPC_NOTICE_CHANNEL} ${message}" >> "${IPC_QUEUE_FILE}"
    sort -o "${IPC_QUEUE_FILE}" "${IPC_QUEUE_FILE}"
  fi
}