Page Menu
Home
GitPull.it
Search
Configure Global Search
Log In
Files
F15169463
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b74decb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/config.php
diff --git a/bot.php b/bot.php
new file mode 100644
index 0000000..0041a1c
--- /dev/null
+++ b/bot.php
@@ -0,0 +1,167 @@
+#!/usr/bin/php
+<?php
+
+// import configuration
+require 'config.php';
+
+// load the IMAPSpooler class
+require IMAPBOT_PATH_SPOOLER;
+
+// declare used classes
+use reyboz\IMAPSpooler;
+
+// use system signals
+pcntl_async_signals( true );
+
+// install new signal handlers
+pcntl_signal( SIGINT, 'sig_handler' );
+
+// imap connection
+$spooler = new IMAPSpooler(
+ IMAPBOT_IMAP_MAILBOX,
+ IMAPBOT_IMAP_USERNAME,
+ IMAPBOT_IMAP_PASSWORD
+);
+
+// import phabricator stuff
+$phab_root = IMAPBOT_PATH_PHAB;
+require_once $phab_root.'/scripts/__init_script__.php';
+require_once $phab_root.'/externals/mimemailparser/MimeMailParser.class.php';
+
+// set the callback for every e-mail
+$spooler->setEmailHandler( function ( $body, $headers, $info ) {
+
+ # The following code is under Apache license (c) Phabricator, Phacility
+ # https://secure.phabricator.com/source/phabricator/browse/master/scripts/mail/mail_handler.php
+ # https://secure.phabricator.com/source/phabricator/browse/master/LICENSE
+
+ // complete message
+ $message = $headers . $body;
+
+ $parser = new MimeMailParser();
+ $parser->setText( $message );
+
+ $content = array();
+ foreach (array('text', 'html') as $part) {
+ $part_body = $parser->getMessageBody($part);
+
+ if (strlen($part_body) && !phutil_is_utf8($part_body)) {
+ $part_headers = $parser->getMessageBodyHeaders($part);
+ if (!is_array($part_headers)) {
+ $part_headers = array();
+ }
+ $content_type = idx($part_headers, 'content-type');
+ if (preg_match('/charset="(.*?)"/', $content_type, $matches) ||
+ preg_match('/charset=(\S+)/', $content_type, $matches)) {
+ $part_body = phutil_utf8_convert($part_body, 'UTF-8', $matches[1]);
+ }
+ }
+
+ $content[$part] = $part_body;
+ }
+
+ $headers = $parser->getHeaders();
+ $headers['subject'] = phutil_decode_mime_header($headers['subject']);
+ $headers['from'] = phutil_decode_mime_header($headers['from']);
+
+ $received = new PhabricatorMetaMTAReceivedMail();
+ $received->setHeaders($headers);
+ $received->setBodies($content);
+
+ $attachments = array();
+ foreach ($parser->getAttachments() as $attachment) {
+ if (preg_match('@text/(plain|html)@', $attachment->getContentType()) &&
+ $attachment->getContentDisposition() == 'inline') {
+ // If this is an "inline" attachment with some sort of text content-type,
+ // do not treat it as a file for attachment. MimeMailParser already picked
+ // it up in the getMessageBody() call above. We still want to treat 'inline'
+ // attachments with other content types (e.g., images) as attachments.
+ continue;
+ }
+
+ $file = PhabricatorFile::newFromFileData(
+ $attachment->getContent(),
+ array(
+ 'name' => $attachment->getFilename(),
+ 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
+ ));
+ $attachments[] = $file->getPHID();
+ }
+
+ $delete = true;
+
+ try {
+ $received->setAttachments($attachments);
+ $received->save();
+ $received->processReceivedMail();
+ } catch (Exception $e) {
+
+ // do not delete messages with errors
+ $delete = false;
+
+ $received
+ ->setMessage(pht('EXCEPTION: %s', $e->getMessage()))
+ ->save();
+
+ throw $e;
+ }
+
+ return $delete;
+} );
+
+// flag indicating that an error raised
+$error = false;
+
+// flag indicating that we can continue to scan again the mailbox
+$loop = true;
+
+do {
+ try {
+ // open the connection
+ $spooler->open();
+
+ // just process all and then quit
+ $spooler->processAll();
+
+ // close the connection
+ $spooler->close();
+
+ // wait some time
+ sleep( IMAPBOT_CYCLE_SLEEP );
+
+ } catch( Exception $e ) {
+ printf(
+ "SMTP bot error (%s): %s\n",
+ get_class( $e ),
+ $e->getMessage()
+ );
+ $loop = false;
+ $error = true;
+ }
+
+} while( $loop );
+
+/**
+ * Operating system signal handler
+ *
+ * @param $signo int
+ * @param $siginfo mixed
+ */
+function sig_handler( $signo, $siginfo ) {
+
+ // stop looping
+ $GLOBALS['loop'] = false;
+
+ // eventually close the spooler
+ $GLOBALS['spooler']->close();
+
+ // just warn about this signal
+ echo "Quitted by SIG $signo\n";
+
+ // quit
+ if( $GLOBALS['error'] ) {
+ exit( 1 );
+ } else {
+ exit( 0 );
+ }
+}
diff --git a/config-example.php b/config-example.php
new file mode 100644
index 0000000..faaa878
--- /dev/null
+++ b/config-example.php
@@ -0,0 +1,8 @@
+<?php
+# copy this file, fill, and save as 'config.php'
+define( 'IMAPBOT_PATH_PHAB', '/path/to/phabricator' );
+define( 'IMAPBOT_PATH_SPOOLER', '/path/to/imap-spooler/class-IMAPSpooler.php' );
+define( 'IMAPBOT_IMAP_MAILBOX', '{HOST:PORT/ssl}INBOX' ); // HOST example: foo.it, PORT example: 993
+define( 'IMAPBOT_IMAP_USERNAME', 'foo@bar.it' );
+define( 'IMAPBOT_IMAP_PASSWORD', 'imap password password' );
+define( 'IMAPBOT_CYCLE_SLEEP', 10 );
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Nov 18, 23:52 (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2263473
Default Alt Text
(4 KB)
Attached To
Mode
R14 Phabricator inboud e-mails using IMAP spooler
Attached
Detach File
Event Timeline