3
// Parts of the code were copy/pasted from the Pidgin plugin (in Pidgin.cs).
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.
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.
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
20
using System.Diagnostics;
22
using System.Collections.Generic;
23
using System.Reflection;
24
using System.Threading;
26
using org.freedesktop.DBus;
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;
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,
49
void PurpleConversationPresent (int conversation_id);
50
int PurpleConvIm (int conv_id);
51
void PurpleConvImSend (int im_id, string message);
54
public static IPidgin FindInstance ()
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));
62
return Bus.Session.GetObject<IPidgin> (BUS_NAME, new ObjectPath (OBJECT_PATH));
65
private static int[] ConnectedAccounts {
70
prpl = FindInstance ();
71
connected = new List<int> ();
73
foreach (int account in prpl.PurpleAccountsGetAllActive ()) {
74
if (prpl.PurpleAccountIsConnected (account))
75
connected.Add (account);
78
return connected.ToArray ();
82
public static bool BuddyIsOnline (string name)
85
return GetBuddyIsOnlineAndAccount (name, out account);
88
public static bool GetBuddyIsOnlineAndAccount (string name, out int account_out)
93
prpl = FindInstance ();
95
foreach (int account in ConnectedAccounts) {
96
buddy = prpl.PurpleFindBuddy (account, name);
97
if (prpl.PurpleBuddyIsOnline (buddy)) {
98
account_out = account;
107
public static void OpenConversationWithBuddy (string name)
110
int account, conversation;
112
prpl = FindInstance ();
114
GetBuddyIsOnlineAndAccount (name, out account);
116
account = prpl.PurpleAccountsFindConnected ("", "");
117
conversation = prpl.PurpleConversationNew (PURPLE_CONV_TYPE_IM,
119
prpl.PurpleConversationPresent (conversation);
120
} catch (Exception e) {
121
Console.Error.WriteLine ("Could not create new Pidgin conversation: {0}", e.Message);
126
public class WoofServer {
127
private string server_url;
128
private string file_path;
129
private string screen_name;
132
public WoofServer (string screen_name)
134
this.screen_name = screen_name;
135
this.timeout = 60 * 5;
138
public string ServerUrl {
139
get { return this.server_url; }
142
public string ScreenName {
143
get { return this.screen_name; }
146
public string FilePath {
147
get { return this.file_path; }
150
public string FileName {
152
// file_path points to a file
153
if (Path.GetFileName (this.file_path).Length > 0)
154
return Path.GetFileName (this.file_path);
156
// file_path points to a directory
158
str_parts = this.file_path.Split (Path.DirectorySeparatorChar);
159
return str_parts[str_parts.Length - 1];
164
get { return this.timeout; }
167
public void SendMessageToBuddy (string message)
170
int account, conversation, im;
172
prpl = Pidgin.FindInstance ();
174
Pidgin.GetBuddyIsOnlineAndAccount (this.screen_name, out account);
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);
187
public void ServeFile(string path)
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 ();
198
// Start process parameters
199
System.Diagnostics.Process p = new System.Diagnostics.Process ();
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);
207
p.OutputDataReceived += new DataReceivedEventHandler (WoofOutputHandler);
209
// Start the python process
213
stdin = p.StandardInput;
214
stdin.AutoFlush = true;
217
p.BeginOutputReadLine ();
220
stdin.Write (content);
224
new Thread ((ThreadStart) delegate {
225
Thread.Sleep (this.timeout * 1000);
228
this.SendMessageToBuddy ("Offer canceled.");
233
// Wait for the process to exit
237
private void WoofOutputHandler (object sendingProcess,
238
DataReceivedEventArgs outLine)
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}",