- 19772A67F431072DE4C51C6600D860C8CBB77F4E5172F4C20F43078565EDA7C07C599E165EE588552741A703CBD13B1043D6C2850858CBE2CC9D3F5249F54C8F
+ 7980970A1D40B7A348A8FED795896D0CFA4568E3B4464768A0EA118EFDE16645BCDF2E934BA1ED42BC1D7708BCCB9EF28F0645C13CBDC80754D00D8DD47A6729
logotron/bot.py
(13 . 6)(13 . 11)
46
47 ##############################################################################
48
49 # Version. If changing this program, always set this to same # as in MANIFEST
50 Ver = 596907
51
52 ##############################################################################
53
54 cfg = ConfigParser.ConfigParser()
55
56 ##############################################################################
(211 . 6)(216 . 9)
58 if act:
59 action = True
60 text = act.group(1)
61 # Remove turd if present:
62 if text[-1:] == '\x01':
63 text = text[:-1]
64 # This line is edible, process it.
65 eat_logline(user, chan, text, action)
66
(312 . 6)(320 . 15)
68
69 ##############################################################################
70
71 # Get perma-URL corresponding to given log line
72 def line_url(l):
73 return "{0}log/{1}/{2}#{3}".format(Base_URL,
74 l['chan'],
75 l['t'].strftime(Date_Short_Format),
76 l['idx'])
77
78 ##############################################################################
79
80 # Commands:
81
82 def cmd_help(arg, user, chan):
(324 . 7)(341 . 68)
84 speak(chan, get_search_res(chan, arg))
85
86 def cmd_seen(arg, user, chan):
87 speak(chan, "%s: this command is not yet implemented." % user);
88 # Empty query is prohibited:
89 if arg == "":
90 speak(chan, "Required argument: USER")
91 return
92
93 # Perform query:
94 seen_line = query_db(
95 '''select t, idx, payload, chan from loglines where
96 chan=%s and speaker=%s order by t desc limit 1;''',
97 [chan, arg], one=True)
98
99 # Where output will go
100 result = ""
101
102 # If user has been seen in THE CURRENT chan:
103 if seen_line != None:
104 time_txt = seen_line['t'].strftime(Date_Long_Format)
105 time_link = "[%s][%s]" % (line_url(seen_line), time_txt)
106 seen_line = "%s last seen here on %s: %s" % (arg,
107 time_link,
108 seen_line['payload'])
109 result = seen_line
110 else:
111 # If user has never been seen in this chan:
112 result = "The user %s has never been seen in #%s." % (arg, chan)
113
114 # Speak the result into the chan where command was issued
115 speak(chan, result)
116
117 def cmd_seen_anywhere(arg, user, chan):
118 # Empty query is prohibited:
119 if arg == "":
120 speak(chan, "Required argument: USER")
121 return
122
123 # Perform query:
124 seen_line = query_db(
125 '''select t, idx, payload, chan from loglines where speaker=%s
126 order by t desc limit 1;''',
127 [arg], one=True)
128
129 # Where output will go
130 result = ""
131
132 # If user has been seen in ANY logged chan:
133 if seen_line != None:
134 time_txt = seen_line['t'].strftime(Date_Long_Format)
135 time_link = "[%s][%s]" % (line_url(seen_line), time_txt)
136 seen_line = "%s last seen in #%s on %s: %s" % (arg,
137 seen_line['chan'],
138 time_link,
139 seen_line['payload'])
140 result = seen_line
141 else:
142 # If user has never been seen at all:
143 result = "The user %s has never been seen by this logger." % arg
144
145 # Speak the result into the chan where command was issued
146 speak(chan, result)
147
148 def cmd_version(arg, user, chan):
149 speak(chan, "I am bot version %s." % Ver);
150
151 def cmd_src(arg, user, chan):
152 speak(chan, "%s: my source code can be seen at: %s" % (user, Src_URL));
(343 . 11)(421 . 13)
154 (user, uptime_txt));
155
156 Commands = {
157 "help" : cmd_help,
158 "s" : cmd_search,
159 "seen" : cmd_seen,
160 "uptime" : cmd_uptime,
161 "src" : cmd_src
162 "help" : cmd_help,
163 "s" : cmd_search,
164 "seen" : cmd_seen,
165 "seen-anywhere" : cmd_seen_anywhere,
166 "uptime" : cmd_uptime,
167 "src" : cmd_src,
168 "version" : cmd_version
169 }
170
171 ##############################################################################
(384 . 8)(464 . 7)
173
174
175 # RE for finding log refs
176 logref_re = re.compile(Base_URL + """log\/([^/]+)/([^/]+)#(\d+)""")
177
178 logref_re = re.compile(Base_URL + """log\/([^/]+)/[^/]+#(\d+)""")
179
180 # All valid received lines end up here
181 def eat_logline(user, chan, text, action):
(422 . 7)(501 . 7)
183 else:
184 # Finally, see if contains log refs:
185 for ref in re.findall(logref_re, text):
186 ref_chan, ref_date, ref_idx = ref
187 ref_chan, ref_idx = ref
188 # Find this line in DB:
189 ref_line = query_db(
190 '''select t, speaker, payload from loglines
(430 . 10)(509 . 20)
192 [ref_chan, ref_idx], one=True)
193 # If retrieved line is valid, echo it:
194 if ref_line != None:
195 time_txt = ref_line['t'].strftime(Date_Long_Format)
196 my_line = "Logged on %s %s: %s" % (time_txt,
197 ref_line['speaker'],
198 ref_line['payload'])
199 # If referred line was spoken in THIS chan:
200 if ref_chan == chan:
201 time_txt = ref_line['t'].strftime(Date_Long_Format)
202 my_line = "Logged on %s %s: %s" % (time_txt,
203 ref_line['speaker'],
204 ref_line['payload'])
205 else:
206 # If this is a cross-chan echo:
207 time_txt = ref_line['t'].strftime(Date_Short_Format)
208 my_line = "(%s) %s %s: %s" % (ref_chan,
209 time_txt,
210 ref_line['speaker'],
211 ref_line['payload'])
212
213 # Speak the line echo into the chan where ref was seen
214 speak(chan, my_line)
215