Friday, July 29, 2011

Fun with Mail (part 2)

Some more details on the mail filtering I'd talked about here. To recap, I'd set up a Linux server in the corner of the laundry room, and was using it for IMAP, with Thunderbird on a Mac as the client. But I was finding Thunderbird's filtering very unreliable. I guessed that might be because it was IMAP rather than local folders. So I decided to do some filtering in Smalltalk. I set up a cron job as

export VISUALWORKS=/home/aknight/vw7.7.1nc
/home/aknight/vw7.7.1nc/bin/linux86/visual /home/aknight/bin/imap.im -nogui -evaluate "10 seconds wait. Net.Filter new run

To do the filtering, I wrote a simple class called Filter. I put it in the Net namespace because that way it would see all the Net classes I wanted to use, and because I was too lazy to make my own namespace for just one class.

When one of these filter objects is created we also set up an IMAP client, as

"Security.X509.X509Registry default 
   addTrusted: Security.X509.AlansGlobal."
"client useSecureConnection."
client := IMAPClient host: '192.168.1.5'.
[client connect] 
   on: Security.SSLWarning 
   do: [:ex | ex proceed].
client user: (Net.Settings defaultIdentity).
client login.client select: 'Inbox'.

You'll notice the first two lines are commented out, because after I'd set up the security I decided I didn't really need it for a process running on the same machine, within my home network. But I left the code there because it might be important in other circumstances. The remaining lines create an IMAPClient, tell it which host to use, tell it to use the identity that I'd entered in the settings, have it log in to the server, and issue the select: command to look at the Inbox.

One thing that's important is that when we're done, we should be careful to close the connection, doing
client close.
client disconnect.
or else the server gets too many connections after a while and complains. Not everything has a nice garbage collector to clean up for us.

Once we're connected, we need to get the messages.

messages
  | unseen tempMessages result notDeleted |

  unseen := client searchMessages: 'UNSEEN'.
  notDeleted := client searchMessages: 'NOT DELETED'.
  notDeleted isEmpty ifTrue: [^Dictionary new].
  tempMessages := client fetchMessages: notDeleted.
  client markAsUnSeen: unseen.
  result := Dictionary new.
  tempMessages do: [:each |
    result at: 
      (Integer readFrom: each key readStream) 
      put: each value].
  ^result.

The searchMessages: API will let you search on the server for a particular criteria. The criteria are pretty self-evident. One thing that I'm working around here is that using these API's marks the messages as read. So what I'm doing is finding all the unread messages and keeping a list of their ids, then fetching all of the messages, and then marking the ones that were previously unread as unread again. Not very elegant, but it worked ok. There's probably a race condition there if new messages arrive in between the steps, but the worst thing that happens is the messages show up as having been read when it's not true.

Once we've got all the messages, we loop over them and run filters. Much of the code for that is actually error handling. Martin Kobetic, who wrote a lot of our Net code, says that spam is a wonderful source of edge cases for the various protocols and formats. The main part of the loop looks like

messages keysAndValuesDo: [:key :eachMessage |
   message := [[MailMessage readFrom: eachMessage first readStream]
      on: KeyNotFoundError
      do: [:ex |ex receiver = StreamEncoder encoderDirectory
         ifTrue: [#undecodeablejunk]
         ifFalse: [ex pass]]]
      on: ParsingSimpleBodyError
      do: [:ex | #undecodeablejunk].
   message = #undecodeablejunk ifTrue: [
      Transcript cr; show: index printString, ' is undecodeable'.
      WindowingSystem isHeadless ifFalse: [eachMessage inspect]].
Messages are keyed by integers (message number in the particular mailbox) on the server. So we loop over the key (message number) and the message itself. Well, the message is actually an array with one element with the message body. We need to read that and extract the various header fields. But the message might be in an encoding we don't have. That comes up as a KeyNotFoundError, meaning we didn't find the encoding name, say, Big10. I chose to interpret that as meaning the message wasn't important, so I just return the special symbol #undecodeablejunk and log it. If I'm running interactively, I inspect the message, so I can validate that. I did have some valid messages get flagged as junk that way, but not a lot.

Even if we've got the encoding, the message may be malformed in interesting ways, and we may get a ParsingSimpleBodyError, so I catch that and also mark things non-decodeable.

Then we want to actually run the filters. I defined a pragma for filters, so what I have is a bunch of methods that look like
filtervwnc
   "self new run"
   
   ^self matchRecipient: 'vwnc@cs.uiuc.edu' andMoveTo: 'INFO.vwnc'

Where matchRecipient:andMoveTo: looks like
matchRecipient: recipient andMoveTo: mailbox
   message to, message cc do: [:eachRecipient |
      ('*', recipient, '*' match: eachRecipient) ifTrue: [
         ^self moveTo: mailbox]]
The pragmas are run by iterating over the collection we get from

filters
   ^(Pragma allNamed: #filter: from: self class to: self class)
      sorted: [:a :b | (a argumentAt: 1) <= (b argumentAt: 1)].

If any of the filters return value is the symbol #stop then we don't run any other filters, otherwise we keep going until the end. So, for example, I put in a filter that if an email was directly addressed to one of my email addresses, don't run any of the other filters, leave it in the Inbox. And once any filter has tagged a particular message, we move the message to the appropriate place and then stop.

Finally, there's moving the messages. The actual move is just a copy and delete in terms of
IMAP operations.
move: messageIdentifier to: mailbox

 | result1 result2 |
 result1 := client copy: messageIdentifier to: mailbox.
 result2 := client markForDelete: messageIdentifier.
but just to make doubly sure I'm not running into trouble I put in some checking ahead of that.
moveTo: mailbox

 | checkMessage |
 "First, check that the message is what we thought it was."
 checkMessage := (client fetchMessages: (Array with: index)) first value first.
 client markAsUnSeen: (Array with: index).
 (messages at: index) first = checkMessage ifFalse: [^#stop].
 self move: (Array with: index) to: mailbox.
 Transcript cr; show: 'Moving ', index printString, ' to ', mailbox.
 message isSymbol 
     ifTrue: [Transcript cr; show: message] 
     ifFalse: [ 
  Transcript cr; show: (message from first, '   ', message subject)].
 ^#stop.

In the end, with a bit of fighting with things that are hard to debug when the right thing just doesn't happen, I got this pretty much working. It had a few issues. One is that even though I was carefully running the filters a few seconds after each fetch, there was often some delay in the filters running, so I'd have things that should get redirected to mailing lists left in the Inbox for a couple of minutes. Another was that every once in a while it'd get stuck on a message that was malformed in a new and interesting way, and I had to go look at the error processing again. Some messages did get falsely caught - there are people sending legitimate emails who used some very peculiar encodings or header formats.

The biggest issue, though, is that in the end this proved mostly unnecessarily because I switched to an email client where the filters work on the Mac (Postbox) and that has a number of other advantages over Thunderbird as well. I'm still using the Smalltalk filtering. It has the advantage that it doesn't require the mail client on my main computer to be running in order for filtering to happen. But I've switched some of the most common filters to just use Postbox's filtering, mostly the ones that are for mailing lists that generate a lot of traffic. But I've still got most of my filters in Smalltalk, and nowadays the need for me to check them is pretty rare. And it was definitely an interesting experience writing it.

Thursday, July 28, 2011

OS X Lion

One thing I'm finding annoys me with Lion is the rmemoval of a feature that it turns out I used all the time -what I'll call mini-exposé. In Snow Leopard, if you held down the mouse button on a dock icon, it would show you all the windows associated with that application. Now if you do that it gives you a text list of them, and if you want to see the windows it's a separate menu item. Especially for something like a web browser with tabs, the textual representation isn't nearly as useful. Sigh.

On the plus side, following Travis' instructions it looks like I have syntax highlighting for Smalltalk code working here.

Wednesday, June 22, 2011

Weltmeisterschaft

I'm off for the next while to see the women's football world cup in Germany, so I'm unlikely to be posting much, and if I do, it probably won't be technical. See you all after the final.

Interesting Bugs

There are lots of interesting ways for software to go wrong. Here's one recent one that we uncovered at Cincom. And I should say that by "we" I mostly mean Tom Robinson, from the Store group.

I first noticed this bug in the middle of a demo in Frankfurt. I was showing some interesting Glorp and StoreGlorp capabilities. I'd run an interesting expression in a Store workbook, inspect the result, then run another expression and get an error. Force a reconnect, and it was fine again, but it occured several times.

Travis Griggs noticed an even odder manifestion of it in trying out some Store expressions. He wrote an expression to find the head of the 7.9 trunk for a particular package or bundle, and it ran fine, returning the expected result. Then he ran it to find the head of the 7.8 trunk, and it returned the exact same package as before, from the 7.9 trunk. Running it again produced the right answer.

Investigation revealed nothing obvious going on at the Glorp caching level. The database appeared to be genuinely returning results that were clearly incorrect for the query that was issued. Sometimes they were obviously for a different query. You might ask for a StorePackage and get back a single column that was obviously for the tw_databaseidentifier. This only happened when using Postgresql. And it apparently only happened when using the Store Workbook. Normal Store operations never showed this.

After much examination, Tom tracked down the cause, an interaction of several causes. First, when you open an inspector, there may be code specific to the type of object being inspected. In particular, the inspector shows the icon for things. The icon for packages shows differently depending if it's the version that's resident in the image or not. Asking that, especially on a completely new connection, could end up doing a database query. That's not really great, but should still work. But...

The inspectors try to be robust. If you write a printString that goes into infinite recursion, raises an exception, or otherwise doesn't return, it tries to stop it and just print an indication that it didn't work. So one of the mechanisms there is a timeout. If it takes too long, it just terminates the process. And in addition...

The Postgresql driver is written purely in Smalltalk. So there's Smalltalk code that manages a socket and the communication on it. This is in contrast to most other database drivers that call out to a C library, which may be just communicating on a socket underneath, but the protocol and details are hidden.

So, what happens is that we issue a query, and open an inspector on the resulting object(s). The inspector is the reason that this didn't affect normal Store operations. The inspector wants to know if this is the current version in the image, so it issues a query to the database. That query will run in a separate Glorp session, but because we're trying to be careful of resources, it'll re-use the underlying database connection. And there's more setup code than is really necessary that runs for each new session (or did, up until more recent builds). If the database is Postgresql, and isn't very close by on a fast machine, the inspector will time out before it gets the answer, so it will terminate the process. The database driver doesn't clean up properly when the process is terminated, so the previous results are left in a buffer. The next query that's issued on that connection will start looking for results, and get the results of the previous query out of the buffer. If the shape of those results matches what we're expecting, we'll just get a wrong answer. If the shape doesn't match, we'll get a confusing error. For example, if it's running the initial setup query to get the tw_databaseidentifier, we might see the result of that.

Fortunately, this only affects operations that use inspectors, so it has no effect on normal Store operations. And databases other than Postgresql keep the individual query results separate by themselves, so that won't happen. But it's a nice example of some very interesting interactions that normally don't show up on their own.

Friday, May 20, 2011

Checking overrides when upgrading

Here's a question that came up in the vwnc mailing list. Suppose that you have an application which has overrides of a number of system methods. I'm also going to start referring to overrides as redefines here, because I never liked the ambiguity between an override in a subclass and the replacement of a definition that VisualWorks uses the term for. So I'll try calling them redefines here, even though it makes for a lot of backspacing.

Anyway, let's say that this was built in VisualWorks 7.6, but now you're upgrading to 7.8, and you want to check which of these methods no longer apply. That can be complicated in general, but a basic first check is if the method we were redefining changed between VisualWorks 7.6 and 7.8. That's a bit tedious to check manually, but we can script it.

Of course, this depends on how we've organized things. The original question came up in the context of having grouped redefinitions by the package that contained the thing being redefined. So, if we redefined a method in "Assets", we'd create a package "Assets patches" and put the redefinition there.

So here's an example of a workspace script for finding these. It assumes that we've published the old version of the base into our local database, and that we've loaded our code into a new image.

     mySession := StoreLoginFactory currentStoreSession.
     versionToUpgradeFrom := '7.7 '.
     patchPackages := Store.Registry allPackages select: [:each |
          each name like: '% patches'].

So, first we need to get a StoreGlorp session which we'll use to do our queries, and we define a variable for how we'll find the old versions. We'll also need the list of patch packages that we have in the image. I used the #like: method to do the matching, which does it in SQL style, rather than the more traditional matches:, or even regular expressions, but they'd all work.

     patchPackages do: [:eachPatchPackage | 
         basePackageName := eachPatchPackage name readStream upTo: Character space.
         currentOverriddenPackage := Store.Registry packageNamed: basePackageName.

   
Now we start a loop over each of our patch packages. The first thing we need to know is what the basic package is, which we do based on the simple naming convention described above.

              baseQuery := Query readOneOf: StorePackage where: [:each | 
                  (each name = basePackageName & 
                          (each version like: '%', versionToUpgradeFrom, '%')].
              baseQuery orderBy: [:each | each timestamp descending].
              baseVersion := mySession execute: baseQuery.


Now we do a query to find the appropriate old version. So this involves a Glorp query to find packages by that name, whose version string matches the variable we set at the beginning. If there are multiples, we take only the latest, by sorting them in descending order and just taking the first one. I'm dealing with the Cincom internal repository, so there will be lots and lots of versions of base code. In a project repository there are probably only a few, making it fairly simple to find this.

Once we've done that, we'll loop over the methods in the patch package, and get the three different versions of the method: ours, the new base image version, and the old base image version. These will be three different kinds of objects, and the APIs for manipulating them and getting them are, well, let's just say not as polymorphic as they might be. Our method will be a CompiledMethod. The new version in the image will be an OverriddenMethod. And the one we read from the database will be a StoreMethodInPackage, mapped to the database. Getting that one is a bit fussy, in that we need to make sure we're asking for the class by name, and the name should be exactly the way it will be in the database.

         eachPatchPackage methods do: [:eachMethodDescription |
                  imageMethod := eachMethodDescription method.
                 oldOverridden := baseVersion 

                       method: imageMethod selector 
                       forClassNamed: imageMethod mclass instanceBehavior
                      absoluteName meta: imageMethod mclass isMeta.
                 newOverridden := Override 

                       selector: imageMethod selector 
                       class: imageMethod mclass 
                       in: currentOverriddenPackage.

Finally, we get the source code for the old and the new versions and check them. If the new base version doesn't exist, then that method was either deleted or moved to another package, and we definitely need to think about our redefinition. And if the source code is different, we also want to think about it. Then there's the question of what to do if there's something to think about. We could easily write that to the file or to the Transcript, but in this case what I've done is open a simple text comparison window on the two. That could get ugly if there are a large number, but is nice to work with for just a few.

                  (newOverridden isNil 
                       or: [oldOverridden sourceCode ~= newOverridden sourceCode]) 
                  ifTrue: [
                         | view |
                         view := SideBySideTextComparisonView new 

                               leftText: oldOverridden sourceCode 
                               rightText: newOverridden sourceCode.
                         ScheduledWindow new component: view; openWithExtent: 800@600]]].


This would need to be tweaked for particular environments, but seems like it might be a good start towards making that sort of migration a bit easier. And if I can figure out how to get the syntax highlighting on this blog working, it might get prettier to read here.

Wednesday, May 18, 2011

Looking at the public repository easily

A quick note. Today someone was wishing there was a way to see what was in the public repository easily without having to fire up Smalltalk. And there is. There's an index of it that's google searchable. It's fairly rough, and it omits things that it thinks aren't interesting, including packages without comments. But it's still quite useful.

Monday, May 16, 2011

Fun with Mail (part 1)

A description of switching around with email clients, and writing some IMAP code in Smalltalk.

I've been a Eudora user for many years. I started using it back when I was a student, and mostly stuck with it, with a bit of time off using VM in Emacs as my primary client. I never liked Outlook, but was able to so I just kept using Eudora in preference to that for corporate email. One of the very nice things in the later versions of Eudora was the search system. The UI was slightly awkward, but it was fast and gave good results. And I keep a lot of email, so that's important to me. I'm at about 5GB right now, with some of it going as far back as 1990.

Unfortunately, Eudora has been abandonware for quite a while now. I resisted switching for a long time, but there were starting to be enough problems that it had to happen.

I wasn't sure what client I'd end up with. The various webmail solutions seemed to be out because none of them seemed to have ways for me to upload huge archives of old mail. So I figured I'd end up with an actual mail client on my machine. In order to be able to try out different ones, and also because I thought it'd be fun, I set up a small Linux server with IMAP (using dovecot) and had it fetch the messages from my various accounts to one location.

Getting the email over was a bit of an adventure. It was possible, in Eudora, to set up an account on the IMAP server, and then to drag folders over onto it, copying their contents. But that was very slow, and tended to crash. Eventually I found a Mac called Eudora Mailbox Cleaner that can also convert the format, and I was able to use that to import both my old Eudora mail, and some other stuff that wasn't in Eudora, but was saved in more or less Unix mailbox format. It crashed a couple of times part-way through, but with a bit of babysitting I got it all converted into local folders in Thunderbird. Thunderbird would let you move stuff from local folders into IMAP as well, and it wasn't quite as slow and didn't crash quite as often.

So ultimately I had my mail converted, and was able to try out some clients. Apple's mail was the main other one I tried, but it missing features I wanted, so I ended up mostly using Thunderbird, particularly with the QuickFolders and Archive This extensions, which were helpful for quick filing and finding folders. But Thunderbird had some problems. The search was not particularly fast, at least not on the kind of volume of mail that I had. Worse, the filters didn't seem to work reliably. I don't know if that was just an issue with Thunderbird and IMAP, but the spam filtering wasn't moving things out of the Inbox reliably, and mailing list messages weren't reliably going into the right folders.

I decided to take this as an opportunity to write some Smalltalk code, and made myself a little cron job that would run a Smalltalk program to filter the messages. Along the way, I learned a bit about IMAP and the VisualWorks libraries for using them. So in part 2, I'll talk about some of the code that I ended up with.