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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
# This is funky :)
# ... implements a mail sender form and socket
# This file is a good example of kvirc scripting
# and good *.kvs file format:
# (everything written in the form of aliases...
# So you can execute this file once, the aliases will be defined
# and saved automatically by KVIrc after that.
# This (internal) alias will define the mailsocket class
# and it has to be called if the mailsocket class is not yet defined
alias mail.definesocketclass {
# Define the socket for sending the mail
# It will connect to an SMTP service of the specified host
# and interact with the daemon
class (mailsocket,socket)
{
# Utilities
function(error)
{
echo -i=$icon(error) "[MAIL]: $1"
destroy $this
}
function(msg)
{
status -t=5000 "[MAIL]: $1-"
}
# This is the "triggering" function; it will start the "send" operation
function(run)
{
# First of all ensure that we have all the strictly necessary parameters
if("$this->%smtphost" == "")
{
# No SMTP host specified... no way
$this->$error("No SMTP host specified")
halt
}
# The port can be the default one
if("$this->%smtpport" == "")$this->%smtpport = 25
if("$this->%mailto" == "")
{
# No recipient specified... no way
$this->$error("No recipient specified")
halt
}
# Anonymous mail ?
if("$this->%mailfrom" == "")
{
$this->$error("No sender specified")
halt
}
# OK, time to resolve the mail host (it will be likely specified in the human readable form)
# Create a DNS class instance (children of this object)
$this->%dns = $new(dns,$this,mydnsobject)
# Connect the "done" signal to the relevant slot
connect $this->%dns done $this doConnect
# And trigger the lookup
if(!($this->%dns->$resolve($this->%smtphost)))
$this->$error("Cannot resolve the SMTP server hostname")
else
$this->$msg("Looking up SMTP host $this->%smtphost")
# Now wait for the DNS object to "call us"
}
# Slot for the "done" slot of the DNS object
function(doConnect)
{
# Check if the lookup was successful
if(!($this->%dns->$success()))
{
# Failed to resolve the host... no way
$this->$error("Failed to resolve the SMTP host: $this->%dns->$lastError()")
halt
}
# Change the SMTP host to its IP address
$this->%smtphost = $this->%dns->$ip()
$this->$msg("SMTP host resolved to $this->%smtphost")
# Not necessary. It whould be destroyed anyway, but...
destroy $this->%dns
# And do connect
if(!($this->$connect($this->%smtphost,$this->%smtpport)))
$this->$error("Cannot connect to $this->%smtphost: $this->$lastError()");
else
$this->$msg("Connecting to $this->%smtphost on port $this->%smtpport");
# Now wait for the OnConnect signal to be received
}
event(OnConnect)
{
# Connected. We are at the step 0 of the operation
$this->%step = 0
$this->$msg("Connected to $1 on port $2");
# Wait for the SMTP welcome message to be received
}
event(OnDataReceived)
{
# Data was received
# Act in a different way depending of our state (%step)
switch($this->%step)
{
case(0)
{
# Just connected. We should have received the welcome message
if("$2" != "220")$this->$error("Bad welcome message from server: $2-");
else {
# Send the HELO command and set the state to the next one
$this->%step++
$this->$msg("Welcome message received: sending HELO command")
$this->$write("HELO $this->$localhost()$cr$lf")
# Now wait for the OK response
}
}
case(1)
{
# HELO command sent. We should have received the "250 OK" response
if("$2" != "250")$this->$error("Bad response to HELO: $2-");
else {
# Send the MAIL command and increase the state again
$this->%step++
$this->$msg("Sending MAIL command")
$this->$write("MAIL From: $this->%mailfrom$cr$lf")
# And wait for OK
}
}
case(2)
{
# MAIL command sent. We should have received the "250 OK" response
if("$2" != "250")$this->$error("Bad response to MAIL: $2-");
else {
# Send the RCPT command
$this->%step++
$this->$msg("Sending RCPT command")
$this->$write("RCPT To: $this->%mailto$cr$lf")
# Wait for OK again
}
}
case(3)
{
# RCPT command sent. We should have received the "250 OK" response
if("$2" != "250")$this->$error("Bad response to RCPT: $2-");
else {
# Send the DATA command
$this->%step++
$this->$msg("Sending DATA command")
$this->$write("DATA$cr$lf")
# Wait for "354..."
}
}
case(4)
{
# DATA command sent. We should have received the "354 Data ok or something" response
if("$2" != "354")$this->$error("Bad response to DATA: $2-");
else {
$this->%step++
$this->$msg("Sending message body")
# Time to write the message body
$this->$write("To: $this->%mailto$cr$lf")
$this->$write("X-Mailer: $version$cr$lf")
# TODO: The syntax of this field should be <dd> <month> <yy> <hh>:<mm>:<ss>
$this->$write("Date: $date $time$cr$lf")
# Terminate only with a $cr. We will add the $lf at the beginning
# of the message body
$this->$write("Subject: $this->%subject$cr")
# The mail body needs a trick.
# We have to "escape" the leading points of each line.
# To avoid the server treating it as the end of the message body
$this->%mailtext = $strReplace("$lf$this->%mailtext","$lf\.$lf","$lf\..$lf")
$this->$write("$this->%mailtext$cr$lf")
# The terminating point
$this->$write(".$cr$lf")
}
}
case(5)
{
# BODY of the message sent. We should have received the "250 OK" response
if("$2" != "250")$this->$error("Bad response to end of message: $2-");
else {
# Job done! Time to die
$this->$msg("Mail to $this->%mailto successfully sent")
destroy $this
}
}
}
}
event(OnConnectFailed)
{
$this->$error("Connect failed: $this->$lastError")
destroy $this
}
event(OnDisconnect)
{
$this->$error("Disconnected: $this->$lastError")
destroy $this
}
}
}
# This (internal) alias will define the mailform class
alias mail.defineformclass {
# Define the mailform class
# It will be a widget subclass
class (mailform,widget)
{
# Define the constructor: everything is built here
function(constructor)
{
# First of all load the config values
$this->$loadConfig()
# Create the main layout
%layout = $new(layout,$this,my_layout)
# And start creating the child widgets
# First the SMTP host label
%label = $new(label,$this,l1,SMTP Host:)
%layout->$add(%label,0,0)
# And the related lineedit
$this->%smtphost = $new(lineedit,$this,editor,$this->%cfgSmtpHost)
%layout->$addMultiCell($this->%smtphost,0,0,1,3)
# Same as above with the SMTP port label and lineedit
%label = $new(label,$this,l2,SMTP Port:)
%layout->$add(%label,1,0)
$this->%smtpport = $new(lineedit,$this,editor,$this->%cfgSmtpPort)
%layout->$addMultiCell($this->%smtpport,1,1,1,3)
# A separator
%frame = $new(label,$this,f1)
%frame->$setFrameShape(hline)
%frame->$setFrameShadow(sunken)
%layout->$addMultiCell(%frame,2,2,0,3)
# To field (set the first parameter passed as default recipient)
%label = $new(label,$this,l1,"<b>To:</b>")
%layout->$add(%label,3,0)
$this->%mailto = $new(lineedit,$this,editor,$1)
%layout->$addMultiCell($this->%mailto,3,3,1,3)
# From field
%label = $new(label,$this,l1,"<b>From:</b>")
%layout->$add(%label,4,0)
$this->%mailfrom = $new(lineedit,$this,editor,$this->%cfgMailFrom)
%layout->$addMultiCell($this->%mailfrom,4,4,1,3)
# The subject
%label = $new(label,$this,l1,"<b>Subject:</b>")
%layout->$add(%label,5,0)
$this->%subject = $new(lineedit,$this,editor)
%layout->$addMultiCell($this->%subject,5,5,1,3)
# The mail body text
$this->%mailtext = $new(multilineedit,$this,mlineedit)
%layout->$addMultiCell($this->%mailtext,6,6,0,3)
# The cancel button
%btn = $new(button,$this,cancel_button,Cancel)
# Connect its clicked signal to our slot
connect %btn clicked $this cancelClicked
%layout->$add(%btn,7,2)
# Similar job for the OK button
%btn = $new(button,$this,send_button,Send)
connect %btn clicked $this sendClicked
%layout->$add(%btn,7,3)
# Adjust the layout so it will look (and behave) nicer
%layout->$setColStretch(1,1)
%layout->$setRowStretch(6,1)
%layout->$setMargin(6)
%layout->$setSpacing(2)
# Set the caption
$this->$setCaption(KVIrc mail)
# And the initial geometry (that was loaded from the config)
$this->$setGeometry($this->%cfgX,$this->%cfgY,$this->%cfgWidth,$this->%cfgHeight)
# And finally show up (self and all the children)
$this->$show(1)
# Be smart: give focus to the To: field
$this->%mailto->$setFocus()
}
# Slot for the cancel button "clicked" signal
function(cancelClicked)
{
# Cancel clicked; just die
destroy $this
}
# Slot for the OK button "clicked" signal
function(sendClicked)
{
# Ensure that the socket class is defined
# Please note that we check if the class is defined
# and then eventually call the alias with the "class" command
# inside. You could be tempted to use the -o switch
# of the "class" command , but it is dangerous here...
# We're inside an object's function.
if(!$classDefined(mailsocket))mail.definesocketclass
# OK, the class is defined now.
# Create the mail socket
# Please note that it is a child of the $root
# so it will be not destroyed when the form will be.
%sock = $new(mailsocket,$root,mail_socket)
# Fill the necessary variables getting it from the lineedit fields
%sock->%smtphost = $this->%smtphost->$text()
%sock->%smtpport = $this->%smtpport->$text()
%sock->%mailto = $this->%mailto->$text()
%sock->%subject = $this->%subject->$text()
%sock->%mailfrom = $this->%mailfrom->$text()
%sock->%mailtext = $this->%mailtext->$text()
# And make it run
%sock->$run()
# Do not forget to save the configuration
$this->$saveConfig()
# And die
destroy $this
# The mailsocket will continue his job alone...
}
# Function to load the configuration
function(loadConfig)
{
$this->%cfgX = 100
$this->%cfgY = 100
$this->%cfgWidth = 300
$this->%cfgHeight = 300
$this->%cfgSmtpHost = localhost
$this->%cfgSmtpPort = 25
$this->%cfgMailFrom = Your email here
# %cfg = $new(config,$this,myconfig,kvirc.mail.conf)
# $this->%cfgX = %cfg->$readEntry(X,100)
# $this->%cfgY = %cfg->$readEntry(Y,100)
# destroy %cfg
}
# Function to save the configuration
function(saveConfig)
{
# %cfg = $new(config,$this,myconfig,kvirc.mail.conf)
# %cfg->$writeEntry(Y,%cfgY)
# destroy %cfg
}
}
}
# The "mail" alias: the user "callable" one.
# It will show a form for inputting the mail.
# The syntax is:
# /mail [recipient]
# The recipient is optional (if specified, it will be set in the "To:" field of the form)
alias mail {
# Define the mailform class if needed (calling the previously defined alias)
if(!$classDefined(mailform))mail.defineformclass
# And just create the form...
%tmp = $new(mailform,$root,azz,$1)
}
echo "The /mail command has been defined. Have fun :)"
|