Mercurial > hgrepos > hgweb.cgi > after5
comparison after5.rb @ 42:07ef43ac4f15 draft
Some ruby-1.9 workarounds and enhancement to logging and messaging.
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Tue, 08 May 2012 00:04:52 +0900 |
parents | c4e517ccfd99 |
children | 7874392b872d |
comparison
equal
deleted
inserted
replaced
41:c4e517ccfd99 | 42:07ef43ac4f15 |
---|---|
2 # -*- coding: euc-jp -*- | 2 # -*- coding: euc-jp -*- |
3 # | 3 # |
4 # Associative Scheduling Table - after5 | 4 # Associative Scheduling Table - after5 |
5 # (C)2003, 2004, 2006, 2008, 2012 by HIROSE Yuuji [yuuji<at>gentei.org] | 5 # (C)2003, 2004, 2006, 2008, 2012 by HIROSE Yuuji [yuuji<at>gentei.org] |
6 # $Id: after5.rb,v 1.19 2012/04/01 11:52:25 yuuji Exp yuuji $ | 6 # $Id: after5.rb,v 1.19 2012/04/01 11:52:25 yuuji Exp yuuji $ |
7 # Last modified Mon May 7 01:18:59 2012 on firestorm | 7 # Last modified Mon May 7 23:58:40 2012 on firestorm |
8 # See http://www.gentei.org/~yuuji/software/after5/ | 8 # See http://www.gentei.org/~yuuji/software/after5/ |
9 # このスクリプトはEUCで保存してください。 | 9 # このスクリプトはEUCで保存してください。 |
10 $hgid = <<_HGID_.split[1..-2].join(" ") | 10 $hgid = <<_HGID_.split[1..-2].join(" ") |
11 $HGid$ | 11 $HGid$ |
12 _HGID_ | 12 _HGID_ |
253 a = array[i%array.length] | 253 a = array[i%array.length] |
254 a[rand(a.length), 1] | 254 a[rand(a.length), 1] |
255 }.join('') | 255 }.join('') |
256 end | 256 end |
257 def users() | 257 def users() |
258 @pdb.keys | 258 @pdb.keys.collect{|u| u.toeuc} # toeuc is for 1.9 :( |
259 end | 259 end |
260 private :newpasswd | 260 private :newpasswd |
261 def setnewpasswd(user, length=8) | 261 def setnewpasswd(user, length=8) |
262 newp = newpasswd(length) | 262 newp = newpasswd(length) |
263 setpasswd(user, newp) | 263 setpasswd(user, newp) |
511 if (list = Dir.glob(path))[0] | 511 if (list = Dir.glob(path))[0] |
512 for p in list | 512 for p in list |
513 p.untaint | 513 p.untaint |
514 system "/bin/rm -rf \"#{p}\"" | 514 system "/bin/rm -rf \"#{p}\"" |
515 end | 515 end |
516 cleanup_files(list[0]) | 516 cleanup_files(list) |
517 end | 517 end |
518 end | 518 end |
519 def account_exists(instance) | 519 def account_exists(instance) |
520 if /@/ =~ instance | 520 if /@/ =~ instance |
521 true | 521 true |
612 # Do not mkdir `path' for | 612 # Do not mkdir `path' for |
613 # absolute paths | 613 # absolute paths |
614 # those paths which contains `../' | 614 # those paths which contains `../' |
615 # for the sake of security reason | 615 # for the sake of security reason |
616 return false if %r,\.\./|^/, =~ path | 616 return false if %r,\.\./|^/, =~ path |
617 path = path.untaint | |
617 p = 0 | 618 p = 0 |
618 i=0 | 619 i=0 |
619 while p=path.index("/", p) | 620 while p=path.index("/", p) |
620 dir = path[0..p].chop | 621 dir = path[0..p].chop |
621 p += 1 | 622 p += 1 |
1231 'management' => ['管理', 'management'], | 1232 'management' => ['管理', 'management'], |
1232 'administrator' => ['管理者', 'Administrator'], | 1233 'administrator' => ['管理者', 'Administrator'], |
1233 'newgroup' => ['新規グループ作成', 'Create new group'], | 1234 'newgroup' => ['新規グループ作成', 'Create new group'], |
1234 'adminop' => ['管理<br>操作', "Administrative<br>operation"], | 1235 'adminop' => ['管理<br>操作', "Administrative<br>operation"], |
1235 'sendall' => ['一斉送信', "write to members"], | 1236 'sendall' => ['一斉送信', "write to members"], |
1237 'sendall_err' => ["%s ファイルで mailprefix と maildomain を定義しとかないと送れまへん。 | |
1238 例: mailprefix=yuuji-after5 | |
1239 maildomain=gentei.org | |
1240 | |
1241 さらに、.qmail-$mailprefix-default も以下のように用意しておこね。 | |
1242 | ./#{@myname} -list", | |
1243 "You should define `mailprefix' and `maildomain' in %s file before | |
1244 sending message to all. | |
1245 (ex.) mailprefix=yuuji-after5 | |
1246 maildomain=gentei.org | |
1247 | |
1248 And then prepare .qmail-$mailprefix-default file as below. | |
1249 | ./#{@myname} -list"], | |
1236 'sendall_head' => ['「%s」宛のメイル送信', "Send message to `%s'"], | 1250 'sendall_head' => ['「%s」宛のメイル送信', "Send message to `%s'"], |
1237 'sendall_note' => ['メンバーへの連絡だけでなく、グループ非加入者がこれから加入する旨の通知などにも有用。', | 1251 'sendall_note' => ['メンバーへの連絡だけでなく、グループ非加入者がこれから加入する旨の通知などにも有用。', |
1238 "Send this message to all of group."], | 1252 "Send this message to all of group."], |
1239 'sendall_done' => ['送信完了', "sending message done"], | 1253 'sendall_done' => ['送信完了', "sending message done"], |
1240 'body' => ['本文', 'Body'], | 1254 'body' => ['本文', 'Body'], |
1499 def putLog(msg) | 1513 def putLog(msg) |
1500 msg += "\n" unless /\n/ =~ msg | 1514 msg += "\n" unless /\n/ =~ msg |
1501 open(@opt["logfile"], "a+") {|lp| | 1515 open(@opt["logfile"], "a+") {|lp| |
1502 lp.print Time.now.to_s + " " + msg | 1516 lp.print Time.now.to_s + " " + msg |
1503 } | 1517 } |
1518 msg | |
1504 end | 1519 end |
1505 | 1520 |
1506 def sendnotify(whom, subj, body) | 1521 def sendnotify(whom, subj, body) |
1507 users = users() | 1522 users = users() |
1508 if grepgroup(whom) | 1523 if grepgroup(whom) |
1553 #recipient.unshift "-f"+header['return-path'] if header['return-path'] | 1568 #recipient.unshift "-f"+header['return-path'] if header['return-path'] |
1554 exec(ENV['MAILCMD'] || @opt['sendmail'], *recipient) | 1569 exec(ENV['MAILCMD'] || @opt['sendmail'], *recipient) |
1555 end | 1570 end |
1556 exit 0; | 1571 exit 0; |
1557 end | 1572 end |
1558 putLog("Sent '#{subject}' to #{to}\n") | 1573 putLog("Sent '#{subject.toeuc}' to #{to}\n") |
1559 return true | 1574 return true |
1560 rescue | 1575 rescue |
1561 putLog("FAILED! - Sent '#{subject}' to #{to}\n") | 1576 putLog("FAILED! - Sent '#{subject}' to #{to}\n") |
1562 return nil | 1577 return nil |
1563 end | 1578 end |
2513 | 2528 |
2514 exit 0 | 2529 exit 0 |
2515 end | 2530 end |
2516 # ML functions | 2531 # ML functions |
2517 def tagify_subj(body, tag) | 2532 def tagify_subj(body, tag) |
2533 # This method should be generic for other headers than `Subject'? | |
2518 hold = [] | 2534 hold = [] |
2519 ret = [] | 2535 ret = [] |
2520 skip = false | 2536 skip = false |
2521 regexp = /^Subject: / | 2537 regexp = /^Subject: / |
2522 while line = body.shift | 2538 while line = body.shift |
2531 when /^\s/ # continued line | 2547 when /^\s/ # continued line |
2532 hold << line | 2548 hold << line |
2533 else | 2549 else |
2534 if skip then | 2550 if skip then |
2535 sj = hold.join.toeuc.sub("Subject: ", "").gsub(tag, "").strip | 2551 sj = hold.join.toeuc.sub("Subject: ", "").gsub(tag, "").strip |
2536 sj = sj.sub(/^(re: *)+/i, "Re: ").gsub("\n", "").squeeze | 2552 sj = sj.sub(/^(re: *)+/i, "Re: ").gsub("\n", "") |
2537 ret << "Subject: "+NKF.nkf('-jM', tag+" "+sj)+"\n" | 2553 ret << "Subject: "+NKF.nkf('-jM', tag+" "+sj).strip+"\n" |
2538 else | 2554 else |
2539 ret += hold | 2555 ret += hold |
2540 end | 2556 end |
2541 skip = false | 2557 skip = false |
2542 hold = [line] | 2558 hold = [line] |
2549 # LOCAL=1 DEFAULT=name ./after5.rb -list | 2565 # LOCAL=1 DEFAULT=name ./after5.rb -list |
2550 # $DEFAULT is ML name | 2566 # $DEFAULT is ML name |
2551 viamail = ENV['LOCAL'] && ENV['DEFAULT'] # called via mail | 2567 viamail = ENV['LOCAL'] && ENV['DEFAULT'] # called via mail |
2552 from = nil | 2568 from = nil |
2553 unless @opt['mailprefix'] && @opt['maildomain'] | 2569 unless @opt['mailprefix'] && @opt['maildomain'] |
2554 STDERR.printf("Set mailprefix and maildomain in %s\n", @opt['conf']) | 2570 if viamail |
2555 STDERR.print "(ex.) mailprefix=yuuji-after5\n" | 2571 STDERR.print msg('sendall_err') % [@opt['conf']] |
2556 STDERR.print " maildomain=gentei.org\n" | 2572 exit 0 |
2557 exit 0 | 2573 else |
2574 @O.print @H.elementln("pre"){msg('sendall_err') % [@opt['conf']]} | |
2575 return true | |
2576 end | |
2558 end | 2577 end |
2559 if viamail then | 2578 if viamail then |
2560 prohibitviahttp() | 2579 prohibitviahttp() |
2561 name = ENV['DEFAULT'] | 2580 name = ENV['DEFAULT'] |
2562 else # via http | 2581 else # via http |
2603 exit 0 | 2622 exit 0 |
2604 end | 2623 end |
2605 def listdraft(name) | 2624 def listdraft(name) |
2606 return nil unless checkauth | 2625 return nil unless checkauth |
2607 return nil unless name | 2626 return nil unless name |
2627 unless @opt['mailprefix'] && @opt['maildomain'] | |
2628 @O.print @H.elementln("pre"){msg('sendall_err') % [@opt['conf']]} | |
2629 return true | |
2630 end | |
2631 | |
2608 user=@params['user'] | 2632 user=@params['user'] |
2609 nickname = @sc.nickname(user) | 2633 nickname = @sc.nickname(user) |
2610 @O.print @H.elementln("h1") { | 2634 @O.print @H.elementln("h1") { |
2611 @mybase+' '+msg('sendall').sub("<br>", " ") | 2635 @mybase+' '+msg('sendall').sub("<br>", " ") |
2612 } | 2636 } |
3099 return nil | 3123 return nil |
3100 end | 3124 end |
3101 @O.print @H.elementln("h1"){ | 3125 @O.print @H.elementln("h1"){ |
3102 msg('group')+" #{group} "+msg('deletion') | 3126 msg('group')+" #{group} "+msg('deletion') |
3103 } | 3127 } |
3104 @O.print @H.p(@sc.destroygroup(group) ? msg("done") : msg("failure")) | 3128 resmsg = @sc.destroygroup(group) ? msg("done") : msg("failure") |
3129 @O.print @H.p(resmsg) | |
3130 putLog("Delete group '#{group}' #{resmsg}\n") | |
3105 | 3131 |
3106 @O.print footer() | 3132 @O.print footer() |
3107 end | 3133 end |
3108 | 3134 |
3109 def deleteuser(user) | 3135 def deleteuser(user) |
3137 return nil | 3163 return nil |
3138 end | 3164 end |
3139 @O.print @H.elementln("h1"){ | 3165 @O.print @H.elementln("h1"){ |
3140 msg('user')+" #{user} "+msg('deletion') | 3166 msg('user')+" #{user} "+msg('deletion') |
3141 } | 3167 } |
3142 @O.print @H.p(deleteuser(user) ? msg("done") : msg("failure")) | 3168 resmsg = deleteuser(user) ? msg("done") : msg("failure") |
3169 @O.print @H.p(resmsg) | |
3170 putLog("Delete user '#{user}' #{resmsg}\n") | |
3143 | 3171 |
3144 @O.print @H.p(@H.a(@myname, msg('login'))) | 3172 @O.print @H.p(@H.a(@myname, msg('login'))) |
3145 end | 3173 end |
3146 | 3174 |
3147 def admgroupsub() | 3175 def admgroupsub() |
3186 @O.print @H.p(sprintf(msg('soleadmin'), u, group)) | 3214 @O.print @H.p(sprintf(msg('soleadmin'), u, group)) |
3187 elsif joinp ^ membp | 3215 elsif joinp ^ membp |
3188 somethingdone = true | 3216 somethingdone = true |
3189 @sc.addgroup(group, [u], !joinp, kind[0]) | 3217 @sc.addgroup(group, [u], !joinp, kind[0]) |
3190 @O.print @H.elementln("p"){ | 3218 @O.print @H.elementln("p"){ |
3191 sprintf "%s [%s](%s) %s %s", msg('user'), u, | 3219 putLog(sprintf "%s [%s](%s) %s %s", msg('user'), u, |
3192 msg(kind[1]), | 3220 msg(kind[1]), |
3193 joinp ? msg('addedtogroup'): msg('removedfromgp'), group | 3221 joinp ? msg('addedtogroup'): msg('removedfromgp'), group) |
3194 } | 3222 } |
3195 end | 3223 end |
3196 end | 3224 end |
3197 end | 3225 end |
3198 end # users() | 3226 end # users() |
3206 membp = (@sc.ismember(g, group) && true) | 3234 membp = (@sc.ismember(g, group) && true) |
3207 if joinp ^ membp | 3235 if joinp ^ membp |
3208 somethingdone = true | 3236 somethingdone = true |
3209 @sc.addgroup(group, [g], !joinp) | 3237 @sc.addgroup(group, [g], !joinp) |
3210 @O.print @H.elementln("p"){ | 3238 @O.print @H.elementln("p"){ |
3211 sprintf "%s [%s] %s %s", msg('group'), g, | 3239 putLog(sprintf("%s [%s] %s %s", |
3212 joinp ? msg('addedtogroup'): msg('removedfromgp'), group | 3240 msg('group'), g, |
3213 } | 3241 joinp ? |
3242 msg('addedtogroup') | |
3243 : msg('removedfromgp'), group)) | |
3244 } | |
3214 end | 3245 end |
3215 end | 3246 end |
3216 end # groups | 3247 end # groups |
3217 unless somethingdone | 3248 unless somethingdone |
3218 # @O.print @H.p(msg('nothingtodo')) | 3249 # @O.print @H.p(msg('nothingtodo')) |
3219 end | 3250 end |
3220 # @O.print footer() | 3251 # @O.print footer() |
3221 link2home | 3252 link2home |
3222 end | 3253 end |
3223 def newgroupsub() | 3254 def newgroupsub() |
3224 if !checkauth | 3255 return nil unless checkauth |
3225 nil | |
3226 end | |
3227 user = @params['user'] | 3256 user = @params['user'] |
3228 newgroup = @params['group'] | 3257 newgroup = @params['group'] |
3229 newgname = @params['gname'] | 3258 newgname = @params['gname'] |
3230 | 3259 |
3231 | 3260 |
3236 if dupl=@sc.name2group(newgname) | 3265 if dupl=@sc.name2group(newgname) |
3237 @O.print @H.p(sprintf(msg('dupname'), newgname)) | 3266 @O.print @H.p(sprintf(msg('dupname'), newgname)) |
3238 @O.print groupnamesString() | 3267 @O.print groupnamesString() |
3239 return nil | 3268 return nil |
3240 end | 3269 end |
3241 @sc.creategroup(newgroup, newgname, [user]) | 3270 @sc.creategroup(newgroup, newgname, [user]) && |
3271 putLog("New group '#{newgroup}'(#{newgname}) created\n") | |
3242 admgroup(newgroup) | 3272 admgroup(newgroup) |
3243 end | 3273 end |
3244 | 3274 |
3245 # | 3275 # |
3246 # Methods Related to viaMail functions | 3276 # Methods Related to viaMail functions |
3551 @job = 'notify' # + exit | 3581 @job = 'notify' # + exit |
3552 when "-list" | 3582 when "-list" |
3553 @job = 'list' # + exit | 3583 @job = 'list' # + exit |
3554 when "-newgroup" | 3584 when "-newgroup" |
3555 @job = 'newgroup' | 3585 @job = 'newgroup' |
3556 when /^-(admgroup|listdraft)/ | 3586 when /^-(admgroup|listdraft)$/ |
3557 ARGV.shift | 3587 ARGV.shift |
3558 gr = safecopy(grepgroup(ARGV[0])) | 3588 gr = safecopy(grepgroup(ARGV[0])) |
3559 ##gr.untaint | 3589 ##gr.untaint |
3560 @job = safecopy($1)+' "'+gr+'"' | 3590 @job = safecopy($1)+' "'+gr+'"' |
3561 when "-admgroupsub" | 3591 when "-admgroupsub" |
3566 ARGV.shift | 3596 ARGV.shift |
3567 usr = safecopy(users().grep(ARGV[0])[0]) | 3597 usr = safecopy(users().grep(ARGV[0])[0]) |
3568 @job = 'delusersub "'+usr+'"' | 3598 @job = 'delusersub "'+usr+'"' |
3569 when "-delgroupsub" | 3599 when "-delgroupsub" |
3570 ARGV.shift | 3600 ARGV.shift |
3571 gr = grepgroup(ARGV[0]) | 3601 gr = safecopy(grepgroup(ARGV[0])) |
3572 @job = 'delgroupsub "'+gr+'"' | 3602 @job = 'delgroupsub "'+gr+'"' |
3573 when /-(setpasswd|deluser|adduser)$/ | 3603 when /-(setpasswd|deluser|adduser)$/ |
3574 ARGV.shift | 3604 ARGV.shift |
3575 @job = $1+ " '#{ARGV[0]}'" # + exit | 3605 @job = $1+ " '#{ARGV[0]}'" # + exit |
3576 when "" | 3606 when "" |