Module: Impostor::Phpbb2::Topic

Defined in:
lib/impostor/phpbb2.rb

Instance Method Summary collapse

Instance Method Details

#get_new_topic_form(page) ⇒ Object

Get the the new topic form on the page



135
136
137
138
139
# File 'lib/impostor/phpbb2.rb', line 135

def get_new_topic_form(page)
  form = page.form('post')
  raise Impostor::TopicError.new("unknown new topic page format") unless form
  form
end

#get_new_topic_uri(forum, subject, message) ⇒ Object

return a uri used to fetch the new topic page based on the forum, subject, and message



126
127
128
129
130
# File 'lib/impostor/phpbb2.rb', line 126

def get_new_topic_uri(forum, subject, message)
  uri = URI.join(self.config.app_root, self.config.config(:posting_page))
  uri.query = "mode=newtopic&f=#{forum}"
  uri
end

#get_topic_from_result(page) ⇒ Object

Get the new topic identifier from the result page



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/impostor/phpbb2.rb', line 179

def get_topic_from_result(page)
  begin
    link = page.links.detect{ |l| l.href =~ /viewtopic\.php/ }
    kv = link.uri.query.split('&').detect{|kv| kv =~ /^t=/ }
    topicid = URI.unescape(kv).split('#').first.split('=').last.to_i
  rescue StandardError => err
    raise Impostor::TopicError.new(err)
  end
  raise Impostor::TopicError.new("Failed to create topic.") if topicid.zero?

  topicid
end

#page_message(page, prepend = '') ⇒ Object



192
193
194
195
196
197
# File 'lib/impostor/phpbb2.rb', line 192

def page_message(page, prepend = '')
  message = page.search("//span[@class='gen']").last || ''
  message = message.text if message.respond_to?(:text)
  prepend = '' if message.empty?
  "#{prepend}#{message}"
end

#set_subject_and_message(form, subject, message) ⇒ Object

Set the subject and message on the new topic form



144
145
146
147
148
149
# File 'lib/impostor/phpbb2.rb', line 144

def set_subject_and_message(form, subject, message)
  form.subject = subject
  form.message = message
  form["post"] = "Submit"
  form
end

#validate_new_topic_result(page) ⇒ Object

Validate the result of posting the new topic



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/impostor/phpbb2.rb', line 154

def validate_new_topic_result(page)
  message = page_message(page)
  if message !~ /Your message has been entered successfully./
    if message =~ /You cannot make another post so soon after your last/
      raise Impostor::ThrottledError.new("too many new topics in too short amount of time")
    else
      raise Impostor::TopicError.new("Topic did not post.")
    end
  end

  begin
    # <td align="center"><span class="gen">Your message has been entered successfully.<br /><br />Click <a href="viewtopic.php?p=9#9">Here</a> to view your message<br /><br />Click <a href="viewforum.php?f=1">Here</a> to return to the forum</span></td>

    # TODO the link has the postid specifically for the post, not all
    # forums make it easy to deduce the post id
    link = page.links.detect{ |l| l.href =~ /viewtopic\.php/ }
    link.click
  rescue StandardError => err
    raise Impostor::TopicError.new(err)
  end
end