SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
FootprintsSubmission.xaml.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Windows;
5 using System.Windows.Controls;
6 using System.Windows.Documents;
7 using System.Windows.Media.Animation;
8 using System.Net;
9 using System.IO;
10 using System.Text.RegularExpressions;
11 
12 
13 namespace SeciControls
14 {
18  public partial class FootprintsSubmission : UserControl
19  {
21  {
22  InitializeComponent();
23  }
24 
25  private void reset()
26  {
27  txtNameError.Visibility = System.Windows.Visibility.Collapsed;
28  txtDescError.Visibility = System.Windows.Visibility.Collapsed;
29  txtDesc.Text = "";
30  txtName.Text = "";
31  }
32 
33  private void btnSubmit_Click(object sender, RoutedEventArgs e)
34  {
35  bool okay = true;
36  txtNameError.Visibility = System.Windows.Visibility.Collapsed;
37  txtDescError.Visibility = System.Windows.Visibility.Collapsed;
38 
39  if (String.IsNullOrEmpty(txtDesc.Text.Trim()))
40  {
41  txtDescError.Visibility = System.Windows.Visibility.Visible;
42  okay = false;
43  }
44 
45  if (String.IsNullOrEmpty(txtName.Text.Trim()))
46  {
47  txtNameError.Visibility = System.Windows.Visibility.Visible;
48  okay = false;
49  }
50 
51  if (okay)
52  {
53  String toAddress = "FBUITservicedesk@rl.ac.uk";
54  String fromAddress = "ISISsupport@rl.ac.uk";
55 
56  //Need to get instrument name from somewhere
57  String machine = Environment.MachineName.ToUpper();
58  String instrument = machine;
59 
60  if (instrument.StartsWith("NDX"))
61  {
62  instrument = instrument.Substring(3);
63  }
64 
65  //Create message
66  String header = "IT Query Type=SIT" + Environment.NewLine;
67  header += "Primary Assignee=ISIS Science Support Team" + Environment.NewLine;
68  String assignees = getCcList(instrument);
69  if (!String.IsNullOrEmpty(assignees))
70  {
71  header += "Assignees=" + assignees + Environment.NewLine;
72  }
73  header += "Status=New Instrument Request" + Environment.NewLine;
74  header += "Full Name=" + instrument + Environment.NewLine;
75  header += "Telephone Number=0000" + Environment.NewLine;
76  header += "Email Address=" + "ISISinstrument@sparrowhawk.nd.rl.ac.uk" + Environment.NewLine;
77  header += "Company=STFC" + Environment.NewLine;
78  header += "Office=" + instrument + Environment.NewLine;
79  header += "Department=Isis" + Environment.NewLine;
80  header += "Site=RAL" + Environment.NewLine;
81  header += "Internal Notes (Agents only)=The Contact email address of ISISInstrument@sparrowhawk is a dummy one;"
82  + " the relevant instrument scientists have been added to the CC list of the query" + Environment.NewLine;
83  header += "Machine Name=" + machine;
84 
85  String comment = "ISIS Instrument: " + instrument + Environment.NewLine;
86  comment = comment + "Name of computer problem was submitted from: " + machine + Environment.NewLine;
87  comment = comment + "Scientist Contact Information: http://www.isis.stfc.ac.uk/people/instrument-scientists-4398.html" + Environment.NewLine;
88  comment = comment + "Instrument Contact List (i.e. who gets these sorts of emails): http://sparrowhawk.nd.rl.ac.uk/footprints/contacts.html";
89 
90  String message = header + Environment.NewLine + Environment.NewLine
91  + txtDesc.Text + Environment.NewLine + Environment.NewLine + txtName.Text + Environment.NewLine + Environment.NewLine + comment;
92 
93  //Send message
94  try
95  {
96  System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
97  msg.To.Add(toAddress);
98 
99  if (txtDesc.Text.Length > 30)
100  {
101  msg.Subject = "INSTRUMENT (" + instrument + "): " + txtDesc.Text.Substring(0, 30);
102  }
103  else
104  {
105  msg.Subject = "INSTRUMENT (" + instrument + "): " + txtDesc.Text;
106  }
107  msg.From = new System.Net.Mail.MailAddress(fromAddress);
108  msg.Body = message;
109  System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("exchsmtp.stfc.ac.uk");
110  smtp.Send(msg);
111 
112  //Show confirmation message
113  txtMsgSent.Text = "Message Submitted";
114  ((Storyboard)FindResource("FadeOut")).Begin(txtMsgSent);
115  ((Storyboard)FindResource("FadeIn")).Begin(btnSubmit);
116  //Reset for next message
117  reset();
118 
119  }
120  catch
121  {
122  txtMsgSent.Text = "Sorry - could not send message at this time";
123  ((Storyboard)FindResource("FadeOut")).Begin(txtMsgSent);
124  ((Storyboard)FindResource("FadeIn")).Begin(btnSubmit);
125  }
126  }
127  }
128 
129  private String getCcList(String instrument)
130  {
131  String url = "http://sparrowhawk.nd.rl.ac.uk/footprints/contacts.html";
132 
133  try
134  {
135  if (String.IsNullOrEmpty(url)) return "";
136 
137  String data;
138  WebRequest wr = WebRequest.Create(url);
139 
140  using (WebResponse res = wr.GetResponse())
141  using (Stream s = res.GetResponseStream())
142  using (StreamReader sr = new StreamReader(s))
143  data = sr.ReadToEnd();
144 
145  data = data.Substring(data.IndexOf(instrument));
146  data = data.Substring(0, data.IndexOf("</TR>"));
147 
148  List<String> lines = new List<string>(data.Split('\n'));
149 
150  //Extract email addresses. Note: each email address appears twice
151  Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
152  MatchCollection emailMatches = emailRegex.Matches(data);
153 
154  StringBuilder sb = new StringBuilder();
155  String last = "";
156 
157  foreach (Match emailMatch in emailMatches)
158  {
159  if (last != emailMatch.Value)
160  {
161  last = emailMatch.Value;
162  sb.Append("CC:" + emailMatch.Value.Trim() + " ");
163  }
164  }
165 
166  return sb.ToString().Trim();
167  }
168  catch
169  {
170  return "";
171  }
172  }
173  }
174 }
Interaction logic for FootprintsSubmission.xaml
void btnSubmit_Click(object sender, RoutedEventArgs e)