~iwarford/do-plugins/fart-plugin-fwiw

« back to all changes in this revision

Viewing changes to Woof/src/Woof.cs

  • Committer: Jason Jones
  • Date: 2008-12-24 04:45:02 UTC
  • mfrom: (335.1.9 do-plugins)
  • Revision ID: jasonedwardjones@gmail.com-20081224044502-ra56ym06cp1iqs7t
MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Woof.cs
 
2
//
 
3
// Parts of the code were copy/pasted from the Pidgin plugin (in Pidgin.cs).
 
4
//
 
5
// This program is free software; you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation; either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program; if not, write to the Free Software
 
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 
 
19
using System;
 
20
using System.Diagnostics;
 
21
using System.IO;
 
22
using System.Collections.Generic;
 
23
using System.Reflection;
 
24
using System.Threading;
 
25
using NDesk.DBus;
 
26
using org.freedesktop.DBus;
 
27
 
 
28
using Do.Universe;
 
29
 
 
30
namespace Woof
 
31
{
 
32
 
 
33
        public static class Pidgin {
 
34
                const string BUS_NAME = "im.pidgin.purple.PurpleService";
 
35
                const string OBJECT_PATH = "/im/pidgin/purple/PurpleObject";
 
36
                public const uint PURPLE_CONV_TYPE_IM = 1;
 
37
                public const uint PURPLE_CONV_TYPE_CHAT = 2;
 
38
 
 
39
                [Interface ("im.pidgin.purple.PurpleInterface")]
 
40
                        public interface IPidgin {
 
41
                                int[] PurpleAccountsGetAllActive ();
 
42
                                int PurpleFindBuddy (int accound_id, string screenname);
 
43
                                bool PurpleAccountIsConnected (int account_id);
 
44
                                int PurpleAccountsFindConnected (string account, string proto);
 
45
                                bool PurpleBuddyIsOnline (int buddy_id);
 
46
                                int PurpleConversationNew (uint conv_type,
 
47
                                                int account_id,
 
48
                                                string screenname);
 
49
                                void PurpleConversationPresent (int conversation_id);
 
50
                                int PurpleConvIm (int conv_id);
 
51
                                void PurpleConvImSend (int im_id, string message);
 
52
                        }
 
53
 
 
54
                public static IPidgin FindInstance ()
 
55
                {
 
56
                        if(!Bus.Session.NameHasOwner (BUS_NAME)) {
 
57
                                Bus.Session.StartServiceByName (BUS_NAME);
 
58
                                System.Threading.Thread.Sleep (5000);
 
59
                                if(!Bus.Session.NameHasOwner (BUS_NAME))
 
60
                                        throw new Exception (String.Format ("Name {0} has no owner.", BUS_NAME));
 
61
                        }
 
62
                        return Bus.Session.GetObject<IPidgin> (BUS_NAME, new ObjectPath (OBJECT_PATH));
 
63
                }
 
64
 
 
65
                private static int[] ConnectedAccounts {
 
66
                        get {
 
67
                                List<int> connected;
 
68
                                IPidgin prpl;
 
69
 
 
70
                                prpl = FindInstance ();
 
71
                                connected = new List<int> ();
 
72
                                try {
 
73
                                        foreach (int account in prpl.PurpleAccountsGetAllActive ()) {
 
74
                                                if (prpl.PurpleAccountIsConnected (account))
 
75
                                                        connected.Add (account);
 
76
                                        }
 
77
                                } catch { }
 
78
                                return connected.ToArray ();
 
79
                        }
 
80
                }
 
81
 
 
82
                public static bool BuddyIsOnline (string name)
 
83
                {
 
84
                        int account;
 
85
                        return GetBuddyIsOnlineAndAccount (name, out account);   
 
86
                }
 
87
 
 
88
                public static bool GetBuddyIsOnlineAndAccount (string name, out int account_out)
 
89
                {
 
90
                        IPidgin prpl;
 
91
                        int buddy;
 
92
 
 
93
                        prpl = FindInstance ();
 
94
                        try {
 
95
                                foreach (int account in ConnectedAccounts) {
 
96
                                        buddy = prpl.PurpleFindBuddy (account, name);
 
97
                                        if (prpl.PurpleBuddyIsOnline (buddy)) {
 
98
                                                account_out = account;
 
99
                                                return true;
 
100
                                        }
 
101
                                }
 
102
                        } catch { }
 
103
                        account_out = -1;
 
104
                        return false;
 
105
                }
 
106
 
 
107
                public static void OpenConversationWithBuddy (string name)
 
108
                {
 
109
                        IPidgin prpl;
 
110
                        int account, conversation;
 
111
 
 
112
                        prpl = FindInstance ();
 
113
                        try {
 
114
                                GetBuddyIsOnlineAndAccount (name, out account);
 
115
                                if (account == -1)
 
116
                                        account = prpl.PurpleAccountsFindConnected ("", "");
 
117
                                conversation = prpl.PurpleConversationNew (PURPLE_CONV_TYPE_IM,
 
118
                                                account, name);
 
119
                                prpl.PurpleConversationPresent (conversation);
 
120
                        } catch (Exception e) {
 
121
                                Console.Error.WriteLine ("Could not create new Pidgin conversation: {0}", e.Message);
 
122
                        }
 
123
                }
 
124
        }
 
125
 
 
126
        public class WoofServer {
 
127
                private string server_url; 
 
128
                private string file_path;
 
129
                private string screen_name;
 
130
                private int timeout;
 
131
 
 
132
                public WoofServer (string screen_name)
 
133
                {
 
134
                        this.screen_name = screen_name;
 
135
                        this.timeout = 60 * 5;
 
136
                }
 
137
 
 
138
                public string ServerUrl {
 
139
                        get { return this.server_url; }
 
140
                }
 
141
 
 
142
                public string ScreenName {
 
143
                        get { return this.screen_name; }
 
144
                }
 
145
 
 
146
                public string FilePath {
 
147
                        get { return this.file_path; }
 
148
                }
 
149
 
 
150
                public string FileName {
 
151
                        get {
 
152
                                // file_path points to a file
 
153
                                if (Path.GetFileName (this.file_path).Length > 0)
 
154
                                        return Path.GetFileName (this.file_path);
 
155
 
 
156
                                // file_path points to a directory
 
157
                                string[] str_parts;
 
158
                                str_parts = this.file_path.Split (Path.DirectorySeparatorChar);
 
159
                                return str_parts[str_parts.Length - 1];
 
160
                        }
 
161
                }
 
162
 
 
163
                public int Timeout {
 
164
                        get { return this.timeout; }
 
165
                }
 
166
 
 
167
                public void SendMessageToBuddy (string message)
 
168
                {
 
169
                        Pidgin.IPidgin prpl;
 
170
                        int account, conversation, im;
 
171
 
 
172
                        prpl = Pidgin.FindInstance ();
 
173
                        try {
 
174
                                Pidgin.GetBuddyIsOnlineAndAccount (this.screen_name, out account);
 
175
                                if (account == -1)
 
176
                                        account = prpl.PurpleAccountsFindConnected ("", "");
 
177
                                conversation = prpl.PurpleConversationNew (Pidgin.PURPLE_CONV_TYPE_IM,
 
178
                                                account, this.screen_name);
 
179
                                prpl.PurpleConversationPresent (conversation);
 
180
                                im = prpl.PurpleConvIm (conversation);
 
181
                                prpl.PurpleConvImSend (im, message);
 
182
                        } catch (Exception e) {
 
183
                                Console.Error.WriteLine ("Could not create new Pidgin conversation: {0}", e.Message);
 
184
                        }
 
185
                }
 
186
 
 
187
                public void ServeFile(string path)
 
188
                {
 
189
                        this.file_path = path;
 
190
                        // Read the content of the python file
 
191
                        Assembly a = Assembly.GetExecutingAssembly ();
 
192
                        string content = string.Empty;
 
193
                        using (Stream f = a.GetManifestResourceStream ("Do.Addins.Woof.woof.py")) {
 
194
                                using (StreamReader sr = new StreamReader (f))
 
195
                                        content = sr.ReadToEnd ();
 
196
                        }
 
197
 
 
198
                        // Start process parameters
 
199
                        System.Diagnostics.Process p = new System.Diagnostics.Process ();
 
200
                        StreamWriter stdin;
 
201
                        ProcessStartInfo pinfo = new ProcessStartInfo ("python");
 
202
                        pinfo.UseShellExecute = false;
 
203
                        pinfo.RedirectStandardInput = true;
 
204
                        pinfo.RedirectStandardOutput = true;
 
205
                        pinfo.EnvironmentVariables.Add ("WOOF_FILE", path);
 
206
                        p.StartInfo = pinfo;
 
207
                        p.OutputDataReceived += new DataReceivedEventHandler (WoofOutputHandler);
 
208
 
 
209
                        // Start the python process
 
210
                        p.Start ();
 
211
 
 
212
                        // Redirect stdin
 
213
                        stdin = p.StandardInput;
 
214
                        stdin.AutoFlush = true;
 
215
 
 
216
                        // Watch stdout
 
217
                        p.BeginOutputReadLine ();
 
218
 
 
219
                        // Feed stdin
 
220
                        stdin.Write (content);
 
221
                        stdin.Close ();
 
222
 
 
223
                        // Set the timeout
 
224
                        new Thread ((ThreadStart) delegate {
 
225
                                        Thread.Sleep (this.timeout * 1000);
 
226
                                        if (! p.HasExited) {
 
227
                                        p.Kill ();
 
228
                                        this.SendMessageToBuddy ("Offer canceled.");
 
229
                                        }
 
230
                                        return;
 
231
                                        }).Start ();
 
232
 
 
233
                        // Wait for the process to exit
 
234
                        p.WaitForExit ();
 
235
                }
 
236
 
 
237
                private void WoofOutputHandler (object sendingProcess,
 
238
                                DataReceivedEventArgs outLine)
 
239
                {
 
240
                        // Print out the sort command output.
 
241
                        if (!String.IsNullOrEmpty (outLine.Data)) {
 
242
                                if (outLine.Data.StartsWith ("Now serving on")) {
 
243
                                        this.server_url = outLine.Data.Substring (14);
 
244
                                        this.SendMessageToBuddy (String.Format ("{0}: {1}",
 
245
                                                                this.FileName,
 
246
                                                                this.server_url));
 
247
                                }
 
248
                        }
 
249
                }
 
250
        }
 
251
}