Sfoglia il codice sorgente

Restrict multiseum parameters to [+,/_:;&-]

Camille Masset 5 anni fa
parent
commit
8b047b6957
1 ha cambiato i file con 13 aggiunte e 4 eliminazioni
  1. 13 4
      bot/views/telegram.py

+ 13 - 4
bot/views/telegram.py

@@ -152,10 +152,7 @@ def webhook(request):
152 152
         if re.match(seum_cmd, text) is not None:
153 153
             # it's a /seum cmd
154 154
             m = re.sub(seum_cmd, r"\3", text)
155
-            maybe_counter = m.split(' ')[0]
156
-            # allow multiple seums: /seum ABC+DEF Message
157
-            counters = [maybe_counter[i:i+3] for i in range(0, len(maybe_counter), 4)]
158
-            yes_counters = Counter.objects.filter(trigramme__in=counters)
155
+            (counters, yes_counters) = _extract_counters(m)
159 156
             seums_to_throw = []
160 157
             user_counter = telegram_user.counter
161 158
             if len(yes_counters):
@@ -184,3 +181,15 @@ def webhook(request):
184 181
             requests.post(telegram_url + 'sendMessage', json={'chat_id': chat['id'], 'text': 'Your Telegram account isn\'t linked to a SeumBook account. Say hello to me in a private chat to link it :-)! https://telegram.me/' + telegram_bot_name + '?start=Hello', 'reply_to_message_id': data['message']['message_id']})
185 182
 
186 183
     return HttpResponse('')
184
+
185
+
186
+def _extract_counters(message):
187
+    maybe_counter = message.split(' ')[0]
188
+    if re.match(r"^\S{3}(?:[+,/_:;&-]\S{3})*$", maybe_counter) is not None:
189
+        # allow multiple seums: /seum ABC+DEF Message
190
+        counters = [maybe_counter[i:i+3] for i in range(0, len(maybe_counter), 4)]
191
+        yes_counters = Counter.objects.filter(trigramme__in=counters)
192
+        return (counters, yes_counters)
193
+
194
+    # the message does not start with a (list of) trigram(s)
195
+    return ([], [])