Huggle  build:^490^dce1e5c
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
message.cpp
1 //This program is free software: you can redistribute it and/or modify
2 //it under the terms of the GNU General Public License as published by
3 //the Free Software Foundation, either version 3 of the License, or
4 //(at your option) any later version.
5 
6 //This program is distributed in the hope that it will be useful,
7 //but WITHOUT ANY WARRANTY; without even the implied warranty of
8 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 //GNU General Public License for more details.
10 
11 #include "message.hpp"
12 
13 using namespace Huggle;
14 
15 Message::Message(WikiUser *target, QString Message, QString Summary)
16 {
17  user = target;
18  text = Message;
19  summary = Summary;
20  Done = false;
21  Sending = false;
22  this->Dependency = NULL;
23  this->query = NULL;
24  this->token = "none";
25  /// \todo LOCALIZE ME
26  // some users might prefer to have a month as header
27  title = "Message from " + Configuration::UserName;
28 }
29 
30 Message::~Message()
31 {
32  //delete query;
33 }
34 
36 {
37  // first we need to get an edit token
38  Sending = true;
39  query = new ApiQuery();
40  query->SetAction(ActionQuery);
41  query->Parameters = "prop=info&intoken=edit&titles=" + QUrl::toPercentEncoding(user->GetTalk());
42  /// \todo LOCALIZE ME
43  query->Target = "Retrieving token to edit " + user->GetTalk();
44  query->RegisterConsumer("Message::Send()");
45  Core::AppendQuery(query);
46  query->Process();
47 }
48 
49 void Message::Fail(QString reason)
50 {
51  /// \todo LOCALIZE ME
52  Core::Log("Error: unable to deliver the message to " + user->Username + "; " + reason);
53  Done = true;
54  Sending = false;
55  query->UnregisterConsumer("Message::Send()");
56  query = NULL;
57 }
58 
60 {
61  if (this->Dependency != NULL)
62  {
63  if (!this->Dependency->Processed())
64  {
65  return false;
66  } else
67  {
68  if (this->Dependency->Result->Failed)
69  {
70  // we can't continue because the dependency is fucked
71  this->Dependency->UnregisterConsumer("keep");
72  this->Dependency = NULL;
73  this->Sending = false;
74  this->Done = true;
75  if (query != NULL)
76  {
77  query->UnregisterConsumer("Message::Send()");
78  }
79  return true;
80  }
81  this->Dependency->UnregisterConsumer("keep");
82  this->Dependency = NULL;
83  }
84  }
85  if (this->Sending)
86  {
87  Finish();
88  }
89  if (this->Done)
90  {
91  return true;
92  }
93  return false;
94 }
95 
97 {
98  if (Done)
99  {
100  // we really need to quit now because query is null
101  return;
102  }
103  // we need to get a token
104  if (token == "none")
105  {
106  if (!query->Processed())
107  {
108  return;
109  }
110  if (query->Result->Failed)
111  {
112  /// \todo LOCALIZE ME
113  Fail("unable to retrieve the edit token");
114  return;
115  }
116  QDomDocument d;
117  d.setContent(query->Result->Data);
118  QDomNodeList l = d.elementsByTagName("page");
119  if (l.count() == 0)
120  {
121  /// \todo LOCALIZE ME
122  Fail("no token was returned by request");
123  Core::DebugLog("No page");
124  return;
125  }
126  QDomElement element = l.at(0).toElement();
127  if (!element.attributes().contains("edittoken"))
128  {
129  /// \todo LOCALIZE ME
130  Fail("the result doesn't contain the token");
131  Core::DebugLog("No token");
132  return;
133  }
134  token = element.attribute("edittoken");
135  query->UnregisterConsumer("Message::Send()");
136  query = new ApiQuery();
137  query->Target = "Writing " + user->GetTalk();
138  query->UsingPOST = true;
139  query->RegisterConsumer("Message::Finish()");
140  query->SetAction(ActionEdit);
141  if (this->Section == false)
142  {
143  query->Parameters = "title=" + QUrl::toPercentEncoding(user->GetTalk()) + "&summary=" + QUrl::toPercentEncoding(summary + " " + Configuration::EditSuffixOfHuggle)
144  + "&text=" + QUrl::toPercentEncoding(this->text)
145  + "&token=" + QUrl::toPercentEncoding(this->token);
146  }else
147  {
148  query->Parameters = "title=" + QUrl::toPercentEncoding(user->GetTalk()) + "&section=new&sectiontitle="
149  + QUrl::toPercentEncoding(this->title) + "&summary=" + QUrl::toPercentEncoding(summary + " " + Configuration::EditSuffixOfHuggle)
150  + "&text=" + QUrl::toPercentEncoding(this->text)
151  + "&token=" + QUrl::toPercentEncoding(this->token);
152  }
153  Core::AppendQuery(query);
154  query->Process();
155  return;
156  }
157  // we need to check the query
158  if (!query->Processed())
159  {
160  return;
161  }
162 
163  if (query->Result->Failed)
164  {
165  Fail("Failed to deliver the message");
166  return;
167  }
168 
169  bool sent = false;
170 
171  QDomDocument d;
172  d.setContent(query->Result->Data);
173  QDomNodeList l = d.elementsByTagName("edit");
174  if (l.count() > 0)
175  {
176  QDomElement element = l.at(0).toElement();
177  if (element.attributes().contains("result"))
178  {
179  if (element.attribute("result") == "Success")
180  {
181  /// \todo LOCALIZE ME
182  Core::Log("Successfuly delivered message to " + user->Username);
183  sent = true;
184  HistoryItem item;
185  item.Result = "Success";
186  item.Type = HistoryMessage;
187  item.Target = user->Username;
188  if (Core::Main != NULL)
189  {
190  Core::Main->_History->Prepend(item);
191  }
192  }
193  }
194  }
195 
196  if (!sent)
197  {
198  /// \todo LOCALIZE ME
199  Core::Log("Failed to deliver a message to " + user->Username + " please check logs");
200  Core::DebugLog(query->Result->Data);
201  }
202 
203  query->UnregisterConsumer("Message::Finish()");
204  Done = true;
205  query = NULL;
206 }
QString title
Title.
Definition: message.hpp:38
Message(WikiUser *target, QString Message, QString Summary)
Definition: message.cpp:15
WikiUser * user
User to deliver a message to.
Definition: message.hpp:44
static void Log(QString Message)
Write text to terminal as well as ring log.
Definition: core.cpp:563
History * _History
Pointer to history.
Definition: mainwindow.hpp:142
QString token
Token that is needed in order to write to page.
Definition: message.hpp:40
QString Target
This is optional property which contains a label of target this query is for.
Definition: apiquery.hpp:98
static QString UserName
User name.
virtual bool Processed()
Returns true in case that query is processed.
Definition: query.cpp:45
void UnregisterConsumer(const int consumer)
This function will remove a string which prevent the object from being removed.
Definition: collectable.cpp:68
void RegisterConsumer(const int consumer)
Registers a consumer.
Definition: collectable.cpp:57
void Fail(QString reason)
Definition: message.cpp:49
static void AppendQuery(Query *item)
Insert a query to internal list of running queries, so that they can be watched This will insert it t...
Definition: core.cpp:557
QString Username
Username.
Definition: wikiuser.hpp:52
void Process()
Run.
Definition: apiquery.cpp:138
This is similar to query, just it's more simple, you can use it to deliver messages to users...
Definition: message.hpp:26
void Prepend(HistoryItem item)
Insert a new item to top of list.
Definition: history.cpp:37
void SetAction(const Action action)
Change the action type.
Definition: apiquery.cpp:185
Query * Dependency
If this dependency is not a NULL then a message is sent after it is Processed (see Query::Processed()...
Definition: message.hpp:36
bool UsingPOST
Whether the query will submit parameters using POST data.
Definition: apiquery.hpp:77
HistoryType Type
Type of item.
Definition: history.hpp:46
static QString EditSuffixOfHuggle
Suffix used by huggle.
QString text
Text of message that will be appended to talk page.
Definition: message.hpp:46
static void DebugLog(QString Message, unsigned int Verbosity=1)
This log is only shown if verbosity is same or larger than requested verbosity.
Definition: core.cpp:641
QString Parameters
Parameters for action, for example page title.
Definition: apiquery.hpp:84
History consist of these items.
Definition: history.hpp:35
bool Finished()
Returns true in case that message was sent.
Definition: message.cpp:59
QString GetTalk()
Return a link to talk page of this user (like User talk:Jimbo)
Definition: wikiuser.cpp:245
void Send()
Send a message to user.
Definition: message.cpp:35
void Finish()
Definition: message.cpp:96
static MainWindow * Main
Pointer to main.
Definition: core.hpp:111
This class can be used to execute any kind of api query on any wiki.
Definition: apiquery.hpp:55
bool Section
If edit will be created in new section.
Definition: message.hpp:42
QString Data
Data retrieved by query.
Definition: queryresult.hpp:25
QueryResult * Result
Result of query, see documentation of QueryResult for more.
Definition: query.hpp:68