{"id":95,"date":"2008-11-11T23:09:26","date_gmt":"2008-11-11T15:09:26","guid":{"rendered":"http:\/\/blog.urdada.net\/2008\/11\/11\/95\/"},"modified":"2008-12-22T01:29:04","modified_gmt":"2008-12-21T17:29:04","slug":"mantis-mail-gateway","status":"publish","type":"post","link":"https:\/\/dada.tw\/blog\/2008\/11\/11\/95\/","title":{"rendered":"Mantis Mail Gateway (perl script)"},"content":{"rendered":"<p>This is a simple Mantis Mail Gateway for mail-in tickets.<br \/>\nIt can be used for alert management for monitoring system.<\/p>\n<p>For installation, add the line below to \/etc\/aliases:<\/p>\n<p><strong>bug:   &#8220;|\/usr\/local\/bin\/mantis-mail-gateway.pl  &nbsp; PROJECT &nbsp;  REPORTER&#8221;<\/strong><\/p>\n<p>and then run newaliases.<\/p>\n<p>The incoming mail will be posted to a Mantis project named &#8216;PROJECT&#8217; on behave of user &#8216;REPORTER&#8217;. The mail subject will become the bug summary and the mail content will become the bug description.<\/p>\n<p>Note that this script does not understand MIME multipart encoding and may have problems on mail contents besides plain-ASCII encoding.<\/p>\n<p>You are welcome to add more features.<\/p>\n<p><img data-recalc-dims=\"1\" src=\"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3286\/3022458848_b690e23167.jpg?w=625\" \/><\/p>\n<p>mantis-mail-gateway.pl: (can be downloaded <a href=http:\/\/urdada.net\/codings\/mantis-mail-gateway.pl>here<\/a>)<\/p>\n<pre lang=\"perl\" line=\"1\">\r\n#!\/usr\/bin\/perl\r\n\r\n# mantis-mail-gateway.pl\r\n\r\n# Mantis Mail Gateway\r\n# Shen Cheng-Da (cdsheen AT gmail.com)\r\n# require DBI to connect MySQL database\r\n# http:\/\/blog.urdada.net\/2008\/11\/11\/95\/\r\n\r\nuse DBI;\r\nuse POSIX qw(strftime);\r\n\r\nmy $db_host    = 'localhost';\r\nmy $db_name    = 'mantis';\r\nmy $db_user    = 'monitor';\r\nmy $db_pass    = 'monitorpass';\r\n\r\nmy $debug      = 0;\r\n\r\nmy $db = \"dbi:mysql:dbname=${db_name};host=${db_host}\";\r\n\r\ndie \"Usage: $0 [project-name] [reporter]\\n\" unless @ARGV > 1;\r\n\r\nmy $project_name  = $ARGV[0];\r\nmy $reporter_name = $ARGV[1];\r\n\r\nmy $dbh;\r\nmy $sql;\r\nmy $sth;\r\nmy $project  = -1;\r\nmy $reporter = -1;\r\n\r\n$dbh = DBI->connect($db, $db_user, $db_pass)\r\n        || die 'ERROR: '.$dbh->errstr;\r\n\r\n$sql = \"SELECT id,name FROM mantis_project_table\r\n        WHERE name LIKE '$project_name'\";\r\n\r\n$sth = $dbh->prepare($sql) || die 'ERROR: '.$dbh->errstr;\r\n$sth->execute() || die 'ERROR: '.$dbh->errstr;\r\nwhile( @data = $sth->fetchrow_array() ) {\r\n        $project = $data[0];\r\n}\r\n$sth->finish;\r\n\r\ndie \"ERROR: project \\`$project_name' does not exist\\n\" unless $project > 0;\r\n\r\nprint \"project: $project_name ($project)\\n\" if $debug;\r\n\r\n$sql = \"SELECT id,username FROM mantis_user_table\r\n        WHERE username = '$reporter_name'\";\r\n\r\n$sth = $dbh->prepare($sql) || die 'ERROR: '.$dbh->errstr;\r\n$sth->execute() || die 'ERROR: '.$dbh->errstr;\r\nwhile( @data = $sth->fetchrow_array() ) {\r\n        $reporter = $data[0];\r\n}\r\n$sth->finish;\r\n\r\ndie \"ERROR: user \\`$reporter_name' does not exist\\n\" unless $reporter > 0;\r\n\r\nprint \"reporter: $reporter_name ($reporter)\\n\" if $debug;\r\n\r\nmy $subject = '';\r\nmy $content = '';\r\n\r\nwhile(<STDIN>) {\r\n        s\/\\s+$\/\/;\r\n        last if $_ eq '';\r\n        $subject = $1 if \/^Subject: (.+)$\/;\r\n}\r\nwhile(<STDIN>) {\r\n        $content .= $_;\r\n}\r\n\r\n$sql = 'INSERT INTO mantis_bug_text_table (description) VALUES (?)';\r\n\r\n$sth = $dbh->prepare($sql) || die 'ERROR: '.$dbh->errstr;\r\n$sth->execute($content) || die 'ERROR: '.$dbh->errstr;\r\n$sth->finish;\r\n\r\nmy $textid = $dbh->{ q{mysql_insertid} };\r\n\r\nprint \"bug text id: $textid\\n\" if $debug;\r\n\r\n$sql = 'INSERT INTO mantis_bug_table\r\n                ( project_id, reporter_id,\r\n                  date_submitted, last_updated,\r\n                  bug_text_id, summary )\r\n        VALUES (?,?,?,?,?,?)';\r\n\r\nmy $now = strftime('%Y-%m-%d %H:%M:%S', localtime(time));\r\n\r\n$sth = $dbh->prepare($sql) || die 'ERROR: '.$dbh->errstr;\r\n$sth->execute($project, $reporter, $now, $now, $textid, $subject)\r\n        || die 'ERROR: '.$dbh->errstr;\r\n$sth->finish;\r\n\r\n$dbh->disconnect;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple Mantis Mail Gateway for mail-in ticket [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-software"],"views":14318,"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pubdi-1x","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":15,"url":"https:\/\/dada.tw\/blog\/2006\/07\/25\/15\/","url_meta":{"origin":95,"position":0},"title":"\u4f7f\u7528 bcompiler \u4f86\u7de8\u8b6f\uff08\u52a0\u5bc6\uff09\u60a8\u7684 PHP \u539f\u59cb\u78bc","author":"dada","date":"2006-07-25","format":false,"excerpt":"Using bcomipler to compile (encode) your PHP scrip\u2026","rel":"","context":"\u5728\u300c\u7a0b\u5f0f\u8a9e\u8a00\u300d\u4e2d","block_context":{"text":"\u7a0b\u5f0f\u8a9e\u8a00","link":"https:\/\/dada.tw\/blog\/category\/comp\/language\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69,"url":"https:\/\/dada.tw\/blog\/2007\/12\/24\/69\/","url_meta":{"origin":95,"position":1},"title":"Script: \u53d6\u5f97 FreeBSD CVSUP \u7684\u66f4\u65b0\u72c0\u614b","author":"dada","date":"2007-12-24","format":false,"excerpt":"\u9019\u662f\u4eca\u5e741\u6708\u5beb\u7684 script... \u7528\u4f86\u76e3\u63a7\u5404\u5927 FreeBSD CVSUP mirror \u7db2\u7ad9\u7684\u2026","rel":"","context":"\u5728\u300c\u8edf\u9ad4\u300d\u4e2d","block_context":{"text":"\u8edf\u9ad4","link":"https:\/\/dada.tw\/blog\/category\/comp\/software\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":18,"url":"https:\/\/dada.tw\/blog\/2005\/06\/23\/18\/","url_meta":{"origin":95,"position":2},"title":"eAccelerator &#8211; PHP \u7db2\u9801\u52a0\u901f\u53ca\u7de8\u78bc","author":"dada","date":"2005-06-23","format":false,"excerpt":"eAccelerator - PHP \u7db2\u9801\u52a0\u901f\u53ca\u7de8\u78bc\u8edf\u9ad4 http:\/\/eaccelerator.n\u2026","rel":"","context":"\u5728\u300c\u7a0b\u5f0f\u8a9e\u8a00\u300d\u4e2d","block_context":{"text":"\u7a0b\u5f0f\u8a9e\u8a00","link":"https:\/\/dada.tw\/blog\/category\/comp\/language\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":88,"url":"https:\/\/dada.tw\/blog\/2008\/07\/30\/88\/","url_meta":{"origin":95,"position":3},"title":"Subversion pre-commit hook","author":"dada","date":"2008-07-30","format":false,"excerpt":"\u5f88\u591a\u4eba\u5beb\u5b8c\u7a0b\u5f0f\u8981 commit \u7684\u6642\u5019\u6703\u5077\u61f6\u4e0d\u5beb log\uff0c\u5c0e\u81f4\u6709\u6642\u5019\u8981\u8ffd\u554f\u984c\u6642\uff0c \u5f88\u96e3\u5f97\u77e5\u5230\u5e95\u5225\u4eba\u6539\u2026","rel":"","context":"\u5728\u300c\u8edf\u9ad4\u300d\u4e2d","block_context":{"text":"\u8edf\u9ad4","link":"https:\/\/dada.tw\/blog\/category\/comp\/software\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24,"url":"https:\/\/dada.tw\/blog\/2005\/09\/08\/24\/","url_meta":{"origin":95,"position":4},"title":"URL \u542b\u4e2d\u6587\u8def\u5f91\u540d\u7a31\u7684\u7d42\u6975\u89e3\u6cd5","author":"dada","date":"2005-09-08","format":false,"excerpt":"URL \u542b\u4e2d\u6587\u8def\u5f91\u540d\u7a31\u7684\u7d42\u6975\u89e3\u6cd5 - \u5229\u7528 mod_fileiri \u89e3\u6c7a\u4e2d\u6587\u6a94\u540d\u554f\u984c \u7576\u7136\uff0c\u5c0d\u4ed8\u4e2d\u6587\u2026","rel":"","context":"\u5728\u300c\u7db2\u8def\u79d1\u6280\u300d\u4e2d","block_context":{"text":"\u7db2\u8def\u79d1\u6280","link":"https:\/\/dada.tw\/blog\/category\/network\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":58,"url":"https:\/\/dada.tw\/blog\/2007\/10\/04\/58\/","url_meta":{"origin":95,"position":5},"title":"File handle is always global in Perl","author":"dada","date":"2007-10-04","format":false,"excerpt":"Perl \u7684 file handle \u53ca directory handle \u4e00\u5b9a\u662f global \u7684\u2026","rel":"","context":"\u5728\u300c\u7a0b\u5f0f\u8a9e\u8a00\u300d\u4e2d","block_context":{"text":"\u7a0b\u5f0f\u8a9e\u8a00","link":"https:\/\/dada.tw\/blog\/category\/comp\/language\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":0,"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dada.tw\/blog\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}