Skip to content

Instantly share code, notes, and snippets.

@h-lame
Last active November 8, 2016 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-lame/1f032a1f8181fe220d6f1c2c4d98f64e to your computer and use it in GitHub Desktop.
Save h-lame/1f032a1f8181fe220d6f1c2c4d98f64e to your computer and use it in GitHub Desktop.
My First Ruby - code snippets from slides - see: http://h-lame.com/talks/my-first-ruby/
@ -24,8 +24,8 @@ class Config < Hash
}
end
- def initialize(name)
- self[Config::Name] = name
+ def initialize(name=nil)
+ self[Config::Name] = name if name != nil
end
end
public Config(String name) {
if (name != null) {
this.put(Config.NAME, name)
}
}
public Config() {
super(null);
}
def initialize(name=nil)
self[Config::Name] = name if name != nil
end
def handleMessage(message)
sender = @users.findByEmail(message.header.from[0])
if (sender == nil)
return
end
message.header.delete('From')
message.header['From'] = sender.preferredFromAddress
processSubject(message) # modify the subject
addListHeaders(message) # add the list headers
archive(message) # archive the mail
processAttachments(message) # modify attachments
sendToMembers(message) # send it out.
end
def processSubject(message)
sub = message.header.subject.dup
prefix = "[" + @config.listHeaders["List"] + "]"
i = sub.index(prefix)
sub.slice!(i..(i+prefix.length)) if (i != nil)
found = 0
re_str = "re: "
i = sub.downcase().index(re_str)
while (i != nil)
sub.slice!(i,re_str.length)
i = sub.downcase().index(re_str)
found = 1
end
prefix = "Re: "+prefix if (found == 1)
message.header.subject = prefix + " " + sub
end
def processSubject(message)
sub = message.header.subject.dup
prefix = "[" + @config.listHeaders["List"] + "]"
sub.gsub!(/#{Regexp.escape(prefix)}/,'')
if sub =~ /re\:\ /i
sub.gsub!(/re\:\ /i, '')
prefix = "Re: "+prefix
end
message.header.subject = prefix + " " + sub
end
# this is a loosely extended Hash with some little
# things to make it's use more convenient
class ListConfiguration < Hash
# this method is a bit overkill, but by caching
# the actual list header object instead of having
# to get it from the main config hash each time
# we should save some overhead. the ListHeaders
# part is the most frequently accessed, because
# it contains a lot of varied list info.
def listHeaders
if @listHeaders.id != self[ListHeaders].id
puts "recaching"
@listHeaders = self[ListHeaders]
end
@listHeaders
end
@config[ListHeaders]
def listHeaders
self[ListHeaders]
end
def listHeaders
if @listHeaders.id != self[ListHeaders].id
puts "recaching"
@listHeaders = self[ListHeaders]
end
@listHeaders
end
def listHeaders
if @listHeaders.id != self[ListHeaders].id
puts "recaching"
@listHeaders = self[ListHeaders]
end
@listHeaders
end
# these just resolve to unique strings,
# used as keys within the config hash
Name = "name"
ListHeaders = "listheaders"
# this is the root directory
BaseDir = "basedir"
log("looking at \"#{part.header.media_type}\"")
if !TypesToKeep.include?(part.header.media_type)
md = name_regex.match(part.header["Content-Type"])
filename = "null"
if md != nil
filename =
newUniqueFile(AttachmentDir, md[3]) { |f|
f.write(part.decode())
}
saveAttachmentMetaData(filename, message)
else
log("error getting filename from \"#{part.header["Content-Type"]}\"")
end
size = File.size(File.join(
base_dir, @config[AttachmentDir], filename
))
#!/home/room206/bin/narf
fucknut_home = '/home/room206/fucknut'
$: << fucknut_home # add our library path to the search path
require 'web'
AddHandler cgi-script .rb
RewriteEngine on
RewriteRule ^(.*)/archives/(.*)$ /handler.rb?↩ listname=$1&mode=archive&$2
listname = Web['listname']
if listname != nil and listname != ""
# main processing when we have a listname param
else
lists = Config.possible
lists = lists.collect { |i|
{'name' => i, 'value' => i}
}
if lists != []
Web.print_template("what_list.html", {'listname' => listname, 'listnamevalues' => lists})
else
Web.print_template("generalerror.html", {'error' => "No lists defined, get a better admin"})
end
end
Web.flush
<html>
<head>
<title>Login to {$listname}</title>
<link href="/arse.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Error during login to {$listname}</h1>
<p>{$error}</p>
<narf:form formname="tryagain" method="POST">
<narf:hidden name="listname"/>
<input type="submit" value="Try Again"/>
</narf:form>
</body>
</html>
class LoginHandler
LoginTemplate = "login.html"
LogoutTemplate = "login.html"
LoginCommand = "login"
LogoutCommand = "logout"
def showLogin()
@web.print_template( *loginTemplateArgs() )
end
def loginTemplateArgs()
[LoginTemplate, {"listname" => @web['listname'],
"cmd" => LoginCommand}]
end
end
def showLogin()
@web.print_template(LoginTemplate,
loginTemplateParams() )
end
def loginTemplateParams()
{"listname" => @web['listname'],
"cmd" => LoginCommand}
end
def showLogin()
@web.print_template(LoginTemplate,
{"listname" => @web['listname'],
"cmd" => LoginCommand}
end
errors = ""
commalist = @web['addresses'].gsub(/\n/, ",")
list = RMail::Address.parse(commalist)
if list.empty?
errors += "<br><h2>no valid email addresses given in list</h2>"
end
user.addresses = list
sendTo = RMail::Address.parse(@web['sendto'])[0]
sendToString = @web['sendto']
if nil == sendTo
errors += "<br><h2>Send To Address is not valid</h2>"
sendToString = "INVALID: " + sendToString
else
sendToString = sendTo.format.to_s
user.sendTo = sendTo
end
if @config[StartYear] < lastyear.year
lastyear = "<a href=\"#{baseurl}year=#{lastyear.year}&month=#{lastyear.month}\">&lt;&lt;</a>"
elsif @config[StartYear] == lastyear.year
if @config[StartMonth] <= lastyear.month
lastyear = "<a href=\"#{baseurl}year=#{lastyear.year}&month=#{lastyear.month}\">&lt;&lt;</a>"
else
lastyear = "&lt;&lt;"
end
else
lastyear = "&lt;&lt;"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment