Removed the assert()'s from ssl.nim, and limited lines to 80 chars.

This commit is contained in:
dom96
2010-10-23 23:02:59 +01:00
parent 18a8590a87
commit 4922b52dea
3 changed files with 50 additions and 27 deletions

View File

@@ -7,7 +7,8 @@
# distribution, for details about the copyright.
#
## This module provides an easy to use sockets-style nimrod interface to the OpenSSL library.
## This module provides an easy to use sockets-style
## nimrod interface to the OpenSSL library.
import openssl, strutils, os
@@ -16,26 +17,34 @@ type
ssl: PSSL
bio: PBIO
proc connect*(sock: var TSecureSocket, address: string, port: int, certResult: var Int) =
## Connects to the specified `address` on the specified `port`. `certResult` will become the result of the certificate validation.
proc connect*(sock: var TSecureSocket, address: string,
port: int, certResult: var Int) =
## Connects to the specified `address` on the specified `port`.
## `certResult` will become the result of the certificate validation.
SslLoadErrorStrings()
ERR_load_BIO_strings()
assert(SSL_library_init() == 1)
if SSL_library_init() != 1:
OSError()
var ctx = SSL_CTX_new(SSLv23_client_method())
if ctx == nil:
ERR_print_errors_fp(stderr)
assert(False)
OSError()
#if SSL_CTX_load_verify_locations(ctx, "/tmp/openssl-0.9.8e/certs/vsign1.pem", NIL) == 0:
#if SSL_CTX_load_verify_locations(ctx,
# "/tmp/openssl-0.9.8e/certs/vsign1.pem", NIL) == 0:
# echo("Failed load verify locations")
# ERR_print_errors_fp(stderr)
sock.bio = BIO_new_ssl_connect(ctx)
assert(BIO_get_ssl(sock.bio, addr(sock.ssl)) != 0)
if BIO_get_ssl(sock.bio, addr(sock.ssl)) == 0:
OSError()
assert(BIO_set_conn_hostname(sock.bio, address & ":" & $port) == 1)
if BIO_set_conn_hostname(sock.bio, address & ":" & $port) != 1:
OSError()
if BIO_do_connect(sock.bio) <= 0:
ERR_print_errors_fp(stderr)

View File

@@ -16,7 +16,9 @@
##
##
## .. code-block:: Nimrod
## var msg = createMessage("Hello from Nimrod's SMTP", "Hello!.\n Is this awesome or what?", @["foo@gmail.com"])
## var msg = createMessage("Hello from Nimrod's SMTP",
## "Hello!.\n Is this awesome or what?",
## @["foo@gmail.com"])
## var smtp = connect("smtp.gmail.com", 465, True, True)
## smtp.auth("username", "password")
## smtp.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)
@@ -72,7 +74,8 @@ proc checkReply(smtp: TSMTP, reply: string) =
if not line.startswith(reply):
quitExcpt(smtp, "Expected " & reply & " reply, got: " & line)
proc connect*(address: String, port: int = 25, ssl: bool = False, debug: bool = False): TSMTP =
proc connect*(address: String, port: int = 25,
ssl: bool = False, debug: bool = False): TSMTP =
## Establishes a connection with a SMTP server.
## May fail with EInvalidReply or with a socket errors.
@@ -103,7 +106,8 @@ proc auth*(smtp: TSMTP, username, password: string) =
smtp.debugSend(encode(password) & "\c\L")
smtp.checkReply("235") # Check whether the authentification was successful.
proc sendmail*(smtp: TSMTP, fromaddr: string, toaddrs: seq[string], msg: string) =
proc sendmail*(smtp: TSMTP, fromaddr: string,
toaddrs: seq[string], msg: string) =
## Sends `msg` from `fromaddr` to `toaddr`.
## Messages may be formed using ``createMessage`` by converting the TMessage into a string.
@@ -124,7 +128,7 @@ proc sendmail*(smtp: TSMTP, fromaddr: string, toaddrs: seq[string], msg: string)
smtp.debugSend("QUIT\c\L")
proc createMessage*(mSubject, mBody: String, mTo, mCc: seq[String],
otherHeaders: openarray[tuple[name, value: String]]): TMessage =
otherHeaders: openarray[tuple[name, value: String]]): TMessage =
## Creates a new MIME compliant message.
result.msgTo = mTo
result.msgCc = mCc
@@ -134,7 +138,8 @@ proc createMessage*(mSubject, mBody: String, mTo, mCc: seq[String],
for n, v in items(otherHeaders):
result.msgOtherHeaders[n] = v
proc createMessage*(mSubject, mBody: String, mTo, mCc: seq[String] = @[]): TMessage =
proc createMessage*(mSubject, mBody: String, mTo,
mCc: seq[String] = @[]): TMessage =
## Alternate version of the above.
result.msgTo = mTo
result.msgCc = mCc
@@ -157,7 +162,8 @@ proc `$`*(msg: TMessage): String =
when isMainModule:
#var msg = createMessage("Test subject!", "Hello, my name is dom96.\n What\'s yours?", @["dominik@localhost"])
#var msg = createMessage("Test subject!",
# "Hello, my name is dom96.\n What\'s yours?", @["dominik@localhost"])
#echo(msg)
#var smtp = connect("localhost", 25, False, True)
@@ -165,12 +171,15 @@ when isMainModule:
#echo(decode("a17sm3701420wbe.12"))
var msg = createMessage("Hello from Nimrod's SMTP!", "Hello!!!!.\n Is this awesome or what?", @["someone@yahoo.com", "someone@gmail.com"])
var msg = createMessage("Hello from Nimrod's SMTP!",
"Hello!!!!.\n Is this awesome or what?",
@["someone@yahoo.com", "someone@gmail.com"])
echo(msg)
var smtp = connect("smtp.gmail.com", 465, True, True)
smtp.auth("someone", "password")
smtp.sendmail("someone@gmail.com", @["someone@yahoo.com", "someone@gmail.com"], $msg)
smtp.sendmail("someone@gmail.com",
@["someone@yahoo.com", "someone@gmail.com"], $msg)

View File

@@ -198,13 +198,17 @@ proc ERR_load_BIO_strings*(){.cdecl, dynlib: DLLSSLName, importc.}
proc SSLv23_client_method*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_CTX_new*(meth: PSSL_METHOD): PSSL_CTX{.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_CTX_load_verify_locations*(ctx: PSSL_CTX, CAfile: cstring, CApath: cstring): cInt{.
cdecl, dynlib: DLLSSLName, importc.}
proc SSL_get_verify_result*(ssl: PSSL): int{.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_CTX_new*(meth: PSSL_METHOD): PSSL_CTX{.cdecl,
dynlib: DLLSSLName, importc.}
proc SSL_CTX_load_verify_locations*(ctx: PSSL_CTX, CAfile: cstring,
CApath: cstring): cInt{.cdecl, dynlib: DLLSSLName, importc.}
proc SSL_get_verify_result*(ssl: PSSL): int{.cdecl,
dynlib: DLLSSLName, importc.}
proc BIO_new_ssl_connect*(ctx: PSSL_CTX): PBIO{.cdecl, dynlib: DLLSSLName, importc.}
proc BIO_ctrl*(bio: PBIO, cmd: cint, larg: int, arg: cstring): int{.cdecl, dynlib: DLLSSLName, importc.}
proc BIO_new_ssl_connect*(ctx: PSSL_CTX): PBIO{.cdecl,
dynlib: DLLSSLName, importc.}
proc BIO_ctrl*(bio: PBIO, cmd: cint, larg: int, arg: cstring): int{.cdecl,
dynlib: DLLSSLName, importc.}
proc BIO_get_ssl*(bio: PBIO, ssl: ptr PSSL): int =
return BIO_ctrl(bio, BIO_C_GET_SSL, 0, cast[cstring](ssl))
proc BIO_set_conn_hostname*(bio: PBIO, name: cstring): int =
@@ -228,7 +232,8 @@ else:
importc.}
proc SslCtxSetCipherList*(arg0: PSSL_CTX, str: cstring): cInt{.cdecl,
dynlib: DLLSSLName, importc.}
proc SslCtxNew*(meth: PSSL_METHOD): PSSL_CTX{.cdecl, dynlib: DLLSSLName, importc.}
proc SslCtxNew*(meth: PSSL_METHOD): PSSL_CTX{.cdecl,
dynlib: DLLSSLName, importc.}
proc SslCtxFree*(arg0: PSSL_CTX){.cdecl, dynlib: DLLSSLName, importc.}
proc SslSetFd*(s: PSSL, fd: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
proc SslCtrl*(ssl: PSSL, cmd: cInt, larg: int, parg: Pointer): int{.cdecl,
@@ -246,10 +251,10 @@ else:
proc SslMethodV23*(): PSSL_METHOD{.cdecl, dynlib: DLLSSLName, importc.}
proc SslCtxUsePrivateKey*(ctx: PSSL_CTX, pkey: SslPtr): cInt{.cdecl,
dynlib: DLLSSLName, importc.}
proc SslCtxUsePrivateKeyASN1*(pk: cInt, ctx: PSSL_CTX, d: cstring, length: int): cInt{.
cdecl, dynlib: DLLSSLName, importc.}
proc SslCtxUsePrivateKeyFile*(ctx: PSSL_CTX, filename: cstring, typ: cInt): cInt{.
cdecl, dynlib: DLLSSLName, importc.}
proc SslCtxUsePrivateKeyASN1*(pk: cInt, ctx: PSSL_CTX,
d: cstring, length: int): cInt{.cdecl, dynlib: DLLSSLName, importc.}
proc SslCtxUsePrivateKeyFile*(ctx: PSSL_CTX,
filename: cstring, typ: cInt): cInt{.cdecl, dynlib: DLLSSLName, importc.}
proc SslCtxUseCertificate*(ctx: PSSL_CTX, x: SslPtr): cInt{.cdecl,
dynlib: DLLSSLName, importc.}
proc SslCtxUseCertificateASN1*(ctx: PSSL_CTX, length: int, d: cstring): cInt{.