I need to mention a fun thing that I did almost exactly when this site went offline. I signed up to volunteer in an RCMP training exercise scheduled for June 18, 2013. At the information session, the head of Risk Management UBC told us that one could either get up at 8am to play a regular hostage or get up at 6am to play a hostage wearing bullet hole makeup. I chose the former.
When the event started, the group I was with went to Totem Park and the other group went to the "Force Sciences Building". People with Twitter accounts also posted that there was a shooter on the campus (with a disclaimer that the message was fake). I guess some sense of panic makes the situation more realistic for the cops. When we got to Totem Park, we met two friendly hostage takers from the LAPD. The RCMP wanted people from the outside to be posing as the shooters to make their actions harder to predict. The LAPD guys had shotguns loaded with blanks and they told us that the Emergency Response Team would be firing paintballs.
They also told us what it was like to work for a SWAT team. Some people were surprised to hear that they resolved situations 99% of the time without firing a shot. They said that one of the criminals from the other 1% was on a rampage under the influence of PCP. This went on for quite awhile, since the police first tried to negotiate with phone calls. The police outside were staying hidden in the bushes but we accidentally saw one of them because of the building's cleaning staff. They asked what the guy in the bushes was doing so they must've been unaware of all the emails about the training exercise.
The negotiation obviously reached an impasse since no one had millions of dollars to trade for hostages. When the stairwell door finally opened, at least 20 cops came running and there was no holding them back. The LAPD guys fired a few blanks into the hall, but they were soon pelted by paintballs and handcuffed. We were all crouched behind chairs during the rescue, but none of the paintballs came close to hitting us. The cops were very accurate. After this, we had to follow a specific route outside and keep our hands visible to the cops at all times. You never know who might be an inside man. We can only speculate about how they figured out which floor we were on.
Something else I worked on in June 2013 was an automated solution to the problem of top posting in emails. This refers to the increasingly common practice of quoting everything a person just said and writing your reply to it on top. In a throwaway email that will only be read once, this is fine because it minimizes scrolling. But in an email of value that people might want to read repeatedly, top posting is rude. It forces the person to read backwards in order to ascertain the context of the post. There is a self-referential rant about top posting that people circulate a lot:
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?
I've had to explain why I bottom post more than once because some people don't even realize that it is saving them time. Strictly speaking, it is not correct to post everything at the bottom either. RFC1855 recommends a summary at the top followed by specific replies below the relevant quotations. But replies that are all at the bottom still look much nicer than replies that are all at the top. Since the task of moving replies down is completely mechanical, I went looking for programs to do it. The only one I found is by Arthur Pemberton. Since this is all about making posts look nice, I have cleaned up the formatting below.
#include <stdio.h>
#define MaxLine 5000 /* Maximum number of characters per line */
#define MaxMesg 5000 /* Maximum number of lines per file */
#define EOLN '\n' /* End of line character */
int interspersed; /* indicator to say whether the message is interspersed or not */
int NumberOfLines; /* Counts the total number of lines in a message */
struct MessageStruct {
int quoteorder; /* The number of leading quote symbols in this line */
int linenumber; /* The initial number of this line */
char data[MaxLine]; /* The line string */
} line[MaxMesg]; /* An array of lines, ie. the whole message buffer */
void ReadTheMessage()
{
char c;
int n = 0; /* Current line counter */
int i = 0; /* Current character counter */
int endoffile = 0; /* End of file indicator */
NumberOfLines = 0; /* Message is empty at this point */
while ((!endoffile) && ((c = getchar()) != EOF)) /* Read in the data from stdin */
{
/* Initialize the quote order. It will be counted later... */
line[n].quoteorder = 0;
i = 0; /* Reset the character counter */
while ((c != EOLN) && (c != EOF)) /* Read in the current line */
{
line[n].data[i] = c; /* Feed the message buffer with the data */
i++; /* Move to the next character */
c = getchar(); /* Read it, and repeat */
}
line[n].data[i] = EOLN; /* Finish the string */
line[n].linenumber = n; /* Set the line number */
n++; /* Move to the next line, and repeat */
if(c == EOF) endoffile = 1; /* Handle the premature end of file */
}
NumberOfLines = n; /* Save the number of lines and finish */
}
void WriteTheMessage()
{
int n = 0; /* Current line counter */
int i = 0; /* Current character counter */
while (n < NumberOfLines) /* Write the data to stdout */
{
i = 0; /* Reset the character counter */
while (line[n].data[i] != EOLN)
putchar(line[n].data[i++]); /* Write the current line */
putchar(EOLN); /* Write the end of line */
n++; /* Move to the next line, and repeat */
}
}
void AnalyzeTheMessage()
{
char c;
int n = 0; /* Current line counter */
int i = 0; /* Current character counter */
int k = 0; /* Current quote order counter */
int orderlessthenk = 0; /* Indicator if there is a quoteorder less then k */
int maxquoteorder = 0; /* Maximum value for the quoteorder */
/* Ok. First we count the so called "quote order" of each line,
* which amounts to number of leading '>'s per line. In the process,
* we need to account for the famous "From" bug in sendmail, and
* decrement the quoteorder if the text starts with the word
* "From", in order to get it right.
*/
for (n = 0; n < NumberOfLines; n++) /* Go through the each line */
{
i = 0; /* Reset the character counter */
c = line[n].data[i]; /* Set the initial data */
/* Count the leading '>'s, skip spaces */
while ( ((c == '>') || (c == ' ')) && (c != EOLN) )
{
/* Increment quoteorder for each '>' */
if (c == '>') line[n].quoteorder++;
i++; /* Go to the next character */
c = line[n].data[i]; /* Read it, and repeat */
}
if ( (c == 'F') && /* Check if the following string is "From" */
(line[n].data[i + 1] == 'r') && /* Now, this is very, very ugly... */
(line[n].data[i + 2] == 'o') &&
(line[n].data[i + 3] == 'm') ) line[n].quoteorder--;
/* If yes, decrease the quoteorder... */
}
/* Now we need to check if the message is interspersed or not. This is tricky,
* as we need to find constructions of the form:
*
* > > >
* >
* > > >
*
* and similar, which amount to quoteorder being 3,1,3 and the like.
* Simoultaneously, we *don't* want to find constrctions of the form:
*
* >
* > > >
* >
*
* because this can happen even if the message is not interspersed.
* But that's life... ;-)
*/
for (n = 0; n < NumberOfLines; n++) /* First find the biggest quote order */
if (line[n].quoteorder > maxquoteorder)
maxquoteorder = line[n].quoteorder;
interspersed = 0; /* Assume message is not interspersed */
/* We need to check the above construction for all nonzero quote orders */
for (k = 1; k < maxquoteorder; k++)
{
n=0; /* Start from the begining of the message */
/* Get to the block of lines of quoteorder k */
while ((line[n].quoteorder != k) && (n < NumberOfLines)) n++;
/* Go through the block */
while ((line[n].quoteorder == k) && (n < NumberOfLines)) n++;
/* Now go down the rest ot the lines, and check if some line has
* quoteorder less then k, and another line after it the quoteorder
* equal to k again, which amounts to interspersed construction.
*/
orderlessthenk = 0; /* Assume there are no quotes of order less then k */
while (n < NumberOfLines) /* For each of the remaining lines */
{
/* alert if there is a quote of lesser order */
if (line[n].quoteorder < k) orderlessthenk = 1;
/* We found the interspersed construction!! */
if ((line[n].quoteorder == k) && (orderlessthenk)) interspersed = 1;
n++; /* Go check the next line */
}
}
/* And that's it. If the message has interspersed structure on any
* level of quoteorder, we have the indicator on. Otherwise, it is off.
*/
}
void SwitchLines(int i, int j)
{
int n;
int tempquoteorder;
int templinenumber;
char tempdata[MaxLine];
/* This switches line i with line j, as needed for sorting (sigh)... */
n = 0; /* Copy line i to temp */
tempquoteorder = line[i].quoteorder;
templinenumber = line[i].linenumber;
while (line[i].data[n] != EOLN)
tempdata[n] = line[i].data[n++];
tempdata[n] = EOLN;
n = 0; /* Copy line j to line i */
line[i].quoteorder = line[j].quoteorder;
line[i].linenumber = line[j].linenumber;
while (line[j].data[n] != EOLN)
line[i].data[n] = line[j].data[n++];
line[i].data[n] = EOLN;
n = 0; /* Copy temp to line j */
line[j].quoteorder = tempquoteorder;
line[j].linenumber = templinenumber;
while (tempdata[n] != EOLN)
line[j].data[n] = tempdata[n++];
line[j].data[n] = EOLN;
}
void SortTheMessage()
{
int i,j;
/* In order to make the message bottom-posted, we simply need to sort
* the lines by quoteorder descending. Further, we need to preserve
* the order of lines of the same quoteorder, which amounts in sorting
* same-quoteorder lines by line number, ascending. N.B.: Maybe this can
* be done in a more clever way, but I am lazy to ponder over it... :-)
*/
/* The algorithm is just a simple, stupid and slow bubble-sort, over
* quoteorder and linenumber simultaneously... */
for (i = 0; i < NumberOfLines; i++)
{
for (j = 0; j <= i; j++)
{
if (line[i].quoteorder > line[j].quoteorder) SwitchLines(i, j);
if ((line[i].quoteorder == line[j].quoteorder) &&
(line[i].linenumber < line[j].linenumber)) SwitchLines(i, j);
}
}
}
int main() /* This is self-explanatory, I hope... :-) */
{
ReadTheMessage();
AnalyzeTheMessage();
if (!interspersed) SortTheMessage();
WriteTheMessage();
}
The problem with this program is that it only makes an email look nice if you copy the email into a text file and then run the program on it. I wanted something with seamless integration into an email client. So naturally I set out to make a Thunderbird extension. The first lesson that became painfully clear is that Javascript is slow, even if you avoid jQuery. Sorting replies involves some very standard string manipulation but everything had to be done with specialized functions. Looping through a string (even a short string with only 1000 or so characters) gave this familiar message:
Doing this with real emails instead of test strings also proved a bit cumbersome. Web browsers all follow the same Document Object Model which makes it easy to manipulate a webpage. To get to the body of a webpage, you simly call document.getElementsByTagName("body"); If you do the same thing in Thunderbird however, you run into a lot of unexpected elements because there is no standard for how to display emails instead of web pages in a web browser layout engine. The messages you could possibly view along with parts of Thunderbird's own interface are stored in an object hierarchy that Mozilla just made up. This took some trial and error but I eventually figured out that the equivalent method to call is document.getElementById("messagepane").contentDocument.getElementsByTagName("body");
Accessing the body this way, it became clear that it was laid out using blockquotes. Messages sent by another Thunderbird user are wrapped in <blockquote type="cite">. Traditional, text-based email clients signify quoting with the ">" symbol which the short program above is designed to use. For the most part, Thunderbird catches these and turns them into blockquotes as well. However, apart from the Mozilla blockquote, there is another type of blockquote that must be moved around: <blockquote class="gmail_quote">. Eventually I decided to just turn all Gmail blockquotes into Mozilla blockquotes as well to keep the style uniform. After working on this and a few other nitpicks, I released the extension as Posting Style Enforcer in case people want to blame the program for policing their posts. For people who have opinions opposite to mine, I included an option that reverses the sorting order and enforces top posting.
There is one type of email that will definitely not work with my addon - something sent from Hotmail. Hotmail does not use blockquotes, ">" or any other symbols for quoting. It simply sticks a bunch of blank lines underneath the new message and appends the old one. Separating paragraphs with multiple blank lines is something people do from time to time even when they aren't quoting anything so there is not much I can do about this. Perhaps it is clear in a discussion with only Hotmail users but Thunderbird users who want proper quoting already know that Hotmail sucks.