| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Text; |
|---|
| 4 | using System.Diagnostics; |
|---|
| 5 | |
|---|
| 6 | using agsXMPP; |
|---|
| 7 | using agsXMPP.Xml.Dom; |
|---|
| 8 | |
|---|
| 9 | namespace Misuzilla.Applications.TwitterIrcGateway |
|---|
| 10 | { |
|---|
| 11 | public class TwitterIMService : IDisposable |
|---|
| 12 | { |
|---|
| 13 | private XmppClientConnection _xmppConnection; |
|---|
| 14 | public static String ServiceSender = "twitter@twitter.com"; |
|---|
| 15 | |
|---|
| 16 | public event EventHandler<StatusUpdateReceivedEventArgs> StatusUpdateReceived; |
|---|
| 17 | public event EventHandler Logined; |
|---|
| 18 | public event EventHandler<ErrorEventArgs> ErrorHandled; |
|---|
| 19 | public event EventHandler<ErrorEventArgs> SocketErrorHandled; |
|---|
| 20 | public event EventHandler Closed; |
|---|
| 21 | public event EventHandler AuthErrored; |
|---|
| 22 | |
|---|
| 23 | public TwitterIMService(String connectServerName, String serverName, String userName, String password) |
|---|
| 24 | { |
|---|
| 25 | _xmppConnection = new XmppClientConnection |
|---|
| 26 | { |
|---|
| 27 | ConnectServer = connectServerName, |
|---|
| 28 | Server = serverName, |
|---|
| 29 | Username = userName, |
|---|
| 30 | Password = password, |
|---|
| 31 | }; |
|---|
| 32 | _xmppConnection.OnXmppConnectionStateChanged += new XmppConnectionStateHandler(_xmppConnection_OnXmppConnectionStateChanged); |
|---|
| 33 | _xmppConnection.OnClose += new ObjectHandler(_xmppConnection_OnClose); |
|---|
| 34 | _xmppConnection.OnLogin += new ObjectHandler(xmppConnection_OnLogin); |
|---|
| 35 | _xmppConnection.OnAuthError += new XmppElementHandler(xmppConnection_OnAuthError); |
|---|
| 36 | _xmppConnection.OnError += new ErrorHandler(xmppConnection_OnError); |
|---|
| 37 | _xmppConnection.OnMessage += new agsXMPP.protocol.client.MessageHandler(xmppConnection_OnMessage); |
|---|
| 38 | _xmppConnection.OnSocketError += new ErrorHandler(xmppConnection_OnSocketError); |
|---|
| 39 | _xmppConnection.OnStreamError += new XmppElementHandler(xmppConnection_OnStreamError); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | void _xmppConnection_OnXmppConnectionStateChanged(object sender, XmppConnectionState state) |
|---|
| 43 | { |
|---|
| 44 | Trace.WriteLine(String.Format("XMPPConnection State: {0}", state)); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public void Open() |
|---|
| 48 | { |
|---|
| 49 | _xmppConnection.Open(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public void Close() |
|---|
| 53 | { |
|---|
| 54 | _xmppConnection.Close(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | #region XMPP Events |
|---|
| 58 | void xmppConnection_OnMessage(object sender, agsXMPP.protocol.client.Message msg) |
|---|
| 59 | { |
|---|
| 60 | //Trace.WriteLine(msg.ToString()); |
|---|
| 61 | if (msg.From.Bare != ServiceSender) |
|---|
| 62 | return; |
|---|
| 63 | |
|---|
| 64 | Element entryE = msg.SelectSingleElement("entry"); |
|---|
| 65 | Element sourceE = entryE.SelectSingleElement("source"); |
|---|
| 66 | Element authorE = sourceE.SelectSingleElement("author"); |
|---|
| 67 | |
|---|
| 68 | User user = new User |
|---|
| 69 | { |
|---|
| 70 | Description = authorE.GetTag("description"), |
|---|
| 71 | Id = authorE.GetTagInt("twitter_id"), |
|---|
| 72 | Location = authorE.GetTag("location"), |
|---|
| 73 | Protected = authorE.GetTagBool("protected"), |
|---|
| 74 | Name = authorE.GetTag("name"), |
|---|
| 75 | ScreenName = authorE.GetTag("screen_name"), |
|---|
| 76 | Url = authorE.GetTag("url"), |
|---|
| 77 | ProfileImageUrl = authorE.GetTag("profile_image_url") |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | String body = msg.Body; |
|---|
| 81 | if (body.IndexOf(": ") > -1) |
|---|
| 82 | body = body.Substring(body.IndexOf(": ") + 2); |
|---|
| 83 | |
|---|
| 84 | Status status = new Status |
|---|
| 85 | { |
|---|
| 86 | CreatedAt = DateTime.Parse(entryE.GetTag("published")), |
|---|
| 87 | Text = body, |
|---|
| 88 | User = user, |
|---|
| 89 | Id = entryE.GetTagInt((entryE.GetTagInt("twitter_id") == 0 ? "status_id" : "twitter_id")) // HACK: 何故かどっちかでくる |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | OnStatusUpdateReceived(status); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void _xmppConnection_OnClose(object sender) |
|---|
| 96 | { |
|---|
| 97 | OnClosed(); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | void xmppConnection_OnLogin(object sender) |
|---|
| 101 | { |
|---|
| 102 | OnLogined(); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | void xmppConnection_OnAuthError(object sender, Element e) |
|---|
| 106 | { |
|---|
| 107 | OnAuthErrored(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void xmppConnection_OnSocketError(object sender, Exception ex) |
|---|
| 111 | { |
|---|
| 112 | OnSocketErrorHandled(ex); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void xmppConnection_OnStreamError(object sender, Element e) |
|---|
| 116 | { |
|---|
| 117 | Trace.WriteLine(String.Format("XMPPConnection OnStreamError: {0}", e)); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void xmppConnection_OnError(object sender, Exception ex) |
|---|
| 121 | { |
|---|
| 122 | OnErrorHandled(ex); |
|---|
| 123 | } |
|---|
| 124 | #endregion |
|---|
| 125 | |
|---|
| 126 | #region Events |
|---|
| 127 | private void OnLogined() |
|---|
| 128 | { |
|---|
| 129 | if (Logined != null) |
|---|
| 130 | Logined(this, EventArgs.Empty); |
|---|
| 131 | } |
|---|
| 132 | private void OnErrorHandled(Exception ex) |
|---|
| 133 | { |
|---|
| 134 | if (ErrorHandled != null) |
|---|
| 135 | ErrorHandled(this, new ErrorEventArgs(ex)); |
|---|
| 136 | } |
|---|
| 137 | private void OnSocketErrorHandled(Exception ex) |
|---|
| 138 | { |
|---|
| 139 | if (SocketErrorHandled != null) |
|---|
| 140 | SocketErrorHandled(this, new ErrorEventArgs(ex)); |
|---|
| 141 | } |
|---|
| 142 | private void OnAuthErrored() |
|---|
| 143 | { |
|---|
| 144 | if (AuthErrored != null) |
|---|
| 145 | AuthErrored(this, EventArgs.Empty); |
|---|
| 146 | } |
|---|
| 147 | private void OnStatusUpdateReceived(Status status) |
|---|
| 148 | { |
|---|
| 149 | if (StatusUpdateReceived != null) |
|---|
| 150 | StatusUpdateReceived(this, new StatusUpdateReceivedEventArgs(status)); |
|---|
| 151 | } |
|---|
| 152 | private void OnClosed() |
|---|
| 153 | { |
|---|
| 154 | if (Closed != null) |
|---|
| 155 | Closed(this, EventArgs.Empty); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | public class ErrorEventArgs : EventArgs |
|---|
| 159 | { |
|---|
| 160 | public Exception Exception { get; set; } |
|---|
| 161 | |
|---|
| 162 | public ErrorEventArgs(Exception ex) |
|---|
| 163 | { |
|---|
| 164 | this.Exception = ex; |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | public class StatusUpdateReceivedEventArgs : EventArgs |
|---|
| 169 | { |
|---|
| 170 | public Status Status { get; set; } |
|---|
| 171 | |
|---|
| 172 | public StatusUpdateReceivedEventArgs(Status status) |
|---|
| 173 | { |
|---|
| 174 | this.Status = status; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | #endregion |
|---|
| 178 | |
|---|
| 179 | #region IDisposable Members |
|---|
| 180 | |
|---|
| 181 | public void Dispose() |
|---|
| 182 | { |
|---|
| 183 | Close(); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | #endregion |
|---|
| 187 | } |
|---|
| 188 | } |
|---|