~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to Twitter/src/Twitter.cs

  • Committer: djsiegel at gmail
  • Date: 2007-11-07 19:51:21 UTC
  • Revision ID: djsiegel@gmail.com-20071107195121-v1niznt43hkai3o1
Added some template files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Twitter.cs
2
 
 *
3
 
 * This is a simple action for posting to twitter using GNOME DO.
4
 
 *
5
 
 * GNOME Do is the legal property of its developers. Please refer to the
6
 
 * COPYRIGHT file distributed with this source distribution.
7
 
 *
8
 
 * This program is free software: you can redistribute it and/or modify
9
 
 * it under the terms of the GNU General Public License as published by
10
 
 * the Free Software Foundation, either version 3 of the License, or
11
 
 * (at your option) any later version.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
 */
21
 
 
22
 
using System;
23
 
using System.Collections.Generic;
24
 
using System.IO;
25
 
using System.Net;
26
 
 
27
 
using Mono;
28
 
using Mono.Unix;
29
 
 
30
 
using GConf;
31
 
 
32
 
using NDesk.DBus;
33
 
using org.freedesktop;
34
 
using org.freedesktop.DBus;
35
 
 
36
 
using Do.Universe;
37
 
 
38
 
namespace Do.Twitter
39
 
{
40
 
 
41
 
        public class Twitter : IAction
42
 
        {
43
 
                
44
 
                public string Name {
45
 
                        get {
46
 
                                return "Tweet";
47
 
                        }
48
 
                }
49
 
                
50
 
                public string Description {
51
 
                        get {
52
 
                                return "Update Twitter Status";
53
 
                        }
54
 
                }
55
 
                
56
 
                public string Icon {
57
 
                        get {
58
 
                                return "twitter-icon.png@Twitter";
59
 
                        }
60
 
                }
61
 
                
62
 
                public Type[] SupportedItemTypes {
63
 
                        get {
64
 
                                return new Type[] {
65
 
                                        typeof (ITextItem),
66
 
                                };
67
 
                        }
68
 
                }
69
 
 
70
 
                public bool SupportsItem (IItem item) 
71
 
                {
72
 
 
73
 
                        return true;
74
 
 
75
 
                }
76
 
 
77
 
 
78
 
 
79
 
 
80
 
                public IItem[] Perform (IItem[] items, IItem[] modItems)
81
 
                {
82
 
 
83
 
                        String tweet = EscapeTweet ((items [0] as ITextItem).Text);
84
 
 
85
 
                        String url = "http://twitter.com/statuses/update.json?status=" + tweet;
86
 
                        HttpWebRequest request = WebRequest.Create (url) as HttpWebRequest;
87
 
 
88
 
                        request.Method = "POST";
89
 
 
90
 
                        GConf.Client gconf = new GConf.Client ();
91
 
 
92
 
                        if (!SetRequestCredentials (request, gconf)) return null;
93
 
                        if (!SetRequestProxy (request, gconf)) return null;
94
 
                        
95
 
                        try {
96
 
 
97
 
                                request.GetResponse ();
98
 
                                SendNotification ("Tweet Successful", "Successfully posted tweet '" 
99
 
                                                + (items [0] as ITextItem).Text
100
 
                                                + "' to Twitter.");
101
 
 
102
 
 
103
 
                        } catch (Exception e) {
104
 
 
105
 
                                Console.WriteLine (e.ToString ());
106
 
                                SendNotification ("Tweet Failed", "Unable to post tweet. Check your login "
107
 
                                                + "settings (/apps/gnome-do/plugins/twitter). If you are "
108
 
                                                + "behind a proxy, also make sure that the settings in "
109
 
                                                + "/system/http_proxy are correct.\n\nDetails:\n" 
110
 
                                                + e.ToString ());
111
 
                        }
112
 
                        
113
 
                        return null;
114
 
                }
115
 
                
116
 
                public Type[] SupportedModifierItemTypes {
117
 
                        get { return new Type[] {}; }
118
 
                }
119
 
 
120
 
                public bool ModifierItemsOptional {
121
 
                        get { return true; }
122
 
                }
123
 
                                
124
 
                public bool SupportsModifierItemForItems (IItem[] items, IItem modItem)
125
 
                {
126
 
                        return true;
127
 
                }
128
 
                
129
 
                public IItem[] DynamicModifierItemsForItem (IItem item)
130
 
                {
131
 
                        return null;
132
 
                }
133
 
              
134
 
 
135
 
 
136
 
 
137
 
 
138
 
 
139
 
                private string EscapeTweet (string tweet) {
140
 
 
141
 
                        String retstring = tweet.Replace ("%", "%25")
142
 
                                                .Replace ("#", "%23")
143
 
                                                .Replace ("{", "%7B")
144
 
                                                .Replace ("}", "%7D")
145
 
                                                .Replace ("|", "%7C")
146
 
                                                .Replace ("\\", "%5C")
147
 
                                                .Replace ("^", "%5E")
148
 
                                                .Replace ("~", "%7E")
149
 
                                                .Replace ("[", "%5B")
150
 
                                                .Replace ("]", "%5D")
151
 
                                                .Replace ("`", "%60")
152
 
                                                .Replace (";", "%3B")
153
 
                                                .Replace (")", "%29")
154
 
                                                .Replace ("/", "%2F");
155
 
 
156
 
                               retstring = tweet.Replace ("(", "%28")
157
 
                                                .Replace ("?", "%3F")
158
 
                                                .Replace (":", "%3A")
159
 
                                                .Replace ("@", "%40")
160
 
                                                .Replace ("=", "%3D")
161
 
                                                .Replace ("&", "%26")
162
 
                                                .Replace ("$", "%24")
163
 
                                                .Replace ("\"", "%22")
164
 
                                                .Replace ("'", "%27")
165
 
                                                .Replace ("*", "%2A")
166
 
                                                .Replace ("+", "%2B")
167
 
                                                .Replace ("!", "%21")
168
 
                                                .Replace (" ", "+");
169
 
 
170
 
                        return retstring;
171
 
 
172
 
                }
173
 
 
174
 
                private bool SetRequestCredentials (HttpWebRequest request, GConf.Client gconf)
175
 
                {
176
 
 
177
 
                        String username;
178
 
                        String password;
179
 
 
180
 
                        try {
181
 
                                username = gconf.Get ("/apps/gnome-do/plugins/twitter/username") as String;
182
 
                                password = gconf.Get ("/apps/gnome-do/plugins/twitter/password") as String;
183
 
                        }
184
 
                        catch (GConf.NoSuchKeyException) {
185
 
                                gconf.Set ("/apps/gnome-do/plugins/twitter/username", "");
186
 
                                gconf.Set ("/apps/gnome-do/plugins/twitter/password", "");
187
 
                                SendNotification ("GConf keys created", "GConf keys for storing your Twitter "
188
 
                                                                      + "login information have been created "
189
 
                                                                      + "in /apps/gnome-do/plugins/twitter/\n"
190
 
                                                                      + "Please set your username and password "
191
 
                                                                      + "in order to post tweets");
192
 
                                return false;
193
 
                        }
194
 
 
195
 
                        request.Credentials = new NetworkCredential (username, password);
196
 
 
197
 
                        return true;
198
 
 
199
 
                }
200
 
 
201
 
                private bool SetRequestProxy (HttpWebRequest request, GConf.Client gconf)
202
 
                {
203
 
 
204
 
                        bool use_proxy;
205
 
 
206
 
                        try {
207
 
                                use_proxy = (bool) gconf.Get ("/system/http_proxy/use_http_proxy");
208
 
                        }
209
 
                        catch (GConf.NoSuchKeyException) {
210
 
                                use_proxy = false;
211
 
                        }
212
 
 
213
 
                        if (use_proxy) {
214
 
 
215
 
                                String phost;
216
 
                                int pport;
217
 
                                String[] pignore;
218
 
                                bool pauth;
219
 
                                
220
 
                                try {
221
 
                                
222
 
                                        phost = gconf.Get ("/system/http_proxy/host") as String;
223
 
                                        pport = (int) gconf.Get ("/system/http_proxy/port");
224
 
                                        pignore = gconf.Get ("/system/http_proxy/ignore_hosts") as String[];
225
 
                                        pauth = (bool) gconf.Get ("/system/http_proxy/use_authentication");
226
 
                                
227
 
                                }
228
 
                                catch (GConf.NoSuchKeyException) {
229
 
 
230
 
                                        SendNotification ("Unable to load proxy settings", 
231
 
                                                          "You have specified in GConf that "
232
 
                                                        + "you are using a proxy server, but no proxy "
233
 
                                                        + "settings could be found. Please check "
234
 
                                                        + "/system/http_proxy/ to make sure the "
235
 
                                                        + "appropriate values are set.");
236
 
 
237
 
                                        return false;
238
 
 
239
 
                                }
240
 
 
241
 
                                WebProxy proxy = new WebProxy (phost, pport);
242
 
 
243
 
                                proxy.BypassList = pignore;
244
 
 
245
 
                                if (pauth) {
246
 
 
247
 
                                        String pauser, papass;
248
 
 
249
 
                                        try {
250
 
 
251
 
                                                pauser = gconf.Get ("/system/http_proxy/authentication_user") as String;
252
 
                                                papass = gconf.Get ("/system/http_proxy/authentication_password") as String;
253
 
 
254
 
                                        }
255
 
                                        catch (GConf.NoSuchKeyException) {
256
 
                                                
257
 
                                                SendNotification ("Unable to load proxy settings",
258
 
                                                                  "You have specified in GConf that "
259
 
                                                                + "you are using a proxy server, but no proxy "
260
 
                                                                + "settings could be found. Please check "
261
 
                                                                + "/system/http_proxy/ to make sure the "
262
 
                                                                + "appropriate values are set.");
263
 
                                                return false;
264
 
 
265
 
                                        }
266
 
 
267
 
                                        proxy.Credentials = new NetworkCredential (pauser, papass);
268
 
 
269
 
                                }
270
 
 
271
 
                                request.Proxy = proxy;
272
 
                        }
273
 
 
274
 
                        return true;
275
 
                }
276
 
                
277
 
                private void SendNotification (string title, string message)
278
 
                {
279
 
 
280
 
                        Bus bus = Bus.Session;
281
 
 
282
 
                        Notifications nf = bus.GetObject<Notifications> ("org.freedesktop.Notifications", new ObjectPath ("/org/freedesktop/Notifications"));
283
 
 
284
 
                        Dictionary <string,object> hints = new Dictionary <string,object> ();
285
 
                        
286
 
                        nf.Notify (title, 0, "", title, message, new string[0], hints, -1);
287
 
                }
288
 
        }
289
 
}