| 1 | // $Id$
|
|---|
| 2 | using System;
|
|---|
| 3 | using System.Collections.Generic;
|
|---|
| 4 | using System.Text;
|
|---|
| 5 | using Microsoft.Build.Framework;
|
|---|
| 6 | using System.Net;
|
|---|
| 7 |
|
|---|
| 8 | namespace Misuzilla.Build.Tasks.Twitter
|
|---|
| 9 | {
|
|---|
| 10 | public class TwitterUpdateStatus : Microsoft.Build.Utilities.Task
|
|---|
| 11 | {
|
|---|
| 12 | private String _message;
|
|---|
| 13 | private String _userName;
|
|---|
| 14 | private String _password;
|
|---|
| 15 |
|
|---|
| 16 | [Required]
|
|---|
| 17 | public String Message
|
|---|
| 18 | {
|
|---|
| 19 | get { return _message; }
|
|---|
| 20 | set { _message = value; }
|
|---|
| 21 | }
|
|---|
| 22 | [Required]
|
|---|
| 23 | public String UserName
|
|---|
| 24 | {
|
|---|
| 25 | get { return _userName; }
|
|---|
| 26 | set { _userName = value; }
|
|---|
| 27 | }
|
|---|
| 28 | [Required]
|
|---|
| 29 | public String Password
|
|---|
| 30 | {
|
|---|
| 31 | get { return _password; }
|
|---|
| 32 | set { _password = value; }
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | public override bool Execute()
|
|---|
| 36 | {
|
|---|
| 37 | try
|
|---|
| 38 | {
|
|---|
| 39 | Log.LogMessage("Updating Twitter Status (User:{0})", _userName);
|
|---|
| 40 | using (PreAuthenticatedWebClient webClient = new PreAuthenticatedWebClient(_userName, _password))
|
|---|
| 41 | {
|
|---|
| 42 | String postUri = String.Format("http://twitter.com/statuses/update.xml?status={0}&source=Misuzilla.Build.Tasks.Twitter", UrlEncode(Message));
|
|---|
| 43 | webClient.UploadData(postUri, new Byte[] { 1 });
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | catch (WebException we)
|
|---|
| 47 | {
|
|---|
| 48 | Log.LogWarning("Twitter Status update failed.");
|
|---|
| 49 | Log.LogWarningFromException(we);
|
|---|
| 50 | return false;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | return true;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /// <summary>
|
|---|
| 57 | /// ������RL�G���R�[�h���܂��B
|
|---|
| 58 | /// </summary>
|
|---|
| 59 | /// <param name="s">URL�G���R�[�h���镶����aram>
|
|---|
| 60 | /// <returns>URL�G���R�[�h���ꂽ������eturns>
|
|---|
| 61 | public static String UrlEncode(String s)
|
|---|
| 62 | {
|
|---|
| 63 | StringBuilder sb = new StringBuilder();
|
|---|
| 64 | Byte[] bytes = Encoding.UTF8.GetBytes(s);
|
|---|
| 65 | foreach (Byte b in bytes)
|
|---|
| 66 | {
|
|---|
| 67 | sb.AppendFormat("%{0:x2}", b);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | return sb.ToString();
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | internal class PreAuthenticatedWebClient : WebClient
|
|---|
| 75 | {
|
|---|
| 76 | public PreAuthenticatedWebClient(String userName, String password)
|
|---|
| 77 | : base()
|
|---|
| 78 | {
|
|---|
| 79 | CredentialCache _credentialCache = new CredentialCache();
|
|---|
| 80 | _credentialCache.Add(new Uri("http://twitter.com"), "Basic", new NetworkCredential(userName, password));
|
|---|
| 81 | Credentials = _credentialCache;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | protected override WebRequest GetWebRequest(Uri address)
|
|---|
| 85 | {
|
|---|
| 86 | // ���̃A�v���P�[�V������HttpWebReqeust �ȊO�����邱�Ƃ͂Ȃ�
|
|---|
| 87 | HttpWebRequest webRequest = base.GetWebRequest(address) as HttpWebRequest;
|
|---|
| 88 | webRequest.PreAuthenticate = true;
|
|---|
| 89 | webRequest.Accept = "text/xml, application/xml";
|
|---|
| 90 | webRequest.UserAgent = String.Format("{0}/{1}", "Misuzilla.Build.Tasks.Twitter", GetType().Assembly.GetName().Version);
|
|---|
| 91 |
|
|---|
| 92 | return webRequest;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|