<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Advogato blog for testing</title>
    <link>http://www.advogato.org/person/testing/</link>
    <description>Advogato blog for testing</description>
    <language>en-us</language>
    <generator>mod_virgule</generator>
    <pubDate>Wed, 19 Jun 2013 09:31:10 GMT</pubDate>
    <item>
      <pubDate>Thu, 21 Dec 2000 02:56:06 GMT</pubDate>
      <title>21 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=14</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=14</guid>
      <description>foo
</description>
    </item>
    <item>
      <pubDate>Fri, 15 Dec 2000 03:03:58 GMT</pubDate>
      <title>15 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=13</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=13</guid>
      <description>Testy is quite disappointed that lettuce-head is president.</description>
    </item>
    <item>
      <pubDate>Thu, 14 Dec 2000 00:50:06 GMT</pubDate>
      <title>14 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=12</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=12</guid>
      <description>&lt;pre&gt;#!/usr/bin/python

import getopt, sys, httplib, string, re, getpass, urllib

def usage():
  print "%s -p (--post-entry) &amp;lt;file&amp;gt;" % (sys.argv[0])

def extract_pass(header):
  '''
  Grab encrypted password from a "Set-Cookie" header.
  Example header:
    "Set-Cookie: id=jordan:32f/XCcPSGAMzsS5/ky/; path=/; Expires=Monday"
  Result:
    "32f/XCcPSGAMzsS5/ky/"
  '''
  # Grab the Set-Cookie: line.
  header = string.split(header, '\n')[2]
  
  return string.split(string.split(header, ':')[2], ';')[0]

def check_response(code, header):
  '''
  Check if HTTP headers were accepted by server.
  '''
  if code != 200:
    print 'Error sending headers -- %s' % (header)
    sys.exit()

def check_auth(header):
  '''
  Check if authentication failed.
  '''
  # Bad login or password.
  if string.find(header, 'Set-Cookie') == -1:
    print 'Error authenticating -- %s' % (header)
    sys.exit()

def extract_entry_number(html):
  '''
  Return the diary entry number from raw HTML code.
  '''
  # Find the line 'name=key value=N' where N is
  # the diary entry number and save it.
  entry_num = ''
  
  for line in html:
    if string.find(line, 'name=key value=') != -1:
      # Extract the number from the string
      entry_num = re.sub('\D', '', line)
      break

  return entry_num

def get_entry_number(userid, password):
  '''
  Get the diary entry number for "userid."  This number is
  necessary in order to post new diary entries. 
  '''
  hlink = httplib.HTTP('www.advogato.org')
  hlink.putrequest('GET', '/diary/ HTTP/1.0')
  hlink.putheader('Host', 'www.advogato.org')
  hlink.putheader('Cookie2', '$Version="1"')
  hlink.putheader('Cookie', 'id=%s:%s' % (userid, password))
  hlink.endheaders()
  errcode, errmsg, header = hlink.getreply()
  check_response(errcode, header)

  # Grab HTML.
  raw_html = string.split(hlink.getfile().read(), '\n')
  hlink.getfile().close()

  entry_num = ''
  entry_num = extract_entry_number(raw_html)
  
  if not entry_num:
    print 'Entry value for %s not found -- %s' % (userid, header)
    sys.exit()

  return entry_num

def get_entry(entry_file):
  '''
  Return the contents of `entry_file`, which contains
  the user`s entry.
  '''
  try:
    file_in = open(entry_file, 'r')
  except:
    print 'Error opening %s' % (entry_file)
    sys.exit()

  # Replace newlines with &lt;p&gt;'s, otherwise the encoded URL
  # will not be accepted.
  # entry = re.sub('\n', '&lt;p&gt;', file_in.read())
  entry = file_in.read()

  return entry

def post_entry(user_info, entry_file):
  '''
  Post `entry_file` to advogato for the user described in `user_info.`
  '''
  userid = user_info[0]
  password = user_info[1]
  urlencoded = urllib.urlencode({'u': userid, 'pass': password})
  hlink = httplib.HTTP('www.advogato.org')
  hlink.putrequest('POST', '/acct/loginsub.html HTTP/1.0')
  hlink.putheader('Host', 'www.advogato.org')
  hlink.putheader('Content-type', 'application/x-www-form-urlencoded')
  hlink.putheader('Content-length', '%d' % len(urlencoded))
  hlink.endheaders()
  # Send encoded URL
  hlink.send(urlencoded)
  errcode, errmsg, header = hlink.getreply()

  # Error response from server (to make sure our requests went
  # through) and user information.
  check_response(errcode, header)
  check_auth(str(header))

  # Extract the encrypted password from header.
  password  = extract_pass(str(header))
  entry_num = get_entry_number(userid, password)
  content = get_entry(entry_file)
  entry = urllib.urlencode({'entry': content, 'post': 'Post', 'key':
                            entry_num})
  hlink2 = httplib.HTTP('www.advogato.org')
  hlink2.putrequest('POST', '/diary/post.html HTTP/1.0')
  hlink2.putheader('Host', 'www.advogato.org')
  hlink2.putheader('Cookie2', '$Version="1"')
  hlink2.putheader('Cookie', 'id=%s:%s' % (userid, password))
  hlink2.putheader('Content-type', 'application/x-www-form-urlencoded')
  hlink2.putheader('Content-length', '%d' % len(entry))
  hlink2.endheaders()
  hlink2.send(entry)
  errcode, errmsg, header = hlink2.getreply()
  check_response(errcode, header)

  print 'Entry \'%s\' posted successfully.' % (entry_file)

def get_user_info():
  '''
  Return the user`s advogato username and password.
  '''
  userid = raw_input('Enter your advogato username: ')
  try:
    password = getpass.getpass('password: ')
  except:
    print '\nError getting password.'
    sys.exit()
  print '\n'

  return [userid, password]
               
def main():
  output = None
  
  try:
    opts, args = getopt.getopt(sys.argv[1:], "p:", ["post-entry="])
  except:
    usage()
    sys.exit()

  for option, argument in opts:
    # Post an entry.
    if option in ("-p", "--post-entry"):
      output = argument
      user_info = get_user_info()
      post_entry(user_info, argument)
      break   # Because it's the only option right now.

  if not output:
    usage()
    sys.exit()

if __name__ == '__main__':
  main()
&lt;/pre&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 14 Dec 2000 00:47:50 GMT</pubDate>
      <title>14 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=11</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=11</guid>
      <description>Hello </description>
    </item>
    <item>
      <pubDate>Thu, 14 Dec 2000 00:46:18 GMT</pubDate>
      <title>14 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=10</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=10</guid>
      <description>Hi.  This is testy.  Today has been a rough day.  jordan's been running me a billion times per second, and kept on messing with my internals.  Arrrg!  I just wish he would leave me be already.  However, I am happy that he made me all pretty and smooth by using urllib.urlencode() instead of using a stupid string hack.  Anyways, later.
</description>
    </item>
    <item>
      <pubDate>Thu, 14 Dec 2000 00:44:09 GMT</pubDate>
      <title>14 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=9</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=9</guid>
      <description>#!/usr/bin/python

import getopt, sys, httplib, string, re, getpass

def usage():
  print "%s -p (--post-entry) &amp;lt;file&amp;gt;" % (sys.argv[0])

def extract_pass(header):
  '''
  Grab encrypted password from a "Set-Cookie" header.
  Example header:
    "Set-Cookie: id=jordan:32f/XCcPSGAMzsS5/ky/; path=/; Expires=Monday"
  Result:
    "32f/XCcPSGAMzsS5/ky/"
  '''
  # Grab the Set-Cookie: line.
  header = string.split(header, '\n')[2]
  
  return string.split(string.split(header, ':')[2], ';')[0]

def check_response(code, header):
  '''
  Check if HTTP headers were accepted by server.
  '''
  if code != 200:
    print 'Error sending headers -- %s' % (header)
    sys.exit()

def check_auth(header):
  '''
  Check if authentication failed.
  '''
  # Bad login or password.
  if string.find(header, 'Set-Cookie') == -1:
    print 'Error authenticating -- %s' % (header)
    sys.exit()

def extract_entry_number(html):
  '''
  Return the diary entry number from raw HTML code.
  '''
  # Find the line 'name=key value=N' where N is
  # the diary entry number and save it.
  entry_num = ''
  
  for line in html:
    if string.find(line, 'name=key value=') != -1:
      # Extract the number from the string
      entry_num = re.sub('\D', '', line)
      break

  return entry_num

def get_entry_number(userid, password):
  '''
  Get the diary entry number for "userid."  This number is
  necessary in order to post new diary entries. 
  '''
  hlink = httplib.HTTP('www.advogato.org')
  hlink.putrequest('GET', '/diary/ HTTP/1.0')
  hlink.putheader('Host', 'www.advogato.org')
  hlink.putheader('Cookie2', '$Version="1"')
  hlink.putheader('Cookie', 'id=%s:%s' % (userid, password))
  hlink.endheaders()
  errcode, errmsg, header = hlink.getreply()
  check_response(errcode, header)

  # Grab HTML.
  raw_html = string.split(hlink.getfile().read(), '\n')
  hlink.getfile().close()

  entry_num = ''
  entry_num = extract_entry_number(raw_html)
  
  if not entry_num:
    print 'Entry value for %s not found -- %s' % (userid, header)
    sys.exit()

  return entry_num

def get_entry(entry_file):
  '''
  Return the contents of `entry_file`, which contains
  the user`s entry.
  '''
  try:
    file_in = open(entry_file, 'r')
  except:
    print 'Error opening %s' % (entry_file)
    sys.exit()

  # Replace newlines with &lt;p&gt;'s, otherwise the encoded URL
  # will not be accepted.
  entry = re.sub('\n', '&lt;p&gt;', file_in.read())
  
  return entry

def post_entry(user_info, entry_file):
  '''
  Post entry to advogato.
  '''
  userid = user_info[0]
  password = user_info[1]
  urlencoded = 'u=' + userid + '&amp;amp;pass=' + password
  
  hlink = httplib.HTTP('www.advogato.org')
  hlink.putrequest('POST', '/acct/loginsub.html HTTP/1.0')
  hlink.putheader('Host', 'www.advogato.org')
  hlink.putheader('Content-type', 'application/x-www-form-urlencoded')
  hlink.putheader('Content-length', '%d\n\n%s' % (len(urlencoded),
                                                  urlencoded))
  hlink.endheaders()
  errcode, errmsg, header = hlink.getreply()

  # Error response from server (to make sure our requests went
  # through) and user information.
  check_response(errcode, header)
  check_auth(str(header))

  # Extract the encrypted password from header.
  password = extract_pass(str(header))
  entry_num = get_entry_number(userid, password)
  content = get_entry(entry_file)
  entry = 'entry=' + content + '&amp;amp;post=Post&amp;amp;key=' + str(entry_num)
  
  hlink = httplib.HTTP('www.advogato.org')
  hlink.putrequest('POST', '/diary/post.html HTTP/1.0')
  hlink.putheader('Host', 'www.advogato.org')
  hlink.putheader('Cookie2', '$Version="1"')
  hlink.putheader('Cookie', 'id=%s:%s' % (userid, password))
  hlink.putheader('Content-type', 'application/x-www-form-urlencoded')
  hlink.putheader('Content-length', '%d\n\n%s' % (len(entry), entry))
  hlink.endheaders()
  errcode, errmsg, header = hlink.getreply()
  check_response(errcode, header)

  print 'Entry \'%s\' posted successfully.' % (entry_file)

def get_user_info():
  '''
  Return the user`s advogato username and password.
  '''
  userid = raw_input('Enter your advogato username: ')
  password = getpass.getpass('password: ')
  print '\n'

  return [userid, password]
               
def main():
  output = None
  
  try:
    opts, args = getopt.getopt(sys.argv[1:], "p:", ["post-entry="])
  except:
    usage()
    sys.exit()

  for option, argument in opts:
    # Post an entry.
    if option in ("-p", "--post-entry"):
      output = argument
      user_info = get_user_info()
      post_entry(user_info, argument)
      break   # Because it's the only option right now.

  if not output:
    usage()
    sys.exit()

if __name__ == "__main__":
  main()
</description>
    </item>
    <item>
      <pubDate>Thu, 14 Dec 2000 00:33:30 GMT</pubDate>
      <title>14 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=8</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=8</guid>
      <description>f..</description>
    </item>
    <item>
      <pubDate>Thu, 14 Dec 2000 00:33:10 GMT</pubDate>
      <title>14 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=7</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=7</guid>
      <description>f..</description>
    </item>
    <item>
      <pubDate>Thu, 14 Dec 2000 00:22:55 GMT</pubDate>
      <title>14 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=6</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=6</guid>
      <description>hello</description>
    </item>
    <item>
      <pubDate>Tue, 12 Dec 2000 01:35:50 GMT</pubDate>
      <title>12 Dec 2000</title>
      <link>http://www.advogato.org/person/testing/diary.html?start=5</link>
      <guid>http://www.advogato.org/person/testing/diary.html?start=5</guid>
      <description>Zup yo!</description>
    </item>
  </channel>
</rss>
