C# Detecting and removing the Read Only bit

August 20, 2010 – 9:03 am

Seems like trying to move a read only file throws an exception on some computers and not on others. I'm not sure why it only happens on some, I couldn't see a common OS or .net version. The function below will remove the read only bit from a file.

 
private void prep_file(string file)
{
  if ((File.GetAttributes(file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
  {
    File.SetAttributes(file, FileAttributes.Normal);
  }
}
 

Here's the exception I was getting that led me to this:

System.UnauthorizedAccessException: Access to the path 'C:\sxr\dictation_duck\temp\08_20_10_09_28_33_00013889.voc' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

Strip Attachments from Email with Perl, Procmail, and mpack/munpack

August 11, 2010 – 6:04 pm

At a place I go to everyday I have a need to strip attachments out of emails to a certain address.

There are some easy ways to do it with PHP, but they involve continually querying an IMAP server. I didn't want to do that, I wanted to work with them as they were received by the mail server.

I came up with a method using Perl's Mail::Audit, Procmail, and mpack/munpack.

I've zipped all of the files mentioned below into one convenient archive, click this to get a copy.

To Begin:

Install Mail::Audit
Install mpack

The version of mpack I linked works alright on Fedora 8, 9, 10, and 11, if it doesn't work for you, you can Google for your own OS, just make sure the package includes munpack.

You probably already have Procmail and Perl.

Create a user that will receive the mail.

Place the .procmailrc file in the user's home directory. Change /your/home/directory/ to your user's home directory.

Place demo_mail_audit.pl in the user's home directory.  Edit the file and change $base_dir to be your user's home directory.

Place list_attachments.php in the user's home directory. Edit the file and change $base_dir to be your user's home directory.

Then:

cd /your/home/directory

mkdir mail_temp attachment_temp
chmod 777 mail_temp attachment_temp
touch attachment.log
chmod 666 attachment.log
chmod 755 list_attachments.php demo_mail_audit.pl

Then send the user you made a message with an attachment.

  1. Procmail will get the email from sendmail (or whatever) and pass it to demo_mail_audit.pl.
  2. demo_mail_audit.pl will write the message as a text file to /your/home/directory/mail_temp and call list_attachments.php.
  3. list_attachments.php will run munpack on the text file from the previous step, stripping the attachments and placing them in /your/home/directory/attachment_temp
  4. list_attachments.php will then loop through the attachment_temp directory and print what it finds to /your/home/directory/attachment.log.

Now you can take list_attachments.php and modify it for your needs. Have it copy the files to another place on disk, insert them into a DB, whatever!

Wall of code:

 
#!/usr/bin/perl
#
# demo_mail_audit.pl
#
use Mail::Audit;
 
$base_dir = "/home/dictations";
$attachment_temp = "$base_dir/attachment_temp";
$mail_temp = "$base_dir/mail_temp";
chdir($base_dir);
 
my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
$year = $year + 1900;
$mon = $mon + 1;
if($mday < 10) { $mday = "0$mday"; }
if($mon < 10) { $mon = "0$mon"; }
if($min < 10) { $min = "0$min"; }
if($hour < 10) { $hour = "0$hour"; }
my $did = "$year$mon$mday$hour$min$sec";
$temp_file = "$mail_temp/$did.txt";
 
sub logger {
  $message = $_[0];
 
  my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
 
  $year = $year + 1900;
  $mon = $mon + 1;
 
  if($mday < 10) { $mday = "0$mday"; }
  if($mon < 10) { $mon = "0$mon"; }
  if($min < 10) { $min = "0$min"; }
  if($hour < 10) { $hour = "0$hour"; }
 
  my $did = "$year$mon$mday $hour:$min";
 
  open(LOG, ">>attachment.log");
  my $to_log = "perl: $did - $message\n";
  print LOG "$to_log";
  close(LOG);
 
  return($to_log);
}
 
my $item = Mail::Audit->new;
my $from = $item->from();
study $from;
my $to = $item->to();
my $cc = $item->cc();
my $subject = $item->subject();
chomp($from, $to, $subject);
 
logger("=================================================================================");
logger("New Message From: $from, Subj: $subject");
 
logger("Writing $temp_file");
$item->accept($temp_file, { noexit => 1 });
 
$php_script = "$base_dir/list_attachments.php $temp_file";
logger("Handing off: $php_script");
system($php_script);
 

 
<?PHP
#
# list_attachments.php
#
 
$base_dir = "/home/dictations";
 
error_reporting(0);
 
function logger($message) {
  global $base_dir;
  $now_time = date("Ymd G:i:s");
 
  $fh = fopen("$base_dir/attachment.log", 'a') or die("can't open file");
  fwrite($fh, "php : $now_time - $message\n");
  fclose($fh);
}
$file = $argv[1];
 
if(!$file) {
  logger("Called without a file.");
  exit;
}
 
logger("Called: $file");
 
chdir("$base_dir/attachment_temp");
$t = `munpack $file`;
chdir($base_dir);
logger($t);
 
$i = 0;
if($handle = opendir("$base_dir/attachment_temp")) {
  while(false !== ($file = readdir($handle))) {
    if($file != "." && $file != "..") {
      $i++;
      logger("$base_dir/attachment_temp/$file");
    }
  }
  closedir($handle);
}
 
logger("Saw $i attachments.");
 
?>
 

C# List all drives and search by drive label

August 5, 2010 – 8:31 am

At a certain place I go to every day we have people who dictate into hand held recorders. These recorders plug into a PC via USB and show as a removable drive. The dictations are then on this drive in MP3 format.

When you plug them in they usually keep the same drive letter. You can also mount them onto a directory and they'll sometimes keep that. Usually and sometimes means it'll work fine for months and then up and change for no reason.

Since Windows doesn't cooperate, I needed an easy way to find the recorders. The solution I came up with was to give them all a standard drive label and then use the code below with some additions to do what I needed to do to get the dictations off of them.

Download Visual Studio 2008 Sample Project

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace DriveLabelDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void logger(string message)
        {
            DateTime time = DateTime.Now;
            string format = "yyyy-MM-dd HH:mm:ss";
            logbox.Text = logbox.Text + time.ToString(format) + ": " + message + "\r\n";
            this.logbox.SelectionStart = logbox.Text.Length;
            this.logbox.ScrollToCaret();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            logger("Searching!");
            // CHECK THE LIST OF DRIVES FOR ONE LABELED RECORDER
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                string label = drive.IsReady ? drive.VolumeLabel : null;
 
                string to_show = drive.Name + " - " + label;
 
                if (label == textBox1.Text)
                {
                    to_show += " <- MATCH";
                }
 
                logger(to_show);
            }
            logger("==================");
        }
    }
}
 

XBMC Keymap for a Diamond RC118N MCE Remote

May 5, 2010 – 9:47 pm

The Diamond RC118N is not a good remote to choose. I messed with this for a while without much luck. Most of the buttons work out of the box. The power buttons, those in the first row (Pictures, Radio, ect), the Back button and the row under mute (Rec TV, Guide, ect) do not work. There is no menu button. The pause button doesn't work, but you can get around that by pressing the play button.

This config below will get your ParentDir function working via the 'Back' button and the ContextMenu via * or #.

This needs to go into the global keyboard section of keymap.xml:

<code>
<key id="61606">ParentDir</key>
<key id="61600">ContextMenu</key>
</code>

Excel Tip of the Day!

March 16, 2010 – 10:02 am

If you need to put a + (plus, positive) or - (minus, negative) sign in an Excel field, Excel tries to start building a formula. This is cool if you actually want to make a formula, not so much when all you want is the + or -.

You can put an ' (apostrophe) in front of the + or -, like this '- or '+. The ' is Excel's comment character. The ' will not be displayed. The + or - will be.

How to save a PHP object to a database

February 11, 2010 – 9:55 am

This is an example from a class that deals with user accounts.

 
  function save() {
    if(!$this->id) {
 
### DUPE PREVENTION
      $sql = "SELECT id FROM users WHERE username = '" . $this->username . "' LIMIT 1";
      $db2 = mysql_query($sql);
      $db3 = mysql_fetch_array($db2);
      if($db3['id']) {
        return(0);
      }
 
### INSERT AND GET THE NEW ID
      $sql = "INSERT INTO users(`name`, `username`) " .
                  "VALUES('" . $this->name . "', '" . $this->username . "')";
      mysql_query($sql);
 
      $sql = "SELECT id FROM users WHERE name = '" . $this->name . "' " .
             "AND username = '" . $this->username . "' ORDER by username DESC LIMIT 1";
      $db2 = mysql_query($sql);
      $db3 = mysql_fetch_array($db2);
      $this->id = $db3['id'];
 
    }
 
    $to_set = '';
 
    foreach($this as $key => $value) {
      if($key == 'id') {
        continue;
      }
 
      if($key != -1) {
        if($to_set) {
          $to_set .= ', ';
        }
 
        if(is_array($value)) {
          $value = serialize($value);
        }
 
        $to_set .= "$key = '" . mysql_real_escape_string($value) . "'";
      }
    }
 



thevedic.net

Part of the thevedic.net.
[Contact Me] - [Mud Emporium] - [Ozark, AR] - [See my Port?]

This site is in no way represents the opinions or positions of any company or organization including those who employ the creators of this web site. We're not responsible for the content, posts, responses by other users or ideas of any sites that are linked to on this page. This site is recommended for ages 18 and up.

VEDIC VEKO IS PROSPEROUS

Arkie Imports | Dean Vaughan | Dean Vaughan | K. Dean Vaughan | Kenneth Vaughan | Pig Trail 23 | Pig Trail Pics | See My Port Forward Info | See My Port Forward Info


I'm not affiliated with the following in anyway.
Arkansas MMA - Inferno Mixed Martial Arts - Inferno Martial Arts Club - Inferno Martial Arts Club - Official Inferno Fight Gear - Inferno Fight Equipment - Inferno Combat Gear - IFC Online - Inferno Fight Gear - Inferno MMA - Inferno Fight Gear